| 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() |
| 37 factory_.EnsurePeerConnectionFactory(); | 33 : number_of_successful_constraints_applied_(0), |
| 34 number_of_failed_constraints_applied_(0) { |
| 38 webkit_source_.initialize(base::UTF8ToUTF16("dummy_source_id"), | 35 webkit_source_.initialize(base::UTF8ToUTF16("dummy_source_id"), |
| 39 blink::WebMediaStreamSource::TypeVideo, | 36 blink::WebMediaStreamSource::TypeVideo, |
| 40 base::UTF8ToUTF16("dummy_source_name")); | 37 base::UTF8ToUTF16("dummy_source_name")); |
| 41 webkit_source_.setExtraData(new DummyMediaStreamVideoSource(&factory_)); | 38 webkit_source_.setExtraData(new DummyMediaStreamVideoSource(&factory_)); |
| 42 } | 39 } |
| 43 | 40 |
| 44 protected: | 41 protected: |
| 45 // Create a track that's associated with |webkit_source_|. | 42 // Create a track that's associated with |webkit_source_|. |
| 46 blink::WebMediaStreamTrack CreateTrack(const std::string& id) { | 43 blink::WebMediaStreamTrack CreateTrack( |
| 44 const std::string& id, |
| 45 const blink::WebMediaConstraints& constraints) { |
| 47 blink::WebMediaStreamTrack track; | 46 blink::WebMediaStreamTrack track; |
| 48 track.initialize(base::UTF8ToUTF16(id), webkit_source_); | 47 track.initialize(base::UTF8ToUTF16(id), webkit_source_); |
| 48 |
| 49 DummyMediaStreamVideoSource* source = |
| 50 static_cast<DummyMediaStreamVideoSource*>(track.source().extraData()); |
| 51 |
| 52 source->AddTrack(track, |
| 53 constraints, |
| 54 base::Bind( |
| 55 &MediaStreamVideoSourceTest::OnConstraintsApplied, |
| 56 base::Unretained(this))); |
| 49 return track; | 57 return track; |
| 50 } | 58 } |
| 51 | 59 |
| 60 // Simulate that the underlying device start successfully. |
| 61 void StartSource() { |
| 62 factory_.last_video_source()->SetLive(); |
| 63 } |
| 64 |
| 65 // Simulate that the underlying device fail to start. |
| 66 void FailToStartSource() { |
| 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() const { |
| 81 return number_of_successful_constraints_applied_; |
| 82 } |
| 83 |
| 84 int NumberOfFailedConstraintsCallbacks() const { |
| 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_successful_constraints_applied_; |
| 94 else |
| 95 ++number_of_failed_constraints_applied_; |
| 96 } |
| 97 |
| 98 int number_of_successful_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 StartSource(); |
| 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 StartSource(); |
| 117 EXPECT_EQ(2, NumberOfSuccessConstraintsCallbacks()); |
| 118 } |
| 119 |
| 120 TEST_F(MediaStreamVideoSourceTest, AddTrackAfterAdapterStart) { |
| 121 blink::WebMediaConstraints constraints; |
| 122 blink::WebMediaStreamTrack track1 = CreateTrack("123", constraints); |
| 123 StartSource(); |
| 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 FailToStartSource(); |
| 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 StartSource(); |
| 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 |