2020-06-04 15:28:45 +00:00
|
|
|
|
#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>
|
|
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
#pragma comment(lib,"WS2_32.lib")
|
|
|
|
|
#pragma comment(lib,"winmm.lib")
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
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
|
|
|
|
|
* <EFBFBD>ڲ<EFBFBD><EFBFBD>ṹ<EFBFBD>塣<EFBFBD>ýṹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD>ڴ洢<EFBFBD>ʹ<EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
|
|
|
|
|
*/
|
|
|
|
|
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;
|
|
|
|
|
// <20><>Ƶͬ<C6B5><CDAC><EFBFBD><EFBFBD>
|
|
|
|
|
int SendVideoSpsPps(unsigned char *pps, int pps_len,
|
|
|
|
|
unsigned char * sps, int sps_len, unsigned int nTimeStamp);
|
|
|
|
|
// <20><>Ƶͬ<C6B5><CDAC><EFBFBD><EFBFBD>
|
|
|
|
|
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);
|
2020-07-07 16:04:24 +00:00
|
|
|
|
uint32_t mStartTime;
|
2020-06-04 15:28:45 +00:00
|
|
|
|
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);
|