Skip to content

Commit 3d687d8

Browse files
committed
improved:pull video of webrtc support for example
1 parent 089fa87 commit 3d687d8

File tree

1 file changed

+96
-30
lines changed

1 file changed

+96
-30
lines changed

XEngine_APPClient/APPClient_WebRTC/APPClient_WebRTC.cpp

Lines changed: 96 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#pragma comment(lib,"XEngine_Client/XClient_OPenSsl")
1010
#pragma comment(lib,"XEngine_Client/XClient_APIHelp")
1111
#pragma comment(lib,"XEngine_StreamMedia/StreamMedia_SDPProtocol")
12+
#pragma comment(lib,"XEngine_StreamMedia/StreamMedia_RTPProtocol")
13+
#pragma comment(lib,"XEngine_AVCodec/XEngine_AVHelp")
1214
#pragma comment(lib,"XEngine_RfcComponents/RfcComponents_NatProtocol")
1315
#pragma comment(lib,"Ws2_32")
1416
#endif
@@ -21,6 +23,8 @@
2123
#include <XEngine_Include/XEngine_ProtocolHdr.h>
2224
#include <XEngine_Include/XEngine_BaseLib/BaseLib_Define.h>
2325
#include <XEngine_Include/XEngine_BaseLib/BaseLib_Error.h>
26+
#include <XEngine_Include/XEngine_Core/ManagePool_Define.h>
27+
#include <XEngine_Include/XEngine_Core/ManagePool_Error.h>
2428
#include <XEngine_Include/XEngine_Client/XClient_Define.h>
2529
#include <XEngine_Include/XEngine_Client/XClient_Error.h>
2630
#include <XEngine_Include/XEngine_Client/APIClient_Define.h>
@@ -29,6 +33,11 @@
2933
#include <XEngine_Include/XEngine_Client/SslClient_Error.h>
3034
#include <XEngine_Include/XEngine_StreamMedia/SDPProtocol_Define.h>
3135
#include <XEngine_Include/XEngine_StreamMedia/SDPProtocol_Error.h>
36+
#include <XEngine_Include/XEngine_StreamMedia/RTPProtocol_Define.h>
37+
#include <XEngine_Include/XEngine_StreamMedia/RTPProtocol_Error.h>
38+
#include <XEngine_Include/XEngine_AVCodec/VideoCodec_Define.h>
39+
#include <XEngine_Include/XEngine_AVCodec/AVHelp_Define.h>
40+
#include <XEngine_Include/XEngine_AVCodec/AVHelp_Error.h>
3241
#include <XEngine_Include/XEngine_RfcComponents/NatProtocol_Define.h>
3342
#include <XEngine_Include/XEngine_RfcComponents/NatProtocol_Error.h>
3443
#include "../../XEngine_Source/XEngine_UserProtocol.h"
@@ -40,6 +49,9 @@ typedef struct
4049
srtp_t st_SRTPRecvCtx;
4150
}SRTPCORE_CLIENTINFO;
4251

52+
std::string m_ClientKey;
53+
std::string m_ServerKey;
54+
4355
bool APPClient_WEBRTC_SDPPacket(LPCXSTR lpszAPIUrl, LPCXSTR lpszFileCert, XCHAR* ptszSDPPacket, int* pInt_SDPLen)
4456
{
4557
XNETHANDLE xhToken = 0;
@@ -48,14 +60,16 @@ bool APPClient_WEBRTC_SDPPacket(LPCXSTR lpszAPIUrl, LPCXSTR lpszFileCert, XCHAR*
4860
SDPProtocol_Packet_Owner(xhToken, _X("-"), 123456789, _X("127.0.0.1"));
4961
SDPProtocol_Packet_Session(xhToken, _X("live/stream"));
5062
SDPProtocol_Packet_KeepTime(xhToken);
51-
SDPProtocol_Packet_Bundle(xhToken);
63+
SDPProtocol_Packet_Bundle(xhToken, 0, -1);
5264

5365
SDPProtocol_Packet_OptionalAddAttr(xhToken, _X("extmap-allow-mixed"));
5466
SDPProtocol_Packet_OptionalAddAttr(xhToken, _X("msid-semantic"), _X(" WMS"));
67+
STREAMMEDIA_SDPPROTOCOL_MEDIAINFO st_SDPMedia = {};
5568
//AUDIO
69+
5670
int nAVCount = 1;
5771
XCHAR** pptszAVList;
58-
BaseLib_OperatorMemory_Malloc((XPPPMEM)&pptszAVList, nAVCount, 64);
72+
BaseLib_OperatorMemory_Malloc((XPPPMEM)&pptszAVList, nAVCount, 64);/*
5973
_xstprintf(pptszAVList[0], _X("111"));
6074
6175
SDPProtocol_Packet_AddMedia(xhToken, _X("audio"), _X("UDP/TLS/RTP/SAVPF"), &pptszAVList, nAVCount, 0, 9);
@@ -67,17 +81,17 @@ bool APPClient_WEBRTC_SDPPacket(LPCXSTR lpszAPIUrl, LPCXSTR lpszFileCert, XCHAR*
6781
SDPProtocol_Packet_OnlyRWFlag(xhToken);
6882
SDPProtocol_Packet_RtcpComm(xhToken, true, false);
6983
70-
STREAMMEDIA_SDPPROTOCOL_MEDIAINFO st_SDPMedia = {};
7184
st_SDPMedia.st_RTPMap.nChannel = 2;
7285
st_SDPMedia.st_RTPMap.nSampleRate = 48000;
7386
st_SDPMedia.st_FmtpAudio.nMinPTime = 10;
7487
st_SDPMedia.st_FmtpAudio.nUseINBandfec = 1;
7588
_xstprintf(st_SDPMedia.st_RTPMap.tszCodecName, _X("opus"));
7689
st_SDPMedia.bTransportCC = true;
7790
SDPProtocol_Packet_AudioFmt(xhToken, 111, &st_SDPMedia, true);
91+
*/
7892
//VIDEO
7993
_xstprintf(pptszAVList[0], _X("106"));
80-
SDPProtocol_Packet_AddMedia(xhToken, _X("video"), _X("UDP/TLS/RTP/SAVPF"), &pptszAVList, nAVCount, 1, 9);
94+
SDPProtocol_Packet_AddMedia(xhToken, _X("video"), _X("UDP/TLS/RTP/SAVPF"), &pptszAVList, nAVCount, 0, 9);
8195
SDPProtocol_Packet_ClientInet(xhToken, _X("0.0.0.0"));
8296
SDPProtocol_Packet_RtcpInet(xhToken, 9);
8397
SDPProtocol_Packet_ICEUser(xhToken, _X("nzWE"), _X("xk/FvO+TXrJy6739etI/y0Kc"), true);
@@ -202,18 +216,12 @@ bool APPClient_WEBRTC_SRTPCreate(LPCXBTR lpszKEYBuffer, SRTPCORE_CLIENTINFO* pSt
202216
nPos += SRTP_MASTER_KEY_SALT_LEN;
203217
std::string m_StrServerSalt(reinterpret_cast<LPCXSTR>(lpszKEYBuffer + nPos), SRTP_MASTER_KEY_SALT_LEN);
204218

205-
std::string m_ClientKey = m_StrClientKey + m_StrClientSalt;
206-
std::string m_ServerKey = m_StrServerKey + m_StrServerSalt;
219+
m_ClientKey = m_StrClientKey + m_StrClientSalt;
220+
m_ServerKey = m_StrServerKey + m_StrServerSalt;
207221

208222
srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(&st_SRTPPolicy.rtp);
209223
srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(&st_SRTPPolicy.rtcp);
210224

211-
st_SRTPPolicy.ssrc.value = 0;
212-
st_SRTPPolicy.window_size = 8192;
213-
st_SRTPPolicy.allow_repeat_tx = 1;
214-
st_SRTPPolicy.next = NULL;
215-
216-
//初始化接受上下文
217225
st_SRTPPolicy.ssrc.type = ssrc_any_inbound;
218226
st_SRTPPolicy.key = (unsigned char*)m_ServerKey.c_str();
219227

@@ -222,17 +230,11 @@ bool APPClient_WEBRTC_SRTPCreate(LPCXBTR lpszKEYBuffer, SRTPCORE_CLIENTINFO* pSt
222230
{
223231
return false;
224232
}
225-
st_SRTPPolicy.ssrc.type = ssrc_any_outbound;
226-
st_SRTPPolicy.key = (unsigned char*)m_ClientKey.c_str();
227-
228-
if (srtp_err_status_ok != (nRet = srtp_create(&pSt_SRTPCore->st_SRTPSendCtx, &st_SRTPPolicy)))
229-
{
230-
return false;
231-
}
232233
return true;
233234
}
234235
bool APPClient_WEBRTC_Dlts(XSOCKET hSocket)
235236
{
237+
LPCXSTR lpszRTPClient = _X("client");
236238
LPCXSTR lpszCertFile = _X("D:\\XEngine_StreamMedia\\XEngine_APPClient\\Debug\\certificate.crt");
237239
LPCXSTR lpszPrivateFile = _X("D:\\XEngine_StreamMedia\\XEngine_APPClient\\Debug\\private.key");
238240

@@ -241,6 +243,9 @@ bool APPClient_WEBRTC_Dlts(XSOCKET hSocket)
241243
{
242244
return false;
243245
}
246+
RTPProtocol_Parse_Init(1);
247+
RTPProtocol_Parse_Insert(lpszRTPClient);
248+
244249
XClient_OPenSsl_ConfigEx(xhSsl);
245250

246251
XCLIENT_SSLCERT_SRVINFO st_SslInfo = {};
@@ -249,6 +254,25 @@ bool APPClient_WEBRTC_Dlts(XSOCKET hSocket)
249254

250255
XBYTE byKEYBuffer[128] = {};
251256
XClient_OPenSsl_GetKeyEx(xhSsl, byKEYBuffer);
257+
for (int i = 0; i < 60; i++)
258+
{
259+
printf("0x%02X, ", byKEYBuffer[i]);
260+
}
261+
printf("\n");
262+
263+
XCHAR tszRTPFile[MAX_PATH] = {};
264+
XCHAR tsz264File[MAX_PATH] = {};
265+
XCHAR tszFSizeFile[MAX_PATH] = {};
266+
267+
_xstprintf(tsz264File, _X("D:\\XEngine_StreamMedia\\XEngine_APPClient\\Debug\\recv.h264"));
268+
_xstprintf(tszRTPFile, _X("D:\\XEngine_StreamMedia\\XEngine_APPClient\\Debug\\recv.rtp"));
269+
_xstprintf(tszFSizeFile, _X("D:\\XEngine_StreamMedia\\XEngine_APPClient\\Debug\\1.txt"));
270+
FILE* pSt_264File = _xtfopen(tsz264File, "wb");
271+
FILE* pSt_RTPFile = _xtfopen(tszRTPFile, "wb");
272+
FILE* pSt_FSFile = _xtfopen(tszFSizeFile, "wb");
273+
274+
bool bKEYFrame = false;
275+
XCHAR* ptszRTPBuffer = (XCHAR*)malloc(XENGINE_MEMORY_SIZE_MAX);
252276
APPClient_WEBRTC_SRTPCreate(byKEYBuffer, &st_SRTPInfo);
253277
while (true)
254278
{
@@ -264,37 +288,77 @@ bool APPClient_WEBRTC_Dlts(XSOCKET hSocket)
264288
{
265289
printf("stun protocol recved\n");
266290
}
267-
else if ((XBYTE)tszMSGBuffer[0] == 0x80)
291+
else
268292
{
269293
int nRet = srtp_unprotect(st_SRTPInfo.st_SRTPRecvCtx, tszMSGBuffer, &nMSGLen);
270294
if (srtp_err_status_ok == nRet)
271295
{
272-
printf("srtp protcol recved unprotocol ok\n");
296+
//char tszFSizeBuffer[64] = {};
297+
//int nFSize = sprintf(tszFSizeBuffer, "%d\r\n", nMSGLen);
298+
//fwrite(tszFSizeBuffer, 1, nFSize, pSt_FSFile);
299+
//fwrite(tszMSGBuffer, 1, nMSGLen, pSt_RTPFile);
300+
301+
RTPProtocol_Parse_Send(lpszRTPClient, tszMSGBuffer, nMSGLen);
302+
while (true)
303+
{
304+
int nRTPLen = XENGINE_MEMORY_SIZE_MAX;
305+
STREAMMEDIA_RTPPROTOCOL_HDR st_RTPHdr = {};
306+
if (!RTPProtocol_Parse_Recv(lpszRTPClient, ptszRTPBuffer, &nRTPLen, &st_RTPHdr))
307+
{
308+
break;
309+
}
310+
if (106 == st_RTPHdr.enPayload)
311+
{
312+
313+
}
314+
315+
if (!bKEYFrame)
316+
{
317+
XENGINE_AVCODEC_VIDEOFRAMETYPE enFrameType;
318+
AVHelp_Parse_NaluType(ptszRTPBuffer, ENUM_XENGINE_AVCODEC_VIDEO_TYPE_H264, &enFrameType);
319+
if ((ENUM_XENGINE_AVCODEC_VIDEO_FRAMETYPE_SPS == enFrameType) || (ENUM_XENGINE_AVCODEC_VIDEO_FRAMETYPE_PPS == enFrameType))
320+
{
321+
bKEYFrame = true;
322+
printf("ok\n");
323+
}
324+
}
325+
326+
if (bKEYFrame)
327+
{
328+
fwrite(ptszRTPBuffer, 1, nRTPLen, pSt_264File);
329+
}
330+
}
273331
}
274332
else
275333
{
276-
printf("srtp protcol recved unprotocol failed\n");
334+
printf("srtp protcol recved unprotocol failed:%d\n", nRet);
277335
}
278336
}
279-
else
280-
{
281-
printf("unknow protocol recved\n");
282-
}
337+
}
338+
else
339+
{
340+
printf("Recv error:%lX\n", XClient_GetLastError());
283341
}
284342
}
343+
free(ptszRTPBuffer);
344+
ptszRTPBuffer = NULL;
345+
fclose(pSt_264File);
346+
XClient_OPenSsl_CloseEx(xhSsl);
347+
RTPProtocol_Parse_Destory();
285348
return true;
286349
}
287350

288351
int main()
289352
{
353+
srtp_init();
290354
int nMSGLen = 0;
291355
XSOCKET hSocket;
292356
XCHAR tszMSGBuffer[2048] = {};
293-
//LPCXSTR lpszAPIUrl = _X("http://10.0.1.88:5600/rtc/v1/whep/?app=live&stream=livestream.flv");
294-
LPCXSTR lpszAPIUrl = _X("http://app.xyry.org:1985/rtc/v1/whep/?app=live&stream=livestream");
357+
//LPCXSTR lpszAPIUrl = _X("http://127.0.0.1:5600/rtc/v1/whep/?app=live&stream=livestream");
358+
//LPCXSTR lpszAPIUrl = _X("http://app.xyry.org:1985/rtc/v1/whep/?app=live&stream=livestream");
359+
LPCXSTR lpszAPIUrl = _X("http://10.0.3.154:1985/rtc/v1/whep/?app=live&stream=livestream");
295360
LPCXSTR lpszFileCert = _X("");
296361

297-
srtp_init();
298362
APPClient_WEBRTC_SDPPacket(lpszAPIUrl, lpszFileCert, tszMSGBuffer, &nMSGLen);
299363

300364
int nHTTPCode = 0;
@@ -310,7 +374,9 @@ int main()
310374
BaseLib_OperatorMemory_FreeCStyle((XPPMEM)&ptszMSGBuffer);
311375

312376
XClient_UDPSelect_Create(&hSocket);
313-
XClient_UDPSelect_Connect(hSocket, "43.139.170.67", 8000);
377+
XClient_UDPSelect_Connect(hSocket, "10.0.3.154", 8000);
378+
//XClient_UDPSelect_Connect(hSocket, "43.139.170.67", 8000);
379+
//XClient_UDPSelect_Connect(hSocket, "127.0.0.1", 5604);
314380

315381
std::thread pSTDThread_Stun(APPClient_WEBRTC_StunSend, hSocket, tszICEUser, tszICEPass);
316382
pSTDThread_Stun.detach();

0 commit comments

Comments
 (0)