Frame.cpp 2.69 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
/**
 * 该对象的功能是把一个不可缓存的帧转换成可缓存的帧
 */
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();
        _codecid = frame->getCodecId();
        _key = frame->keyFrame();
        _config = frame->configFrame();
    }

    virtual ~FrameCacheAble() = default;

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

    CodecId getCodecId() const override{
        return _codecid;
    }

    bool keyFrame() const override{
        return _key;
    }

    bool configFrame() const override{
        return _config;
    }
private:
    Frame::Ptr _frame;
    BufferRaw::Ptr _buffer;
    CodecId _codecid;
    bool _key;
    bool _config;
};

71 72 73 74 75 76 77
Frame::Ptr Frame::getCacheAbleFrame(const Frame::Ptr &frame){
    if(frame->cacheAble()){
        return frame;
    }
    return std::make_shared<FrameCacheAble>(frame);
}

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

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

xiongziliang committed
103 104 105 106 107 108 109
const char *CodecInfo::getCodecName() {
    return mediakit::getCodecName(getCodecId());
}

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