UBQuickTimeFile.h 4.32 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1
/*
2
 * Copyright (C) 2015-2018 Département de l'Instruction Publique (DIP-SEM)
Craig Watson's avatar
Craig Watson committed
3
 *
Claudio Valerio's avatar
Claudio Valerio committed
4
 * Copyright (C) 2013 Open Education Foundation
Claudio Valerio's avatar
Claudio Valerio committed
5
 *
Claudio Valerio's avatar
Claudio Valerio committed
6 7
 * Copyright (C) 2010-2013 Groupement d'Intérêt Public pour
 * l'Education Numérique en Afrique (GIP ENA)
8
 *
Claudio Valerio's avatar
Claudio Valerio committed
9 10 11
 * This file is part of OpenBoard.
 *
 * OpenBoard is free software: you can redistribute it and/or modify
Claudio Valerio's avatar
Claudio Valerio committed
12 13
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License,
14 15 16 17
 * with a specific linking exception for the OpenSSL project's
 * "OpenSSL" library (or with modified versions of it that use the
 * same license as the "OpenSSL" library).
 *
Claudio Valerio's avatar
Claudio Valerio committed
18
 * OpenBoard is distributed in the hope that it will be useful,
Claudio Valerio's avatar
Claudio Valerio committed
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Claudio Valerio's avatar
Claudio Valerio committed
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Claudio Valerio's avatar
Claudio Valerio committed
21
 * GNU General Public License for more details.
Claudio Valerio's avatar
Claudio Valerio committed
22
 *
Claudio Valerio's avatar
Claudio Valerio committed
23
 * You should have received a copy of the GNU General Public License
Claudio Valerio's avatar
Claudio Valerio committed
24
 * along with OpenBoard. If not, see <http://www.gnu.org/licenses/>.
Claudio Valerio's avatar
Claudio Valerio committed
25 26
 */

27

Claudio Valerio's avatar
Claudio Valerio committed
28

Claudio Valerio's avatar
Claudio Valerio committed
29

Claudio Valerio's avatar
Claudio Valerio committed
30 31 32 33 34
#ifndef UBQUICKTIMEFILE_H_
#define UBQUICKTIMEFILE_H_

#include <QtCore>

35
#include <CoreVideo/CoreVideo.h>
36
#include <CoreMedia/CoreMedia.h>
Claudio Valerio's avatar
Claudio Valerio committed
37 38 39

#include "UBAudioQueueRecorder.h"

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58


// Trick to get around the fact that the C++ compiler doesn't
// like Objective C code.

#ifdef __OBJC__ // defined by the Objective C compiler
    @class AVAssetWriter;
    @class AVAssetWriterInput;
    @class AVAssetWriterInputPixelBufferAdaptor;

    typedef AVAssetWriter* AssetWriterPTR;
    typedef AVAssetWriterInput* AssetWriterInputPTR;
    typedef AVAssetWriterInputPixelBufferAdaptor* AssetWriterInputAdaptorPTR;
#else
    typedef void* AssetWriterPTR;
    typedef void* AssetWriterInputPTR;
    typedef void* AssetWriterInputAdaptorPTR;
#endif

Claudio Valerio's avatar
Claudio Valerio committed
59 60 61 62 63
class UBQuickTimeFile : public QThread
{
    Q_OBJECT;

    public:
64 65 66 67 68 69 70 71
        struct VideoFrame
        {
            CVPixelBufferRef buffer;
            long timestamp;
        };

        static QWaitCondition frameBufferNotEmpty;

Claudio Valerio's avatar
Claudio Valerio committed
72 73 74 75 76 77 78 79 80 81
        UBQuickTimeFile(QObject * pParent = 0);
        virtual ~UBQuickTimeFile();

        bool init(const QString& videoFileName, const QString& profileData
                , int pFramesPerSecond, const QSize& pFrameSize
                , bool recordAudio = true, const QString& audioDeviceName = QString("Default"));

        void stop();

        CVPixelBufferRef newPixelBuffer();
82
        void enqueueVideoFrame(VideoFrame frame);
Claudio Valerio's avatar
Claudio Valerio committed
83

84 85 86
        bool isCompressionSessionRunning() { return mCompressionSessionRunning; }
        QString lastErrorMessage() const { return mLastErrorMessage; }

Claudio Valerio's avatar
Claudio Valerio committed
87 88 89
    signals:
        void audioLevelChanged(quint8 level);
        void compressionSessionStarted();
90
        void compressionFinished();
Claudio Valerio's avatar
Claudio Valerio committed
91 92 93 94

    protected:
        void run();

95
    private slots:
96
        void enqueueAudioBuffer(void* pBuffer, long pLength);
Claudio Valerio's avatar
Claudio Valerio committed
97 98

    private:
99
        QString mLastErrorMessage;
Claudio Valerio's avatar
Claudio Valerio committed
100

101
        // Format information
Claudio Valerio's avatar
Claudio Valerio committed
102
        QString mVideoFileName;
103 104 105 106
        QSize mFrameSize;
        long mTimeScale;
        bool mRecordAudio;
        QString mAudioRecordingDeviceName;
Claudio Valerio's avatar
Claudio Valerio committed
107

108
        // Video/audio encoders and associated objects
109 110 111 112
        AssetWriterPTR mVideoWriter;
        AssetWriterInputPTR mVideoWriterInput;
        AssetWriterInputAdaptorPTR mAdaptor;
        AssetWriterInputPTR mAudioWriterInput;
Claudio Valerio's avatar
Claudio Valerio committed
113

114
        // Audio recorder
115
        QPointer<UBAudioQueueRecorder> mWaveRecorder;
116 117 118
        CMAudioFormatDescriptionRef mAudioFormatDescription;

        // Variables used during encoding
119
        CFAbsoluteTime mStartTime;
120
        CMTime mLastFrameTimestamp;
121

122 123 124
        volatile bool mShouldStopCompression;
        volatile bool mCompressionSessionRunning;

125
        // Dispatch queues to handle passing data to the A/V encoders
126 127 128
        dispatch_queue_t mVideoDispatchQueue;
        dispatch_queue_t mAudioDispatchQueue;

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
        // Queues for frames and audio buffers to be encoded
        QQueue<VideoFrame> frameQueue;
        QQueue<CMSampleBufferRef> audioQueue;

        QMutex frameQueueMutex;
        QMutex audioQueueMutex;
        QMutex audioWriterMutex;


        // Private functions

        void setLastErrorMessage(const QString& error);

        bool beginSession();
        void endSession();

        void appendFrameToVideo(CVPixelBufferRef pixelBuffer, long msTimeStamp);
        bool appendSampleBuffer(CMSampleBufferRef sampleBuffer);
147

Claudio Valerio's avatar
Claudio Valerio committed
148 149 150
};

#endif /* UBQUICKTIMEFILE_H_ */