qt_rtmp_demo/media/RtmpPusher.h

121 lines
2.8 KiB
C++

#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include "librtmp_send264.h"
#include "librtmp\rtmp.h"
#include "librtmp\rtmp_sys.h"
#ifdef __cplusplus
}
#endif
#include "librtmp\amf.h"
#include "AACAudioCoder.h"
#include "sps_decode.h"
#include "VideoCoder.h"
#include <mutex>
#include <thread>
#include <queue>
#include<iostream>
#include <string>
#include <QDebug>
#include <QString>
using namespace std;
#define RTMP_HEAD_SIZE (sizeof(RTMPPacket)+RTMP_MAX_HEADER_SIZE)
class RtmpPusher
{
protected:
RTMP *m_pRtmp;
string mUrl;
int mTick = 10;
std::mutex mMux;
std::thread *mThread;
bool mIfConnected = false;
std::thread::id mThreadId;
public:
bool IfConnect();
int RTMP264_Connect(const char* url);
void RTMP264_Close();
int SendPacket(unsigned int nPacketType, unsigned char *data, unsigned int size, unsigned int nTimestamp);
int SendVideoPacket(unsigned int nPacketType, unsigned char *data, unsigned int size, unsigned int nTimestamp);
int SetTick(int tick) { this->mTick = tick; };
virtual int StartPush() { return 0; };
RtmpPusher();
virtual ~RtmpPusher();
};
/**
* _RTMPMetadata
* 内部结构体。该结构体主要用于存储和传递元数据信息
*/
typedef struct _RTMPMetadata
{
// video, must be h264 type
int nWidth;
int nHeight;
int nFrameRate;
unsigned int nSpsLen;
unsigned char *Sps;
unsigned int nPpsLen;
unsigned char *Pps;
} RTMPMetadata, *LPRTMPMetadata;
enum Payload_Type {
PAYLOAD_TYPE_VIDEO = 0,
PAYLOAD_TYPE_AUDIO = 1
};
typedef struct _T_Buffer {
uint8_t *buf;
int len;
Payload_Type type;
}Buffer;
class H264RtmpPuser : public RtmpPusher ,
public VideoCodeObserver,
public AAC_CODER::AACAudioCoder::EncodeAudioObserver {
private:
bool mFirtACC;
uint16_t mAudioPts;
bool mIfStart = false;
// 视频同步包
int SendVideoSpsPps(unsigned char *pps, int pps_len,
unsigned char * sps, int sps_len, unsigned int nTimeStamp);
// 音频同步包
int SendAudioSync(int audioType, int sampleIndex, int channel, unsigned int nTimeStamp);
int SendAudioData(unsigned char*dat, unsigned int size, unsigned int nTimeStamp);
int SendH264Packet(unsigned char *data,
unsigned int size, int bIsKeyFrame, unsigned int nTimeStamp);
int sendDataPackH264(unsigned char *data,
unsigned int size, int bIsKeyFrame, unsigned int nTimeStamp);
int sendDataPackAAC(unsigned char *data, unsigned int size, unsigned int nTimeStamp);
uint32_t mStartTime;
public:
queue<Buffer> mPack;
RTMPMetadata metaData;
H264RtmpPuser();
int sortAndSendNal(uint8_t *data, int len);
int SetSpsPps(unsigned char *pps, int pps_len,
unsigned char * sps, int sps_len);
void OnAudioEncode(const void *frameaddress, uint32_t framelen, uint16_t pts);
void OnGetCodeFrame(uint8_t *data, int len);
void ProcessSend();
int StartPush();
int StopPush();
};
int ThreadEncode(H264RtmpPuser*p);