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. 4 5 #include <string> 6 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" 24 25 using ::testing::_; 26 27 namespace content { 28 29 ACTION_P(RunClosure, closure) { 30 closure.Run(); 31 } 32 33 static const std::string kTestStreamUrl = "stream_url"; 34 static const std::string kUnknownStreamUrl = "unknown_stream_url"; 35 36 class VideoDestinationHandlerTest : public PpapiUnittest { 37 public: 38 VideoDestinationHandlerTest() 39 : child_process_(new ChildProcess()), 40 registry_(new MockMediaStreamRegistry()) { 41 registry_->Init(kTestStreamUrl); 42 } 43 44 virtual void TearDown() OVERRIDE { 45 registry_.reset(); 46 blink::WebHeap::collectAllGarbageForTesting(); 47 PpapiUnittest::TearDown(); 48 } 49 50 base::MessageLoop* io_message_loop() const { 51 return child_process_->io_message_loop(); 52 } 53 54 protected: 55 scoped_ptr<ChildProcess> child_process_; 56 scoped_ptr<MockMediaStreamRegistry> registry_; 57 }; 58 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. 67 delete frame_writer; 68 } 69 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); 75 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()); 81 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); 86 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); 93 { 94 base::RunLoop run_loop; 95 base::Closure quit_closure = run_loop.QuitClosure(); 96 97 EXPECT_CALL(sink, OnVideoFrame()).WillOnce( 98 RunClosure(quit_closure)); 99 frame_writer->PutFrame(image.get(), 10); 100 run_loop.Run(); 101 } 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); 106 107 // The |frame_writer| is a proxy and is owned by whoever call Open. 108 delete frame_writer; 109 } 110 111 } // namespace content 112