Skip to content

Commit 3519568

Browse files
committed
camera: Fix rotations on iOS.
1 parent 0db2c8e commit 3519568

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

src/camera/coremedia/SDL_camera_coremedia.m

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,8 @@
2929
#import <AVFoundation/AVFoundation.h>
3030
#import <CoreMedia/CoreMedia.h>
3131

32-
// this is a codepath that tracks iOS device orientation to try to correct
33-
// video frame rotation, but currently it looks like iOS is doing this work
34-
// for us, and better. This is disabled for now, so the code lives in revision
35-
// control before I remove it outright.
3632
#if defined(SDL_PLATFORM_IOS) && !defined(SDL_PLATFORM_TVOS)
37-
//#define USE_UIKIT_DEVICE_ROTATION
33+
#define USE_UIKIT_DEVICE_ROTATION
3834
#endif
3935

4036
#ifdef USE_UIKIT_DEVICE_ROTATION
@@ -242,12 +238,26 @@ static SDL_CameraFrameResult COREMEDIA_AcquireFrame(SDL_Camera *device, SDL_Surf
242238
hidden.last_device_orientation = device_orientation; // update the last known-good orientation for later.
243239
}
244240

245-
switch (device_orientation) {
246-
case UIDeviceOrientationPortrait: *rotation = 0; break;
247-
case UIDeviceOrientationPortraitUpsideDown: *rotation = 180; break;
248-
case UIDeviceOrientationLandscapeRight: *rotation = 90; break; // !!! FIXME: might be backwards.
249-
case UIDeviceOrientationLandscapeLeft: *rotation = 270; break; // !!! FIXME: might be backwards.
250-
default: SDL_assert(!"Unexpected device orientation!"); *rotation = 0; break;
241+
const UIInterfaceOrientation ui_orientation = [UIApplication sharedApplication].statusBarOrientation;
242+
243+
// there is probably math for this, but this is easy to slap into a table.
244+
// rotation = rotations[uiorientation-1][devorientation-1];
245+
if (device->position == SDL_CAMERA_POSITION_BACK_FACING) {
246+
static const int back_rotations[4][4] = {
247+
{ 90, 90, 90, 90 }, // ui portrait
248+
{ 270, 270, 270, 270 }, // ui portait upside down
249+
{ 0, 0, 0, 0 }, // ui landscape left
250+
{ 180, 180, 180, 180 } // ui landscape right
251+
};
252+
*rotation = back_rotations[ui_orientation - 1][device_orientation - 1];
253+
} else {
254+
static const int front_rotations[4][4] = {
255+
{ 90, 90, 270, 270 }, // ui portrait
256+
{ 270, 270, 90, 90 }, // ui portait upside down
257+
{ 0, 0, 180, 180 }, // ui landscape left
258+
{ 180, 180, 0, 0 } // ui landscape right
259+
};
260+
*rotation = front_rotations[ui_orientation - 1][device_orientation - 1];
251261
}
252262
#endif
253263

0 commit comments

Comments
 (0)