| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "content/renderer/media/media_stream.h" | 8 #include "content/renderer/media/media_stream.h" |
| 9 #include "content/renderer/media/media_stream_video_track.h" |
| 9 #include "content/renderer/media/mock_media_stream_dependency_factory.h" | 10 #include "content/renderer/media/mock_media_stream_dependency_factory.h" |
| 10 #include "content/renderer/media/mock_media_stream_registry.h" | 11 #include "content/renderer/media/mock_media_stream_registry.h" |
| 11 #include "content/renderer/media/video_destination_handler.h" | 12 #include "content/renderer/media/mock_media_stream_video_sink.h" |
| 12 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "content/renderer/media/webrtc/video_destination_handler.h" |
| 14 #include "content/renderer/pepper/pepper_plugin_instance_impl.h" |
| 15 #include "content/renderer/pepper/ppb_image_data_impl.h" |
| 16 #include "content/test/ppapi_unittest.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" | 18 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" |
| 15 #include "third_party/WebKit/public/platform/WebString.h" | 19 #include "third_party/WebKit/public/platform/WebString.h" |
| 16 | 20 |
| 17 using cricket::CapturedFrame; | |
| 18 using cricket::CaptureState; | |
| 19 using cricket::VideoCapturer; | |
| 20 using cricket::VideoFormat; | |
| 21 using cricket::VideoFormatPod; | |
| 22 | |
| 23 namespace content { | 21 namespace content { |
| 24 | 22 |
| 25 static const std::string kTestStreamUrl = "stream_url"; | 23 static const std::string kTestStreamUrl = "stream_url"; |
| 26 static const std::string kUnknownStreamUrl = "unknown_stream_url"; | 24 static const std::string kUnknownStreamUrl = "unknown_stream_url"; |
| 27 static const VideoFormatPod kTestFormat = { | |
| 28 640, 360, FPS_TO_INTERVAL(30), cricket::FOURCC_ANY | |
| 29 }; | |
| 30 | 25 |
| 31 class PpFrameWriterTest | 26 class VideoDestinationHandlerTest : public PpapiUnittest { |
| 32 : public ::testing::Test, | |
| 33 public sigslot::has_slots<> { | |
| 34 public: | |
| 35 PpFrameWriterTest() | |
| 36 : last_capture_state_(cricket::CS_FAILED), | |
| 37 captured_frame_count_(0), | |
| 38 captured_frame_(NULL) { | |
| 39 writer_.SignalStateChange.connect(this, &PpFrameWriterTest::OnStateChange); | |
| 40 writer_.SignalFrameCaptured.connect( | |
| 41 this, &PpFrameWriterTest::OnFrameCaptured); | |
| 42 } | |
| 43 | |
| 44 void OnStateChange(VideoCapturer* capturer, CaptureState state) { | |
| 45 last_capture_state_ = state; | |
| 46 } | |
| 47 | |
| 48 void OnFrameCaptured(VideoCapturer* capturer, const CapturedFrame* frame) { | |
| 49 ++captured_frame_count_; | |
| 50 captured_frame_ = const_cast<CapturedFrame*>(frame); | |
| 51 } | |
| 52 | |
| 53 protected: | |
| 54 PpFrameWriter writer_; | |
| 55 CaptureState last_capture_state_; | |
| 56 int captured_frame_count_; | |
| 57 CapturedFrame* captured_frame_; | |
| 58 }; | |
| 59 | |
| 60 class VideoDestinationHandlerTest : public ::testing::Test { | |
| 61 public: | 27 public: |
| 62 VideoDestinationHandlerTest() : registry_(&factory_) { | 28 VideoDestinationHandlerTest() : registry_(&factory_) { |
| 63 registry_.Init(kTestStreamUrl); | 29 registry_.Init(kTestStreamUrl); |
| 64 } | 30 } |
| 65 | 31 |
| 66 protected: | 32 protected: |
| 67 MockMediaStreamDependencyFactory factory_; | 33 MockMediaStreamDependencyFactory factory_; |
| 68 MockMediaStreamRegistry registry_; | 34 MockMediaStreamRegistry registry_; |
| 69 }; | 35 }; |
| 70 | 36 |
| 71 TEST_F(PpFrameWriterTest, StartStop) { | |
| 72 EXPECT_FALSE(writer_.IsRunning()); | |
| 73 EXPECT_EQ(cricket::CS_STARTING, writer_.Start(VideoFormat(kTestFormat))); | |
| 74 EXPECT_TRUE(writer_.IsRunning()); | |
| 75 EXPECT_EQ(cricket::CS_FAILED, writer_.Start(VideoFormat(kTestFormat))); | |
| 76 writer_.Stop(); | |
| 77 EXPECT_EQ(cricket::CS_STOPPED, last_capture_state_); | |
| 78 } | |
| 79 | |
| 80 TEST_F(PpFrameWriterTest, GetPreferredFourccs) { | |
| 81 std::vector<uint32> fourccs; | |
| 82 EXPECT_TRUE(writer_.GetPreferredFourccs(&fourccs)); | |
| 83 EXPECT_EQ(1u, fourccs.size()); | |
| 84 EXPECT_EQ(cricket::FOURCC_BGRA, fourccs[0]); | |
| 85 } | |
| 86 | |
| 87 TEST_F(PpFrameWriterTest, GetBestCaptureFormat) { | |
| 88 VideoFormat desired(kTestFormat); | |
| 89 VideoFormat best_format; | |
| 90 EXPECT_FALSE(writer_.GetBestCaptureFormat(desired, NULL)); | |
| 91 EXPECT_TRUE(writer_.GetBestCaptureFormat(desired, &best_format)); | |
| 92 EXPECT_EQ(cricket::FOURCC_BGRA, best_format.fourcc); | |
| 93 | |
| 94 desired.fourcc = best_format.fourcc; | |
| 95 EXPECT_EQ(desired, best_format); | |
| 96 } | |
| 97 | |
| 98 TEST_F(VideoDestinationHandlerTest, Open) { | 37 TEST_F(VideoDestinationHandlerTest, Open) { |
| 99 FrameWriterInterface* frame_writer = NULL; | 38 FrameWriterInterface* frame_writer = NULL; |
| 100 // Unknow url will return false. | 39 // Unknow url will return false. |
| 101 EXPECT_FALSE(VideoDestinationHandler::Open(&factory_, ®istry_, | 40 EXPECT_FALSE(VideoDestinationHandler::Open(&factory_, ®istry_, |
| 102 kUnknownStreamUrl, &frame_writer)); | 41 kUnknownStreamUrl, &frame_writer)); |
| 103 EXPECT_TRUE(VideoDestinationHandler::Open(&factory_, ®istry_, | 42 EXPECT_TRUE(VideoDestinationHandler::Open(&factory_, ®istry_, |
| 104 kTestStreamUrl, &frame_writer)); | 43 kTestStreamUrl, &frame_writer)); |
| 105 EXPECT_TRUE(frame_writer); | 44 // The |frame_writer| is a proxy and is owned by who call Open. |
| 45 delete frame_writer; |
| 46 } |
| 47 |
| 48 TEST_F(VideoDestinationHandlerTest, PutFrame) { |
| 49 FrameWriterInterface* frame_writer = NULL; |
| 50 EXPECT_TRUE(VideoDestinationHandler::Open(&factory_, ®istry_, |
| 51 kTestStreamUrl, &frame_writer)); |
| 52 ASSERT_TRUE(frame_writer); |
| 106 | 53 |
| 107 // Verify the video track has been added. | 54 // Verify the video track has been added. |
| 108 const blink::WebMediaStream test_stream = registry_.test_stream(); | 55 const blink::WebMediaStream test_stream = registry_.test_stream(); |
| 109 blink::WebVector<blink::WebMediaStreamTrack> video_tracks; | 56 blink::WebVector<blink::WebMediaStreamTrack> video_tracks; |
| 110 test_stream.videoTracks(video_tracks); | 57 test_stream.videoTracks(video_tracks); |
| 111 EXPECT_EQ(1u, video_tracks.size()); | 58 ASSERT_EQ(1u, video_tracks.size()); |
| 112 | 59 |
| 113 // Verify the native video track has been added. | 60 // Verify the native video track has been added. |
| 114 MediaStream* native_stream = MediaStream::GetMediaStream(test_stream); | 61 MediaStreamVideoTrack* native_track = |
| 115 DCHECK(native_stream); | 62 MediaStreamVideoTrack::GetVideoTrack(video_tracks[0]); |
| 116 webrtc::MediaStreamInterface* webrtc_stream = | 63 ASSERT_TRUE(native_track != NULL); |
| 117 MediaStream::GetAdapter(test_stream); | |
| 118 DCHECK(webrtc_stream); | |
| 119 webrtc::VideoTrackVector webrtc_video_tracks = | |
| 120 webrtc_stream->GetVideoTracks(); | |
| 121 EXPECT_EQ(1u, webrtc_video_tracks.size()); | |
| 122 | 64 |
| 65 MockMediaStreamVideoSink sink; |
| 66 native_track->AddSink(&sink); |
| 67 |
| 68 scoped_refptr<PPB_ImageData_Impl> image( |
| 69 new PPB_ImageData_Impl(instance()->pp_instance(), |
| 70 PPB_ImageData_Impl::ForTest())); |
| 71 image->Init(PP_IMAGEDATAFORMAT_BGRA_PREMUL, 640, 360, true); |
| 72 frame_writer->PutFrame(image, 10); |
| 73 EXPECT_EQ(1, sink.number_of_frames()); |
| 74 // TODO(perkj): Verify that the track output I420 when |
| 75 // https://codereview.chromium.org/213423006/ is landed. |
| 76 |
| 77 native_track->RemoveSink(&sink); |
| 78 |
| 79 // The |frame_writer| is a proxy and is owned by who call Open. |
| 123 delete frame_writer; | 80 delete frame_writer; |
| 124 } | 81 } |
| 125 | 82 |
| 126 } // namespace content | 83 } // namespace content |
| OLD | NEW |