Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(292)

Side by Side Diff: webrtc/sdk/objc/Framework/UnitTests/RTCCameraVideoCapturerTests.mm

Issue 2964703002: [iOS] Fix incorrectly oriented frames when rapidly switching between cameras. (Closed)
Patch Set: Refactored checking of image's source device. Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2017 The WebRTC project authors. All Rights Reserved. 2 * Copyright 2017 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 #import <OCMock/OCMock.h> 11 #import <OCMock/OCMock.h>
12 12
13 #if TARGET_OS_IPHONE 13 #if TARGET_OS_IPHONE
14 #import <UIKit/UIKit.h> 14 #import <UIKit/UIKit.h>
15 #endif 15 #endif
16 16
17 #include "webrtc/rtc_base/gunit.h" 17 #include "webrtc/rtc_base/gunit.h"
18 18
19 #import <WebRTC/RTCCameraVideoCapturer.h> 19 #import <WebRTC/RTCCameraVideoCapturer.h>
20 #import <WebRTC/RTCDispatcher.h> 20 #import <WebRTC/RTCDispatcher.h>
21 #import <WebRTC/RTCVideoFrame.h> 21 #import <WebRTC/RTCVideoFrame.h>
22 #import "AVCaptureSession+Device.h"
22 23
23 #if TARGET_OS_IPHONE 24 #if TARGET_OS_IPHONE
24 // Helper method. 25 // Helper method.
25 CMSampleBufferRef createTestSampleBufferRef() { 26 CMSampleBufferRef createTestSampleBufferRef() {
26 27
27 // This image is already in the testing bundle. 28 // This image is already in the testing bundle.
28 UIImage *image = [UIImage imageNamed:@"Default.png"]; 29 UIImage *image = [UIImage imageNamed:@"Default.png"];
29 CGSize size = image.size; 30 CGSize size = image.size;
30 CGImageRef imageRef = [image CGImage]; 31 CGImageRef imageRef = [image CGImage];
31 32
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 [self.delegateMock verify]; 212 [self.delegateMock verify];
212 213
213 [(id)currentDeviceMock stopMocking]; 214 [(id)currentDeviceMock stopMocking];
214 currentDeviceMock = nil; 215 currentDeviceMock = nil;
215 [classMock stopMocking]; 216 [classMock stopMocking];
216 classMock = nil; 217 classMock = nil;
217 CFRelease(sampleBuffer); 218 CFRelease(sampleBuffer);
218 #endif 219 #endif
219 } 220 }
220 221
222 - (void)testRotationCamera:(AVCaptureDevicePosition)camera
223 withOrientation:(UIDeviceOrientation)deviceOrientation {
224 #if TARGET_OS_IPHONE
225 // Mock the AVCaptureConnection as we will get the camera position from the co nnection's
226 // input ports.
227 AVCaptureDeviceInput *inputPortMock = OCMClassMock([AVCaptureDeviceInput class ]);
228 AVCaptureInputPort *captureInputPort = OCMClassMock([AVCaptureInputPort class] );
229 NSArray *inputPortsArrayMock = @[captureInputPort];
230 AVCaptureDevice *captureDeviceMock = OCMClassMock([AVCaptureDevice class]);
231 OCMStub(((AVCaptureConnection *)self.captureConnectionMock).inputPorts).
232 andReturn(inputPortsArrayMock);
233 OCMStub(captureInputPort.input).andReturn(inputPortMock);
234 OCMStub(inputPortMock.device).andReturn(captureDeviceMock);
235 OCMStub(captureDeviceMock.position).andReturn(camera);
236
237 // UpsideDown -> RTCVideoRotation_0.
238 UIDevice *currentDeviceMock = OCMClassMock([UIDevice class]);
239 OCMStub(currentDeviceMock.orientation).andReturn(deviceOrientation);
240 id classMock = OCMClassMock([UIDevice class]);
241 OCMStub([classMock currentDevice]).andReturn(currentDeviceMock);
242
243 CMSampleBufferRef sampleBuffer = createTestSampleBufferRef();
244
245 [[self.delegateMock expect] capturer:self.capturer
246 didCaptureVideoFrame:[OCMArg checkWithBlock:^BOOL(RTCVideoFram e *expectedFrame) {
247 if (camera == AVCaptureDevicePositionFront) {
248 if (deviceOrientation == UIDeviceOrientationLandscapeLeft) {
249 EXPECT_EQ(expectedFrame.rotation, RTCVideoRotation_180);
250 } else if (deviceOrientation == UIDeviceOrientationLandscapeRight) {
251 EXPECT_EQ(expectedFrame.rotation, RTCVideoRotation_0);
252 }
253 } else if (camera == AVCaptureDevicePositionBack) {
254 if (deviceOrientation == UIDeviceOrientationLandscapeLeft) {
255 EXPECT_EQ(expectedFrame.rotation, RTCVideoRotation_0);
256 } else if (deviceOrientation == UIDeviceOrientationLandscapeRight) {
257 EXPECT_EQ(expectedFrame.rotation, RTCVideoRotation_180);
258 }
259 }
260 return YES;
261 }]];
262
263 NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
264 [center postNotificationName:UIDeviceOrientationDidChangeNotification object:n il];
265
266 // We need to wait for the dispatch to finish.
267 WAIT(0, 1000);
268
269 [self.capturer captureOutput:self.capturer.captureSession.outputs[0]
270 didOutputSampleBuffer:sampleBuffer
271 fromConnection:self.captureConnectionMock];
272
273 [self.delegateMock verify];
274
275 [(id)currentDeviceMock stopMocking];
276 currentDeviceMock = nil;
277 [classMock stopMocking];
278 classMock = nil;
279 CFRelease(sampleBuffer);
280 #endif
281 }
282
283 - (void)setExif:(CMSampleBufferRef)sampleBuffer {
284 CFMutableDictionaryRef exif = CFDictionaryCreateMutable(kCFAllocatorDefault, 0 , NULL, NULL);
285 CFDictionarySetValue(exif, CFSTR("LensModel"), CFSTR("iPhone SE back camera 4. 15mm f/2.2"));
286 CMSetAttachment(sampleBuffer, CFSTR("{Exif}"), exif, kCMAttachmentMode_ShouldP ropagate);
287 }
288
289 - (void)testRotationFrame {
290 #if TARGET_OS_IPHONE
291 // Mock the AVCaptureConnection as we will get the camera position from the co nnection's
292 // input ports.
293 AVCaptureDeviceInput *inputPortMock = OCMClassMock([AVCaptureDeviceInput class ]);
294 AVCaptureInputPort *captureInputPort = OCMClassMock([AVCaptureInputPort class] );
295 NSArray *inputPortsArrayMock = @[captureInputPort];
296 AVCaptureDevice *captureDeviceMock = OCMClassMock([AVCaptureDevice class]);
297 OCMStub(((AVCaptureConnection *)self.captureConnectionMock).inputPorts).
298 andReturn(inputPortsArrayMock);
299 OCMStub(captureInputPort.input).andReturn(inputPortMock);
300 OCMStub(inputPortMock.device).andReturn(captureDeviceMock);
301 OCMStub(captureDeviceMock.position).andReturn(AVCaptureDevicePositionFront);
302
303 // UpsideDown -> RTCVideoRotation_0.
304 UIDevice *currentDeviceMock = OCMClassMock([UIDevice class]);
305 OCMStub(currentDeviceMock.orientation).andReturn(UIDeviceOrientationLandscapeL eft);
306 id classMock = OCMClassMock([UIDevice class]);
307 OCMStub([classMock currentDevice]).andReturn(currentDeviceMock);
308
309 CMSampleBufferRef sampleBuffer = createTestSampleBufferRef();
310
311 [[self.delegateMock expect] capturer:self.capturer
312 didCaptureVideoFrame:[OCMArg checkWithBlock:^BOOL(RTCVideoFram e *expectedFrame) {
313 // Front camera and landscape left should return 180. But the frame says its from the back
314 // camera, so rotation should be 0.
315 EXPECT_EQ(expectedFrame.rotation, RTCVideoRotation_0);
316 return YES;
317 }]];
318
319 NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
320 [center postNotificationName:UIDeviceOrientationDidChangeNotification object:n il];
321
322 // We need to wait for the dispatch to finish.
323 WAIT(0, 1000);
324
325 [self setExif:sampleBuffer];
326
327 [self.capturer captureOutput:self.capturer.captureSession.outputs[0]
328 didOutputSampleBuffer:sampleBuffer
329 fromConnection:self.captureConnectionMock];
330
331 [self.delegateMock verify];
332
333 [(id)currentDeviceMock stopMocking];
334 currentDeviceMock = nil;
335 [classMock stopMocking];
336 classMock = nil;
337 CFRelease(sampleBuffer);
338 #endif
339 }
340
341 - (void)testImageExif {
342 #if TARGET_OS_IPHONE
343 CMSampleBufferRef sampleBuffer = createTestSampleBufferRef();
344 [self setExif:sampleBuffer];
345
346 AVCaptureDevicePosition cameraPosition = [AVCaptureSession
347 devicePositionForSampleBuffer:sample Buffer];
348 EXPECT_EQ(cameraPosition, AVCaptureDevicePositionBack);
349 #endif
350 }
351
221 @end 352 @end
222 353
223 // TODO(kthelgason): Reenable these tests on simulator. 354 // TODO(kthelgason): Reenable these tests on simulator.
224 // See bugs.webrtc.org/7813 355 // See bugs.webrtc.org/7813
225 #if TARGET_IPHONE_SIMULATOR 356 #if TARGET_IPHONE_SIMULATOR
226 #define MAYBE_TEST(f, name) TEST(f, DISABLED_##name) 357 #define MAYBE_TEST(f, name) TEST(f, DISABLED_##name)
227 #else 358 #else
228 #define MAYBE_TEST TEST 359 #define MAYBE_TEST TEST
229 #endif 360 #endif
230 361
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 [test testDelegateCallbackNotCalledWhenInvalidBuffer]; 393 [test testDelegateCallbackNotCalledWhenInvalidBuffer];
263 [test tearDown]; 394 [test tearDown];
264 } 395 }
265 396
266 MAYBE_TEST(RTCCameraVideoCapturerTests, DelegateCallbackWithValidBufferAndOrient ationUpdate) { 397 MAYBE_TEST(RTCCameraVideoCapturerTests, DelegateCallbackWithValidBufferAndOrient ationUpdate) {
267 RTCCameraVideoCapturerTests *test = [[RTCCameraVideoCapturerTests alloc] init] ; 398 RTCCameraVideoCapturerTests *test = [[RTCCameraVideoCapturerTests alloc] init] ;
268 [test setup]; 399 [test setup];
269 [test testDelegateCallbackWithValidBufferAndOrientationUpdate]; 400 [test testDelegateCallbackWithValidBufferAndOrientationUpdate];
270 [test tearDown]; 401 [test tearDown];
271 } 402 }
403
404 MAYBE_TEST(RTCCameraVideoCapturerTests, RotationCameraBackLandscapeLeft) {
405 RTCCameraVideoCapturerTests *test = [[RTCCameraVideoCapturerTests alloc] init] ;
406 [test setup];
407 [test testRotationCamera:AVCaptureDevicePositionBack
408 withOrientation:UIDeviceOrientationLandscapeLeft];
409 [test tearDown];
410 }
411
412 MAYBE_TEST(RTCCameraVideoCapturerTests, RotationCameraFrontLandscapeLeft) {
413 RTCCameraVideoCapturerTests *test = [[RTCCameraVideoCapturerTests alloc] init] ;
414 [test setup];
415 [test testRotationCamera:AVCaptureDevicePositionFront
416 withOrientation:UIDeviceOrientationLandscapeLeft];
417 [test tearDown];
418 }
419
420 MAYBE_TEST(RTCCameraVideoCapturerTests, RotationCameraBackLandscapeRight) {
421 RTCCameraVideoCapturerTests *test = [[RTCCameraVideoCapturerTests alloc] init] ;
422 [test setup];
423 [test testRotationCamera:AVCaptureDevicePositionBack
424 withOrientation:UIDeviceOrientationLandscapeRight];
425 [test tearDown];
426 }
427
428 MAYBE_TEST(RTCCameraVideoCapturerTests, RotationCameraFrontLandscapeRight) {
429 RTCCameraVideoCapturerTests *test = [[RTCCameraVideoCapturerTests alloc] init] ;
430 [test setup];
431 [test testRotationCamera:AVCaptureDevicePositionFront
432 withOrientation:UIDeviceOrientationLandscapeRight];
433 [test tearDown];
434 }
435
436 MAYBE_TEST(RTCCameraVideoCapturerTests, RotationCameraFrame) {
437 RTCCameraVideoCapturerTests *test = [[RTCCameraVideoCapturerTests alloc] init] ;
438 [test setup];
439 [test testRotationFrame];
440 [test tearDown];
441 }
442
443 MAYBE_TEST(RTCCameraVideoCapturerTests, ImageExif) {
444 RTCCameraVideoCapturerTests *test = [[RTCCameraVideoCapturerTests alloc] init] ;
445 [test setup];
446 [test testImageExif];
447 [test tearDown];
448 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698