Anders Carlsson | fe9d817 | 2018-04-03 11:40:39 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #import <Foundation/Foundation.h> |
| 12 | #import <XCTest/XCTest.h> |
| 13 | |
Anders Carlsson | 7bca8ca | 2018-08-30 09:30:29 +0200 | [diff] [blame] | 14 | #include "sdk/objc/native/src/objc_video_track_source.h" |
Anders Carlsson | fe9d817 | 2018-04-03 11:40:39 +0200 | [diff] [blame] | 15 | |
Anders Carlsson | 4e5af96 | 2018-09-03 14:44:50 +0200 | [diff] [blame] | 16 | #import "api/video_frame_buffer/RTCNativeI420Buffer+Private.h" |
Anders Carlsson | 7bca8ca | 2018-08-30 09:30:29 +0200 | [diff] [blame] | 17 | #import "base/RTCVideoFrame.h" |
| 18 | #import "base/RTCVideoFrameBuffer.h" |
| 19 | #import "components/video_frame_buffer/RTCCVPixelBuffer.h" |
| 20 | #import "frame_buffer_helpers.h" |
| 21 | |
Mirko Bonadei | d970807 | 2019-01-25 20:26:48 +0100 | [diff] [blame] | 22 | #include "api/scoped_refptr.h" |
Anders Carlsson | fe9d817 | 2018-04-03 11:40:39 +0200 | [diff] [blame] | 23 | #include "common_video/libyuv/include/webrtc_libyuv.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 24 | #include "media/base/fake_video_renderer.h" |
| 25 | #include "rtc_base/ref_counted_object.h" |
Anders Carlsson | 7bca8ca | 2018-08-30 09:30:29 +0200 | [diff] [blame] | 26 | #include "sdk/objc/native/api/video_frame.h" |
Anders Carlsson | fe9d817 | 2018-04-03 11:40:39 +0200 | [diff] [blame] | 27 | |
| 28 | typedef void (^VideoSinkCallback)(RTCVideoFrame *); |
| 29 | |
| 30 | namespace { |
| 31 | |
| 32 | class ObjCCallbackVideoSink : public rtc::VideoSinkInterface<webrtc::VideoFrame> { |
| 33 | public: |
| 34 | ObjCCallbackVideoSink(VideoSinkCallback callback) : callback_(callback) {} |
| 35 | |
Mirko Bonadei | 17aff35 | 2018-07-26 12:20:40 +0200 | [diff] [blame] | 36 | void OnFrame(const webrtc::VideoFrame &frame) override { |
Anders Carlsson | fe9d817 | 2018-04-03 11:40:39 +0200 | [diff] [blame] | 37 | callback_(NativeToObjCVideoFrame(frame)); |
| 38 | } |
| 39 | |
| 40 | private: |
| 41 | VideoSinkCallback callback_; |
| 42 | }; |
| 43 | |
| 44 | } // namespace |
| 45 | |
| 46 | @interface ObjCVideoTrackSourceTests : XCTestCase |
| 47 | @end |
| 48 | |
| 49 | @implementation ObjCVideoTrackSourceTests { |
| 50 | rtc::scoped_refptr<webrtc::ObjCVideoTrackSource> _video_source; |
| 51 | } |
| 52 | |
| 53 | - (void)setUp { |
| 54 | _video_source = new rtc::RefCountedObject<webrtc::ObjCVideoTrackSource>(); |
| 55 | } |
| 56 | |
| 57 | - (void)tearDown { |
| 58 | _video_source = NULL; |
| 59 | } |
| 60 | |
| 61 | - (void)testOnCapturedFrameAdaptsFrame { |
| 62 | CVPixelBufferRef pixelBufferRef = NULL; |
| 63 | CVPixelBufferCreate( |
| 64 | NULL, 720, 1280, kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange, NULL, &pixelBufferRef); |
| 65 | |
| 66 | RTCCVPixelBuffer *buffer = [[RTCCVPixelBuffer alloc] initWithPixelBuffer:pixelBufferRef]; |
| 67 | |
| 68 | RTCVideoFrame *frame = |
| 69 | [[RTCVideoFrame alloc] initWithBuffer:buffer rotation:RTCVideoRotation_0 timeStampNs:0]; |
| 70 | |
| 71 | cricket::FakeVideoRenderer *video_renderer = new cricket::FakeVideoRenderer(); |
| 72 | const rtc::VideoSinkWants video_sink_wants; |
| 73 | rtc::VideoSourceInterface<webrtc::VideoFrame> *video_source_interface = _video_source; |
| 74 | video_source_interface->AddOrUpdateSink(video_renderer, video_sink_wants); |
| 75 | |
| 76 | _video_source->OnOutputFormatRequest(640, 360, 30); |
| 77 | _video_source->OnCapturedFrame(frame); |
| 78 | |
| 79 | XCTAssertEqual(video_renderer->num_rendered_frames(), 1); |
| 80 | XCTAssertEqual(video_renderer->width(), 360); |
| 81 | XCTAssertEqual(video_renderer->height(), 640); |
| 82 | |
| 83 | CVBufferRelease(pixelBufferRef); |
| 84 | } |
| 85 | |
Peter Hanspers | 488eb98 | 2018-06-08 14:49:51 +0200 | [diff] [blame] | 86 | - (void)testOnCapturedFrameAdaptsFrameWithAlignment { |
| 87 | // Requesting to adapt 1280x720 to 912x514 gives 639x360 without alignment. The 639 causes issues |
| 88 | // with some hardware encoders (e.g. HEVC) so in this test we verify that the alignment is set and |
| 89 | // respected. |
| 90 | |
| 91 | CVPixelBufferRef pixelBufferRef = NULL; |
| 92 | CVPixelBufferCreate( |
| 93 | NULL, 720, 1280, kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange, NULL, &pixelBufferRef); |
| 94 | |
| 95 | RTCCVPixelBuffer *buffer = [[RTCCVPixelBuffer alloc] initWithPixelBuffer:pixelBufferRef]; |
| 96 | |
| 97 | RTCVideoFrame *frame = |
| 98 | [[RTCVideoFrame alloc] initWithBuffer:buffer rotation:RTCVideoRotation_0 timeStampNs:0]; |
| 99 | |
| 100 | cricket::FakeVideoRenderer *video_renderer = new cricket::FakeVideoRenderer(); |
| 101 | const rtc::VideoSinkWants video_sink_wants; |
| 102 | rtc::VideoSourceInterface<webrtc::VideoFrame> *video_source_interface = _video_source; |
| 103 | video_source_interface->AddOrUpdateSink(video_renderer, video_sink_wants); |
| 104 | |
| 105 | _video_source->OnOutputFormatRequest(912, 514, 30); |
| 106 | _video_source->OnCapturedFrame(frame); |
| 107 | |
| 108 | XCTAssertEqual(video_renderer->num_rendered_frames(), 1); |
| 109 | XCTAssertEqual(video_renderer->width(), 360); |
| 110 | XCTAssertEqual(video_renderer->height(), 640); |
| 111 | |
| 112 | CVBufferRelease(pixelBufferRef); |
| 113 | } |
| 114 | |
Kári Tristan Helgason | bf45add | 2019-08-23 12:57:50 +0200 | [diff] [blame] | 115 | - (void)testOnCapturedFrameAdaptationResultsInCommonResolutions { |
| 116 | // Some of the most common resolutions used in the wild are 640x360, 480x270 and 320x180. |
| 117 | // Make sure that we properly scale down to exactly these resolutions. |
| 118 | CVPixelBufferRef pixelBufferRef = NULL; |
| 119 | CVPixelBufferCreate( |
| 120 | NULL, 720, 1280, kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange, NULL, &pixelBufferRef); |
| 121 | |
| 122 | RTCCVPixelBuffer *buffer = [[RTCCVPixelBuffer alloc] initWithPixelBuffer:pixelBufferRef]; |
| 123 | |
| 124 | RTCVideoFrame *frame = [[RTCVideoFrame alloc] initWithBuffer:buffer |
| 125 | rotation:RTCVideoRotation_0 |
| 126 | timeStampNs:0]; |
| 127 | |
| 128 | cricket::FakeVideoRenderer *video_renderer = new cricket::FakeVideoRenderer(); |
| 129 | const rtc::VideoSinkWants video_sink_wants; |
| 130 | rtc::VideoSourceInterface<webrtc::VideoFrame> *video_source_interface = _video_source; |
| 131 | video_source_interface->AddOrUpdateSink(video_renderer, video_sink_wants); |
| 132 | |
| 133 | _video_source->OnOutputFormatRequest(640, 360, 30); |
| 134 | _video_source->OnCapturedFrame(frame); |
| 135 | |
| 136 | XCTAssertEqual(video_renderer->num_rendered_frames(), 1); |
| 137 | XCTAssertEqual(video_renderer->width(), 360); |
| 138 | XCTAssertEqual(video_renderer->height(), 640); |
| 139 | |
| 140 | _video_source->OnOutputFormatRequest(480, 270, 30); |
| 141 | _video_source->OnCapturedFrame(frame); |
| 142 | |
| 143 | XCTAssertEqual(video_renderer->num_rendered_frames(), 2); |
| 144 | XCTAssertEqual(video_renderer->width(), 270); |
| 145 | XCTAssertEqual(video_renderer->height(), 480); |
| 146 | |
| 147 | _video_source->OnOutputFormatRequest(320, 180, 30); |
| 148 | _video_source->OnCapturedFrame(frame); |
| 149 | |
| 150 | XCTAssertEqual(video_renderer->num_rendered_frames(), 3); |
| 151 | XCTAssertEqual(video_renderer->width(), 180); |
| 152 | XCTAssertEqual(video_renderer->height(), 320); |
| 153 | |
| 154 | CVBufferRelease(pixelBufferRef); |
| 155 | } |
| 156 | |
Anders Carlsson | fe9d817 | 2018-04-03 11:40:39 +0200 | [diff] [blame] | 157 | - (void)testOnCapturedFrameWithoutAdaptation { |
| 158 | CVPixelBufferRef pixelBufferRef = NULL; |
| 159 | CVPixelBufferCreate( |
| 160 | NULL, 360, 640, kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange, NULL, &pixelBufferRef); |
| 161 | |
| 162 | RTCCVPixelBuffer *buffer = [[RTCCVPixelBuffer alloc] initWithPixelBuffer:pixelBufferRef]; |
| 163 | RTCVideoFrame *frame = |
| 164 | [[RTCVideoFrame alloc] initWithBuffer:buffer rotation:RTCVideoRotation_0 timeStampNs:0]; |
| 165 | |
| 166 | XCTestExpectation *callbackExpectation = [self expectationWithDescription:@"videoSinkCallback"]; |
| 167 | ObjCCallbackVideoSink callback_video_sink(^void(RTCVideoFrame *outputFrame) { |
| 168 | XCTAssertEqual(frame.width, outputFrame.width); |
| 169 | XCTAssertEqual(frame.height, outputFrame.height); |
| 170 | |
| 171 | RTCCVPixelBuffer *outputBuffer = outputFrame.buffer; |
| 172 | XCTAssertEqual(buffer.cropX, outputBuffer.cropX); |
| 173 | XCTAssertEqual(buffer.cropY, outputBuffer.cropY); |
| 174 | XCTAssertEqual(buffer.pixelBuffer, outputBuffer.pixelBuffer); |
| 175 | |
| 176 | [callbackExpectation fulfill]; |
| 177 | }); |
| 178 | |
| 179 | const rtc::VideoSinkWants video_sink_wants; |
| 180 | rtc::VideoSourceInterface<webrtc::VideoFrame> *video_source_interface = _video_source; |
| 181 | video_source_interface->AddOrUpdateSink(&callback_video_sink, video_sink_wants); |
| 182 | |
| 183 | _video_source->OnOutputFormatRequest(640, 360, 30); |
| 184 | _video_source->OnCapturedFrame(frame); |
| 185 | |
| 186 | [self waitForExpectations:@[ callbackExpectation ] timeout:10.0]; |
| 187 | CVBufferRelease(pixelBufferRef); |
| 188 | } |
| 189 | |
| 190 | - (void)testOnCapturedFrameCVPixelBufferNeedsAdaptation { |
| 191 | CVPixelBufferRef pixelBufferRef = NULL; |
| 192 | CVPixelBufferCreate( |
| 193 | NULL, 720, 1280, kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange, NULL, &pixelBufferRef); |
| 194 | |
| 195 | RTCCVPixelBuffer *buffer = [[RTCCVPixelBuffer alloc] initWithPixelBuffer:pixelBufferRef]; |
| 196 | RTCVideoFrame *frame = |
| 197 | [[RTCVideoFrame alloc] initWithBuffer:buffer rotation:RTCVideoRotation_0 timeStampNs:0]; |
| 198 | |
| 199 | XCTestExpectation *callbackExpectation = [self expectationWithDescription:@"videoSinkCallback"]; |
| 200 | ObjCCallbackVideoSink callback_video_sink(^void(RTCVideoFrame *outputFrame) { |
| 201 | XCTAssertEqual(outputFrame.width, 360); |
| 202 | XCTAssertEqual(outputFrame.height, 640); |
| 203 | |
| 204 | RTCCVPixelBuffer *outputBuffer = outputFrame.buffer; |
| 205 | XCTAssertEqual(outputBuffer.cropX, 0); |
| 206 | XCTAssertEqual(outputBuffer.cropY, 0); |
| 207 | XCTAssertEqual(buffer.pixelBuffer, outputBuffer.pixelBuffer); |
| 208 | |
| 209 | [callbackExpectation fulfill]; |
| 210 | }); |
| 211 | |
| 212 | const rtc::VideoSinkWants video_sink_wants; |
| 213 | rtc::VideoSourceInterface<webrtc::VideoFrame> *video_source_interface = _video_source; |
| 214 | video_source_interface->AddOrUpdateSink(&callback_video_sink, video_sink_wants); |
| 215 | |
| 216 | _video_source->OnOutputFormatRequest(640, 360, 30); |
| 217 | _video_source->OnCapturedFrame(frame); |
| 218 | |
| 219 | [self waitForExpectations:@[ callbackExpectation ] timeout:10.0]; |
| 220 | CVBufferRelease(pixelBufferRef); |
| 221 | } |
| 222 | |
| 223 | - (void)testOnCapturedFrameCVPixelBufferNeedsCropping { |
| 224 | CVPixelBufferRef pixelBufferRef = NULL; |
| 225 | CVPixelBufferCreate( |
| 226 | NULL, 380, 640, kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange, NULL, &pixelBufferRef); |
| 227 | |
| 228 | RTCCVPixelBuffer *buffer = [[RTCCVPixelBuffer alloc] initWithPixelBuffer:pixelBufferRef]; |
| 229 | RTCVideoFrame *frame = |
| 230 | [[RTCVideoFrame alloc] initWithBuffer:buffer rotation:RTCVideoRotation_0 timeStampNs:0]; |
| 231 | |
| 232 | XCTestExpectation *callbackExpectation = [self expectationWithDescription:@"videoSinkCallback"]; |
| 233 | ObjCCallbackVideoSink callback_video_sink(^void(RTCVideoFrame *outputFrame) { |
| 234 | XCTAssertEqual(outputFrame.width, 360); |
| 235 | XCTAssertEqual(outputFrame.height, 640); |
| 236 | |
| 237 | RTCCVPixelBuffer *outputBuffer = outputFrame.buffer; |
| 238 | XCTAssertEqual(outputBuffer.cropX, 10); |
| 239 | XCTAssertEqual(outputBuffer.cropY, 0); |
| 240 | XCTAssertEqual(buffer.pixelBuffer, outputBuffer.pixelBuffer); |
| 241 | |
| 242 | [callbackExpectation fulfill]; |
| 243 | }); |
| 244 | |
| 245 | const rtc::VideoSinkWants video_sink_wants; |
| 246 | rtc::VideoSourceInterface<webrtc::VideoFrame> *video_source_interface = _video_source; |
| 247 | video_source_interface->AddOrUpdateSink(&callback_video_sink, video_sink_wants); |
| 248 | |
| 249 | _video_source->OnOutputFormatRequest(640, 360, 30); |
| 250 | _video_source->OnCapturedFrame(frame); |
| 251 | |
| 252 | [self waitForExpectations:@[ callbackExpectation ] timeout:10.0]; |
| 253 | CVBufferRelease(pixelBufferRef); |
| 254 | } |
| 255 | |
| 256 | - (void)testOnCapturedFramePreAdaptedCVPixelBufferNeedsAdaptation { |
| 257 | CVPixelBufferRef pixelBufferRef = NULL; |
| 258 | CVPixelBufferCreate( |
| 259 | NULL, 720, 1280, kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange, NULL, &pixelBufferRef); |
| 260 | |
Kári Tristan Helgason | bf45add | 2019-08-23 12:57:50 +0200 | [diff] [blame] | 261 | // Create a frame that's already adapted down. |
Anders Carlsson | fe9d817 | 2018-04-03 11:40:39 +0200 | [diff] [blame] | 262 | RTCCVPixelBuffer *buffer = [[RTCCVPixelBuffer alloc] initWithPixelBuffer:pixelBufferRef |
Kári Tristan Helgason | bf45add | 2019-08-23 12:57:50 +0200 | [diff] [blame] | 263 | adaptedWidth:640 |
| 264 | adaptedHeight:360 |
Anders Carlsson | fe9d817 | 2018-04-03 11:40:39 +0200 | [diff] [blame] | 265 | cropWidth:720 |
| 266 | cropHeight:1280 |
| 267 | cropX:0 |
| 268 | cropY:0]; |
| 269 | RTCVideoFrame *frame = |
| 270 | [[RTCVideoFrame alloc] initWithBuffer:buffer rotation:RTCVideoRotation_0 timeStampNs:0]; |
| 271 | |
| 272 | XCTestExpectation *callbackExpectation = [self expectationWithDescription:@"videoSinkCallback"]; |
| 273 | ObjCCallbackVideoSink callback_video_sink(^void(RTCVideoFrame *outputFrame) { |
Kári Tristan Helgason | bf45add | 2019-08-23 12:57:50 +0200 | [diff] [blame] | 274 | XCTAssertEqual(outputFrame.width, 480); |
| 275 | XCTAssertEqual(outputFrame.height, 270); |
Anders Carlsson | fe9d817 | 2018-04-03 11:40:39 +0200 | [diff] [blame] | 276 | |
| 277 | RTCCVPixelBuffer *outputBuffer = outputFrame.buffer; |
Kári Tristan Helgason | bf45add | 2019-08-23 12:57:50 +0200 | [diff] [blame] | 278 | XCTAssertEqual(outputBuffer.cropX, 0); |
| 279 | XCTAssertEqual(outputBuffer.cropY, 0); |
| 280 | XCTAssertEqual(outputBuffer.cropWidth, 640); |
| 281 | XCTAssertEqual(outputBuffer.cropHeight, 360); |
Anders Carlsson | fe9d817 | 2018-04-03 11:40:39 +0200 | [diff] [blame] | 282 | XCTAssertEqual(buffer.pixelBuffer, outputBuffer.pixelBuffer); |
| 283 | |
| 284 | [callbackExpectation fulfill]; |
| 285 | }); |
| 286 | |
| 287 | const rtc::VideoSinkWants video_sink_wants; |
| 288 | rtc::VideoSourceInterface<webrtc::VideoFrame> *video_source_interface = _video_source; |
| 289 | video_source_interface->AddOrUpdateSink(&callback_video_sink, video_sink_wants); |
| 290 | |
Kári Tristan Helgason | bf45add | 2019-08-23 12:57:50 +0200 | [diff] [blame] | 291 | _video_source->OnOutputFormatRequest(480, 270, 30); |
Anders Carlsson | fe9d817 | 2018-04-03 11:40:39 +0200 | [diff] [blame] | 292 | _video_source->OnCapturedFrame(frame); |
| 293 | |
| 294 | [self waitForExpectations:@[ callbackExpectation ] timeout:10.0]; |
| 295 | CVBufferRelease(pixelBufferRef); |
| 296 | } |
| 297 | |
| 298 | - (void)testOnCapturedFramePreCroppedCVPixelBufferNeedsCropping { |
| 299 | CVPixelBufferRef pixelBufferRef = NULL; |
| 300 | CVPixelBufferCreate( |
| 301 | NULL, 380, 640, kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange, NULL, &pixelBufferRef); |
| 302 | |
| 303 | RTCCVPixelBuffer *buffer = [[RTCCVPixelBuffer alloc] initWithPixelBuffer:pixelBufferRef |
| 304 | adaptedWidth:370 |
| 305 | adaptedHeight:640 |
| 306 | cropWidth:370 |
| 307 | cropHeight:640 |
| 308 | cropX:10 |
| 309 | cropY:0]; |
| 310 | RTCVideoFrame *frame = |
| 311 | [[RTCVideoFrame alloc] initWithBuffer:buffer rotation:RTCVideoRotation_0 timeStampNs:0]; |
| 312 | |
| 313 | XCTestExpectation *callbackExpectation = [self expectationWithDescription:@"videoSinkCallback"]; |
| 314 | ObjCCallbackVideoSink callback_video_sink(^void(RTCVideoFrame *outputFrame) { |
| 315 | XCTAssertEqual(outputFrame.width, 360); |
| 316 | XCTAssertEqual(outputFrame.height, 640); |
| 317 | |
| 318 | RTCCVPixelBuffer *outputBuffer = outputFrame.buffer; |
| 319 | XCTAssertEqual(outputBuffer.cropX, 14); |
| 320 | XCTAssertEqual(outputBuffer.cropY, 0); |
| 321 | XCTAssertEqual(outputBuffer.cropWidth, 360); |
| 322 | XCTAssertEqual(outputBuffer.cropHeight, 640); |
| 323 | XCTAssertEqual(buffer.pixelBuffer, outputBuffer.pixelBuffer); |
| 324 | |
| 325 | [callbackExpectation fulfill]; |
| 326 | }); |
| 327 | |
| 328 | const rtc::VideoSinkWants video_sink_wants; |
| 329 | rtc::VideoSourceInterface<webrtc::VideoFrame> *video_source_interface = _video_source; |
| 330 | video_source_interface->AddOrUpdateSink(&callback_video_sink, video_sink_wants); |
| 331 | |
| 332 | _video_source->OnOutputFormatRequest(640, 360, 30); |
| 333 | _video_source->OnCapturedFrame(frame); |
| 334 | |
| 335 | [self waitForExpectations:@[ callbackExpectation ] timeout:10.0]; |
| 336 | CVBufferRelease(pixelBufferRef); |
| 337 | } |
| 338 | |
| 339 | - (void)testOnCapturedFrameSmallerPreCroppedCVPixelBufferNeedsCropping { |
| 340 | CVPixelBufferRef pixelBufferRef = NULL; |
| 341 | CVPixelBufferCreate( |
| 342 | NULL, 380, 640, kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange, NULL, &pixelBufferRef); |
| 343 | |
| 344 | RTCCVPixelBuffer *buffer = [[RTCCVPixelBuffer alloc] initWithPixelBuffer:pixelBufferRef |
| 345 | adaptedWidth:300 |
| 346 | adaptedHeight:640 |
| 347 | cropWidth:300 |
| 348 | cropHeight:640 |
| 349 | cropX:40 |
| 350 | cropY:0]; |
| 351 | RTCVideoFrame *frame = |
| 352 | [[RTCVideoFrame alloc] initWithBuffer:buffer rotation:RTCVideoRotation_0 timeStampNs:0]; |
| 353 | |
| 354 | XCTestExpectation *callbackExpectation = [self expectationWithDescription:@"videoSinkCallback"]; |
| 355 | ObjCCallbackVideoSink callback_video_sink(^void(RTCVideoFrame *outputFrame) { |
| 356 | XCTAssertEqual(outputFrame.width, 300); |
Kári Tristan Helgason | bf45add | 2019-08-23 12:57:50 +0200 | [diff] [blame] | 357 | XCTAssertEqual(outputFrame.height, 534); |
Anders Carlsson | fe9d817 | 2018-04-03 11:40:39 +0200 | [diff] [blame] | 358 | |
| 359 | RTCCVPixelBuffer *outputBuffer = outputFrame.buffer; |
| 360 | XCTAssertEqual(outputBuffer.cropX, 40); |
| 361 | XCTAssertEqual(outputBuffer.cropY, 52); |
| 362 | XCTAssertEqual(outputBuffer.cropWidth, 300); |
Kári Tristan Helgason | bf45add | 2019-08-23 12:57:50 +0200 | [diff] [blame] | 363 | XCTAssertEqual(outputBuffer.cropHeight, 534); |
Anders Carlsson | fe9d817 | 2018-04-03 11:40:39 +0200 | [diff] [blame] | 364 | XCTAssertEqual(buffer.pixelBuffer, outputBuffer.pixelBuffer); |
| 365 | |
| 366 | [callbackExpectation fulfill]; |
| 367 | }); |
| 368 | |
| 369 | const rtc::VideoSinkWants video_sink_wants; |
| 370 | rtc::VideoSourceInterface<webrtc::VideoFrame> *video_source_interface = _video_source; |
| 371 | video_source_interface->AddOrUpdateSink(&callback_video_sink, video_sink_wants); |
| 372 | |
| 373 | _video_source->OnOutputFormatRequest(640, 360, 30); |
| 374 | _video_source->OnCapturedFrame(frame); |
| 375 | |
| 376 | [self waitForExpectations:@[ callbackExpectation ] timeout:10.0]; |
| 377 | CVBufferRelease(pixelBufferRef); |
| 378 | } |
| 379 | |
| 380 | - (void)testOnCapturedFrameI420BufferNeedsAdaptation { |
| 381 | rtc::scoped_refptr<webrtc::I420Buffer> i420Buffer = CreateI420Gradient(720, 1280); |
| 382 | RTCI420Buffer *buffer = [[RTCI420Buffer alloc] initWithFrameBuffer:i420Buffer]; |
| 383 | RTCVideoFrame *frame = |
| 384 | [[RTCVideoFrame alloc] initWithBuffer:buffer rotation:RTCVideoRotation_0 timeStampNs:0]; |
| 385 | |
| 386 | XCTestExpectation *callbackExpectation = [self expectationWithDescription:@"videoSinkCallback"]; |
| 387 | ObjCCallbackVideoSink callback_video_sink(^void(RTCVideoFrame *outputFrame) { |
| 388 | XCTAssertEqual(outputFrame.width, 360); |
| 389 | XCTAssertEqual(outputFrame.height, 640); |
| 390 | |
| 391 | RTCI420Buffer *outputBuffer = (RTCI420Buffer *)outputFrame.buffer; |
| 392 | |
| 393 | double psnr = I420PSNR(*[buffer nativeI420Buffer], *[outputBuffer nativeI420Buffer]); |
| 394 | XCTAssertEqual(psnr, webrtc::kPerfectPSNR); |
| 395 | |
| 396 | [callbackExpectation fulfill]; |
| 397 | }); |
| 398 | |
| 399 | const rtc::VideoSinkWants video_sink_wants; |
| 400 | rtc::VideoSourceInterface<webrtc::VideoFrame> *video_source_interface = _video_source; |
| 401 | video_source_interface->AddOrUpdateSink(&callback_video_sink, video_sink_wants); |
| 402 | |
| 403 | _video_source->OnOutputFormatRequest(640, 360, 30); |
| 404 | _video_source->OnCapturedFrame(frame); |
| 405 | |
| 406 | [self waitForExpectations:@[ callbackExpectation ] timeout:10.0]; |
| 407 | } |
| 408 | |
| 409 | - (void)testOnCapturedFrameI420BufferNeedsCropping { |
| 410 | rtc::scoped_refptr<webrtc::I420Buffer> i420Buffer = CreateI420Gradient(380, 640); |
| 411 | RTCI420Buffer *buffer = [[RTCI420Buffer alloc] initWithFrameBuffer:i420Buffer]; |
| 412 | RTCVideoFrame *frame = |
| 413 | [[RTCVideoFrame alloc] initWithBuffer:buffer rotation:RTCVideoRotation_0 timeStampNs:0]; |
| 414 | |
| 415 | XCTestExpectation *callbackExpectation = [self expectationWithDescription:@"videoSinkCallback"]; |
| 416 | ObjCCallbackVideoSink callback_video_sink(^void(RTCVideoFrame *outputFrame) { |
| 417 | XCTAssertEqual(outputFrame.width, 360); |
| 418 | XCTAssertEqual(outputFrame.height, 640); |
| 419 | |
| 420 | RTCI420Buffer *outputBuffer = (RTCI420Buffer *)outputFrame.buffer; |
| 421 | |
| 422 | double psnr = I420PSNR(*[buffer nativeI420Buffer], *[outputBuffer nativeI420Buffer]); |
| 423 | XCTAssertGreaterThanOrEqual(psnr, 40); |
| 424 | |
| 425 | [callbackExpectation fulfill]; |
| 426 | }); |
| 427 | |
| 428 | const rtc::VideoSinkWants video_sink_wants; |
| 429 | rtc::VideoSourceInterface<webrtc::VideoFrame> *video_source_interface = _video_source; |
| 430 | video_source_interface->AddOrUpdateSink(&callback_video_sink, video_sink_wants); |
| 431 | |
| 432 | _video_source->OnOutputFormatRequest(640, 360, 30); |
| 433 | _video_source->OnCapturedFrame(frame); |
| 434 | |
| 435 | [self waitForExpectations:@[ callbackExpectation ] timeout:10.0]; |
| 436 | } |
| 437 | |
| 438 | @end |