Frame.h 9.23 KB
Newer Older
1 2 3
/*
 * MIT License
 *
xiongziliang committed
4
 * Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
26 27 28 29

#ifndef ZLMEDIAKIT_FRAME_H
#define ZLMEDIAKIT_FRAME_H

30
#include <mutex>
31
#include <functional>
32
#include "Util/RingBuffer.h"
33
#include "Network/Socket.h"
34 35

using namespace std;
xiongziliang committed
36
using namespace toolkit;
37

xiongziliang committed
38
namespace mediakit{
39

40 41 42
typedef enum {
    CodecInvalid = -1,
    CodecH264 = 0,
43
    CodecH265,
44 45
    CodecAAC,
    CodecMax = 0x7FFF
46 47 48 49 50 51
} CodecId;

typedef enum {
    TrackInvalid = -1,
    TrackVideo = 0,
    TrackAudio,
52
    TrackTitle,
53
    TrackMax = 0x7FFF
54 55
} TrackType;

xiongziliang committed
56 57 58
/**
 * 编码信息的抽象接口
 */
59 60
class CodecInfo {
public:
xiongziliang committed
61 62
    typedef std::shared_ptr<CodecInfo> Ptr;

63 64 65 66 67 68 69 70 71 72 73 74 75 76
    CodecInfo(){}
    virtual ~CodecInfo(){}

    /**
     * 获取音视频类型
     */
    virtual TrackType getTrackType() const  = 0;

    /**
     * 获取编解码器类型
     */
    virtual CodecId getCodecId() const = 0;
};

xiongziliang committed
77 78 79
/**
 * 帧类型的抽象接口
 */
80
class Frame : public Buffer, public CodecInfo {
81 82 83
public:
    typedef std::shared_ptr<Frame> Ptr;
    virtual ~Frame(){}
84
    /**
xiongziliang committed
85
     * 时间戳,已经废弃,请使用dts() 、pts()接口
86
     */
xiongziliang committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    inline uint32_t stamp() const {
        return dts();
    };


    /**
     * 返回解码时间戳,单位毫秒
     * @return
     */
    virtual uint32_t dts() const = 0;



    /**
     * 返回显示时间戳,单位毫秒
     * @return
     */
    virtual uint32_t pts() const {
        return dts();
    }
107 108 109 110 111

    /**
     * 前缀长度,譬如264前缀为0x00 00 00 01,那么前缀长度就是4
     * aac前缀则为7个字节
     */
112 113 114 115 116 117 118
    virtual uint32_t prefixSize() const = 0;

    /**
     * 返回是否为关键帧
     * @return
     */
    virtual bool keyFrame() const = 0;
119 120

    /**
xiongziliang committed
121 122 123 124 125 126
     * 是否为配置帧,譬如sps pps vps
     * @return
     */
    virtual bool configFrame() const = 0;

    /**
127 128 129
     * 是否可以缓存
     */
    virtual bool cacheAble() const { return true; }
130 131 132 133 134 135

    /**
     * 返回可缓存的frame
     * @return
     */
    static Ptr getCacheAbleFrame(const Ptr &frame);
136 137
};

xiongziliang committed
138 139 140 141
/**
 * 循环池辅助类
 * @tparam T
 */
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
template <typename T>
class ResourcePoolHelper{
public:
    ResourcePoolHelper(int size = 8){
        _pool.setSize(size);
    }
    virtual ~ResourcePoolHelper(){}

    std::shared_ptr<T> obtainObj(){
        return _pool.obtain();
    }
private:
    ResourcePool<T> _pool;
};

xiongziliang committed
157 158 159
/**
 * 写帧接口的抽闲接口
 */
160
class FrameWriterInterface {
161
public:
162
    typedef std::shared_ptr<FrameWriterInterface> Ptr;
163

164 165
    FrameWriterInterface(){}
    virtual ~FrameWriterInterface(){}
166 167 168 169 170 171 172
    /**
    * 写入帧数据
    * @param frame 帧
    */
    virtual void inputFrame(const Frame::Ptr &frame) = 0;
};

xiongziliang committed
173 174 175
/**
 * 写帧接口转function,辅助类
 */
176 177 178 179 180
class FrameWriterInterfaceHelper : public FrameWriterInterface {
public:
    typedef std::shared_ptr<FrameWriterInterfaceHelper> Ptr;
    typedef std::function<void(const Frame::Ptr &frame)> onWriteFrame;

xiongziliang committed
181 182 183 184
    /**
     * inputFrame后触发onWriteFrame回调
     * @param cb
     */
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
    FrameWriterInterfaceHelper(const onWriteFrame& cb){
        _writeCallback = cb;
    }
    virtual ~FrameWriterInterfaceHelper(){}
    /**
    * 写入帧数据
    * @param frame 帧
    */
    void inputFrame(const Frame::Ptr &frame) override {
        _writeCallback(frame);
    }
private:
    onWriteFrame _writeCallback;
};


201 202 203
/**
 * 帧环形缓存接口类
 */
204
class FrameRingInterface : public FrameWriterInterface{
205 206
public:
    typedef RingBuffer<Frame::Ptr> RingType;
xiongziliang committed
207
    typedef std::shared_ptr<FrameRingInterface> Ptr;
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224

    FrameRingInterface(){}
    virtual ~FrameRingInterface(){}

    /**
     * 获取帧环形缓存
     * @return
     */
    virtual RingType::Ptr getFrameRing() const = 0;

    /**
     * 设置帧环形缓存
     * @param ring
     */
    virtual void setFrameRing(const RingType::Ptr &ring)  = 0;
};

xiongziliang committed
225 226 227
/**
 * 帧环形缓存
 */
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
class FrameRing : public FrameRingInterface{
public:
    typedef std::shared_ptr<FrameRing> Ptr;

    FrameRing(){
    }
    virtual ~FrameRing(){}

    /**
     * 获取帧环形缓存
     * @return
     */
    RingType::Ptr getFrameRing() const override {
        return _frameRing;
    }

    /**
     * 设置帧环形缓存
     * @param ring
     */
    void setFrameRing(const RingType::Ptr &ring) override {
        _frameRing = ring;
    }

    /**
     * 输入数据帧
     * @param frame
     */
256
    void inputFrame(const Frame::Ptr &frame) override{
xiongziliang committed
257 258 259
        if(_frameRing){
            _frameRing->write(frame,frame->keyFrame());
        }
260 261 262 263 264
    }
protected:
    RingType::Ptr _frameRing;
};

xiongziliang committed
265 266 267
/**
 * 支持代理转发的帧环形缓存
 */
268
class FrameRingInterfaceDelegate : public FrameRing {
269 270 271
public:
    typedef std::shared_ptr<FrameRingInterfaceDelegate> Ptr;

272
    FrameRingInterfaceDelegate(){}
273 274
    virtual ~FrameRingInterfaceDelegate(){}

275
    void addDelegate(const FrameWriterInterface::Ptr &delegate){
276 277
        lock_guard<mutex> lck(_mtx);
        _delegateMap.emplace(delegate.get(),delegate);
278 279
    }

280 281 282
    void delDelegate(void *ptr){
        lock_guard<mutex> lck(_mtx);
        _delegateMap.erase(ptr);
283 284 285 286 287 288 289
    }

    /**
     * 写入帧数据
     * @param frame 帧
     */
    void inputFrame(const Frame::Ptr &frame) override{
290 291 292 293
        FrameRing::inputFrame(frame);
        lock_guard<mutex> lck(_mtx);
        for(auto &pr : _delegateMap){
            pr.second->inputFrame(frame);
294 295 296
        }
    }
private:
297
    mutex _mtx;
298
    map<void *,FrameWriterInterface::Ptr>  _delegateMap;
299 300
};

301 302 303 304
/**
 * 通过Frame接口包装指针,方便使用者把自己的数据快速接入ZLMediaKit
 */
class FrameFromPtr : public Frame{
305
public:
306
    typedef std::shared_ptr<FrameFromPtr> Ptr;
307
    char *data() const override{
xiongziliang committed
308
        return _ptr;
309 310
    }
    uint32_t size() const override {
xiongziliang committed
311
        return _size;
312
    }
xiongziliang committed
313

xiongziliang committed
314
    uint32_t dts() const override {
xiongziliang committed
315 316 317 318 319 320 321 322
        return _dts;
    }

    uint32_t pts() const override{
        if(_pts){
            return _pts;
        }
        return dts();
323
    }
xiongziliang committed
324

325
    uint32_t prefixSize() const override{
xiongziliang committed
326
        return _prefixSize;
327
    }
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
protected:
    char *_ptr;
    uint32_t _size;
    uint32_t _dts;
    uint32_t _pts = 0;
    uint32_t _prefixSize;
};

/**
 * 不可缓存的帧,在DevChannel类中有用到。
 * 该帧类型用于防止内存拷贝,直接使用指针传递数据
 * 在大多数情况下,ZLMediaKit是同步对帧数据进行使用和处理的
 * 所以提供此类型的帧很有必要,但是有时又无法避免缓存帧做后续处理
 * 所以可以通过Frame::getCacheAbleFrame方法拷贝一个可缓存的帧
 */
class FrameNoCacheAble : public FrameFromPtr{
public:
    typedef std::shared_ptr<FrameNoCacheAble> Ptr;
xiongziliang committed
346

347 348 349 350 351 352 353
    /**
     * 该帧不可缓存
     * @return
     */
    bool cacheAble() const override {
        return false;
    }
354 355
};

356 357 358 359 360
/**
 * 该对象的功能是把一个不可缓存的帧转换成可缓存的帧
 * @see FrameNoCacheAble
 */
class FrameCacheAble : public FrameFromPtr {
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
public:
    typedef std::shared_ptr<FrameCacheAble> Ptr;

    FrameCacheAble(const Frame::Ptr &frame){
        if(frame->cacheAble()){
            _frame = frame;
            _ptr = frame->data();
        }else{
            _buffer = std::make_shared<BufferRaw>();
            _buffer->assign(frame->data(),frame->size());
            _ptr = _buffer->data();
        }
        _size = frame->size();
        _dts = frame->dts();
        _pts = frame->pts();
        _prefixSize = frame->prefixSize();
        _trackType = frame->getTrackType();
        _codec = frame->getCodecId();
        _key = frame->keyFrame();
xiongziliang committed
380
        _config = frame->configFrame();
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
    }

    virtual ~FrameCacheAble() = default;

    /**
     * 可以被缓存
     * @return
     */
    bool cacheAble() const override {
        return true;
    }

    TrackType getTrackType() const override{
        return _trackType;
    }

    CodecId getCodecId() const override{
        return _codec;
    }

    bool keyFrame() const override{
        return _key;
    }
xiongziliang committed
404 405 406 407

    bool configFrame() const override{
        return _config;
    }
408 409 410 411 412 413
private:
    Frame::Ptr _frame;
    BufferRaw::Ptr _buffer;
    TrackType _trackType;
    CodecId _codec;
    bool _key;
xiongziliang committed
414
    bool _config;
415 416
};

417

xiongziliang committed
418
}//namespace mediakit
419 420

#endif //ZLMEDIAKIT_FRAME_H