 Chromium Code Reviews
 Chromium Code Reviews Issue 131763002:
  Adds MediaStreamSource, MediaStreamAudioSource and MediaStreamVideoCaptureDeviceSource  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src
    
  
    Issue 131763002:
  Adds MediaStreamSource, MediaStreamAudioSource and MediaStreamVideoCaptureDeviceSource  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src| OLD | NEW | 
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_video_source.h" | 8 #include "content/renderer/media/media_stream_video_source.h" | 
| 9 #include "content/renderer/media/mock_media_stream_dependency_factory.h" | 9 #include "content/renderer/media/mock_media_stream_dependency_factory.h" | 
| 10 #include "media/base/video_frame.h" | 10 #include "media/base/video_frame.h" | 
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" | 
| 12 | 12 | 
| 13 namespace content { | 13 namespace content { | 
| 14 | 14 | 
| 15 class DummyMediaStreamVideoSource : public MediaStreamVideoSource { | 15 class DummyMediaStreamVideoSource : public MediaStreamVideoSource { | 
| 16 public: | 16 public: | 
| 17 DummyMediaStreamVideoSource(MediaStreamDependencyFactory* factory) | 17 DummyMediaStreamVideoSource(MediaStreamDependencyFactory* factory) | 
| 18 : MediaStreamVideoSource(factory) { | 18 : MediaStreamVideoSource(factory) { | 
| 19 Init(); | |
| 20 SetVideoSource(GetAdapter()); | |
| 21 SetReadyState(blink::WebMediaStreamSource::ReadyStateLive); | |
| 22 } | 19 } | 
| 23 | 20 | 
| 24 virtual ~DummyMediaStreamVideoSource() { | 21 virtual ~DummyMediaStreamVideoSource() { | 
| 25 SetReadyState(blink::WebMediaStreamSource::ReadyStateEnded); | |
| 26 } | 22 } | 
| 27 | 23 | 
| 28 void OnNewFrame(const scoped_refptr<media::VideoFrame>& frame) { | 24 void OnNewFrame(const scoped_refptr<media::VideoFrame>& frame) { | 
| 29 MediaStreamVideoSource::DeliverVideoFrame(frame); | 25 MediaStreamVideoSource::DeliverVideoFrame(frame); | 
| 30 } | 26 } | 
| 31 }; | 27 }; | 
| 32 | 28 | 
| 33 class MediaStreamVideoSourceTest | 29 class MediaStreamVideoSourceTest | 
| 34 : public ::testing::Test { | 30 : public ::testing::Test { | 
| 35 public: | 31 public: | 
| 36 MediaStreamVideoSourceTest() { | 32 MediaStreamVideoSourceTest() | 
| 33 : number_of_successfull_constraints_applied_(0), | |
| 34 number_of_failed_constraints_applied_(0) { | |
| 37 factory_.EnsurePeerConnectionFactory(); | 35 factory_.EnsurePeerConnectionFactory(); | 
| 38 webkit_source_.initialize(base::UTF8ToUTF16("dummy_source_id"), | 36 webkit_source_.initialize(base::UTF8ToUTF16("dummy_source_id"), | 
| 39 blink::WebMediaStreamSource::TypeVideo, | 37 blink::WebMediaStreamSource::TypeVideo, | 
| 40 base::UTF8ToUTF16("dummy_source_name")); | 38 base::UTF8ToUTF16("dummy_source_name")); | 
| 41 webkit_source_.setExtraData(new DummyMediaStreamVideoSource(&factory_)); | 39 webkit_source_.setExtraData(new DummyMediaStreamVideoSource(&factory_)); | 
| 42 } | 40 } | 
| 43 | 41 | 
| 44 protected: | 42 protected: | 
| 45 // Create a track that's associated with |webkit_source_|. | 43 // Create a track that's associated with |webkit_source_|. | 
| 46 blink::WebMediaStreamTrack CreateTrack(const std::string& id) { | 44 blink::WebMediaStreamTrack CreateTrack( | 
| 45 const std::string& id, | |
| 46 const blink::WebMediaConstraints& constraints) { | |
| 47 blink::WebMediaStreamTrack track; | 47 blink::WebMediaStreamTrack track; | 
| 48 track.initialize(base::UTF8ToUTF16(id), webkit_source_); | 48 track.initialize(base::UTF8ToUTF16(id), webkit_source_); | 
| 49 | |
| 50 DummyMediaStreamVideoSource* source = | |
| 51 static_cast<DummyMediaStreamVideoSource*>(track.source().extraData()); | |
| 52 | |
| 53 source->AddTrack(track, | |
| 54 constraints, | |
| 55 base::Bind( | |
| 56 &MediaStreamVideoSourceTest::OnConstraintsApplied, | |
| 57 base::Unretained(this))); | |
| 49 return track; | 58 return track; | 
| 50 } | 59 } | 
| 51 | 60 | 
| 61 // Simulate that the underlying device start successfully. | |
| 62 void StartDevice() { | |
| 
Ronghua Wu (Left Chromium)
2014/01/22 01:44:04
StartSource
 | |
| 63 factory_.last_video_source()->SetLive(); | |
| 64 } | |
| 65 | |
| 66 // Simulate that the underlying device fail to start. | |
| 67 void FailToStartDevice() { | |
| 68 factory_.last_video_source()->SetEnded(); | |
| 69 } | |
| 70 | |
| 52 void VerifyFrame(int width, int height, int num) { | 71 void VerifyFrame(int width, int height, int num) { | 
| 53 DummyMediaStreamVideoSource* source = | 72 DummyMediaStreamVideoSource* source = | 
| 54 static_cast<DummyMediaStreamVideoSource*>(webkit_source_.extraData()); | 73 static_cast<DummyMediaStreamVideoSource*>(webkit_source_.extraData()); | 
| 55 MockVideoSource* adapter = | 74 MockVideoSource* adapter = | 
| 56 static_cast<MockVideoSource*>(source->GetAdapter()); | 75 static_cast<MockVideoSource*>(source->GetAdapter()); | 
| 57 EXPECT_EQ(width, adapter->GetLastFrameWidth()); | 76 EXPECT_EQ(width, adapter->GetLastFrameWidth()); | 
| 58 EXPECT_EQ(height, adapter->GetLastFrameHeight()); | 77 EXPECT_EQ(height, adapter->GetLastFrameHeight()); | 
| 59 EXPECT_EQ(num, adapter->GetFrameNum()); | 78 EXPECT_EQ(num, adapter->GetFrameNum()); | 
| 60 } | 79 } | 
| 80 | |
| 81 int NumberOfSuccessConstraintsCallbacks() { | |
| 
Ronghua Wu (Left Chromium)
2014/01/22 01:44:04
nit const
 | |
| 82 return number_of_successfull_constraints_applied_; | |
| 83 } | |
| 84 | |
| 85 int NumberOfFailedConstraintsCallbacks() { | |
| 
Ronghua Wu (Left Chromium)
2014/01/22 01:44:04
dito
 | |
| 86 return number_of_failed_constraints_applied_; | |
| 87 } | |
| 88 | |
| 61 private: | 89 private: | 
| 90 void OnConstraintsApplied(MediaStreamSource* source, bool success) { | |
| 91 ASSERT_EQ(source, webkit_source_.extraData()); | |
| 92 | |
| 93 if (success) | |
| 94 ++number_of_successfull_constraints_applied_; | |
| 95 else | |
| 96 ++number_of_failed_constraints_applied_; | |
| 97 } | |
| 98 | |
| 99 int number_of_successfull_constraints_applied_; | |
| 100 int number_of_failed_constraints_applied_; | |
| 62 MockMediaStreamDependencyFactory factory_; | 101 MockMediaStreamDependencyFactory factory_; | 
| 63 blink::WebMediaStreamSource webkit_source_; | 102 blink::WebMediaStreamSource webkit_source_; | 
| 64 }; | 103 }; | 
| 65 | 104 | 
| 105 TEST_F(MediaStreamVideoSourceTest, AddTrackAndStartAdapter) { | |
| 106 blink::WebMediaConstraints constraints; | |
| 107 blink::WebMediaStreamTrack track = CreateTrack("123", constraints); | |
| 108 StartDevice(); | |
| 109 EXPECT_EQ(1, NumberOfSuccessConstraintsCallbacks()); | |
| 110 } | |
| 111 | |
| 112 TEST_F(MediaStreamVideoSourceTest, AddTwoTracksBeforeAdapterStart) { | |
| 113 blink::WebMediaConstraints constraints; | |
| 114 blink::WebMediaStreamTrack track1 = CreateTrack("123", constraints); | |
| 115 blink::WebMediaStreamTrack track2 = CreateTrack("123", constraints); | |
| 116 EXPECT_EQ(0, NumberOfSuccessConstraintsCallbacks()); | |
| 117 StartDevice(); | |
| 118 EXPECT_EQ(2, NumberOfSuccessConstraintsCallbacks()); | |
| 119 } | |
| 120 | |
| 121 TEST_F(MediaStreamVideoSourceTest, AddTrackAfterAdapterStart) { | |
| 122 blink::WebMediaConstraints constraints; | |
| 123 blink::WebMediaStreamTrack track1 = CreateTrack("123", constraints); | |
| 124 StartDevice(); | |
| 125 EXPECT_EQ(1, NumberOfSuccessConstraintsCallbacks()); | |
| 126 blink::WebMediaStreamTrack track2 = CreateTrack("123", constraints); | |
| 127 EXPECT_EQ(2, NumberOfSuccessConstraintsCallbacks()); | |
| 128 } | |
| 129 | |
| 130 TEST_F(MediaStreamVideoSourceTest, AddTrackAndFailToStartAdapter) { | |
| 131 blink::WebMediaConstraints constraints; | |
| 132 blink::WebMediaStreamTrack track = CreateTrack("123", constraints); | |
| 133 FailToStartDevice(); | |
| 134 EXPECT_EQ(1, NumberOfFailedConstraintsCallbacks()); | |
| 135 } | |
| 136 | |
| 66 TEST_F(MediaStreamVideoSourceTest, DeliverVideoFrame) { | 137 TEST_F(MediaStreamVideoSourceTest, DeliverVideoFrame) { | 
| 67 blink::WebMediaConstraints constraints; | 138 blink::WebMediaConstraints constraints; | 
| 68 blink::WebMediaStreamTrack track = CreateTrack("123"); | 139 blink::WebMediaStreamTrack track = CreateTrack("123", constraints); | 
| 140 StartDevice(); | |
| 69 DummyMediaStreamVideoSource* source = | 141 DummyMediaStreamVideoSource* source = | 
| 70 static_cast<DummyMediaStreamVideoSource*>(track.source().extraData()); | 142 static_cast<DummyMediaStreamVideoSource*>(track.source().extraData()); | 
| 71 source->AddTrack(track, constraints); | |
| 72 VerifyFrame(0, 0, 0); | 143 VerifyFrame(0, 0, 0); | 
| 73 const int kWidth = 640; | 144 const int kWidth = 640; | 
| 74 const int kHeight = 480; | 145 const int kHeight = 480; | 
| 75 scoped_refptr<media::VideoFrame> frame = | 146 scoped_refptr<media::VideoFrame> frame = | 
| 76 media::VideoFrame::CreateBlackFrame(gfx::Size(kWidth, kHeight)); | 147 media::VideoFrame::CreateBlackFrame(gfx::Size(kWidth, kHeight)); | 
| 77 ASSERT_TRUE(frame.get()); | 148 ASSERT_TRUE(frame.get()); | 
| 78 source->OnNewFrame(frame); | 149 source->OnNewFrame(frame); | 
| 79 VerifyFrame(640, 480, 1); | 150 VerifyFrame(640, 480, 1); | 
| 80 source->OnNewFrame(frame); | 151 source->OnNewFrame(frame); | 
| 81 VerifyFrame(640, 480, 2); | 152 VerifyFrame(640, 480, 2); | 
| 82 source->RemoveTrack(track); | 153 source->RemoveTrack(track); | 
| 83 } | 154 } | 
| 84 | 155 | 
| 85 } // namespace content | 156 } // namespace content | 
| OLD | NEW |