video_base.h 5.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*
 *  Copyright (C) 2013 Savoir-Faire Linux Inc.
 *  Author: Guillaume Roguez <Guillaume.Roguez@savoirfairelinux.com>
 *
 *  This program 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; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *  MA  02110-1301 USA.
 *
 *  Additional permission under GNU GPL version 3 section 7:
 *
 *  If you modify this program, or any covered work, by linking or
 *  combining it with the OpenSSL project's OpenSSL library (or a
 *  modified version of that library), containing parts covered by the
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
 *  grants you additional permission to convey the resulting work.
 *  Corresponding Source for a non-source form of such a combination
 *  shall include the source code for the parts of OpenSSL used as well
 *  as that of the covered work.
 */

32 33
#ifndef __VIDEO_FRAME_H__
#define __VIDEO_FRAME_H__
34 35 36

#include "noncopyable.h"

37 38
#include <cstdlib>
#include <cstdint>
39
#include <memory>
40 41 42
#include <set>
#include <mutex>
#include <condition_variable>
43 44


45
class AVFrame;
46 47 48
class AVPacket;
class AVDictionary;

49 50 51 52 53 54
#ifndef AVFORMAT_AVIO_H
class AVIOContext;
#endif

enum VideoPixelFormat {
    VIDEO_PIXFMT_BGRA = -1,
55
    VIDEO_PIXFMT_YUV420P = -2,
56 57
};

58
namespace sfl_video {
59

60
template <typename T> class Observer;
61 62 63
template <typename T> class Observable;
class VideoFrame;

64 65 66 67
typedef int(*io_readcallback)(void *opaque, uint8_t *buf, int buf_size);
typedef int(*io_writecallback)(void *opaque, uint8_t *buf, int buf_size);
typedef int64_t(*io_seekcallback)(void *opaque, int64_t offset, int whence);

68 69 70 71 72 73 74 75 76
typedef std::shared_ptr<VideoFrame> VideoFrameUP;
typedef std::shared_ptr<VideoFrame> VideoFrameSP;

/*=== Observable =============================================================*/

template <typename T>
class Observable
{
public:
77
    Observable() : observers_(), mutex_() {}
78 79
    virtual ~Observable() {};

80
    void attach(Observer<T>* o) {
81
        std::unique_lock<std::mutex> lk(mutex_);
82 83
        if (o)
            observers_.insert(o);
84 85
    }

86
    void detach(Observer<T>* o) {
87
        std::unique_lock<std::mutex> lk(mutex_);
88 89
        if (o)
            observers_.erase(o);
90 91 92 93
    }

    void notify(T& data) {
        std::unique_lock<std::mutex> lk(mutex_);
94 95
        for (auto observer : observers_)
            observer->update(this, data);
96 97 98 99 100
    }

private:
    NON_COPYABLE(Observable<T>);

101 102
	std::set<Observer<T>*> observers_;
    std::mutex mutex_; // lock observers_
103 104
};

105
/*=== Observer =============================================================*/
106 107

template <typename T>
108
class Observer
109 110
{
public:
111
    virtual ~Observer() {};
112 113 114
	virtual void update(Observable<T>*, T&) = 0;
};

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
/*=== VideoPacket  ===========================================================*/

class VideoPacket {

public:
    VideoPacket();
    ~VideoPacket();
    AVPacket* get() { return packet_; };

private:
    NON_COPYABLE(VideoPacket);
    AVPacket *packet_;
};

/*=== VideoIOHandle  =========================================================*/

class VideoIOHandle {
public:
    VideoIOHandle(ssize_t buffer_size,
                  bool writeable,
                  io_readcallback read_cb,
                  io_writecallback write_cb,
                  io_seekcallback seek_cb,
                  void *opaque);
    ~VideoIOHandle();

    AVIOContext* get() { return ctx_; }

private:
    NON_COPYABLE(VideoIOHandle);
    AVIOContext *ctx_;
    unsigned char *buf_;
};

class VideoCodec {
public:
    VideoCodec();
    virtual ~VideoCodec() {}

    void setOption(const char *name, const char *value);

private:
    NON_COPYABLE(VideoCodec);

protected:
    AVDictionary *options_;
};

/*=== VideoFrame =============================================================*/

class VideoFrame {
public:
    VideoFrame();
    ~VideoFrame();

    AVFrame* get() { return frame_; };
171
    int getPixelFormat() const;
172 173
    int getWidth() const;
    int getHeight() const;
174 175 176
    void setGeometry(int width, int height, int pix_fmt);
    void setDestination(void *data);
    size_t getSize();
177
    static size_t getSize(int width, int height, int format);
178
    void setdefaults();
179 180 181 182
    bool allocBuffer(int width, int height, int pix_fmt);
    int blit(VideoFrame& src, int xoff, int yoff);
    void copy(VideoFrame &src);
    void test();
183 184 185 186

private:
    NON_COPYABLE(VideoFrame);
    AVFrame *frame_;
187
    bool allocated_;
188 189
};

190
/*=== VideoNode ============================================================*/
191

192 193 194 195 196 197 198 199 200
class VideoNode { public: virtual ~VideoNode() {}; };
typedef VideoNode VideoSource;

class VideoFrameActiveWriter :
        public Observable<VideoFrameSP>,
        public VideoNode
{};

class VideoFramePassiveReader :
201
        public Observer<VideoFrameSP>,
202 203
        public VideoNode
{};
204

205
/*=== VideoGenerator =========================================================*/
206

207 208
class VideoGenerator : public VideoFrameActiveWriter
{
209
public:
210 211 212 213 214
    VideoGenerator() : writableFrame_(), lastFrame_() {}

    virtual int getWidth() const = 0;
    virtual int getHeight() const = 0;
    virtual int getPixelFormat() const = 0;
215

216
    VideoFrameSP obtainLastFrame();
217 218

protected:
219
    // getNewFrame and publishFrame must be called by the same thread only
220
    VideoFrame& getNewFrame();
221
    void publishFrame();
222 223

private:
224 225
    VideoFrameUP writableFrame_;
    VideoFrameSP lastFrame_;
226 227
};

228 229
}

230
#endif // __VIDEO_BASE_H__