| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2016 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/api/video/i420_buffer.h" | 11 #include "webrtc/api/video/i420_buffer.h" |
| 12 #include "webrtc/api/video/video_frame.h" | 12 #include "webrtc/api/video/video_frame.h" |
| 13 #include "webrtc/base/gunit.h" | |
| 14 #include "webrtc/media/base/fakevideorenderer.h" | 13 #include "webrtc/media/base/fakevideorenderer.h" |
| 15 #include "webrtc/media/base/videobroadcaster.h" | 14 #include "webrtc/media/base/videobroadcaster.h" |
| 15 #include "webrtc/test/gtest.h" |
| 16 | 16 |
| 17 using rtc::VideoBroadcaster; | 17 using rtc::VideoBroadcaster; |
| 18 using rtc::VideoSinkWants; | 18 using rtc::VideoSinkWants; |
| 19 using cricket::FakeVideoRenderer; | 19 using cricket::FakeVideoRenderer; |
| 20 | 20 |
| 21 namespace test { |
| 22 namespace { |
| 23 void ExpectEqualSinkWantsValueRange(const VideoSinkWants::Range range1, |
| 24 const VideoSinkWants::Range range2) { |
| 25 EXPECT_EQ(range1.min, range2.min); |
| 26 EXPECT_EQ(range1.target, range2.target); |
| 27 EXPECT_EQ(range1.max, range2.max); |
| 28 } |
| 29 |
| 30 void ExpectEqualSinkWants(const VideoSinkWants& wants1, |
| 31 const VideoSinkWants& wants2) { |
| 32 EXPECT_EQ(wants1.rotation_applied, wants2.rotation_applied); |
| 33 EXPECT_EQ(wants1.black_frames, wants2.black_frames); |
| 34 |
| 35 if (wants1.pixel_count && wants2.pixel_count) { |
| 36 ExpectEqualSinkWantsValueRange(*wants1.pixel_count, *wants2.pixel_count); |
| 37 } else { |
| 38 EXPECT_EQ(static_cast<bool>(wants1.pixel_count), |
| 39 static_cast<bool>(wants2.pixel_count)); |
| 40 } |
| 41 |
| 42 if (wants1.framerate_fps_ && wants2.framerate_fps_) { |
| 43 ExpectEqualSinkWantsValueRange(*wants1.framerate_fps_, |
| 44 *wants2.framerate_fps_); |
| 45 } else { |
| 46 EXPECT_EQ(static_cast<bool>(wants1.framerate_fps_), |
| 47 static_cast<bool>(wants2.framerate_fps_)); |
| 48 } |
| 49 } |
| 50 } |
| 21 | 51 |
| 22 TEST(VideoBroadcasterTest, frame_wanted) { | 52 TEST(VideoBroadcasterTest, frame_wanted) { |
| 23 VideoBroadcaster broadcaster; | 53 VideoBroadcaster broadcaster; |
| 24 EXPECT_FALSE(broadcaster.frame_wanted()); | 54 EXPECT_FALSE(broadcaster.frame_wanted()); |
| 25 | 55 |
| 26 FakeVideoRenderer sink; | 56 FakeVideoRenderer sink; |
| 27 broadcaster.AddOrUpdateSink(&sink, rtc::VideoSinkWants()); | 57 broadcaster.AddOrUpdateSink(&sink, rtc::VideoSinkWants()); |
| 28 EXPECT_TRUE(broadcaster.frame_wanted()); | 58 EXPECT_TRUE(broadcaster.frame_wanted()); |
| 29 | 59 |
| 30 broadcaster.RemoveSink(&sink); | 60 broadcaster.RemoveSink(&sink); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 VideoSinkWants wants2; | 108 VideoSinkWants wants2; |
| 79 wants2.rotation_applied = true; | 109 wants2.rotation_applied = true; |
| 80 | 110 |
| 81 broadcaster.AddOrUpdateSink(&sink2, wants2); | 111 broadcaster.AddOrUpdateSink(&sink2, wants2); |
| 82 EXPECT_TRUE(broadcaster.wants().rotation_applied); | 112 EXPECT_TRUE(broadcaster.wants().rotation_applied); |
| 83 | 113 |
| 84 broadcaster.RemoveSink(&sink2); | 114 broadcaster.RemoveSink(&sink2); |
| 85 EXPECT_FALSE(broadcaster.wants().rotation_applied); | 115 EXPECT_FALSE(broadcaster.wants().rotation_applied); |
| 86 } | 116 } |
| 87 | 117 |
| 88 TEST(VideoBroadcasterTest, AppliesMinOfSinkWantsMaxPixelCount) { | 118 TEST(VideoBroadcasterTest, MergedSinkWantsRespectsBounds) { |
| 89 VideoBroadcaster broadcaster; | 119 VideoBroadcaster broadcaster; |
| 90 EXPECT_TRUE(!broadcaster.wants().max_pixel_count); | 120 EXPECT_FALSE(static_cast<bool>(broadcaster.wants().pixel_count)); |
| 91 | 121 |
| 92 FakeVideoRenderer sink1; | 122 FakeVideoRenderer sink1; |
| 93 VideoSinkWants wants1; | 123 VideoSinkWants wants1; |
| 94 wants1.max_pixel_count = rtc::Optional<int>(1280 * 720); | 124 wants1.pixel_count.emplace(VideoSinkWants::Range(100, 200, 300)); |
| 95 | |
| 96 broadcaster.AddOrUpdateSink(&sink1, wants1); | 125 broadcaster.AddOrUpdateSink(&sink1, wants1); |
| 97 EXPECT_EQ(1280 * 720, *broadcaster.wants().max_pixel_count); | 126 ExpectEqualSinkWants(wants1, broadcaster.wants()); |
| 98 | 127 |
| 99 FakeVideoRenderer sink2; | 128 FakeVideoRenderer sink2; |
| 100 VideoSinkWants wants2; | 129 VideoSinkWants wants2; |
| 101 wants2.max_pixel_count = rtc::Optional<int>(640 * 360); | 130 // Min above sink1, target below, max below. New range overrides all values. |
| 131 wants2.pixel_count.emplace(VideoSinkWants::Range(110, 190, 290)); |
| 102 broadcaster.AddOrUpdateSink(&sink2, wants2); | 132 broadcaster.AddOrUpdateSink(&sink2, wants2); |
| 103 EXPECT_EQ(640 * 360, *broadcaster.wants().max_pixel_count); | 133 ExpectEqualSinkWants(wants2, broadcaster.wants()); |
| 104 | 134 |
| 135 FakeVideoRenderer sink3; |
| 136 VideoSinkWants wants3; |
| 137 // Min below sink1, target above, max above. New values at bottom of pile. |
| 138 wants3.pixel_count.emplace(VideoSinkWants::Range(90, 210, 310)); |
| 139 broadcaster.AddOrUpdateSink(&sink3, wants3); |
| 140 ExpectEqualSinkWants(wants2, broadcaster.wants()); |
| 141 |
| 142 // Removes sink2, sink1 should rule again. |
| 105 broadcaster.RemoveSink(&sink2); | 143 broadcaster.RemoveSink(&sink2); |
| 106 EXPECT_EQ(1280 * 720, *broadcaster.wants().max_pixel_count); | 144 ExpectEqualSinkWants(wants1, broadcaster.wants()); |
| 145 |
| 146 // Removes sink1, sink3 is only one left. |
| 147 broadcaster.RemoveSink(&sink1); |
| 148 ExpectEqualSinkWants(wants3, broadcaster.wants()); |
| 149 |
| 150 // None left alive. |
| 151 broadcaster.RemoveSink(&sink3); |
| 152 EXPECT_FALSE(static_cast<bool>(broadcaster.wants().pixel_count)); |
| 107 } | 153 } |
| 108 | 154 |
| 109 TEST(VideoBroadcasterTest, AppliesMinOfSinkWantsMaxAndTargetPixelCount) { | 155 TEST(VideoBroadcasterTest, MergedSinkWantsRespectsHighMin) { |
| 110 VideoBroadcaster broadcaster; | 156 VideoBroadcaster broadcaster; |
| 111 EXPECT_TRUE(!broadcaster.wants().target_pixel_count); | 157 EXPECT_FALSE(static_cast<bool>(broadcaster.wants().pixel_count)); |
| 112 | 158 |
| 113 FakeVideoRenderer sink1; | 159 FakeVideoRenderer sink1; |
| 114 VideoSinkWants wants1; | 160 VideoSinkWants wants1; |
| 115 wants1.target_pixel_count = rtc::Optional<int>(1280 * 720); | 161 wants1.pixel_count.emplace(VideoSinkWants::Range(100, 200, 300)); |
| 116 | |
| 117 broadcaster.AddOrUpdateSink(&sink1, wants1); | 162 broadcaster.AddOrUpdateSink(&sink1, wants1); |
| 118 EXPECT_EQ(1280 * 720, *broadcaster.wants().target_pixel_count); | 163 ExpectEqualSinkWants(wants1, broadcaster.wants()); |
| 119 | 164 |
| 120 FakeVideoRenderer sink2; | 165 FakeVideoRenderer sink2; |
| 121 VideoSinkWants wants2; | 166 VideoSinkWants wants2; |
| 122 wants2.target_pixel_count = rtc::Optional<int>(640 * 360); | 167 // Min above wants1, so overrides. Goes above max of sink 1, so will be capped |
| 168 // at sink1.max. |
| 169 wants2.pixel_count.emplace(VideoSinkWants::Range(310, 400, 500)); |
| 123 broadcaster.AddOrUpdateSink(&sink2, wants2); | 170 broadcaster.AddOrUpdateSink(&sink2, wants2); |
| 124 EXPECT_EQ(640 * 360, *broadcaster.wants().target_pixel_count); | 171 VideoSinkWants expected_merged_wants; |
| 172 expected_merged_wants.pixel_count.emplace( |
| 173 VideoSinkWants::Range(300, 300, 300)); |
| 174 ExpectEqualSinkWants(expected_merged_wants, broadcaster.wants()); |
| 125 | 175 |
| 176 // Removes sink2, sink1 should rule again. |
| 126 broadcaster.RemoveSink(&sink2); | 177 broadcaster.RemoveSink(&sink2); |
| 127 EXPECT_EQ(1280 * 720, *broadcaster.wants().target_pixel_count); | 178 ExpectEqualSinkWants(wants1, broadcaster.wants()); |
| 179 |
| 180 FakeVideoRenderer sink3; |
| 181 VideoSinkWants wants3; |
| 182 // Max above wants1, so overrides. Goes below target of sink 1, overriding |
| 183 // that, but keeps min. |
| 184 wants3.pixel_count.emplace(VideoSinkWants::Range(210, 300, 400)); |
| 185 broadcaster.AddOrUpdateSink(&sink3, wants3); |
| 186 expected_merged_wants.pixel_count.emplace( |
| 187 VideoSinkWants::Range(210, 210, 300)); |
| 188 ExpectEqualSinkWants(expected_merged_wants, broadcaster.wants()); |
| 189 } |
| 190 |
| 191 TEST(VideoBroadcasterTest, MergedSinkWantsRespectsLowMax) { |
| 192 VideoBroadcaster broadcaster; |
| 193 EXPECT_FALSE(static_cast<bool>(broadcaster.wants().pixel_count)); |
| 194 |
| 195 FakeVideoRenderer sink1; |
| 196 VideoSinkWants wants1; |
| 197 wants1.pixel_count.emplace(VideoSinkWants::Range(100, 200, 300)); |
| 198 broadcaster.AddOrUpdateSink(&sink1, wants1); |
| 199 ExpectEqualSinkWants(wants1, broadcaster.wants()); |
| 200 |
| 201 FakeVideoRenderer sink2; |
| 202 VideoSinkWants wants2; |
| 203 // Max below wants1, so overrides. Goes below min of sink 1, so will be capped |
| 204 // at sink2.max. Target then has to follow min. |
| 205 wants2.pixel_count.emplace(VideoSinkWants::Range(40, 50, 90)); |
| 206 broadcaster.AddOrUpdateSink(&sink2, wants2); |
| 207 VideoSinkWants expected_wants; |
| 208 expected_wants.pixel_count.emplace(VideoSinkWants::Range(90, 90, 90)); |
| 209 ExpectEqualSinkWants(expected_wants, broadcaster.wants()); |
| 210 |
| 211 // Removes sink2, sink1 should rule again. |
| 212 broadcaster.RemoveSink(&sink2); |
| 213 ExpectEqualSinkWants(wants1, broadcaster.wants()); |
| 214 |
| 215 FakeVideoRenderer sink3; |
| 216 VideoSinkWants wants3; |
| 217 // Max below wants1, so overrides. Goes below target of sink 1, so will cap |
| 218 // the target but not affect the min. |
| 219 wants3.pixel_count.emplace(VideoSinkWants::Range(90, 150, 150)); |
| 220 broadcaster.AddOrUpdateSink(&sink3, wants3); |
| 221 expected_wants.pixel_count.emplace(VideoSinkWants::Range(100, 150, 150)); |
| 222 ExpectEqualSinkWants(expected_wants, broadcaster.wants()); |
| 128 } | 223 } |
| 129 | 224 |
| 130 TEST(VideoBroadcasterTest, SinkWantsBlackFrames) { | 225 TEST(VideoBroadcasterTest, SinkWantsBlackFrames) { |
| 131 VideoBroadcaster broadcaster; | 226 VideoBroadcaster broadcaster; |
| 132 EXPECT_TRUE(!broadcaster.wants().black_frames); | 227 EXPECT_TRUE(!broadcaster.wants().black_frames); |
| 133 | 228 |
| 134 FakeVideoRenderer sink1; | 229 FakeVideoRenderer sink1; |
| 135 VideoSinkWants wants1; | 230 VideoSinkWants wants1; |
| 136 wants1.black_frames = true; | 231 wants1.black_frames = true; |
| 137 broadcaster.AddOrUpdateSink(&sink1, wants1); | 232 broadcaster.AddOrUpdateSink(&sink1, wants1); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 161 broadcaster.AddOrUpdateSink(&sink2, wants2); | 256 broadcaster.AddOrUpdateSink(&sink2, wants2); |
| 162 | 257 |
| 163 webrtc::VideoFrame frame2(buffer, webrtc::kVideoRotation_0, | 258 webrtc::VideoFrame frame2(buffer, webrtc::kVideoRotation_0, |
| 164 30 /* timestamp_us */); | 259 30 /* timestamp_us */); |
| 165 broadcaster.OnFrame(frame2); | 260 broadcaster.OnFrame(frame2); |
| 166 EXPECT_FALSE(sink1.black_frame()); | 261 EXPECT_FALSE(sink1.black_frame()); |
| 167 EXPECT_EQ(30, sink1.timestamp_us()); | 262 EXPECT_EQ(30, sink1.timestamp_us()); |
| 168 EXPECT_TRUE(sink2.black_frame()); | 263 EXPECT_TRUE(sink2.black_frame()); |
| 169 EXPECT_EQ(30, sink2.timestamp_us()); | 264 EXPECT_EQ(30, sink2.timestamp_us()); |
| 170 } | 265 } |
| 266 |
| 267 } // namespace test |
| OLD | NEW |