summaryrefslogtreecommitdiff
path: root/src/plugins/avfoundation/camera/avfmediarecordercontrol_ios.mm
diff options
context:
space:
mode:
authorTimur Pocheptsov <timur.pocheptsov@theqtcompany.com>2016-07-22 14:29:45 +0200
committerTimur Pocheptsov <timur.pocheptsov@theqtcompany.com>2016-07-26 13:01:56 +0000
commit7788feea5d55418ab61681e122c1d2d29f7bddce (patch)
tree7e79602d22aef1bb079b70b55ffca2ea54a888f4 /src/plugins/avfoundation/camera/avfmediarecordercontrol_ios.mm
parentab940d8fe9affd2ae1bb9cdf0e3f9d41fa49a662 (diff)
downloadqtmultimedia-7788feea5d55418ab61681e122c1d2d29f7bddce.tar.gz
AVFMediaAssetWriter - fix race conditions
Apple recommends starting/setting up a session (AVCaptureSession) on a special queue and in the past we did this on the writer's queue. Unfortunately, we also can access the same session from a different thread and this results in race conditions and different weird crashes, for example, we're calling start/stopRunning from the writer's queue, while the session is being configured (in between beginConfiguration/commitConfiguration on the recorder control's thread). So we have to limit access to the session by the control's thread. Apple docs say we have to ensure all appendSampleBuffer calls done _before_ finishWriting. We ensure this by dispatching_sync an empty block on the writer's queue after store-release of m_state. We also do the same with video queue to ensure it does not try to access viewfinder's data after stop/abort executed. All these changes also make lock/mutex unneeded. Task-number: QTBUG-54890 Change-Id: I38e86c879b6b62306bdfbeade65405d6ac3be9f3 Reviewed-by: Yoann Lopes <yoann.lopes@qt.io>
Diffstat (limited to 'src/plugins/avfoundation/camera/avfmediarecordercontrol_ios.mm')
-rw-r--r--src/plugins/avfoundation/camera/avfmediarecordercontrol_ios.mm26
1 files changed, 12 insertions, 14 deletions
diff --git a/src/plugins/avfoundation/camera/avfmediarecordercontrol_ios.mm b/src/plugins/avfoundation/camera/avfmediarecordercontrol_ios.mm
index e65c98257..292b449d2 100644
--- a/src/plugins/avfoundation/camera/avfmediarecordercontrol_ios.mm
+++ b/src/plugins/avfoundation/camera/avfmediarecordercontrol_ios.mm
@@ -86,13 +86,7 @@ AVFMediaRecorderControlIOS::AVFMediaRecorderControlIOS(AVFCameraService *service
{
Q_ASSERT(service);
- m_writerQueue.reset(dispatch_queue_create("asset-writer-queue", DISPATCH_QUEUE_SERIAL));
- if (!m_writerQueue) {
- qDebugCamera() << Q_FUNC_INFO << "failed to create an asset writer's queue";
- return;
- }
-
- m_writer.reset([[QT_MANGLE_NAMESPACE(AVFMediaAssetWriter) alloc] initWithQueue:m_writerQueue delegate:this]);
+ m_writer.reset([[QT_MANGLE_NAMESPACE(AVFMediaAssetWriter) alloc] initWithDelegate:this]);
if (!m_writer) {
qDebugCamera() << Q_FUNC_INFO << "failed to create an asset writer";
return;
@@ -289,9 +283,15 @@ void AVFMediaRecorderControlIOS::setState(QMediaRecorder::State state)
Q_EMIT stateChanged(m_state);
Q_EMIT statusChanged(m_lastStatus);
- dispatch_async(m_writerQueue, ^{
- [m_writer start];
- });
+ // Apple recommends to call startRunning and do all
+ // setup on a special queue, and that's what we had
+ // initially (dispatch_async to writerQueue). Unfortunately,
+ // writer's queue is not the only queue/thread that can
+ // access/modify the session, and as a result we have
+ // all possible data/race-conditions with Obj-C exceptions
+ // at best and something worse in general.
+ // Now we try to only modify session on the same thread.
+ [m_writer start];
} else {
[session startRunning];
Q_EMIT error(QMediaRecorder::FormatError, tr("Failed to start recording"));
@@ -318,7 +318,7 @@ void AVFMediaRecorderControlIOS::setMuted(bool muted)
void AVFMediaRecorderControlIOS::setVolume(qreal volume)
{
- Q_UNUSED(volume);
+ Q_UNUSED(volume)
qDebugCamera() << Q_FUNC_INFO << "not implemented";
}
@@ -403,9 +403,7 @@ void AVFMediaRecorderControlIOS::stopWriter()
Q_EMIT stateChanged(m_state);
Q_EMIT statusChanged(m_lastStatus);
- dispatch_async(m_writerQueue, ^{
- [m_writer stop];
- });
+ [m_writer stop];
}
}