UBFFmpegVideoEncoder.h 5.16 KB
Newer Older
Craig Watson's avatar
Craig Watson committed
1
/*
2
 * Copyright (C) 2015-2018 Département de l'Instruction Publique (DIP-SEM)
Craig Watson's avatar
Craig Watson committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This file is part of OpenBoard.
 *
 * OpenBoard is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License,
 * 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).
 *
 * OpenBoard is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with OpenBoard. If not, see <http://www.gnu.org/licenses/>.
 */

22 23 24 25 26 27 28
#ifndef UBFFMPEGVIDEOENCODER_H
#define UBFFMPEGVIDEOENCODER_H

extern "C" {
    #include <libavcodec/avcodec.h>
    #include <libavformat/avformat.h>
    #include <libavformat/avio.h>
29
    #include <libavutil/audio_fifo.h>
30 31 32 33
    #include <libavutil/avutil.h>
    #include <libavutil/imgutils.h>
    #include <libavutil/opt.h>
    #include <libavutil/mathematics.h>
34
    #include <libavutil/time.h>
35
    #include <libavutil/avstring.h>
36
    #include <libswscale/swscale.h>
37 38

// Due to the whole ffmpeg / libAV silliness, we have to support libavresample on some platforms
39 40 41 42
// Should libswresample be used with libavformat.*.*.100 ? compiles fine but crashes in swr_convert (internal call of soxr_create causes a SIGSEV (EXC_BAD_ACCESS))
// Bad config ?
// With libavresample, no crash, but the sound is inaudible (lot of noise)
#if LIBAVFORMAT_VERSION_MICRO >= 100
43
    #include <libswresample/swresample.h>
44 45 46 47 48
#else
    #include <libavresample/avresample.h>
    #define SwrContext AVAudioResampleContext 
#endif

49 50 51 52 53 54 55 56
}

#include <atomic>

#include <QtCore>
#include <QImage>

#include "podcast/UBAbstractVideoEncoder.h"
57
#include "podcast/ffmpeg/UBMicrophoneInput.h"
58 59 60 61

class UBFFmpegVideoEncoderWorker;
class UBPodcastController;

62 63 64 65 66 67 68 69 70 71
/**
 * This class provides an interface between the podcast controller and the ffmpeg
 * back-end.
 * It includes all the necessary objects and methods to record video (muxer, audio and
 * video streams and encoders, etc) from inputs consisting of raw PCM audio and raw RGBA
 * images.
 *
 * A worker thread is used to encode and write the audio and video on-the-fly.
 */

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
class UBFFmpegVideoEncoder : public UBAbstractVideoEncoder
{
    Q_OBJECT

    friend class UBFFmpegVideoEncoderWorker;

public:

    UBFFmpegVideoEncoder(QObject* parent = NULL);
    virtual ~UBFFmpegVideoEncoder();

    bool start();
    bool stop();

    void newPixmap(const QImage& pImage, long timestamp);

    QString videoFileExtension() const { return "mp4"; }

    QString lastErrorMessage() { return mLastErrorMessage; }

    void setRecordAudio(bool pRecordAudio) { mShouldRecordAudio = pRecordAudio; }

private slots:

    void setLastErrorMessage(const QString& pMessage);
97
    void onAudioAvailable(QByteArray data);
98 99 100 101 102 103 104 105 106 107
    void finishEncoding();

private:

    struct ImageFrame
    {
        QImage image;
        long timestamp; // unit: ms
    };

108 109 110
    AVFrame* convertImageFrame(ImageFrame frame);
    AVFrame* convertAudio(QByteArray data);
    void processAudio(QByteArray& data);
111 112 113 114 115 116 117 118
    bool init();

    QString mLastErrorMessage;

    QThread* mVideoEncoderThread;
    UBFFmpegVideoEncoderWorker* mVideoWorker;

    // Muxer
119
    // ------------------------------------------
120
    AVFormatContext* mOutputFormatContext;
121 122
    AVStream* mVideoStream;
    AVStream* mAudioStream;
123 124

    // Video
125 126
    // ------------------------------------------
    QQueue<ImageFrame> mPendingFrames;
127 128
    struct SwsContext * mSwsContext;

129
    int mVideoTimebase;
130

131 132 133
    // Audio
    // ------------------------------------------
    bool mShouldRecordAudio;
134

135 136 137 138
    UBMicrophoneInput * mAudioInput;
    struct SwrContext * mSwrContext;
    /// Queue for audio that has been rescaled/converted but not encoded yet
    AVAudioFifo *mAudioOutBuffer;
139

140 141 142 143
    /// Sample rate for encoded audio
    int mAudioSampleRate;
    /// Total audio frames sent to encoder
    int mAudioFrameCount;
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
};


class UBFFmpegVideoEncoderWorker : public QObject
{
    Q_OBJECT

    friend class UBFFmpegVideoEncoder;

public:
    UBFFmpegVideoEncoderWorker(UBFFmpegVideoEncoder* controller);
    ~UBFFmpegVideoEncoderWorker();

    bool isRunning() { return mIsRunning; }

159 160
    void queueVideoFrame(AVFrame* frame);
    void queueAudioFrame(AVFrame* frame);
161 162 163 164 165 166 167 168 169 170 171

public slots:
    void runEncoding();
    void stopEncoding();

signals:
    void encodingFinished();
    void error(QString message);

private:
    void writeLatestVideoFrame();
172
    void writeLatestAudioFrame();
173 174 175 176

    UBFFmpegVideoEncoder* mController;

    // std::atomic is C++11. This won't work with msvc2010, so a
177
    // newer compiler must be used if this class is to be used on Windows
178 179 180
    std::atomic<bool> mStopRequested;
    std::atomic<bool> mIsRunning;

181 182 183
    QQueue<AVFrame*> mImageQueue;
    QQueue<AVFrame*> mAudioQueue;

184 185 186
    QMutex mFrameQueueMutex;
    QWaitCondition mWaitCondition;

187 188
    AVPacket* mVideoPacket;
    AVPacket* mAudioPacket;
189 190 191
};

#endif // UBFFMPEGVIDEOENCODER_H