AVFoundation: fix orientation of recorded videos.

Make sure the video is recorded in the same orientation as the device.
That means the top of the video matches the physical side of the device
which is on top when starting recording.
This patch makes sure the behavior is the same as on Android. In a
future version, we should have an API to pick the desired orientation
of the recorded video.

Change-Id: Ia8bbfe621a0e54de3cb6bfe0a520f37e8a932539
Reviewed-by: Christian Stromme <christian.stromme@qt.io>
This commit is contained in:
Yoann Lopes
2016-02-09 23:44:33 +01:00
committed by Yoann Lopes
parent f5de141070
commit c6a8c2c846
4 changed files with 28 additions and 4 deletions

View File

@@ -98,7 +98,8 @@ QT_END_NAMESPACE
- (bool)setupWithFileURL:(NSURL *)fileURL
cameraService:(QT_PREPEND_NAMESPACE(AVFCameraService) *)service
audioSettings:(NSDictionary *)audioSettings
videoSettings:(NSDictionary *)videoSettings;
videoSettings:(NSDictionary *)videoSettings
transform:(CGAffineTransform)transform;
- (void)start;
- (void)stop;

View File

@@ -108,6 +108,7 @@ bool qt_camera_service_isValid(AVFCameraService *service)
cameraService:(AVFCameraService *)service
audioSettings:(NSDictionary *)audioSettings
videoSettings:(NSDictionary *)videoSettings
transform:(CGAffineTransform)transform
{
Q_ASSERT(fileURL);
@@ -157,6 +158,9 @@ bool qt_camera_service_isValid(AVFCameraService *service)
m_assetWriter.reset();
return false;
}
m_cameraWriterInput.data().transform = transform;
// Ready to start ...
return true;
}

View File

@@ -39,6 +39,7 @@
#include "avfcamerautility.h"
#include <QtMultimedia/qmediarecordercontrol.h>
#include <private/qvideooutputorientationhandler_p.h>
#include <QtCore/qglobal.h>
#include <QtCore/qurl.h>
@@ -102,6 +103,7 @@ private:
NSDictionary *m_audioSettings;
NSDictionary *m_videoSettings;
QVideoOutputOrientationHandler m_orientationHandler;
};
QT_END_NAMESPACE

View File

@@ -262,9 +262,26 @@ void AVFMediaRecorderControlIOS::setState(QMediaRecorder::State state)
applySettings();
if ([m_writer setupWithFileURL:nsFileURL cameraService:m_service
audioSettings:m_audioSettings
videoSettings:m_videoSettings]) {
// Make sure the video is recorded in device orientation.
// The top of the video will match the side of the device which is on top
// when recording starts (regardless of the UI orientation).
AVFCameraInfo cameraInfo = m_service->session()->activeCameraInfo();
int screenOrientation = 360 - m_orientationHandler.currentOrientation();
float rotation = 0;
if (cameraInfo.position == QCamera::FrontFace)
rotation = (screenOrientation + cameraInfo.orientation) % 360;
else
rotation = (screenOrientation + (360 - cameraInfo.orientation)) % 360;
// convert to radians
rotation *= M_PI / 180.f;
if ([m_writer setupWithFileURL:nsFileURL
cameraService:m_service
audioSettings:m_audioSettings
videoSettings:m_videoSettings
transform:CGAffineTransformMakeRotation(rotation)]) {
m_state = QMediaRecorder::RecordingState;
m_lastStatus = QMediaRecorder::StartingStatus;