UBFFmpegVideoEncoder.h 4.91 KB
Newer Older
Craig Watson's avatar
Craig Watson committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright (C) 2015-2016 Département de l'Instruction Publique (DIP-SEM)
 *
 * 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 39

// Due to the whole ffmpeg / libAV silliness, we have to support libavresample on some platforms
#if LIBAVFORMAT_VERSION_MICRO > 100
40
    #include <libswresample/swresample.h>
41 42 43 44 45
#else
    #include <libavresample/avresample.h>
    #define SwrContext AVAudioResampleContext 
#endif

46 47 48 49 50 51 52 53
}

#include <atomic>

#include <QtCore>
#include <QImage>

#include "podcast/UBAbstractVideoEncoder.h"
54
#include "podcast/ffmpeg/UBMicrophoneInput.h"
55 56 57 58

class UBFFmpegVideoEncoderWorker;
class UBPodcastController;

59 60 61 62 63 64 65 66 67 68
/**
 * 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.
 */

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
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);
94
    void onAudioAvailable(QByteArray data);
95 96 97 98 99 100 101 102 103 104
    void finishEncoding();

private:

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

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

    QString mLastErrorMessage;

    QThread* mVideoEncoderThread;
    UBFFmpegVideoEncoderWorker* mVideoWorker;

    // Muxer
116
    // ------------------------------------------
117
    AVFormatContext* mOutputFormatContext;
118 119
    AVStream* mVideoStream;
    AVStream* mAudioStream;
120 121

    // Video
122 123
    // ------------------------------------------
    QQueue<ImageFrame> mPendingFrames;
124 125
    struct SwsContext * mSwsContext;

126
    int mVideoTimebase;
127

128 129 130
    // Audio
    // ------------------------------------------
    bool mShouldRecordAudio;
131

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

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


class UBFFmpegVideoEncoderWorker : public QObject
{
    Q_OBJECT

    friend class UBFFmpegVideoEncoder;

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

    bool isRunning() { return mIsRunning; }

156 157
    void queueVideoFrame(AVFrame* frame);
    void queueAudioFrame(AVFrame* frame);
158 159 160 161 162 163 164 165 166 167 168

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

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

private:
    void writeLatestVideoFrame();
169
    void writeLatestAudioFrame();
170 171 172 173

    UBFFmpegVideoEncoder* mController;

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

178 179 180
    QQueue<AVFrame*> mImageQueue;
    QQueue<AVFrame*> mAudioQueue;

181 182 183
    QMutex mFrameQueueMutex;
    QWaitCondition mWaitCondition;

184 185
    AVPacket* mVideoPacket;
    AVPacket* mAudioPacket;
186 187 188
};

#endif // UBFFMPEGVIDEOENCODER_H