Frame.cpp 2.6 KB
Newer Older
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
3 4 5
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
xiongziliang committed
6 7 8
 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
9 10 11 12 13 14 15 16 17
 */

#include "Frame.h"

using namespace std;
using namespace toolkit;

namespace mediakit{

xiongziliang committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/**
 * 该对象的功能是把一个不可缓存的帧转换成可缓存的帧
 */
class FrameCacheAble : public FrameFromPtr {
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();
        _prefix_size = frame->prefixSize();
38
        _codec_id = frame->getCodecId();
xiongziliang committed
39 40 41 42
        _key = frame->keyFrame();
        _config = frame->configFrame();
    }

43
    ~FrameCacheAble() override = default;
xiongziliang committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

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

    bool keyFrame() const override{
        return _key;
    }

    bool configFrame() const override{
        return _config;
    }
59

xiongziliang committed
60 61 62 63 64 65 66
private:
    Frame::Ptr _frame;
    BufferRaw::Ptr _buffer;
    bool _key;
    bool _config;
};

67 68 69 70 71 72 73
Frame::Ptr Frame::getCacheAbleFrame(const Frame::Ptr &frame){
    if(frame->cacheAble()){
        return frame;
    }
    return std::make_shared<FrameCacheAble>(frame);
}

74
#define SWITCH_CASE(codec_id) case codec_id : return #codec_id
xiongziliang committed
75 76
const char *getCodecName(CodecId codecId) {
    switch (codecId) {
77 78 79
        SWITCH_CASE(CodecH264);
        SWITCH_CASE(CodecH265);
        SWITCH_CASE(CodecAAC);
80 81
        SWITCH_CASE(CodecG711A);
        SWITCH_CASE(CodecG711U);
xiongziliang committed
82 83
        SWITCH_CASE(CodecOpus);
        default : return "unknown codec";
84 85 86
    }
}

xiongziliang committed
87 88
TrackType getTrackType(CodecId codecId){
    switch (codecId){
xiongziliang committed
89 90 91 92 93 94 95 96 97
        case CodecH264:
        case CodecH265: return TrackVideo;
        case CodecAAC:
        case CodecG711A:
        case CodecG711U:
        case CodecOpus: return TrackAudio;
        default: return TrackInvalid;
    }
}
98

xiongziliang committed
99 100 101 102 103 104 105
const char *CodecInfo::getCodecName() {
    return mediakit::getCodecName(getCodecId());
}

TrackType CodecInfo::getTrackType() {
    return mediakit::getTrackType(getCodecId());
}
xiongziliang committed
106
}//namespace mediakit