Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

rtsp-client-input-test.cpp 5.7KB

6 månader sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #if defined(_DEBUG) || defined(DEBUG)
  2. #include "sockutil.h"
  3. #include "rtsp-client.h"
  4. #include <assert.h>
  5. #include <stdlib.h>
  6. #include "sockpair.h"
  7. #include "cstringext.h"
  8. #include "sys/system.h"
  9. #include "cpm/unuse.h"
  10. #include "sdp.h"
  11. //#define UDP_MULTICAST_ADDR "239.0.0.2"
  12. extern "C" void rtp_receiver_tcp_input(uint8_t channel, const void* data, uint16_t bytes);
  13. extern "C" void rtp_receiver_test(socket_t rtp[2], const char* peer, int peerport[2], int payload, const char* encoding);
  14. extern "C" void* rtp_receiver_tcp_test(uint8_t interleave1, uint8_t interleave2, int payload, const char* encoding);
  15. extern "C" int rtsp_addr_is_multicast(const char* ip);
  16. struct rtsp_client_input_test_t
  17. {
  18. rtsp_client_t* rtsp;
  19. int transport;
  20. socket_t rtp[5][2];
  21. unsigned short port[5][2];
  22. };
  23. static int rtsp_client_send(void* param, const char* uri, const void* req, size_t bytes)
  24. {
  25. //TODO: check uri and make socket
  26. //1. uri != rtsp describe uri(user input)
  27. //2. multi-uri if media_count > 1
  28. struct rtsp_client_input_test_t* ctx = (struct rtsp_client_input_test_t*)param;
  29. return bytes;
  30. }
  31. static int rtpport(void* param, int media, const char* source, unsigned short rtp[2], char* ip, int len)
  32. {
  33. struct rtsp_client_input_test_t* ctx = (struct rtsp_client_input_test_t*)param;
  34. int m = rtsp_client_get_media_type(ctx->rtsp, media);
  35. //if (SDP_M_MEDIA_AUDIO != m && SDP_M_MEDIA_VIDEO != m)
  36. if (SDP_M_MEDIA_VIDEO != m)
  37. return 0; // ignore
  38. switch (ctx->transport)
  39. {
  40. case RTSP_TRANSPORT_RTP_UDP:
  41. // TODO: ipv6
  42. assert(0 == sockpair_create("127.0.0.1", ctx->rtp[media], ctx->port[media]));
  43. rtp[0] = ctx->port[media][0];
  44. rtp[1] = ctx->port[media][1];
  45. if (rtsp_addr_is_multicast(ip))
  46. {
  47. if (0 != socket_udp_multicast(ctx->rtp[media][0], ip, source, 16) || 0 != socket_udp_multicast(ctx->rtp[media][1], ip, source, 16))
  48. return -1;
  49. }
  50. #if defined(UDP_MULTICAST_ADDR)
  51. else
  52. {
  53. if (0 != socket_udp_multicast(ctx->rtp[media][0], UDP_MULTICAST_ADDR, source, 16) || 0 != socket_udp_multicast(ctx->rtp[media][1], UDP_MULTICAST_ADDR, source, 16))
  54. return -1;
  55. snprintf(ip, len, "%s", UDP_MULTICAST_ADDR);
  56. }
  57. #endif
  58. break;
  59. case RTSP_TRANSPORT_RTP_TCP:
  60. rtp[0] = 2 * media;
  61. rtp[1] = 2 * media + 1;
  62. break;
  63. default:
  64. assert(0);
  65. return -1;
  66. }
  67. return ctx->transport;
  68. }
  69. int rtsp_client_options(rtsp_client_t* rtsp, const char* commands);
  70. static void onrtp(void* param, uint8_t channel, const void* data, uint16_t bytes)
  71. {
  72. static int keepalive = 0;
  73. struct rtsp_client_input_test_t* ctx = (struct rtsp_client_input_test_t*)param;
  74. rtp_receiver_tcp_input(channel, data, bytes);
  75. if (++keepalive % 1000 == 0)
  76. {
  77. rtsp_client_play(ctx->rtsp, NULL, NULL);
  78. }
  79. }
  80. static int ondescribe(void* param, const char* sdp, int len)
  81. {
  82. struct rtsp_client_input_test_t* ctx = (struct rtsp_client_input_test_t*)param;
  83. return rtsp_client_setup(ctx->rtsp, sdp, len);
  84. }
  85. static int onsetup(void* param, int timeout, int64_t duration)
  86. {
  87. int i;
  88. uint64_t npt = 0;
  89. char ip[65];
  90. u_short rtspport;
  91. struct rtsp_client_input_test_t* ctx = (struct rtsp_client_input_test_t*)param;
  92. assert(0 == rtsp_client_play(ctx->rtsp, &npt, NULL));
  93. for (i = 0; i < rtsp_client_media_count(ctx->rtsp); i++)
  94. {
  95. int payload, port[2];
  96. const char* encoding;
  97. const struct rtsp_header_transport_t* transport;
  98. transport = rtsp_client_get_media_transport(ctx->rtsp, i);
  99. encoding = rtsp_client_get_media_encoding(ctx->rtsp, i);
  100. payload = rtsp_client_get_media_payload(ctx->rtsp, i);
  101. if (RTSP_TRANSPORT_RTP_UDP == transport->transport)
  102. {
  103. //assert(RTSP_TRANSPORT_RTP_UDP == transport->transport); // udp only
  104. assert(0 == transport->multicast); // unicast only
  105. assert(transport->rtp.u.client_port1 == ctx->port[i][0]);
  106. assert(transport->rtp.u.client_port2 == ctx->port[i][1]);
  107. port[0] = transport->rtp.u.server_port1;
  108. port[1] = transport->rtp.u.server_port2;
  109. if (*transport->source)
  110. {
  111. rtp_receiver_test(ctx->rtp[i], transport->source, port, payload, encoding);
  112. }
  113. else
  114. {
  115. assert(0);
  116. //socket_getpeername(ctx->socket, ip, &rtspport);
  117. rtp_receiver_test(ctx->rtp[i], ip, port, payload, encoding);
  118. }
  119. }
  120. else if (RTSP_TRANSPORT_RTP_TCP == transport->transport)
  121. {
  122. //assert(transport->rtp.u.client_port1 == transport->interleaved1);
  123. //assert(transport->rtp.u.client_port2 == transport->interleaved2);
  124. rtp_receiver_tcp_test(transport->interleaved1, transport->interleaved2, payload, encoding);
  125. }
  126. else
  127. {
  128. assert(0); // TODO
  129. }
  130. }
  131. return 0;
  132. }
  133. static int onteardown(void* param)
  134. {
  135. return 0;
  136. }
  137. static int onplay(void* param, int media, const uint64_t* nptbegin, const uint64_t* nptend, const double* scale, const struct rtsp_rtp_info_t* rtpinfo, int count)
  138. {
  139. return 0;
  140. }
  141. static int onpause(void* param)
  142. {
  143. return 0;
  144. }
  145. void rtsp_client_input_test(const char* file)
  146. {
  147. int r;
  148. struct rtsp_client_input_test_t ctx;
  149. struct rtsp_client_handler_t handler;
  150. static char packet[2 * 1024 * 1024];
  151. memset(&ctx, 0, sizeof(ctx));
  152. handler.send = rtsp_client_send;
  153. handler.rtpport = rtpport;
  154. handler.ondescribe = ondescribe;
  155. handler.onsetup = onsetup;
  156. handler.onplay = onplay;
  157. handler.onpause = onpause;
  158. handler.onteardown = onteardown;
  159. handler.onrtp = onrtp;
  160. ctx.transport = RTSP_TRANSPORT_RTP_TCP;
  161. snprintf(packet, sizeof(packet), "rtsp://%s/%s", "127.0.0.1", file); // url
  162. //ctx.rtsp = rtsp_client_create(NULL, NULL, &handler, &ctx);
  163. ctx.rtsp = rtsp_client_create(packet, "username1", "password1", &handler, &ctx);
  164. assert(ctx.rtsp);
  165. assert(0 == rtsp_client_describe(ctx.rtsp));
  166. FILE* fp = fopen(file, "rb");
  167. while ( (r = fread(packet, 1, sizeof(packet), fp)) > 0)
  168. {
  169. assert(0 == rtsp_client_input(ctx.rtsp, packet, r));
  170. }
  171. assert(0 == rtsp_client_teardown(ctx.rtsp));
  172. rtsp_client_destroy(ctx.rtsp);
  173. fclose(fp);
  174. }
  175. #endif