clean-up; temporary solution for waiting until audio assetWriter is ready before appending sample buffers

preferencesAboutTextFull
Craig Watson 9 years ago
parent 2b75543ee3
commit fc554baecd
  1. 18
      src/podcast/quicktime/UBQuickTimeFile.h
  2. 28
      src/podcast/quicktime/UBQuickTimeFile.mm

@ -101,24 +101,12 @@ class UBQuickTimeFile : public QThread
bool beginSession(); bool beginSession();
void setLastErrorMessage(const QString& error); void setLastErrorMessage(const QString& error);
void appendVideoFrame(CVPixelBufferRef pixelBuffer, long msTimeStamp); void appendVideoFrame(CVPixelBufferRef pixelBuffer, long msTimeStamp);
bool flushPendingFrames();
volatile bool mShouldStopCompression;
volatile bool mCompressionSessionRunning;
volatile int mPendingFrames;
QString mSpatialQuality;
int mFramesPerSecond;
QSize mFrameSize; QSize mFrameSize;
QString mVideoFileName; QString mVideoFileName;
bool mRecordAudio;
AssetWriterPTR mVideoWriter; AssetWriterPTR mVideoWriter;
AssetWriterInputPTR mVideoWriterInput; AssetWriterInputPTR mVideoWriterInput;
@ -132,6 +120,12 @@ class UBQuickTimeFile : public QThread
CMAudioFormatDescriptionRef mAudioFormatDescription; CMAudioFormatDescriptionRef mAudioFormatDescription;
long mTimeScale; long mTimeScale;
bool mRecordAudio;
volatile bool mShouldStopCompression;
volatile bool mCompressionSessionRunning;
QString mLastErrorMessage; QString mLastErrorMessage;

@ -41,18 +41,15 @@ QQueue<UBQuickTimeFile::VideoFrame> UBQuickTimeFile::frameQueue;
QMutex UBQuickTimeFile::frameQueueMutex; QMutex UBQuickTimeFile::frameQueueMutex;
QWaitCondition UBQuickTimeFile::frameBufferNotEmpty; QWaitCondition UBQuickTimeFile::frameBufferNotEmpty;
UBQuickTimeFile::UBQuickTimeFile(QObject * pParent) UBQuickTimeFile::UBQuickTimeFile(QObject * pParent)
: QThread(pParent) : QThread(pParent)
, mVideoWriter(0) , mVideoWriter(0)
, mVideoWriterInput(0) , mVideoWriterInput(0)
, mAdaptor(0) , mAdaptor(0)
, mFramesPerSecond(-1)
, mTimeScale(1000) , mTimeScale(1000)
, mRecordAudio(true) , mRecordAudio(true)
, mShouldStopCompression(false) , mShouldStopCompression(false)
, mCompressionSessionRunning(false) , mCompressionSessionRunning(false)
, mPendingFrames(0)
{ {
} }
@ -67,10 +64,8 @@ bool UBQuickTimeFile::init(const QString& pVideoFileName, const QString& pProfil
, const QSize& pFrameSize, bool pRecordAudio, const QString& audioRecordingDevice) , const QSize& pFrameSize, bool pRecordAudio, const QString& audioRecordingDevice)
{ {
mFrameSize = pFrameSize; mFrameSize = pFrameSize;
mFramesPerSecond = pFramesPerSecond;
mVideoFileName = pVideoFileName; mVideoFileName = pVideoFileName;
mRecordAudio = pRecordAudio; mRecordAudio = pRecordAudio;
mSpatialQuality = pProfileData;
if (mRecordAudio) if (mRecordAudio)
mAudioRecordingDeviceName = audioRecordingDevice; mAudioRecordingDeviceName = audioRecordingDevice;
@ -88,7 +83,6 @@ bool UBQuickTimeFile::init(const QString& pVideoFileName, const QString& pProfil
void UBQuickTimeFile::run() void UBQuickTimeFile::run()
{ {
mShouldStopCompression = false; mShouldStopCompression = false;
mPendingFrames = 0;
if (!beginSession()) if (!beginSession())
return; return;
@ -97,6 +91,7 @@ void UBQuickTimeFile::run()
emit compressionSessionStarted(); emit compressionSessionStarted();
do { do {
// Video
frameQueueMutex.lock(); frameQueueMutex.lock();
frameBufferNotEmpty.wait(&UBQuickTimeFile::frameQueueMutex); frameBufferNotEmpty.wait(&UBQuickTimeFile::frameQueueMutex);
@ -118,6 +113,7 @@ void UBQuickTimeFile::run()
} }
else else
frameQueueMutex.unlock(); frameQueueMutex.unlock();
} while(!mShouldStopCompression); } while(!mShouldStopCompression);
endSession(); endSession();
@ -257,7 +253,7 @@ bool UBQuickTimeFile::beginSession()
void UBQuickTimeFile::endSession() void UBQuickTimeFile::endSession()
{ {
[mVideoWriterInput markAsFinished]; [mVideoWriterInput markAsFinished];
bool success = [mVideoWriter finishWriting]; [mVideoWriter finishWritingWithCompletionHandler:^{}];
[mAdaptor release]; [mAdaptor release];
[mVideoWriterInput release]; [mVideoWriterInput release];
@ -375,17 +371,27 @@ void UBQuickTimeFile::appendAudioBuffer(void* pBuffer,
&sampleBuffer); &sampleBuffer);
// Add the audio sample to the asset writer input // Wait until the AssetWriterInput is ready, but no more than 100ms
if ([mAudioWriterInput isReadyForMoreMediaData]) // (bit of a duct tape solution; cleaner solution would be to use a QQueue,
// similar to the VideoWriter)
int waitTime = 0;
while(![mAudioWriterInput isReadyForMoreMediaData] && waitTime < 100) {
waitTime += 10;
usleep(10000);
}
if ([mAudioWriterInput isReadyForMoreMediaData]) {
if(![mAudioWriterInput appendSampleBuffer:sampleBuffer]) if(![mAudioWriterInput appendSampleBuffer:sampleBuffer])
setLastErrorMessage(QString("Failed to append sample buffer to audio input")); setLastErrorMessage(QString("Failed to append sample buffer to audio input"));
}
else else
setLastErrorMessage(QString("Audio Writer not ready; sample dropped")); setLastErrorMessage(QString("AudioWriterInput not ready. Buffer dropped."));
CFRelease(sampleBuffer); CFRelease(sampleBuffer);
CFRelease(blockBuffer); CFRelease(blockBuffer);
// The audioQueueBuffers are all freed when UBAudioQueueRecorder::close() is called // The audioQueueBuffers are all freed when UBAudioQueueRecorder::close() is called
} }

Loading…
Cancel
Save