Device.h 3.71 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
xiongziliang committed
3
 *
4
 * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
xiongziliang committed
5
 *
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 10 11 12
 */

#ifndef DEVICE_DEVICE_H_
#define DEVICE_DEVICE_H_
xiongzilaing committed
13 14

#include <memory>
xzl committed
15 16 17
#include <string>
#include <functional>
#include "Util/util.h"
xiongziliang committed
18
#include "Util/TimeTicker.h"
19
#include "Common/MultiMediaSourceMuxer.h"
xzl committed
20
using namespace std;
xiongziliang committed
21
using namespace toolkit;
xzl committed
22

xiongziliang committed
23
namespace mediakit {
xzl committed
24

25 26 27
class H264Encoder;
class AACEncoder;

xzl committed
28 29
class VideoInfo {
public:
xiongziliang committed
30
    CodecId codecId = CodecH264;
31 32 33
    int iWidth;
    int iHeight;
    float iFrameRate;
xzl committed
34
};
xiongziliang committed
35

xzl committed
36 37
class AudioInfo {
public:
xiongziliang committed
38
    CodecId codecId = CodecAAC;
39 40 41
    int iChannel;
    int iSampleBit;
    int iSampleRate;
xzl committed
42 43
};

44
/**
xiongziliang committed
45
 * MultiMediaSourceMuxer类的包装,方便初学者使用
46 47
 */
class DevChannel  : public MultiMediaSourceMuxer{
xzl committed
48
public:
49
    typedef std::shared_ptr<DevChannel> Ptr;
50
    //fDuration<=0为直播,否则为点播
51 52
    DevChannel(const string &vhost, const string &app, const string &stream_id,
               float duration = 0, bool enable_hls = true, bool enable_mp4 = false);
53

54
    ~DevChannel() override ;
xzl committed
55

56
    /**
xiongziliang committed
57 58
     * 初始化视频Track
     * 相当于MultiMediaSourceMuxer::addTrack(VideoTrack::Ptr );
xiongziliang committed
59
     * @param info 视频相关信息
60
     */
61 62
    void initVideo(const VideoInfo &info);

63
    /**
xiongziliang committed
64 65
     * 初始化音频Track
     * 相当于MultiMediaSourceMuxer::addTrack(AudioTrack::Ptr );
xiongziliang committed
66
     * @param info 音频相关信息
67
     */
68 69 70 71
    void initAudio(const AudioInfo &info);

    /**
     * 输入264帧
xiongziliang committed
72 73
     * @param data 264单帧数据指针
     * @param len 数据指针长度
74 75 76
     * @param dts 解码时间戳,单位毫秒;等于0时内部会自动生成时间戳
     * @param pts 播放时间戳,单位毫秒;等于0时内部会赋值为dts
     */
xiongziliang committed
77
    void inputH264(const char *data, int len, uint32_t dts, uint32_t pts = 0);
78 79 80

    /**
     * 输入265帧
xiongziliang committed
81 82
     * @param data 265单帧数据指针
     * @param len 数据指针长度
83 84 85
     * @param dts 解码时间戳,单位毫秒;等于0时内部会自动生成时间戳
     * @param pts 播放时间戳,单位毫秒;等于0时内部会赋值为dts
     */
xiongziliang committed
86
    void inputH265(const char *data, int len, uint32_t dts, uint32_t pts = 0);
87 88

    /**
xiongziliang committed
89 90 91 92 93
     * 输入aac帧
     * @param data_without_adts 不带adts头的aac帧
     * @param len 帧数据长度
     * @param dts 时间戳,单位毫秒
     * @param adts_header adts头
94
     */
xiongziliang committed
95
    void inputAAC(const char *data_without_adts, int len, uint32_t dts, const char *adts_header);
96

97
    /**
98
     * 输入OPUS/G711音频帧
xiongziliang committed
99 100 101
     * @param data 音频帧
     * @param len 帧数据长度
     * @param dts 时间戳,单位毫秒
102
     */
103
    void inputAudio(const char *data, int len, uint32_t dts);
xiongziliang committed
104

105 106 107 108 109 110
    /**
     * 输入yuv420p视频帧,内部会完成编码并调用inputH264方法
     * @param apcYuv
     * @param aiYuvLen
     * @param uiStamp
     */
111
    void inputYUV(char *apcYuv[3], int aiYuvLen[3], uint32_t uiStamp);
112

113 114 115 116 117 118 119 120

    /**
     * 输入pcm数据,内部会完成编码并调用inputAAC方法
     * @param pcData
     * @param iDataLen
     * @param uiStamp
     */
    void inputPCM(char *pcData, int iDataLen, uint32_t uiStamp);
121

xzl committed
122
private:
123 124 125
    MediaOriginType getOriginType(MediaSource &sender) const override;

private:
126 127
    std::shared_ptr<H264Encoder> _pH264Enc;
    std::shared_ptr<AACEncoder> _pAacEnc;
128 129
    std::shared_ptr<VideoInfo> _video;
    std::shared_ptr<AudioInfo> _audio;
130
    SmoothTicker _aTicker[2];
xzl committed
131 132
};

xiongziliang committed
133
} /* namespace mediakit */
xzl committed
134 135

#endif /* DEVICE_DEVICE_H_ */