Device.cpp 5.89 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
xiongziliang committed
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.
xzl committed
9
 */
xiongziliang committed
10

xzl committed
11 12
#include "Device.h"
#include "Util/logger.h"
xiongziliang committed
13
#include "Util/base64.h"
xiongziliang committed
14
#include "Extension/AAC.h"
15
#include "Extension/Opus.h"
16
#include "Extension/G711.h"
xiongziliang committed
17
#include "Extension/H264.h"
xiongziliang committed
18
#include "Extension/H265.h"
xiongziliang committed
19
using namespace toolkit;
xzl committed
20

xiongziliang committed
21
namespace mediakit {
xzl committed
22

23 24 25
DevChannel::DevChannel(const string &vhost, const string &app, const string &stream_id,
                       float duration, bool enable_hls, bool enable_mp4) :
        MultiMediaSourceMuxer(vhost, app, stream_id, duration, true, true, enable_hls, enable_mp4) {}
xzl committed
26

27
DevChannel::~DevChannel() {}
xzl committed
28

29
#ifdef ENABLE_X264
xzl committed
30
void DevChannel::inputYUV(char* apcYuv[3], int aiYuvLen[3], uint32_t uiStamp) {
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    //TimeTicker1(50);
    if (!_pH264Enc) {
        _pH264Enc.reset(new H264Encoder());
        if (!_pH264Enc->init(_video->iWidth, _video->iHeight, _video->iFrameRate)) {
            _pH264Enc.reset();
            WarnL << "H264Encoder init failed!";
        }
    }
    if (_pH264Enc) {
        H264Encoder::H264Frame *pOut;
        int iFrames = _pH264Enc->inputData(apcYuv, aiYuvLen, uiStamp, &pOut);
        for (int i = 0; i < iFrames; i++) {
            inputH264((char *) pOut[i].pucData, pOut[i].iLength, uiStamp);
        }
    }
xzl committed
46
}
47
#endif //ENABLE_X264
xzl committed
48 49

#ifdef ENABLE_FAAC
50
void DevChannel::inputPCM(char* pcData, int iDataLen, uint32_t uiStamp) {
51 52 53 54 55 56 57 58 59 60
    if (!_pAacEnc) {
        _pAacEnc.reset(new AACEncoder());
        if (!_pAacEnc->init(_audio->iSampleRate, _audio->iChannel, _audio->iSampleBit)) {
            _pAacEnc.reset();
            WarnL << "AACEncoder init failed!";
        }
    }
    if (_pAacEnc) {
        unsigned char *pucOut;
        int iRet = _pAacEnc->inputData(pcData, iDataLen, &pucOut);
xiongziliang committed
61 62
        if (iRet > 7) {
            inputAAC((char *) pucOut + 7, iRet - 7, uiStamp, (char *)pucOut);
63 64
        }
    }
xzl committed
65
}
66
#endif //ENABLE_FAAC
xzl committed
67

xiongziliang committed
68
void DevChannel::inputH264(const char *data, int len, uint32_t dts, uint32_t pts) {
xiongziliang committed
69
    if(dts == 0){
70 71 72 73
        dts = (uint32_t)_aTicker[0].elapsedTime();
    }
    if(pts == 0){
        pts = dts;
xiongziliang committed
74
    }
75

xiongziliang committed
76 77 78
    //由于rtmp/hls/mp4需要缓存时间戳相同的帧,
    //所以使用FrameNoCacheAble类型的帧反而会在转换成FrameCacheAble时多次内存拷贝
    //在此处只拷贝一次,性能开销更低
79 80 81
    H264Frame::Ptr frame = std::make_shared<H264Frame>();
    frame->_dts = dts;
    frame->_pts = pts;
82 83
    frame->_buffer.assign(data, len);
    frame->_prefix_size = prefixSize(data,len);
84
    inputFrame(frame);
xzl committed
85 86
}

xiongziliang committed
87
void DevChannel::inputH265(const char *data, int len, uint32_t dts, uint32_t pts) {
88 89 90 91 92 93 94
    if(dts == 0){
        dts = (uint32_t)_aTicker[0].elapsedTime();
    }
    if(pts == 0){
        pts = dts;
    }

xiongziliang committed
95 96 97
    //由于rtmp/hls/mp4需要缓存时间戳相同的帧,
    //所以使用FrameNoCacheAble类型的帧反而会在转换成FrameCacheAble时多次内存拷贝
    //在此处只拷贝一次,性能开销更低
98 99 100
    H265Frame::Ptr frame = std::make_shared<H265Frame>();
    frame->_dts = dts;
    frame->_pts = pts;
101 102
    frame->_buffer.assign(data, len);
    frame->_prefix_size = prefixSize(data,len);
103
    inputFrame(frame);
xiongziliang committed
104 105
}

106
class FrameAutoDelete : public FrameFromPtr{
xiongziliang committed
107 108
public:
    template <typename ... ARGS>
109 110 111
    FrameAutoDelete(ARGS && ...args) : FrameFromPtr(std::forward<ARGS>(args)...){}

    ~FrameAutoDelete() override {
xiongziliang committed
112 113
        delete [] _ptr;
    };
114

xiongziliang committed
115 116
    bool cacheAble() const override {
        return true;
xiongziliang committed
117
    }
xiongziliang committed
118 119 120
};

void DevChannel::inputAAC(const char *data_without_adts, int len, uint32_t dts, const char *adts_header){
121 122
    if (dts == 0) {
        dts = (uint32_t) _aTicker[1].elapsedTime();
xiongziliang committed
123 124
    }

125 126
    if (adts_header) {
        if (adts_header + ADTS_HEADER_LEN == data_without_adts) {
xiongziliang committed
127
            //adts头和帧在一起
128 129
            inputFrame(std::make_shared<FrameFromPtr>(_audio->codecId, (char *) data_without_adts - ADTS_HEADER_LEN, len + ADTS_HEADER_LEN, dts, 0, ADTS_HEADER_LEN));
        } else {
xiongziliang committed
130
            //adts头和帧不在一起
131 132 133 134
            char *data_with_adts = new char[len + ADTS_HEADER_LEN];
            memcpy(data_with_adts, adts_header, ADTS_HEADER_LEN);
            memcpy(data_with_adts + ADTS_HEADER_LEN, data_without_adts, len);
            inputFrame(std::make_shared<FrameAutoDelete>(_audio->codecId, data_with_adts, len + ADTS_HEADER_LEN, dts, 0, ADTS_HEADER_LEN));
xiongziliang committed
135
        }
136 137 138
    } else {
        //没有adts头
        inputFrame(std::make_shared<FrameFromPtr>(_audio->codecId, (char *) data_without_adts, len, dts, 0, 0));
139
    }
xzl committed
140 141
}

142
void DevChannel::inputAudio(const char *data, int len, uint32_t dts){
xiongziliang committed
143
    if (dts == 0) {
144
        dts = (uint32_t) _aTicker[1].elapsedTime();
145
    }
146
    inputFrame(std::make_shared<FrameFromPtr>(_audio->codecId, (char *) data, len, dts, 0));
147 148
}

xiongziliang committed
149
void DevChannel::initVideo(const VideoInfo &info) {
150
    _video = std::make_shared<VideoInfo>(info);
xiongziliang committed
151 152 153 154 155
    switch (info.codecId){
        case CodecH265 : addTrack(std::make_shared<H265Track>()); break;
        case CodecH264 : addTrack(std::make_shared<H264Track>()); break;
        default: WarnL << "不支持该类型的视频编码类型:" << info.codecId; break;
    }
xzl committed
156 157
}

xiongziliang committed
158
void DevChannel::initAudio(const AudioInfo &info) {
xiongziliang committed
159 160
    _audio = std::make_shared<AudioInfo>(info);
    switch (info.codecId) {
xiongziliang committed
161
        case CodecAAC : addTrack(std::make_shared<AACTrack>()); break;
xiongziliang committed
162
        case CodecG711A :
xiongziliang committed
163
        case CodecG711U : addTrack(std::make_shared<G711Track>(info.codecId, info.iSampleRate, info.iChannel, info.iSampleBit)); break;
164
        case CodecOpus : addTrack(std::make_shared<OpusTrack>()); break;
xiongziliang committed
165
        default: WarnL << "不支持该类型的音频编码类型:" << info.codecId; break;
166
    }
xzl committed
167
}
168

xiongziliang committed
169
} /* namespace mediakit */
xzl committed
170