 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(); | 19 blink::WebMediaConstraints constraints; | 
| 
Ronghua Wu (Left Chromium)
2014/01/16 23:02:37
remove
 
perkj_chrome
2014/01/17 13:19:45
Done.
 | |
| 20 SetVideoSource(GetAdapter()); | |
| 21 SetReadyState(blink::WebMediaStreamSource::ReadyStateLive); | |
| 22 } | 20 } | 
| 23 | 21 | 
| 24 virtual ~DummyMediaStreamVideoSource() { | 22 virtual ~DummyMediaStreamVideoSource() { | 
| 25 SetReadyState(blink::WebMediaStreamSource::ReadyStateEnded); | |
| 26 } | 23 } | 
| 27 | 24 | 
| 28 void OnNewFrame(const scoped_refptr<media::VideoFrame>& frame) { | 25 void OnNewFrame(const scoped_refptr<media::VideoFrame>& frame) { | 
| 29 MediaStreamVideoSource::DeliverVideoFrame(frame); | 26 MediaStreamVideoSource::DeliverVideoFrame(frame); | 
| 30 } | 27 } | 
| 31 }; | 28 }; | 
| 32 | 29 | 
| 33 class MediaStreamVideoSourceTest | 30 class MediaStreamVideoSourceTest | 
| 34 : public ::testing::Test { | 31 : public ::testing::Test { | 
| 35 public: | 32 public: | 
| 36 MediaStreamVideoSourceTest() { | 33 MediaStreamVideoSourceTest() | 
| 34 : number_of_successfull_constraints_applied_(0), | |
| 35 number_of_failed_constraints_applied_(0) { | |
| 37 factory_.EnsurePeerConnectionFactory(); | 36 factory_.EnsurePeerConnectionFactory(); | 
| 38 webkit_source_.initialize(base::UTF8ToUTF16("dummy_source_id"), | 37 webkit_source_.initialize(base::UTF8ToUTF16("dummy_source_id"), | 
| 39 blink::WebMediaStreamSource::TypeVideo, | 38 blink::WebMediaStreamSource::TypeVideo, | 
| 40 base::UTF8ToUTF16("dummy_source_name")); | 39 base::UTF8ToUTF16("dummy_source_name")); | 
| 41 webkit_source_.setExtraData(new DummyMediaStreamVideoSource(&factory_)); | 40 webkit_source_.setExtraData(new DummyMediaStreamVideoSource(&factory_)); | 
| 42 } | 41 } | 
| 43 | 42 | 
| 44 protected: | 43 protected: | 
| 45 // Create a track that's associated with |webkit_source_|. | 44 // Create a track that's associated with |webkit_source_|. | 
| 46 blink::WebMediaStreamTrack CreateTrack(const std::string& id) { | 45 blink::WebMediaStreamTrack CreateTrack( | 
| 46 const std::string& id, | |
| 47 const blink::WebMediaConstraints& constraints) { | |
| 47 blink::WebMediaStreamTrack track; | 48 blink::WebMediaStreamTrack track; | 
| 48 track.initialize(base::UTF8ToUTF16(id), webkit_source_); | 49 track.initialize(base::UTF8ToUTF16(id), webkit_source_); | 
| 50 | |
| 51 DummyMediaStreamVideoSource* source = | |
| 52 static_cast<DummyMediaStreamVideoSource*>(track.source().extraData()); | |
| 53 | |
| 54 source->AddTrack(track, | |
| 55 constraints, | |
| 56 base::Bind( | |
| 57 &MediaStreamVideoSourceTest::OnConstraintsApplied, | |
| 58 base::Unretained(this))); | |
| 49 return track; | 59 return track; | 
| 50 } | 60 } | 
| 51 | 61 | 
| 62 void StartAdapter() { | |
| 63 factory_.last_video_source()->SetLive(); | |
| 64 } | |
| 65 | |
| 66 void FailToStartAdapter() { | |
| 67 factory_.last_video_source()->SetEnded(); | |
| 68 } | |
| 69 | |
| 52 void VerifyFrame(int width, int height, int num) { | 70 void VerifyFrame(int width, int height, int num) { | 
| 53 DummyMediaStreamVideoSource* source = | 71 DummyMediaStreamVideoSource* source = | 
| 54 static_cast<DummyMediaStreamVideoSource*>(webkit_source_.extraData()); | 72 static_cast<DummyMediaStreamVideoSource*>(webkit_source_.extraData()); | 
| 55 MockVideoSource* adapter = | 73 MockVideoSource* adapter = | 
| 56 static_cast<MockVideoSource*>(source->GetAdapter()); | 74 static_cast<MockVideoSource*>(source->GetAdapter()); | 
| 57 EXPECT_EQ(width, adapter->GetLastFrameWidth()); | 75 EXPECT_EQ(width, adapter->GetLastFrameWidth()); | 
| 58 EXPECT_EQ(height, adapter->GetLastFrameHeight()); | 76 EXPECT_EQ(height, adapter->GetLastFrameHeight()); | 
| 59 EXPECT_EQ(num, adapter->GetFrameNum()); | 77 EXPECT_EQ(num, adapter->GetFrameNum()); | 
| 60 } | 78 } | 
| 79 | |
| 80 int NumberOfSuccessConstraintsCallbacks() { | |
| 81 return number_of_successfull_constraints_applied_; | |
| 82 } | |
| 83 | |
| 84 int NumberOfFailedConstraintsCallbacks() { | |
| 85 return number_of_failed_constraints_applied_; | |
| 86 } | |
| 87 | |
| 61 private: | 88 private: | 
| 89 void OnConstraintsApplied(MediaStreamSource* source, bool success) { | |
| 90 ASSERT_EQ(source, webkit_source_.extraData()); | |
| 91 | |
| 92 if (success) | |
| 93 ++number_of_successfull_constraints_applied_; | |
| 94 else | |
| 95 ++number_of_failed_constraints_applied_; | |
| 96 } | |
| 97 | |
| 98 int number_of_successfull_constraints_applied_; | |
| 99 int number_of_failed_constraints_applied_; | |
| 62 MockMediaStreamDependencyFactory factory_; | 100 MockMediaStreamDependencyFactory factory_; | 
| 63 blink::WebMediaStreamSource webkit_source_; | 101 blink::WebMediaStreamSource webkit_source_; | 
| 64 }; | 102 }; | 
| 65 | 103 | 
| 104 TEST_F(MediaStreamVideoSourceTest, AddTrackAndStartAdapter) { | |
| 105 blink::WebMediaConstraints constraints; | |
| 106 blink::WebMediaStreamTrack track = CreateTrack("123", constraints); | |
| 107 StartAdapter(); | |
| 108 EXPECT_EQ(1, NumberOfSuccessConstraintsCallbacks()); | |
| 109 } | |
| 110 | |
| 111 TEST_F(MediaStreamVideoSourceTest, AddTwoTracksBeforeAdapterStart) { | |
| 112 blink::WebMediaConstraints constraints; | |
| 113 blink::WebMediaStreamTrack track1 = CreateTrack("123", constraints); | |
| 114 blink::WebMediaStreamTrack track2 = CreateTrack("123", constraints); | |
| 115 EXPECT_EQ(0, NumberOfSuccessConstraintsCallbacks()); | |
| 116 StartAdapter(); | |
| 117 EXPECT_EQ(2, NumberOfSuccessConstraintsCallbacks()); | |
| 118 } | |
| 119 | |
| 120 TEST_F(MediaStreamVideoSourceTest, AddTrackAfterAdapterStart) { | |
| 121 blink::WebMediaConstraints constraints; | |
| 122 blink::WebMediaStreamTrack track1 = CreateTrack("123", constraints); | |
| 123 StartAdapter(); | |
| 124 EXPECT_EQ(1, NumberOfSuccessConstraintsCallbacks()); | |
| 125 blink::WebMediaStreamTrack track2 = CreateTrack("123", constraints); | |
| 126 EXPECT_EQ(2, NumberOfSuccessConstraintsCallbacks()); | |
| 127 } | |
| 128 | |
| 129 TEST_F(MediaStreamVideoSourceTest, AddTrackAndFailToStartAdapter) { | |
| 130 blink::WebMediaConstraints constraints; | |
| 131 blink::WebMediaStreamTrack track = CreateTrack("123", constraints); | |
| 132 FailToStartAdapter(); | |
| 133 EXPECT_EQ(1, NumberOfFailedConstraintsCallbacks()); | |
| 134 } | |
| 135 | |
| 66 TEST_F(MediaStreamVideoSourceTest, DeliverVideoFrame) { | 136 TEST_F(MediaStreamVideoSourceTest, DeliverVideoFrame) { | 
| 67 blink::WebMediaConstraints constraints; | 137 blink::WebMediaConstraints constraints; | 
| 68 blink::WebMediaStreamTrack track = CreateTrack("123"); | 138 blink::WebMediaStreamTrack track = CreateTrack("123", constraints); | 
| 139 StartAdapter(); | |
| 
Ronghua Wu (Left Chromium)
2014/01/16 23:02:37
we don't need to start adapter in the real case, r
 
perkj_chrome
2014/01/17 13:19:45
I guess that depend on the webrtc::MediaStreamVide
 | |
| 69 DummyMediaStreamVideoSource* source = | 140 DummyMediaStreamVideoSource* source = | 
| 70 static_cast<DummyMediaStreamVideoSource*>(track.source().extraData()); | 141 static_cast<DummyMediaStreamVideoSource*>(track.source().extraData()); | 
| 71 source->AddTrack(track, constraints); | |
| 72 VerifyFrame(0, 0, 0); | 142 VerifyFrame(0, 0, 0); | 
| 73 const int kWidth = 640; | 143 const int kWidth = 640; | 
| 74 const int kHeight = 480; | 144 const int kHeight = 480; | 
| 75 scoped_refptr<media::VideoFrame> frame = | 145 scoped_refptr<media::VideoFrame> frame = | 
| 76 media::VideoFrame::CreateBlackFrame(gfx::Size(kWidth, kHeight)); | 146 media::VideoFrame::CreateBlackFrame(gfx::Size(kWidth, kHeight)); | 
| 77 ASSERT_TRUE(frame.get()); | 147 ASSERT_TRUE(frame.get()); | 
| 78 source->OnNewFrame(frame); | 148 source->OnNewFrame(frame); | 
| 79 VerifyFrame(640, 480, 1); | 149 VerifyFrame(640, 480, 1); | 
| 80 source->OnNewFrame(frame); | 150 source->OnNewFrame(frame); | 
| 81 VerifyFrame(640, 480, 2); | 151 VerifyFrame(640, 480, 2); | 
| 82 source->RemoveTrack(track); | 152 source->RemoveTrack(track); | 
| 83 } | 153 } | 
| 84 | 154 | 
| 85 } // namespace content | 155 } // namespace content | 
| OLD | NEW |