Skip to content

Commit 99e0334

Browse files
committed
modify:srtp unprotocol support for webrtc example
1 parent ac62849 commit 99e0334

File tree

1 file changed

+93
-7
lines changed

1 file changed

+93
-7
lines changed

XEngine_APPClient/APPClient_WebRTC/APPClient_WebRTC.cpp

Lines changed: 93 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <list>
1616
#include <thread>
1717
#include <memory>
18+
#include <srtp2/srtp.h>
1819
#include <XEngine_Include/XEngine_CommHdr.h>
1920
#include <XEngine_Include/XEngine_Types.h>
2021
#include <XEngine_Include/XEngine_ProtocolHdr.h>
@@ -33,6 +34,12 @@
3334
#include "../../XEngine_Source/XEngine_UserProtocol.h"
3435
using namespace std;
3536

37+
typedef struct
38+
{
39+
srtp_t st_SRTPSendCtx;
40+
srtp_t st_SRTPRecvCtx;
41+
}SRTPCORE_CLIENTINFO;
42+
3643
bool APPClient_WEBRTC_SDPPacket(LPCXSTR lpszAPIUrl, LPCXSTR lpszFileCert, XCHAR* ptszSDPPacket, int* pInt_SDPLen)
3744
{
3845
XNETHANDLE xhToken = 0;
@@ -169,6 +176,61 @@ bool APPClient_WEBRTC_StunSend(XSOCKET hSocket, LPCXSTR lpszICEUser, LPCXSTR lps
169176

170177
return true;
171178
}
179+
180+
bool PullStream_ClientProtocol_Dtls(LPCXSTR lpszMSGBuffer, int nMSGLen)
181+
{
182+
// DTLS有可能以多种不同的记录层类型开头,这里检查它是否是handshake(0x16)
183+
return ((nMSGLen >= 13) && (lpszMSGBuffer[0] == 0x16));
184+
}
185+
bool PullStream_ClientProtocol_Stun(LPCXSTR lpszMSGBuffer, int nMSGLen)
186+
{
187+
// STUN消息的类型字段(前两位为00)以及魔术cookie字段
188+
return (nMSGLen >= 20) && ((lpszMSGBuffer[0] & 0xC0) == 0x00) && (lpszMSGBuffer[4] == 0x21) && (lpszMSGBuffer[5] == 0x12) && ((XBYTE)lpszMSGBuffer[6] == 0xA4) && (lpszMSGBuffer[7] == 0x42);
189+
}
190+
bool APPClient_WEBRTC_SRTPCreate(LPCXBTR lpszKEYBuffer, SRTPCORE_CLIENTINFO* pSt_SRTPCore)
191+
{
192+
int nPos = 0;
193+
const int SRTP_MASTER_KEY_KEY_LEN = 16;
194+
const int SRTP_MASTER_KEY_SALT_LEN = 14;
195+
srtp_policy_t st_SRTPPolicy = {};
196+
197+
std::string m_StrClientKey(reinterpret_cast<LPCXSTR>(lpszKEYBuffer), SRTP_MASTER_KEY_KEY_LEN);
198+
nPos += SRTP_MASTER_KEY_KEY_LEN;
199+
std::string m_StrServerKey(reinterpret_cast<LPCXSTR>(lpszKEYBuffer + nPos), SRTP_MASTER_KEY_KEY_LEN);
200+
nPos += SRTP_MASTER_KEY_KEY_LEN;
201+
std::string m_StrClientSalt(reinterpret_cast<LPCXSTR>(lpszKEYBuffer + nPos), SRTP_MASTER_KEY_SALT_LEN);
202+
nPos += SRTP_MASTER_KEY_SALT_LEN;
203+
std::string m_StrServerSalt(reinterpret_cast<LPCXSTR>(lpszKEYBuffer + nPos), SRTP_MASTER_KEY_SALT_LEN);
204+
205+
std::string m_ClientKey = m_StrClientKey + m_StrClientSalt;
206+
std::string m_ServerKey = m_StrServerKey + m_StrServerSalt;
207+
208+
srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(&st_SRTPPolicy.rtp);
209+
srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(&st_SRTPPolicy.rtcp);
210+
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+
//初始化接受上下文
217+
st_SRTPPolicy.ssrc.type = ssrc_any_inbound;
218+
st_SRTPPolicy.key = (unsigned char*)m_ServerKey.c_str();
219+
220+
srtp_err_status_t nRet = srtp_err_status_ok;
221+
if (srtp_err_status_ok != (nRet = srtp_create(&pSt_SRTPCore->st_SRTPRecvCtx, &st_SRTPPolicy)))
222+
{
223+
return false;
224+
}
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+
}
232+
return true;
233+
}
172234
bool APPClient_WEBRTC_Dlts(XSOCKET hSocket)
173235
{
174236
LPCXSTR lpszCertFile = _X("D:\\XEngine_StreamMedia\\XEngine_APPClient\\Debug\\certificate.crt");
@@ -181,35 +243,58 @@ bool APPClient_WEBRTC_Dlts(XSOCKET hSocket)
181243
}
182244
XClient_OPenSsl_ConfigEx(xhSsl);
183245

184-
XCLIENT_SSLCERT_SRVINFO st_SslInfo;
246+
XCLIENT_SSLCERT_SRVINFO st_SslInfo = {};
247+
SRTPCORE_CLIENTINFO st_SRTPInfo = {};
185248
XClient_OPenSsl_ConnectEx(xhSsl, hSocket, &st_SslInfo);
186249

187250
XBYTE byKEYBuffer[128] = {};
188251
XClient_OPenSsl_GetKeyEx(xhSsl, byKEYBuffer);
252+
APPClient_WEBRTC_SRTPCreate(byKEYBuffer, &st_SRTPInfo);
189253
while (true)
190254
{
191255
int nMSGLen = 2048;
192256
XCHAR tszMSGBuffer[2048] = {};
193257
if (XClient_UDPSelect_RecvMsg(hSocket, tszMSGBuffer, &nMSGLen))
194258
{
195-
int nRVLen = 0;
196-
XCHAR* ptszMSGBuffer = NULL;
197-
XClient_OPenSsl_RecvMemoryEx(xhSsl, &ptszMSGBuffer, &nRVLen, tszMSGBuffer, nMSGLen);
198-
printf("%d\n", nRVLen);
259+
if (PullStream_ClientProtocol_Dtls(tszMSGBuffer, nMSGLen))
260+
{
261+
printf("dtls protcol recved\n");
262+
}
263+
else if (PullStream_ClientProtocol_Stun(tszMSGBuffer, nMSGLen))
264+
{
265+
printf("stun protocol recved\n");
266+
}
267+
else if ((XBYTE)tszMSGBuffer[0] == 0x80)
268+
{
269+
int nRet = srtp_unprotect(st_SRTPInfo.st_SRTPRecvCtx, tszMSGBuffer, &nMSGLen);
270+
if (srtp_err_status_ok == nRet)
271+
{
272+
printf("srtp protcol recved unprotocol ok\n");
273+
}
274+
else
275+
{
276+
printf("srtp protcol recved unprotocol failed\n");
277+
}
278+
}
279+
else
280+
{
281+
printf("unknow protocol recved\n");
282+
}
199283
}
200284
}
201285
return true;
202286
}
203287

204288
int main()
205289
{
290+
int nMSGLen = 0;
206291
XSOCKET hSocket;
292+
XCHAR tszMSGBuffer[2048] = {};
207293
//LPCXSTR lpszAPIUrl = _X("http://10.0.1.88:5600/rtc/v1/whep/?app=live&stream=livestream.flv");
208294
LPCXSTR lpszAPIUrl = _X("http://app.xyry.org:1985/rtc/v1/whep/?app=live&stream=livestream");
209295
LPCXSTR lpszFileCert = _X("");
210-
int nMSGLen = 0;
211-
XCHAR tszMSGBuffer[2048] = {};
212296

297+
srtp_init();
213298
APPClient_WEBRTC_SDPPacket(lpszAPIUrl, lpszFileCert, tszMSGBuffer, &nMSGLen);
214299

215300
int nHTTPCode = 0;
@@ -238,5 +323,6 @@ int main()
238323
std::this_thread::sleep_for(std::chrono::seconds(1));
239324
}
240325
XClient_UDPSelect_Close(hSocket);
326+
srtp_shutdown();
241327
return 0;
242328
}

0 commit comments

Comments
 (0)