1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "base/message_loop/message_loop.h"
8 #include "base/run_loop.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "content/child/child_process.h"
11 #include "content/renderer/media/media_stream.h"
12 #include "content/renderer/media/media_stream_video_track.h"
13 #include "content/renderer/media/mock_media_stream_registry.h"
14 #include "content/renderer/media/mock_media_stream_video_sink.h"
15 #include "content/renderer/media/webrtc/video_destination_handler.h"
16 #include "content/renderer/pepper/pepper_plugin_instance_impl.h"
17 #include "content/renderer/pepper/ppb_image_data_impl.h"
18 #include "content/test/ppapi_unittest.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
22 #include "third_party/WebKit/public/platform/WebString.h"
23 #include "third_party/WebKit/public/web/WebHeap.h"
29 ACTION_P(RunClosure
, closure
) {
33 static const std::string kTestStreamUrl
= "stream_url";
34 static const std::string kUnknownStreamUrl
= "unknown_stream_url";
36 class VideoDestinationHandlerTest
: public PpapiUnittest
{
38 VideoDestinationHandlerTest()
39 : child_process_(new ChildProcess()),
40 registry_(new MockMediaStreamRegistry()) {
41 registry_
->Init(kTestStreamUrl
);
44 virtual void TearDown() override
{
46 blink::WebHeap::collectAllGarbageForTesting();
47 PpapiUnittest::TearDown();
50 base::MessageLoop
* io_message_loop() const {
51 return child_process_
->io_message_loop();
55 scoped_ptr
<ChildProcess
> child_process_
;
56 scoped_ptr
<MockMediaStreamRegistry
> registry_
;
59 TEST_F(VideoDestinationHandlerTest
, Open
) {
60 FrameWriterInterface
* frame_writer
= NULL
;
61 // Unknow url will return false.
62 EXPECT_FALSE(VideoDestinationHandler::Open(registry_
.get(),
63 kUnknownStreamUrl
, &frame_writer
));
64 EXPECT_TRUE(VideoDestinationHandler::Open(registry_
.get(),
65 kTestStreamUrl
, &frame_writer
));
66 // The |frame_writer| is a proxy and is owned by whoever call Open.
70 TEST_F(VideoDestinationHandlerTest
, PutFrame
) {
71 FrameWriterInterface
* frame_writer
= NULL
;
72 EXPECT_TRUE(VideoDestinationHandler::Open(registry_
.get(),
73 kTestStreamUrl
, &frame_writer
));
74 ASSERT_TRUE(frame_writer
);
76 // Verify the video track has been added.
77 const blink::WebMediaStream test_stream
= registry_
->test_stream();
78 blink::WebVector
<blink::WebMediaStreamTrack
> video_tracks
;
79 test_stream
.videoTracks(video_tracks
);
80 ASSERT_EQ(1u, video_tracks
.size());
82 // Verify the native video track has been added.
83 MediaStreamVideoTrack
* native_track
=
84 MediaStreamVideoTrack::GetVideoTrack(video_tracks
[0]);
85 ASSERT_TRUE(native_track
!= NULL
);
87 MockMediaStreamVideoSink sink
;
88 native_track
->AddSink(&sink
, sink
.GetDeliverFrameCB());
89 scoped_refptr
<PPB_ImageData_Impl
> image(
90 new PPB_ImageData_Impl(instance()->pp_instance(),
91 PPB_ImageData_Impl::ForTest()));
92 image
->Init(PP_IMAGEDATAFORMAT_BGRA_PREMUL
, 640, 360, true);
94 base::RunLoop run_loop
;
95 base::Closure quit_closure
= run_loop
.QuitClosure();
97 EXPECT_CALL(sink
, OnVideoFrame()).WillOnce(
98 RunClosure(quit_closure
));
99 frame_writer
->PutFrame(image
.get(), 10);
102 // TODO(perkj): Verify that the track output I420 when
103 // https://codereview.chromium.org/213423006/ is landed.
104 EXPECT_EQ(1, sink
.number_of_frames());
105 native_track
->RemoveSink(&sink
);
107 // The |frame_writer| is a proxy and is owned by whoever call Open.
111 } // namespace content