G711.h 2.67 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
 * 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.
 */

#ifndef ZLMEDIAKIT_G711_H
#define ZLMEDIAKIT_G711_H

#include "Frame.h"
#include "Track.h"

namespace mediakit{

/**
xiongziliang committed
20
 * G711帧
21
 */
xiongziliang committed
22
class G711Frame : public FrameImp {
23
public:
xiongziliang committed
24 25
    G711Frame(){
        _codecid = CodecG711A;
26
    }
xiongziliang committed
27
};
28

xiongziliang committed
29
class G711FrameNoCacheAble : public FrameFromPtr {
30 31 32
public:
    typedef std::shared_ptr<G711FrameNoCacheAble> Ptr;

xiongziliang committed
33
    G711FrameNoCacheAble(char *ptr,uint32_t size,uint32_t dts, uint32_t pts = 0,int prefix_size = 0){
34 35 36
        _ptr = ptr;
        _size = size;
        _dts = dts;
xiongziliang committed
37
        _prefix_size = prefix_size;
38 39 40 41 42 43
    }

    void setCodec(CodecId codecId){
        _codecId = codecId;
    }

44 45 46 47 48 49 50 51 52 53 54 55 56 57
    CodecId getCodecId() const override{
        return _codecId;
    }

    bool keyFrame() const override {
        return false;
    }

    bool configFrame() const override{
        return false;
    }

private:
    CodecId _codecId;
xiongziliang committed
58
};
59 60

/**
xiongziliang committed
61
 * G711音频通道
62
 */
xiongziliang committed
63
class G711Track : public AudioTrackImp{
64 65
public:
    typedef std::shared_ptr<G711Track> Ptr;
xiongziliang committed
66
    G711Track(CodecId codecId,int sample_rate, int channels, int sample_bit) : AudioTrackImp(codecId,sample_rate,channels,sample_bit){}
67 68

private:
xiongziliang committed
69
    //克隆该Track
70 71 72 73 74 75 76 77
    Track::Ptr clone() override {
        return std::make_shared<std::remove_reference<decltype(*this)>::type >(*this);
    }
    //生成sdp
    Sdp::Ptr getSdp() override ;
};

/**
xiongziliang committed
78 79
 * G711类型SDP
 */
80 81 82
class G711Sdp : public Sdp {
public:
    /**
xiongziliang committed
83 84
     * G711采样率固定为8000
     * @param codecId G711A G711U
xiongziliang committed
85
     * @param sample_rate 音频采样率
xiongziliang committed
86
     * @param payload_type rtp payload
xiongziliang committed
87
     * @param bitrate 比特率
88
     */
xiongziliang committed
89 90 91
    G711Sdp(CodecId codecId,
            int sample_rate,
            int channels,
xiongziliang committed
92 93 94 95
            int payload_type = 98,
            int bitrate = 128) : Sdp(sample_rate,payload_type), _codecId(codecId){
        _printer << "m=audio 0 RTP/AVP " << payload_type << "\r\n";
        _printer << "a=rtpmap:" << payload_type << (codecId == CodecG711A ? " PCMA/" : " PCMU/") << sample_rate  << "/" << channels << "\r\n";
xiongziliang committed
96
        _printer << "a=control:trackID=" << (int)TrackAudio << "\r\n";
97 98 99 100 101 102 103 104 105 106 107
    }

    string getSdp() const override {
        return _printer;
    }

    CodecId getCodecId() const override {
        return _codecId;
    }
private:
    _StrPrinter _printer;
xiongziliang committed
108
    CodecId _codecId;
109 110 111
};

}//namespace mediakit
xiongziliang committed
112
#endif //ZLMEDIAKIT_G711_H