switch_rtp.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #include <switch.h>
  2. #include <test/switch_test.h>
  3. #ifndef MSG_CONFIRM
  4. #define MSG_CONFIRM 0
  5. #endif
  6. static const char *rx_host = "127.0.0.1";
  7. static switch_port_t rx_port = 1234;
  8. static const char *tx_host = "127.0.0.1";
  9. static switch_port_t tx_port = 54320;
  10. static switch_memory_pool_t *pool = NULL;
  11. static switch_rtp_t *rtp_session = NULL;
  12. static switch_rtp_flag_t flags[SWITCH_RTP_FLAG_INVALID] = {0};
  13. const char *err = NULL;
  14. static const switch_payload_t TEST_PT = 8;
  15. switch_rtp_packet_t rtp_packet;
  16. switch_frame_flag_t *frame_flags;
  17. switch_io_flag_t io_flags;
  18. switch_payload_t read_pt;
  19. int send_rtcp_test_success = 0;
  20. static void show_event(switch_event_t *event) {
  21. char *str;
  22. /*print the event*/
  23. switch_event_serialize_json(event, &str);
  24. if (str) {
  25. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%s\n", str);
  26. switch_safe_free(str);
  27. }
  28. }
  29. static void send_rtcp_event_handler(switch_event_t *event)
  30. {
  31. const char *new_ev = switch_event_get_header(event, "Event-Name");
  32. if (new_ev && !strcmp(new_ev, "SEND_RTCP_MESSAGE")) {
  33. send_rtcp_test_success = 1;
  34. }
  35. show_event(event);
  36. }
  37. FST_CORE_BEGIN("./conf")
  38. {
  39. FST_SUITE_BEGIN(switch_rtp)
  40. {
  41. FST_SETUP_BEGIN()
  42. {
  43. fst_requires_module("mod_loopback");
  44. }
  45. FST_SETUP_END()
  46. FST_TEARDOWN_BEGIN()
  47. {
  48. }
  49. FST_TEARDOWN_END()
  50. FST_TEST_BEGIN(test_rtp)
  51. {
  52. switch_rtp_stats_t *stats;
  53. switch_core_new_memory_pool(&pool);
  54. rtp_session = switch_rtp_new(rx_host, rx_port, tx_host, tx_port, TEST_PT, 8000, 20 * 1000, flags, "soft", &err, pool, 0, 0);
  55. fst_xcheck(rtp_session != NULL, "get RTP session");
  56. fst_requires(rtp_session);
  57. fst_requires(switch_rtp_ready(rtp_session));
  58. switch_rtp_activate_rtcp(rtp_session, 5, rx_port + 1, 0);
  59. switch_rtp_set_default_payload(rtp_session, TEST_PT);
  60. fst_xcheck(switch_rtp_get_default_payload(rtp_session) == TEST_PT, "get Payload Type");
  61. switch_rtp_set_ssrc(rtp_session, 0xabcd);
  62. switch_rtp_set_remote_ssrc(rtp_session, 0xcdef);
  63. fst_xcheck(switch_rtp_get_ssrc(rtp_session) == 0xabcd, "get SSRC");
  64. stats = switch_rtp_get_stats(rtp_session, pool);
  65. fst_requires(stats);
  66. switch_rtp_destroy(&rtp_session);
  67. switch_core_destroy_memory_pool(&pool);
  68. }
  69. FST_TEST_END()
  70. FST_TEST_BEGIN(test_session_with_rtp)
  71. {
  72. switch_core_session_t *session = NULL;
  73. switch_channel_t *channel = NULL;
  74. switch_status_t status;
  75. switch_call_cause_t cause;
  76. switch_core_new_memory_pool(&pool);
  77. status = switch_ivr_originate(NULL, &session, &cause, "null/+15553334444", 2, NULL, NULL, NULL, NULL, NULL, SOF_NONE, NULL, NULL);
  78. fst_requires(session);
  79. fst_check(status == SWITCH_STATUS_SUCCESS);
  80. channel = switch_core_session_get_channel(session);
  81. fst_requires(channel);
  82. switch_core_memory_pool_set_data(pool, "__session", session);
  83. session = switch_core_memory_pool_get_data(pool, "__session");
  84. fst_requires(session);
  85. rtp_session = switch_rtp_new(rx_host, rx_port, tx_host, tx_port, TEST_PT, 8000, 20 * 1000, flags, "soft", &err, pool, 0, 0);
  86. fst_xcheck(rtp_session != NULL, "switch_rtp_new()");
  87. fst_requires(switch_rtp_ready(rtp_session));
  88. switch_rtp_activate_rtcp(rtp_session, 5, rx_port + 1, 0);
  89. switch_rtp_set_default_payload(rtp_session, TEST_PT);
  90. switch_core_media_set_rtp_session(session, SWITCH_MEDIA_TYPE_AUDIO, rtp_session);
  91. channel = switch_core_session_get_channel(session);
  92. fst_requires(channel);
  93. session = switch_rtp_get_core_session(rtp_session);
  94. fst_requires(session);
  95. status = switch_rtp_activate_jitter_buffer(rtp_session, 1, 10, 80, 8000);
  96. fst_xcheck(status == SWITCH_STATUS_SUCCESS, "switch_rtp_activate_jitter_buffer()");
  97. status = switch_rtp_debug_jitter_buffer(rtp_session, "debug");
  98. fst_xcheck(status == SWITCH_STATUS_SUCCESS, "switch_rtp_debug_jitter_buffer()");
  99. fst_requires(switch_rtp_get_jitter_buffer(rtp_session));
  100. status = switch_rtp_pause_jitter_buffer(rtp_session, SWITCH_TRUE);
  101. fst_xcheck(status == SWITCH_STATUS_SUCCESS, "switch_rtp_pause_jitter_buffer()");
  102. status = switch_rtp_deactivate_jitter_buffer(rtp_session);
  103. fst_xcheck(status == SWITCH_STATUS_SUCCESS, "switch_rtp_deactivate_jitter_buffer()");
  104. switch_rtp_destroy(&rtp_session);
  105. switch_core_session_rwunlock(session);
  106. switch_core_destroy_memory_pool(&pool);
  107. }
  108. FST_TEST_END()
  109. FST_TEST_BEGIN(test_send_rtcp_event_audio)
  110. {
  111. switch_core_session_t *session = NULL;
  112. switch_channel_t *channel = NULL;
  113. switch_status_t status;
  114. switch_call_cause_t cause;
  115. switch_stream_handle_t stream = { 0 };
  116. const unsigned char packet[]="\x80\x00\xcd\x15\xfd\x86\x00\x00\x61\x5a\xe1\x37";
  117. uint32_t plen = 12;
  118. char rpacket[SWITCH_RECOMMENDED_BUFFER_SIZE];
  119. switch_payload_t pt = { 0 };
  120. switch_frame_flag_t frameflags = { 0 };
  121. static switch_port_t audio_rx_port = 1234;
  122. switch_media_handle_t *media_handle;
  123. switch_core_media_params_t *mparams;
  124. char *r_sdp;
  125. uint8_t match = 0, p = 0;
  126. struct sockaddr_in sin;
  127. socklen_t len = sizeof(sin);
  128. int x;
  129. struct sockaddr_in servaddr_rtp;
  130. int sockfd_rtp;
  131. struct hostent *server;
  132. int ret;
  133. switch_frame_t *read_frame, *write_frame;
  134. switch_event_bind("", SWITCH_EVENT_ALL, SWITCH_EVENT_SUBCLASS_ANY, send_rtcp_event_handler, NULL);
  135. status = switch_ivr_originate(NULL, &session, &cause, "null/+15553334444", 2, NULL, NULL, NULL, NULL, NULL, SOF_NONE, NULL, NULL);
  136. fst_requires(session);
  137. fst_check(status == SWITCH_STATUS_SUCCESS);
  138. channel = switch_core_session_get_channel(session);
  139. fst_requires(channel);
  140. mparams = switch_core_session_alloc(session, sizeof(switch_core_media_params_t));
  141. mparams->num_codecs = 1;
  142. mparams->inbound_codec_string = switch_core_session_strdup(session, "PCMU");
  143. mparams->outbound_codec_string = switch_core_session_strdup(session, "PCMU");
  144. mparams->rtpip = switch_core_session_strdup(session, (char *)rx_host);
  145. status = switch_media_handle_create(&media_handle, session, mparams);
  146. fst_requires(status == SWITCH_STATUS_SUCCESS);
  147. switch_channel_set_variable(channel, "absolute_codec_string", "PCMU");
  148. switch_channel_set_variable(channel, "fire_rtcp_events", "true");
  149. switch_channel_set_variable(channel, "send_silence_when_idle", "-1");
  150. switch_channel_set_variable(channel, SWITCH_LOCAL_MEDIA_IP_VARIABLE, rx_host);
  151. switch_channel_set_variable_printf(channel, SWITCH_LOCAL_MEDIA_PORT_VARIABLE, "%d", audio_rx_port);
  152. r_sdp = switch_core_session_sprintf(session,
  153. "v=0\n"
  154. "o=FreeSWITCH 1632033305 1632033306 IN IP4 %s\n"
  155. "s=-\n"
  156. "c=IN IP4 %s\n"
  157. "t=0 0\n"
  158. "m=audio 11114 RTP/AVP 0 101\n"
  159. "a=rtpmap:0 PCMU/8000\n"
  160. "a=rtpmap:101 telephone-event/8000\n"
  161. "a=rtcp:11115\n",
  162. tx_host, tx_host);
  163. switch_core_media_prepare_codecs(session, SWITCH_FALSE);
  164. match = switch_core_media_negotiate_sdp(session, r_sdp, &p, SDP_TYPE_REQUEST);
  165. fst_requires(match == 1);
  166. status = switch_core_media_choose_ports(session, SWITCH_TRUE, SWITCH_FALSE);
  167. fst_requires(status == SWITCH_STATUS_SUCCESS);
  168. status = switch_core_media_activate_rtp(session);
  169. fst_requires(status == SWITCH_STATUS_SUCCESS);
  170. switch_core_media_set_rtp_flag(session, SWITCH_MEDIA_TYPE_AUDIO, SWITCH_RTP_FLAG_DEBUG_RTP_READ);
  171. switch_core_media_set_rtp_flag(session, SWITCH_MEDIA_TYPE_AUDIO, SWITCH_RTP_FLAG_DEBUG_RTP_WRITE);
  172. switch_core_media_set_rtp_flag(session, SWITCH_MEDIA_TYPE_AUDIO, SWITCH_RTP_FLAG_AUDIO_FIRE_SEND_RTCP_EVENT);
  173. switch_core_media_set_rtp_flag(session, SWITCH_MEDIA_TYPE_AUDIO, SWITCH_RTP_FLAG_ENABLE_RTCP);
  174. switch_frame_alloc(&write_frame, SWITCH_RECOMMENDED_BUFFER_SIZE);
  175. write_frame->codec = switch_core_session_get_write_codec(session);
  176. SWITCH_STANDARD_STREAM(stream);
  177. switch_api_execute("fsctl", "debug_level 9", session, &stream);
  178. switch_safe_free(stream.data);
  179. if ((sockfd_rtp = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  180. perror("socket creation failed");
  181. fst_requires(0); /*exit*/
  182. }
  183. memset(&servaddr_rtp, 0, sizeof(servaddr_rtp));
  184. servaddr_rtp.sin_family = AF_INET;
  185. servaddr_rtp.sin_port = htons(audio_rx_port);
  186. server = gethostbyname(rx_host);
  187. bcopy((char *)server->h_addr, (char *)&servaddr_rtp.sin_addr.s_addr, server->h_length);
  188. /*get local UDP port (tx side) to trick FS into accepting our packets*/
  189. ret = sendto(sockfd_rtp, NULL, 0, MSG_CONFIRM, (const struct sockaddr *) &servaddr_rtp, sizeof(servaddr_rtp));
  190. if (ret < 0){
  191. perror("sendto");
  192. fst_requires(0);
  193. }
  194. rtp_session = switch_core_media_get_rtp_session(session, SWITCH_MEDIA_TYPE_AUDIO);
  195. len = sizeof(sin);
  196. if (getsockname(sockfd_rtp, (struct sockaddr *)&sin, &len) == -1) {
  197. perror("getsockname");
  198. fst_requires(0);
  199. } else {
  200. switch_rtp_set_remote_address(rtp_session, tx_host, ntohs(sin.sin_port), 0, SWITCH_FALSE, &err);
  201. switch_rtp_reset(rtp_session);
  202. }
  203. write_frame->datalen = plen;
  204. memcpy(write_frame->data, &packet, plen);
  205. switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_PAUSE);
  206. for (x = 0; x < 3; x++) {
  207. switch_rtp_write_frame(rtp_session, write_frame); /* rtp_session->stats.rtcp.sent_pkt_count++; */
  208. switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Sent RTP. Packet size = [%u]\n", plen);
  209. ret = sendto(sockfd_rtp, (const char *) &packet, plen, MSG_CONFIRM, (const struct sockaddr *) &servaddr_rtp, sizeof(servaddr_rtp));
  210. if (ret < 0){
  211. perror("sendto");
  212. fst_requires(0);
  213. }
  214. status = switch_rtp_read(rtp_session, (void *)&rpacket, &plen, &pt, &frameflags, io_flags);
  215. fst_requires(status == SWITCH_STATUS_SUCCESS);
  216. plen = 12;
  217. if (pt == SWITCH_RTP_CNG_PAYLOAD /*timeout*/) continue;
  218. status = switch_core_session_read_frame(session, &read_frame, frameflags, 0);
  219. fst_requires(status == SWITCH_STATUS_SUCCESS);
  220. }
  221. switch_sleep(3000 * 1000);
  222. fst_requires(send_rtcp_test_success);
  223. switch_channel_hangup(channel, SWITCH_CAUSE_NORMAL_CLEARING);
  224. if (write_frame) switch_frame_free(&write_frame);
  225. switch_rtp_destroy(&rtp_session);
  226. switch_media_handle_destroy(session);
  227. switch_core_session_rwunlock(session);
  228. }
  229. FST_TEST_END()
  230. }
  231. FST_SUITE_END()
  232. }
  233. FST_CORE_END()