rtpsession.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. #include "precompile.h"
  2. #include "toolkit.h"
  3. #include "rtpsession.h"
  4. #include "memutil.h"
  5. #include <winpr/winsock.h>
  6. #ifndef _WIN32
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <arpa/inet.h>
  10. #endif //NOT _WIN32
  11. #define MAX_RTCP_BUF 1500
  12. #define TAG TOOLKIT_TAG("rtpsession")
  13. struct rtp_session_t
  14. {
  15. int rtp_fd;
  16. int rtcp_fd;
  17. struct sockaddr_in local_rtp_addr;
  18. struct sockaddr_in local_rtcp_addr;
  19. struct sockaddr_in remote_rtp_addr;
  20. struct sockaddr_in remote_rtcp_addr;
  21. unsigned flags;
  22. rtp_state *rtpstat;
  23. };
  24. static __inline int bind_udp_socket_port(int sock,
  25. unsigned long local_ip,
  26. unsigned short port)
  27. {
  28. struct sockaddr_in addr = {0};
  29. addr.sin_family = AF_INET;
  30. addr.sin_port = port;
  31. addr.sin_addr.s_addr = local_ip;
  32. return _bind(sock, (struct sockaddr*)&addr, sizeof(addr));
  33. }
  34. static __inline int bind_rtp_socket_pair(int rtp_sock,
  35. int rtcp_sock,
  36. unsigned long local_ip,
  37. unsigned short rtp_port,
  38. unsigned short rtcp_port)
  39. {
  40. if (bind_udp_socket_port(rtp_sock, local_ip, rtp_port) == 0 &&
  41. bind_udp_socket_port(rtcp_sock, local_ip, rtcp_port) == 0) {
  42. return 0;
  43. }
  44. return TOOLKIT_UNKNOWN;
  45. }
  46. static __inline int is_addr_equal(const struct sockaddr_in *addr1, const struct sockaddr_in *addr2)
  47. {
  48. return addr1->sin_addr.s_addr == addr2->sin_addr.s_addr &&
  49. addr1->sin_port == addr2->sin_port;
  50. }
  51. #ifndef SIO_UDP_CONNRESET
  52. #define SIO_UDP_CONNRESET _WSAIOW(IOC_VENDOR,12)
  53. #endif
  54. /*TODO: remove it and use sockutil one*/
  55. static __inline int enable_udp_connreset(int sock, BOOL bOn)
  56. {
  57. #if defined(_MSC_VER)
  58. DWORD dwBytesReturned = 0;
  59. BOOL bNewBehavior = bOn;
  60. return WSAIoctl(sock, SIO_UDP_CONNRESET, &bNewBehavior, sizeof(bNewBehavior),
  61. NULL, 0, &dwBytesReturned, NULL, NULL);
  62. #else
  63. return 0;
  64. #endif //_MSC_VER
  65. }
  66. static int udp_block_send(int sock,
  67. const struct sockaddr*addr,
  68. int addrlen,
  69. const char *buf,
  70. int n)
  71. {
  72. int ret;
  73. WSABUF wsabuf[1];
  74. DWORD BytesSent = 0;
  75. wsabuf[0].buf = (char*)buf;
  76. wsabuf[0].len = n;
  77. ret = WSASendTo(sock, &wsabuf[0], 1, &BytesSent, 0, addr, addrlen, NULL, NULL);
  78. if (ret == 0) {
  79. return 0;
  80. } else {
  81. if (WSAGetLastError() == WSAEWOULDBLOCK) {
  82. fd_set fs;
  83. FD_ZERO(&fs);
  84. FD_SET(sock, &fs);
  85. #if defined(_MSC_VER)
  86. return select(1, NULL, &fs, NULL, NULL);
  87. #else
  88. return _select(1 + sock, NULL, &fs, NULL, NULL);
  89. #endif //_MSC_VER
  90. }
  91. }
  92. return -1;
  93. }
  94. static int udp_block_send2(int sock,
  95. const struct sockaddr*addr,
  96. int addrlen,
  97. const char *buf0,
  98. int n0,
  99. const char *buf1,
  100. int n1)
  101. {
  102. int ret;
  103. WSABUF wsabuf[2];
  104. DWORD BytesSent;
  105. wsabuf[0].buf = (char*)(buf0);
  106. wsabuf[0].len = n0;
  107. wsabuf[1].buf = (char*)(buf1);
  108. wsabuf[1].len = n1;
  109. ret = WSASendTo(sock, &wsabuf[0], 2, &BytesSent, 0, addr, addrlen, NULL, NULL);
  110. if (ret == 0) {
  111. return 0;
  112. } else {
  113. if (WSAGetLastError() == WSAEWOULDBLOCK) {
  114. fd_set fs;
  115. FD_ZERO(&fs);
  116. FD_SET(sock, &fs);
  117. #if defined(_MSC_VER)
  118. return select(1, NULL, &fs, NULL, NULL);
  119. #else
  120. return _select(sock + 1, NULL, &fs, NULL, NULL);
  121. #endif //_MSC_VER
  122. }
  123. }
  124. return -1;
  125. }
  126. static int udp_poll_recv(int sock, struct sockaddr*addr, int *addrlen, char *buf, int n)
  127. {
  128. int ret;
  129. WSABUF wsabuf[1];
  130. DWORD dwBytesRecv = 0;
  131. DWORD dwFlags = 0;
  132. wsabuf[0].buf = buf;
  133. wsabuf[0].len = n;
  134. ret = WSARecvFrom(sock, &wsabuf[0], 1, &dwBytesRecv, &dwFlags, addr, addrlen, NULL, NULL);
  135. return ret == 0 ? (int)dwBytesRecv : -1;
  136. }
  137. static int udp_poll_recv2(int sock,
  138. struct sockaddr*addr,
  139. int *addrlen,
  140. char *buf0,
  141. int n0,
  142. char *buf1,
  143. int n1)
  144. {
  145. int ret;
  146. WSABUF wsabuf[2];
  147. DWORD dwBytesRecv = 0;
  148. DWORD dwFlags = 0;
  149. wsabuf[0].buf = buf0;
  150. wsabuf[0].len = n0;
  151. wsabuf[1].buf = buf1;
  152. wsabuf[1].len = n1;
  153. ret = WSARecvFrom(sock, &wsabuf[0], 2, &dwBytesRecv, &dwFlags, addr, addrlen, NULL, NULL);
  154. return ret == 0 ? (int)dwBytesRecv : -1;
  155. }
  156. static int rtp_session_send_rtcp_fb_tmmbn(rtp_session_t *sess, unsigned int ssrc)
  157. {
  158. if (sess && !(sess->flags & RTP_SESSION_FLAG_NO_RTCP)) {
  159. char tmp[MAX_RTCP_BUF];
  160. int n;
  161. n = rtp_state_rtcp_make_rtcp_fb_tmmbn(sess->rtpstat, tmp, sizeof(tmp), ssrc);
  162. if (n > 0) {
  163. udp_block_send(sess->rtcp_fd, (struct sockaddr*)&sess->remote_rtcp_addr,
  164. sizeof(struct sockaddr_in), tmp, n);
  165. }
  166. return 0;
  167. }
  168. return TOOLKIT_EINVAL;
  169. }
  170. //notice,this function must be called after rtp_state_on_recv_rtcp
  171. //because rtp_state_on_recv_rtcp will update tmmbr_info.received before we send tmmbn.
  172. static void handle_rtcp_feedback_packet(rtp_session_t *sess, const char *buf, unsigned *flags) {
  173. rtcp_fb_header_t *fbh;
  174. rtcp_common_t *ch = (rtcp_common_t *)&buf[0];
  175. //we send tmmbn
  176. switch ((rtcp_rtpfb_type_t)ch->count) {
  177. case RTCP_RTPFB_TMMBR:
  178. fbh = (rtcp_fb_header_t *)(buf + sizeof(rtcp_common_t));
  179. rtp_session_send_rtcp_fb_tmmbn(sess, ntohl(fbh->packet_sender_ssrc));
  180. *flags |= RTP_SESSION_HAS_RTPFB_TMMBR;
  181. break;
  182. default:
  183. break;
  184. }
  185. }
  186. static void handle_rtcp_sr_packet(rtp_session_t *sess, const char *buf) {
  187. //RTP_SESSION_FLAG_RECVONLY, we send RR
  188. if (!(sess->flags & RTP_SESSION_FLAG_SENDONLY)) {
  189. char tmp[MAX_RTCP_BUF];
  190. int length = rtp_state_rtcp_make_rr(sess->rtpstat, tmp, sizeof(tmp));
  191. if (length > 0) {
  192. udp_block_send(sess->rtcp_fd, (struct sockaddr*)&sess->remote_rtcp_addr,
  193. sizeof(struct sockaddr_in), tmp, length);
  194. }
  195. }
  196. }
  197. static void handle_rtcp_packet(rtp_session_t *sess, const char *buf, int len) {
  198. int n = 0;
  199. while( n < len) {
  200. const rtcp_common_t *common = (rtcp_common_t *)((const char*)buf + n);
  201. if (common->version != 2)
  202. return ;
  203. n += sizeof(rtcp_common_t);
  204. switch (common->pt) {
  205. case RTCP_SR:
  206. handle_rtcp_sr_packet(sess, (char *)common);
  207. break;
  208. default:
  209. break;
  210. }
  211. n += ntohs(common->length) * 4;
  212. }
  213. }
  214. static int recv_rtcp_raw(rtp_session_t *sess, char *buf, int n)
  215. {
  216. int rc;
  217. struct sockaddr_in addr;
  218. int addrlen = sizeof(addr);
  219. if (!sess || !buf || n <= 0)
  220. return TOOLKIT_EINVAL;
  221. /* try recv rtcp packet */
  222. memset(&addr, 0, sizeof(struct sockaddr_in));
  223. rc = udp_poll_recv(sess->rtcp_fd, (struct sockaddr*)&addr, &addrlen, buf, n);
  224. if (rc > 0) {
  225. if (is_addr_equal(&sess->remote_rtcp_addr, &addr)) {
  226. handle_rtcp_packet(sess, buf, n);
  227. return rc;
  228. }
  229. }
  230. return -1;
  231. }
  232. static int recv_rtcp(rtp_session_t *sess, unsigned *flags)
  233. {
  234. int ret;
  235. char tmp[MAX_RTCP_BUF];
  236. if (flags)
  237. *flags = 0;
  238. ret = recv_rtcp_raw(sess, tmp, sizeof(tmp));
  239. if (ret > 0 && rtp_state_on_recv_rtcp(sess->rtpstat, tmp, ret) == 0) {
  240. unsigned f = 0;
  241. int left = ret;
  242. while (left > 0) {
  243. rtcp_common_t *common = (rtcp_common_t*)&tmp[ret - left];
  244. switch (common->pt) {
  245. case RTCP_SDES:
  246. f |= RTP_SESSION_HAS_SDES;
  247. break;
  248. case RTCP_SR:
  249. f |= RTP_SESSION_HAS_SR;
  250. break;
  251. case RTCP_RR:
  252. f |= RTP_SESSION_HAS_RR;
  253. break;
  254. case RTCP_BYE:
  255. f |= RTP_SESSION_HAS_BYE;
  256. break;
  257. case RTCP_APP:
  258. f |= RTP_SESSION_HAS_APP;
  259. break;
  260. case RTCP_FIR:
  261. f |= RTP_SESSION_HAS_FIR;
  262. break;
  263. case RTCP_NACK:
  264. f |= RTP_SESSION_HAS_NACK;
  265. break;
  266. case RTCP_RTPFB:
  267. handle_rtcp_feedback_packet(sess, (char *)common, &f);
  268. break;
  269. default:
  270. break;
  271. }
  272. left -= (ntohs(common->length)+1)*4;
  273. }
  274. if (flags)
  275. *flags = f;
  276. return 0;
  277. }
  278. return -1;
  279. }
  280. TOOLKIT_API int rtp_session_create(const char *ip,
  281. unsigned short start_port,
  282. unsigned short num_port,
  283. rtp_session_t **pp_s)
  284. {
  285. return rtp_session_create2(inet_addr(ip), start_port, num_port, pp_s);
  286. }
  287. TOOLKIT_API int rtp_session_create2(unsigned long local_ip,
  288. unsigned short start_port,
  289. unsigned short num_port,
  290. rtp_session_t **pp_s)
  291. {
  292. rtp_session_t *sess;
  293. unsigned short rtp_port, end_port;
  294. int rtp_fd = INVALID_SOCKET;
  295. int rtcp_fd = INVALID_SOCKET;
  296. if ((start_port & 1) || num_port < 2) {
  297. return TOOLKIT_EINVAL;
  298. }
  299. end_port = start_port + num_port;
  300. for (rtp_port = start_port; rtp_port < end_port; rtp_port+=2)
  301. {
  302. rtp_fd = _socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  303. if (rtp_fd == INVALID_SOCKET) {
  304. return toolkit_translate_sys_error(errno);
  305. }
  306. rtcp_fd = _socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  307. if (rtcp_fd == INVALID_SOCKET) {
  308. const int err = errno;
  309. closesocket(rtp_fd);
  310. return toolkit_translate_sys_error(err);
  311. }
  312. /** 联系下面逻辑,这里一定要执行成功,否则将直接报错*/
  313. if (bind_rtp_socket_pair(rtp_fd, rtcp_fd, local_ip, htons(rtp_port), htons(rtp_port+1)) == 0) {
  314. u_long mode = 1;
  315. _ioctlsocket(rtp_fd, FIONBIO, &mode);
  316. _ioctlsocket(rtcp_fd, FIONBIO, &mode);
  317. enable_udp_connreset(rtp_fd, FALSE);
  318. enable_udp_connreset(rtcp_fd, FALSE);
  319. break;
  320. }
  321. if (rtp_fd != INVALID_SOCKET) {
  322. closesocket(rtp_fd);
  323. }
  324. if (rtcp_fd != INVALID_SOCKET) {
  325. closesocket(rtcp_fd);
  326. }
  327. }
  328. if (rtp_port == end_port) {
  329. return TOOLKIT_EINVAL;
  330. }
  331. sess = malloc(sizeof(struct rtp_session_t));
  332. sess->local_rtp_addr.sin_family = AF_INET;
  333. sess->local_rtp_addr.sin_port = _htons(rtp_port);
  334. sess->local_rtp_addr.sin_addr.s_addr = local_ip;
  335. sess->local_rtcp_addr.sin_family = AF_INET;
  336. sess->local_rtcp_addr.sin_port = _htons(rtp_port + 1);
  337. sess->local_rtcp_addr.sin_addr.s_addr = local_ip;
  338. sess->rtp_fd = rtp_fd;
  339. sess->rtcp_fd = rtcp_fd;
  340. sess->rtpstat = rtp_state_create(-1);
  341. *pp_s = sess;
  342. return 0;
  343. }
  344. TOOLKIT_API int rtp_session_destroy(rtp_session_t *sess)
  345. {
  346. if (!sess)
  347. return TOOLKIT_EINVAL;
  348. if (sess->rtpstat)
  349. rtp_state_destroy(sess->rtpstat);
  350. if (sess->rtp_fd != INVALID_SOCKET)
  351. closesocket(sess->rtp_fd);
  352. if (sess->rtcp_fd != INVALID_SOCKET)
  353. closesocket(sess->rtcp_fd);
  354. free(sess);
  355. return 0;
  356. }
  357. TOOLKIT_API int rtp_session_reset(rtp_session_t *sess,
  358. unsigned flags, // 0:none 1:tx 2:rx 3:both
  359. /* remote address , network byte order, NULL for wait for peer to connect in, in NAT traverse case */
  360. const char *remote_ip,
  361. unsigned short remote_rtp_port,
  362. unsigned short remote_rtcp_port) // if rtcp port == 0, no rtcp
  363. {
  364. return rtp_session_reset2(sess, flags, inet_addr(remote_ip), remote_rtp_port, remote_rtcp_port);
  365. }
  366. TOOLKIT_API int rtp_session_reset2(rtp_session_t *sess,
  367. unsigned flags,
  368. unsigned long iremote_ip, /* remote address , network byte order */
  369. unsigned short remote_rtp_port,
  370. unsigned short remote_rtcp_port)
  371. {
  372. int rc = 1;
  373. char tmp[MAX_RTCP_BUF];
  374. if (!sess || !sess->rtpstat) {
  375. return TOOLKIT_EINVAL;
  376. }
  377. if (!iremote_ip) {
  378. remote_rtp_port = 0;
  379. remote_rtcp_port = 0;
  380. }
  381. sess->remote_rtp_addr.sin_family = AF_INET;
  382. sess->remote_rtp_addr.sin_port = _htons(remote_rtp_port);
  383. sess->remote_rtp_addr.sin_addr.s_addr = iremote_ip;
  384. sess->remote_rtcp_addr.sin_family = AF_INET;
  385. sess->remote_rtcp_addr.sin_port = _htons(remote_rtcp_port);
  386. sess->remote_rtcp_addr.sin_addr.s_addr = iremote_ip;
  387. sess->flags = flags;
  388. rtp_state_reset(sess->rtpstat, GetTickCount(), 0, 0);
  389. rtp_state_set_rtcp_sdes_string(sess->rtpstat, RTCP_SDES_CNAME, "vvvrtp_statevvv");
  390. /* clear all kernel buffer */
  391. do {
  392. struct sockaddr_in addr;
  393. int addrlen = sizeof(addr);
  394. memset(&addr, 0, sizeof(struct sockaddr_in));
  395. rc = udp_poll_recv(sess->rtp_fd, (struct sockaddr*)&addr, &addrlen, tmp, sizeof(tmp));
  396. } while (rc > 0);
  397. do {
  398. struct sockaddr_in addr;
  399. int addrlen = sizeof(addr);
  400. memset(&addr, 0, sizeof(struct sockaddr_in));
  401. rc = udp_poll_recv(sess->rtcp_fd, (struct sockaddr*)&addr, &addrlen, tmp, sizeof(tmp));
  402. } while (rc > 0);
  403. return 0;
  404. }
  405. TOOLKIT_API int rtp_session_send(rtp_session_t *sess,
  406. unsigned *rtcp_rx_flags,
  407. unsigned pt,
  408. unsigned mark,
  409. unsigned delta_ts,
  410. const char *buf,
  411. int n)
  412. {
  413. return rtp_session_send_hook(sess, rtcp_rx_flags, pt, mark, delta_ts, buf, n, 0, 0);
  414. }
  415. TOOLKIT_API int rtp_session_send_hook(rtp_session_t *sess,
  416. unsigned *rtcp_rx_flags,
  417. unsigned pt,
  418. unsigned mark,
  419. unsigned delta_ts,
  420. const char *buf,
  421. int n,
  422. void (*on_tx_hook)(const char *buf, int size, void *arg), void *hook_arg)
  423. {
  424. rtp_hdr hdr;
  425. int ret;
  426. if (!sess || !buf || n <= 0) {
  427. return TOOLKIT_EINVAL;
  428. }
  429. rtp_state_fill_rtp(sess->rtpstat, &hdr, pt, mark, delta_ts);
  430. return rtp_session_send_hook2(sess, rtcp_rx_flags, &hdr, buf, n, on_tx_hook, hook_arg);
  431. }
  432. TOOLKIT_API int rtp_session_send_hook2(rtp_session_t *sess,
  433. unsigned *rtcp_rx_flags,
  434. const rtp_hdr *hdr,
  435. const char *buf,
  436. int n,
  437. void (*on_tx_hook)(const char *buf, int size, void *arg), void *hook_arg)
  438. {
  439. int ret;
  440. if (!sess || !buf || n <= 0) {
  441. return TOOLKIT_EINVAL;
  442. }
  443. if (rtcp_rx_flags)
  444. *rtcp_rx_flags = 0;
  445. if (!(sess->flags & RTP_SESSION_FLAG_NO_RTCP)) {
  446. //only in sendonly we process recv_rtcp in rtp_session_send_hook function.
  447. if (!(sess->flags & RTP_SESSION_FLAG_RECVONLY)) {
  448. if (sess->remote_rtcp_addr.sin_port != 0)
  449. recv_rtcp(sess, rtcp_rx_flags);
  450. }
  451. } else {
  452. WLog_DBG(TAG, "%s::%d", __FUNCTION__, __LINE__);
  453. }
  454. {
  455. static int ss_seq = 0;
  456. if (++ss_seq == 60) {
  457. ret = 0;
  458. } else {
  459. WLog_DBG(TAG, "to invoke udp_block_send2...");
  460. ret = udp_block_send2(sess->rtp_fd, (struct sockaddr*)&sess->remote_rtp_addr,
  461. sizeof(struct sockaddr_in), (char*)hdr, sizeof(*hdr), buf, n);
  462. WLog_DBG(TAG, "invoke udp_block_send2 returned %d", ret);
  463. }
  464. }
  465. if (on_tx_hook) {
  466. int size = sizeof(*hdr) + n;
  467. char *tbuf = (char*)malloc(size);
  468. memcpy(tbuf, hdr, sizeof(*hdr));
  469. memcpy(tbuf+sizeof(*hdr), buf, n);
  470. (*on_tx_hook)(tbuf, size, hook_arg);
  471. free(tbuf);
  472. }
  473. if (ret != 0) {
  474. WLog_DBG(TAG, "return %d before rtp_state_on_send_rtp");
  475. return ret;
  476. }
  477. rtp_state_on_send_rtp(sess->rtpstat, sizeof(*hdr)+n);
  478. if (!(sess->flags & RTP_SESSION_FLAG_NO_RTCP)) {
  479. if (rtp_state_need_send_rtcp(sess->rtpstat, TRUE) && sess->remote_rtcp_addr.sin_port != 0) {
  480. char tmp[MAX_RTCP_BUF];
  481. u__int64_t mxtbr;
  482. ret = rtp_state_rtcp_make_sr(sess->rtpstat, tmp, sizeof(tmp));
  483. udp_block_send(sess->rtcp_fd, (struct sockaddr*)&sess->remote_rtcp_addr,
  484. sizeof(struct sockaddr_in), tmp, ret);
  485. //retry send tmmbr
  486. if (rtp_state_get_tmmbr_wait_send_maxbitrate(sess->rtpstat, &mxtbr) == 0){
  487. rtp_session_send_rtcp_fb_tmmbr(sess, mxtbr);
  488. } else {
  489. WLog_DBG(TAG, "%s:: %d", __FUNCTION__, __LINE__);
  490. }
  491. } else {
  492. WLog_DBG(TAG, "%s:: %d", __FUNCTION__, __LINE__);
  493. }
  494. } else {
  495. WLog_DBG(TAG, "session flag %d not has RTP_SESSION_FLAG_NO_RTCP", sess->flags);
  496. }
  497. return 0;
  498. }
  499. TOOLKIT_API int rtp_session_send_raw(rtp_session_t *sess,
  500. unsigned *rtcp_rx_flags,
  501. const char *buf,
  502. int n)
  503. {
  504. int ret;
  505. if (!sess || !buf || n <= 0) {
  506. return TOOLKIT_EINVAL;
  507. }
  508. if (rtcp_rx_flags)
  509. *rtcp_rx_flags = 0;
  510. if (!(sess->flags & RTP_SESSION_FLAG_NO_RTCP)) {
  511. if (!(sess->flags & RTP_SESSION_FLAG_RECVONLY)) {
  512. if (sess->remote_rtcp_addr.sin_port != 0)
  513. recv_rtcp(sess, rtcp_rx_flags);
  514. }
  515. }
  516. ret = udp_block_send(sess->rtp_fd, (struct sockaddr*)&sess->remote_rtp_addr,
  517. sizeof(struct sockaddr_in), buf, n);
  518. if (ret != 0) {
  519. WLog_DBG(TAG, "%s:: %d", __FUNCTION__, __LINE__);
  520. return ret;
  521. }
  522. rtp_state_on_send_rtp(sess->rtpstat, n);
  523. if (!(sess->flags & RTP_SESSION_FLAG_NO_RTCP)) {
  524. if (rtp_state_need_send_rtcp(sess->rtpstat, TRUE) && sess->remote_rtcp_addr.sin_port != 0) {
  525. char tmp[MAX_RTCP_BUF];
  526. u__int64_t mxtbr;
  527. ret = rtp_state_rtcp_make_sr(sess->rtpstat, tmp, sizeof(tmp));
  528. udp_block_send(sess->rtcp_fd, (struct sockaddr*)&sess->remote_rtcp_addr,
  529. sizeof(struct sockaddr_in), tmp, ret);
  530. //retry send tmmbr
  531. if (rtp_state_get_tmmbr_wait_send_maxbitrate(sess->rtpstat, &mxtbr) == 0){
  532. rtp_session_send_rtcp_fb_tmmbr(sess, mxtbr);
  533. } else {
  534. WLog_DBG(TAG, "%s::%d", __FUNCTION__, __LINE__);
  535. }
  536. } else {
  537. WLog_DBG(TAG, "%s::%d", __FUNCTION__, __LINE__);
  538. }
  539. } else {
  540. WLog_DBG(TAG, "%s::%d", __FUNCTION__, __LINE__);
  541. }
  542. return 0;
  543. }
  544. TOOLKIT_API int rtp_session_send_rtcp_h261_fir(rtp_session_t *sess)
  545. {
  546. if (sess && !(sess->flags & RTP_SESSION_FLAG_NO_RTCP)) {
  547. char tmp[MAX_RTCP_BUF];
  548. int n;
  549. n = rtp_state_rtcp_make_h261_fir(sess->rtpstat, tmp, sizeof(tmp));
  550. udp_block_send(sess->rtcp_fd, (struct sockaddr*)&sess->remote_rtcp_addr,
  551. sizeof(struct sockaddr_in), tmp, n);
  552. } else {
  553. WLog_DBG(TAG, "%s::%d", __FUNCTION__, __LINE__);
  554. return -1;
  555. }
  556. return 0;
  557. }
  558. TOOLKIT_API int rtp_session_send_rtcp_fb_tmmbr(rtp_session_t *sess, u__int64_t mxtbr) {
  559. if (sess && !(sess->flags & RTP_SESSION_FLAG_NO_RTCP)) {
  560. char tmp[MAX_RTCP_BUF];
  561. int n;
  562. n = rtp_state_rtcp_make_rtcp_fb_tmmbr(sess->rtpstat, tmp, sizeof(tmp), mxtbr, IP_UDP_OVERHEAD);
  563. udp_block_send(sess->rtcp_fd, (struct sockaddr*)&sess->remote_rtcp_addr,
  564. sizeof(struct sockaddr_in), tmp, n);
  565. } else {
  566. WLog_DBG(TAG, "%s::%d", __FUNCTION__, __LINE__);
  567. return -1;
  568. }
  569. return 0;
  570. }
  571. TOOLKIT_API int rtp_session_recv_hook(rtp_session_t *sess,
  572. unsigned *pt,
  573. unsigned *mark,
  574. unsigned *ts,
  575. unsigned short *seq,
  576. char *buf,
  577. int n,
  578. void (*on_rx_hook)(const char *buf, int size, void *arg), void *hook_arg)
  579. {
  580. int result;
  581. rtp_hdr hdr;
  582. if (!sess || !buf || n <= 0) {
  583. WLog_DBG(TAG, "%s::%d", __FUNCTION__, __LINE__);
  584. return -1;
  585. }
  586. result = rtp_session_recv_hook2(sess, &hdr, buf, n, on_rx_hook, hook_arg);
  587. if (result > 0) {
  588. if (pt)
  589. *pt = hdr.pt;
  590. if (mark)
  591. *mark = hdr.m;
  592. if (ts)
  593. *ts = ntohl(hdr.ts);
  594. if (seq)
  595. *seq = ntohs(hdr.seq);
  596. } else {
  597. WLog_DBG(TAG, "%s::%d", __FUNCTION__, __LINE__);
  598. }
  599. return result;
  600. }
  601. TOOLKIT_API int rtp_session_recv_hook2(rtp_session_t *sess,
  602. rtp_hdr *hdr,
  603. char *buf,
  604. int n,
  605. void (*on_rx_hook)(const char *buf, int size, void *arg), void *hook_arg)
  606. {
  607. int ret;
  608. int max_cnt;
  609. int i;
  610. struct sockaddr_in addr = {0};
  611. int addrlen = sizeof(addr);
  612. if (!sess || !buf || n <= 0) {
  613. return TOOLKIT_EINVAL;
  614. }
  615. max_cnt = 5;
  616. for (i = 0 ; i < max_cnt; ++i) {
  617. memset(&addr, 0, sizeof(struct sockaddr_in));
  618. addrlen = sizeof(addr);
  619. ret = udp_poll_recv2(sess->rtp_fd, (struct sockaddr*)&addr, &addrlen, (char*)hdr, sizeof(*hdr), buf, n);
  620. if (ret > 0) {
  621. if (on_rx_hook) {
  622. if (ret <= sizeof(*hdr)) {
  623. (*on_rx_hook)((char*)hdr, ret, hook_arg);
  624. } else {
  625. char *tbuf = (char*)malloc(ret);
  626. memcpy(tbuf, (char*)hdr, sizeof(*hdr));
  627. memcpy(tbuf+sizeof(*hdr), buf, ret - sizeof(*hdr));
  628. (*on_rx_hook)(tbuf, ret, hook_arg);
  629. free(tbuf);
  630. }
  631. }
  632. if (is_addr_equal(&sess->remote_rtp_addr, &addr)) {
  633. if (rtp_state_on_recv_rtp(sess->rtpstat, hdr, ret)==0) {
  634. break;
  635. }
  636. }
  637. } else {
  638. WLog_DBG(TAG, "%s::%d", __FUNCTION__, __LINE__);
  639. return -1;
  640. }
  641. }
  642. if (i >= 5) {
  643. WLog_DBG(TAG, "%s::%d", __FUNCTION__, __LINE__);
  644. return -1;
  645. }
  646. return ret - sizeof(*hdr);
  647. }
  648. TOOLKIT_API int rtp_session_recv(rtp_session_t *sess,
  649. unsigned *pt,
  650. unsigned *mark,
  651. unsigned *ts,
  652. unsigned short *seq,
  653. char *buf,
  654. int n)
  655. {
  656. return rtp_session_recv_hook(sess, pt, mark, ts, seq, buf, n, 0, 0);
  657. }
  658. TOOLKIT_API int rtp_session_recv_raw(rtp_session_t *sess, char *buf, int n)
  659. {
  660. int ret;
  661. int max_cnt;
  662. int i;
  663. struct sockaddr_in addr;
  664. int addrlen = sizeof(addr);
  665. if (!sess || !buf || n <= 0) {
  666. return TOOLKIT_EINVAL;
  667. }
  668. max_cnt = 5;
  669. for (i = 0 ; i < max_cnt; ++i) {
  670. memset(&addr, 0, sizeof(struct sockaddr_in));
  671. addrlen = sizeof(addr);
  672. ret = udp_poll_recv(sess->rtp_fd, (struct sockaddr*)&addr, &addrlen, buf, n);
  673. if (ret > 0) {
  674. if (is_addr_equal(&sess->remote_rtp_addr, &addr)) {
  675. rtp_hdr *p_hdr = (rtp_hdr*)&buf[0];
  676. if (rtp_state_on_recv_rtp(sess->rtpstat, p_hdr, ret)==0) {
  677. break;
  678. }
  679. }
  680. }
  681. }
  682. if (i >= 5) {
  683. WLog_DBG(TAG, "%s::%d", __FUNCTION__, __LINE__);
  684. return -1;
  685. }
  686. return ret;
  687. }
  688. TOOLKIT_API int rtp_session_recv_rtcp(rtp_session_t *sess, unsigned *rtcp_rx_flags)
  689. {
  690. if (!sess || !rtcp_rx_flags) {
  691. return TOOLKIT_EINVAL;
  692. }
  693. *rtcp_rx_flags = 0;
  694. if (!(sess->flags & RTP_SESSION_FLAG_NO_RTCP)) {
  695. if (sess->remote_rtcp_addr.sin_port != 0) {
  696. recv_rtcp(sess, rtcp_rx_flags);
  697. }
  698. } else {
  699. WLog_DBG(TAG, "%s::%d", __FUNCTION__, __LINE__);
  700. }
  701. return 0;
  702. }
  703. TOOLKIT_API int rtp_session_advance_timestamp(rtp_session_t *sess, unsigned int delta_ts)
  704. {
  705. if (!sess) {
  706. return TOOLKIT_EINVAL;
  707. }
  708. return rtp_state_advance_timestamp(sess->rtpstat, delta_ts);
  709. }
  710. TOOLKIT_API int rtp_session_get_rtcp_stat(rtp_session_t *sess, rtcp_statistics *stat)
  711. {
  712. if (!sess || !stat) {
  713. return TOOLKIT_EINVAL;
  714. }
  715. return rtp_state_get_stat(sess->rtpstat, stat);
  716. }
  717. TOOLKIT_API rtp_state *rtp_session_get_rtp_state(rtp_session_t *sess)
  718. {
  719. if (!sess) {
  720. return NULL;
  721. }
  722. return sess->rtpstat;
  723. }
  724. TOOLKIT_API int rtp_session_get_local_ip(rtp_session_t *sess, unsigned long *p_local_ip)
  725. {
  726. if (!sess || !p_local_ip) {
  727. return TOOLKIT_EINVAL;
  728. }
  729. *p_local_ip = sess->local_rtp_addr.sin_addr.s_addr;
  730. return 0;
  731. }
  732. TOOLKIT_API int rtp_session_get_local_rtp_port(rtp_session_t *sess, unsigned short *p_rtp)
  733. {
  734. if (!sess || !p_rtp) {
  735. return TOOLKIT_EINVAL;
  736. }
  737. *p_rtp = ntohs(sess->local_rtp_addr.sin_port);
  738. return 0;
  739. }
  740. TOOLKIT_API int rtp_session_get_local_rtcp_port(rtp_session_t *sess, unsigned short *p_rtcp)
  741. {
  742. if (!sess || !p_rtcp) {
  743. return TOOLKIT_EINVAL;
  744. }
  745. *p_rtcp = ntohs(sess->local_rtcp_addr.sin_port);
  746. return 0;
  747. }
  748. TOOLKIT_API int rtp_session_get_remote_ip(rtp_session_t *sess, unsigned long *p_remote_ip)
  749. {
  750. if (!sess || !p_remote_ip) {
  751. return TOOLKIT_EINVAL;
  752. }
  753. *p_remote_ip = sess->remote_rtp_addr.sin_addr.s_addr;
  754. return 0;
  755. }
  756. TOOLKIT_API int rtp_session_get_remote_rtp_port(rtp_session_t *sess, unsigned short *p_rtp)
  757. {
  758. if (!sess || !p_rtp) {
  759. return TOOLKIT_EINVAL;
  760. }
  761. *p_rtp = ntohs(sess->remote_rtp_addr.sin_port);
  762. return 0;
  763. }
  764. TOOLKIT_API int rtp_session_get_remote_rtcp_port(rtp_session_t *sess, unsigned short *p_rtcp)
  765. {
  766. if (!sess || !p_rtcp) {
  767. return TOOLKIT_EINVAL;
  768. }
  769. *p_rtcp = ntohs(sess->remote_rtcp_addr.sin_port);
  770. return 0;
  771. }
  772. TOOLKIT_API int rtp_session_get_raw_fd(rtp_session_t *sess, int *rtp_fd, int *rtcp_fd)
  773. {
  774. if (!sess) {
  775. return TOOLKIT_EINVAL;
  776. }
  777. if (rtp_fd)
  778. *rtp_fd = sess->rtp_fd;
  779. if (rtcp_fd)
  780. *rtcp_fd = sess->rtcp_fd;
  781. return 0;
  782. }