audiortp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. #include "precompile.h"
  2. #include "audiortp.h"
  3. #include "audioframe.h"
  4. #include "audiostream.h"
  5. #include "audiocontext.h"
  6. #include "audiolog.h"
  7. #include "other/jbuf.h"
  8. #include <assert.h>
  9. #include "memutil.h"
  10. #define CIRC_LEN(rd, wr, n) (((wr)+(n)-(rd))%(n))
  11. #define CIRC_ADD(old, val, n) (((old)+(n)+(val))%(n))
  12. #define CIRC_SUB(old, val, n) (((old)+(n)-(val))%(n))
  13. #define CIRC_INC(i, n) CIRC_ADD(i, 1, n)
  14. #define CIRC_DEC(i, n) CIRC_SUB(i, 1, n)
  15. #define CIRC_IS_EMPTY(rd, wr) ((rd) == (wr))
  16. #define JB_MAX 10
  17. #define DTMF_DURATION 1600
  18. #define DTMF_DURATION_STEP 320
  19. #define DTMF_VOLUME 10
  20. static const char digitmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  21. '*', '#','A', 'B', 'C', 'D'};
  22. static __inline int digit2event(int digit) {
  23. int event;
  24. switch (digit) {
  25. case '0':case '1':case '2':case '3':case '4':
  26. case '5':case '6':case '7':case '8':case '9':
  27. event = digit - '0';
  28. break;
  29. case '*':
  30. event = 10;
  31. break;
  32. case '#':
  33. event = 11;
  34. break;
  35. case 'a':case 'b':case 'c':case 'd':
  36. event = 12 + digit - 'a';
  37. break;
  38. default:
  39. return -1;
  40. }
  41. return event;
  42. }
  43. static int send_dtmf_digit(rtp_session_t *sess, rtp_dtmf_event *digit, unsigned dtmf_pt)
  44. {
  45. rtp_state *st = rtp_session_get_rtp_state(sess);
  46. rtp_session_send(sess, 0, dtmf_pt, digit->e, 0, (const char*)digit, sizeof(rtp_dtmf_event));
  47. if (digit->e) {
  48. rtp_session_send(sess, 0, dtmf_pt, digit->e, 0, (const char*)digit, sizeof(rtp_dtmf_event));
  49. rtp_session_send(sess, 0, dtmf_pt, digit->e, 0, (const char*)digit, sizeof(rtp_dtmf_event));
  50. }
  51. return 0;
  52. }
  53. static int pt2bitsample(int pt)
  54. {
  55. switch (pt) {
  56. case RTP_PT_PCM16:
  57. return 16;
  58. case RTP_PT_PCM8:
  59. case RTP_PT_PCMA:
  60. case RTP_PT_PCMU:
  61. return 8;
  62. case RTP_PT_G729:
  63. return 1;
  64. default:
  65. break;
  66. }
  67. assert(0);
  68. return 0;
  69. }
  70. static apr_status_t read_frame(void *self, audioframe_t *frame)
  71. {
  72. audiortp_t *audiortp = CONTAINING_RECORD(self, audiortp_t, base);
  73. char buf[SUGGEST_FRAME_SIZE];
  74. unsigned short seq;
  75. unsigned mark;
  76. unsigned ts;
  77. unsigned pt;
  78. int i;
  79. int max_recv_cnt = 5; // max
  80. int new_dtmf = 0;
  81. audiortp->m_rtp_tick++;
  82. /* read packet from net */
  83. for (i = 0 ; i < max_recv_cnt; ++i) {
  84. int ret = rtp_session_recv_hook(audiortp->m_rtpsession, &pt, &mark, &ts, &seq, buf, sizeof(buf), audiortp->m_on_recv_hook, audiortp->m_hook_data);
  85. if (ret <= 0)
  86. break;
  87. audiortp->m_last_rtp_tick = audiortp->m_rtp_tick;
  88. if (pt == audiortp->m_recv_dtmf_pt) { /* dtmf */
  89. if (ret == sizeof(rtp_dtmf_event)) {
  90. int digit[2];
  91. int digit_cnt = 0;
  92. int j;
  93. rtp_dtmf_event *dtmf = (rtp_dtmf_event *)&buf[0];
  94. if (ts != audiortp->m_recving_dtmf_ts) {
  95. audiortp->m_recving_dtmf_ts = ts;
  96. if (!audiortp->m_recving_dtmf_event.e)
  97. digit[digit_cnt++] = audiortp->m_recving_dtmf_event.event;
  98. audiortp->m_recving_dtmf_event.event = dtmf->event;
  99. audiortp->m_recving_dtmf_event.e = dtmf->e;
  100. if (audiortp->m_recving_dtmf_event.e)
  101. digit[digit_cnt++] = audiortp->m_recving_dtmf_event.event;
  102. } else {
  103. if (dtmf->e) { /* end bit set */
  104. if (!audiortp->m_recving_dtmf_event.e) {
  105. audiortp->m_recving_dtmf_event.e = 1;
  106. audiortp->m_recving_dtmf_event.event = dtmf->event;
  107. digit[digit_cnt++] = audiortp->m_recving_dtmf_event.event;
  108. } else {
  109. if (audiortp->m_recving_dtmf_event.event == 0xff) {
  110. audiortp->m_recving_dtmf_event.event = dtmf->event;
  111. digit[digit_cnt++] = audiortp->m_recving_dtmf_event.event;
  112. }
  113. }
  114. } else {
  115. audiortp->m_recving_dtmf_event.event = dtmf->event;
  116. }
  117. }
  118. for (j = 0; j < digit_cnt; ++j) {
  119. if (digit[j] < sizeof(digitmap)) {
  120. int ch = digitmap[digit[j]];
  121. audiostream_raise_event(self, STREAM_EVT_RTP_DTMF, digit[j], 0);
  122. EnterCriticalSection(&audiortp->m_dtmf_lock);
  123. if (audiortp->m_recv_dtmf_cnt < MAX_DTMF)
  124. audiortp->m_recv_dtmf[audiortp->m_recv_dtmf_cnt++] = digit[j];
  125. LeaveCriticalSection(&audiortp->m_dtmf_lock);
  126. }
  127. }
  128. if (digit_cnt > 0)
  129. new_dtmf = digit[digit_cnt-1]; // the last one
  130. }
  131. } else if (pt == audiortp->m_recv_pt) { /* audio frame */
  132. if (ret == audiortp->m_recv_psize) {
  133. int time_span = (audiortp->m_recv_clock / 1000 * audiortp->m_recv_ptime);
  134. jbuf_put_frame2(audiortp->m_jitterbuf, &buf[0], ret, 0, ts / time_span, NULL);
  135. }
  136. } else {
  137. break;
  138. }
  139. audiortp->m_last_rtp_tick = audiortp->m_rtp_tick;
  140. }
  141. if (audiortp->m_timeout > 0) {
  142. unsigned int diffms = (audiortp->m_rtp_tick - audiortp->m_last_rtp_tick) * FRAME_TIME;
  143. if (diffms >= (unsigned)audiortp->m_timeout) {
  144. audiostream_raise_event(self, STREAM_EVT_RTP_TIMEOUT, 0, 0);
  145. }
  146. }
  147. {
  148. char frm_type;
  149. jbuf_get_frame(audiortp->m_jitterbuf, frame->buffer, &frm_type);
  150. if (frm_type == JB_NORMAL_FRAME) {
  151. frame->size = audiortp->m_jbuf_psize;
  152. } else {
  153. frame->size = 0;
  154. }
  155. }
  156. frame->dtmf = new_dtmf;
  157. return APR_SUCCESS;
  158. }
  159. static apr_status_t write_frame(void *self, const audioframe_t *frame)
  160. {
  161. audiortp_t *audiortp = CONTAINING_RECORD(self, audiortp_t, base);
  162. int dtmf = 0;
  163. EnterCriticalSection(&audiortp->m_dtmf_lock);
  164. if (frame->dtmf && CIRC_LEN(audiortp->m_send_dtmf_rd, audiortp->m_send_dtmf_wr, MAX_DTMF) < MAX_DTMF-1) {
  165. rtp_dtmf_event *dtmf = &audiortp->m_send_dtmf[audiortp->m_send_dtmf_wr];
  166. dtmf->event = (unsigned char)digit2event(frame->dtmf);
  167. dtmf->e_vol = DTMF_VOLUME;
  168. dtmf->p = 0;
  169. dtmf->e = 0;
  170. dtmf->duration = 0;
  171. audiortp->m_send_dtmf_wr = CIRC_INC(audiortp->m_send_dtmf_wr, MAX_DTMF);
  172. }
  173. if (!CIRC_IS_EMPTY(audiortp->m_send_dtmf_rd, audiortp->m_send_dtmf_wr)) {
  174. rtp_dtmf_event *digit = &audiortp->m_send_dtmf[audiortp->m_send_dtmf_rd];
  175. send_dtmf_digit(audiortp->m_rtpsession, digit, audiortp->m_send_dtmf_pt);
  176. dtmf = 1;
  177. digit->duration += DTMF_DURATION_STEP;
  178. if (!digit->e) {
  179. if (digit->duration == DTMF_DURATION) {
  180. digit->e = 1;
  181. }
  182. } else {
  183. audiortp->m_send_dtmf_rd = CIRC_INC(audiortp->m_send_dtmf_rd, MAX_DTMF);
  184. rtp_session_advance_timestamp(audiortp->m_rtpsession, DTMF_DURATION);
  185. }
  186. }
  187. LeaveCriticalSection(&audiortp->m_dtmf_lock);
  188. if (dtmf) {
  189. /* because we send dtmf, so ignore this frame */
  190. return 0;
  191. }
  192. if (frame->size < (unsigned long)audiortp->m_send_psize) {
  193. if (frame->size) {
  194. memcpy(audiortp->m_SendSample.buffer + audiortp->m_SendSample.size, frame->buffer, frame->size);
  195. audiortp->m_SendSample.size += frame->size;
  196. } else {
  197. char tmp[160];
  198. int tmp_cnt = FRAME_TIME * audiortp->m_send_clock / 1000 * audiortp->m_send_samplebit / 8;
  199. memset(tmp, 0, tmp_cnt);
  200. memcpy(audiortp->m_SendSample.buffer + audiortp->m_SendSample.size, tmp, tmp_cnt);
  201. audiortp->m_SendSample.size += tmp_cnt;
  202. }
  203. if (audiortp->m_SendSample.size >= (unsigned long)audiortp->m_send_psize) {
  204. //printf("send out %d\n", audiortp->m_send_psize);
  205. rtp_session_send_hook(audiortp->m_rtpsession, 0,
  206. audiortp->m_send_pt, 0, audiortp->m_send_ptime*audiortp->m_send_clock/1000,
  207. audiortp->m_SendSample.buffer, audiortp->m_send_psize, audiortp->m_on_send_hook, audiortp->m_hook_data);
  208. if (audiortp->m_SendSample.size > (unsigned long)audiortp->m_send_psize) {
  209. audiortp->m_SendSample.size -= audiortp->m_send_psize;
  210. memmove(audiortp->m_SendSample.buffer,
  211. audiortp->m_SendSample.buffer+audiortp->m_send_psize,
  212. audiortp->m_SendSample.size);
  213. } else {
  214. audiortp->m_SendSample.size = 0;
  215. }
  216. }
  217. } else {
  218. int delta_ts = audiortp->m_send_ptime*audiortp->m_send_clock/1000;
  219. rtp_session_send_hook(audiortp->m_rtpsession, 0,
  220. audiortp->m_send_pt, 0, delta_ts, frame->buffer, audiortp->m_send_psize, audiortp->m_on_send_hook, audiortp->m_hook_data);
  221. if (frame->size > (unsigned long)audiortp->m_send_psize) {
  222. audiortp->m_SendSample.size = frame->size-audiortp->m_send_psize;
  223. memcpy(audiortp->m_SendSample.buffer,
  224. frame->buffer+audiortp->m_send_psize,
  225. audiortp->m_SendSample.size);
  226. }
  227. }
  228. return APR_SUCCESS;
  229. }
  230. static audiostream_vtbl_t g_stream_vtbl = {
  231. &read_frame,
  232. &write_frame,
  233. };
  234. void audiortp_destroy(audiortp_t *self)
  235. {
  236. audiortp_t *audiortp = (audiortp_t *)self;
  237. DeleteCriticalSection(&audiortp->m_dtmf_lock);
  238. }
  239. apr_status_t audiortp_create(apr_pool_t *pool, audioengine_t *engine, rtp_session_t *sess, audiortp_t **p_audiortp)
  240. {
  241. audiortp_t *audiortp;
  242. audiortp = apr_palloc(pool, sizeof(audiortp_t));
  243. memset(audiortp, 0, sizeof(audiortp_t));
  244. audiostream_init(engine, &g_stream_vtbl, &audiortp->base);
  245. audiortp->m_rtpsession = sess;
  246. audiortp->m_SendSample.buffer = apr_palloc(pool, SUGGEST_FRAME_SIZE);
  247. audiortp->m_SendSample.size = 0;
  248. audiortp->m_SendSample.dtmf = 0;
  249. audiortp->m_RecvSample.buffer = apr_palloc(pool, SUGGEST_FRAME_SIZE);
  250. audiortp->m_RecvSample.size = 0;
  251. audiortp->m_RecvSample.dtmf = 0;
  252. audiortp->m_send_ptime = 0;
  253. audiortp->m_recv_ptime = 0;
  254. audiortp->m_send_psize = 0;
  255. audiortp->m_recv_psize = 0;
  256. audiortp->m_send_samplebit = 0;
  257. audiortp->m_recv_samplebit = 0;
  258. audiortp->m_timeout = -1;
  259. audiortp->m_send_pt = -1;
  260. audiortp->m_recv_pt = -1;
  261. audiortp->m_send_clock = 8000;
  262. audiortp->m_recv_clock = 8000;
  263. audiortp->m_send_dtmf_rd = 0;
  264. audiortp->m_send_dtmf_wr = 0;
  265. audiortp->m_recv_dtmf_cnt = 0;
  266. audiortp->m_recving_dtmf_ts = 0;
  267. audiortp->m_recving_dtmf_event.e = 1;
  268. audiortp->m_recving_dtmf_event.event = 0xff;
  269. audiortp->m_rtp_tick = 0;
  270. audiortp->m_last_rtp_tick = 0;
  271. audiortp->m_jitterbuf = 0;
  272. audiortp->m_on_recv_hook = NULL;
  273. audiortp->m_on_send_hook = NULL;
  274. audiortp->m_hook_data = NULL;
  275. InitializeCriticalSection(&audiortp->m_dtmf_lock);
  276. *p_audiortp = audiortp;
  277. return APR_SUCCESS;
  278. }
  279. apr_status_t audiortp_set_param(audiortp_t *audiortp, int flag, const void *ptr)
  280. {
  281. audiortp_t *rtp = (audiortp_t *)audiortp;
  282. switch (flag) {
  283. case AUDIO_RTP_FLAG_RECV_PTIME:
  284. rtp->m_recv_ptime = *(const int *)ptr;
  285. break;
  286. case AUDIO_RTP_FLAG_SEND_PTIME:
  287. rtp->m_send_ptime = *(const int *)ptr;
  288. break;
  289. case AUDIO_RTP_FLAG_RECV_PT:
  290. rtp->m_recv_pt = *(const int *)ptr;
  291. break;
  292. case AUDIO_RTP_FLAG_SEND_PT:
  293. rtp->m_send_pt = *(const int*)ptr;
  294. break;
  295. case AUDIO_RTP_FLAG_TIMEOUT:
  296. rtp->m_timeout = *(const int *)ptr;
  297. break;
  298. case AUDIO_RTP_FLAG_SEND_DTMF:
  299. rtp->m_send_dtmf_pt = *(const int *)ptr;
  300. break;
  301. case AUDIO_RTP_FLAG_RECV_DTMF:
  302. rtp->m_recv_dtmf_pt = *(const int *)ptr;
  303. break;
  304. case AUDIO_RTP_FLAG_SEND_CLOCK:
  305. rtp->m_send_clock = *(const int*)ptr;
  306. break;
  307. case AUDIO_RTP_FLAG_RECV_CLOCK:
  308. rtp->m_recv_clock = *(const int*)ptr;
  309. break;
  310. case AUDIO_RTP_FLAG_SEND_HOOK:
  311. rtp->m_on_send_hook = /**(void (**)(const char*,int,void*))*/(void*)ptr;
  312. break;
  313. case AUDIO_RTP_FLAG_RECV_HOOK:
  314. rtp->m_on_recv_hook = /**(void (**)(const char*,int,void*))*/(void*)ptr;
  315. break;
  316. case AUDIO_RTP_FLAG_HOOK_ARG:
  317. rtp->m_hook_data = /**(void**)*/(void*)ptr;
  318. break;
  319. default:
  320. return APR_BADARG;
  321. }
  322. return APR_SUCCESS;
  323. }
  324. apr_status_t audiortp_get_param(audiortp_t *audiortp, int flag, void *ptr)
  325. {
  326. audiortp_t *rtp = (audiortp_t *)audiortp;
  327. switch (flag) {
  328. case AUDIO_RTP_FLAG_RECV_PTIME:
  329. *(int *)ptr = rtp->m_recv_ptime;
  330. break;
  331. case AUDIO_RTP_FLAG_SEND_PTIME:
  332. *(int *)ptr = rtp->m_send_ptime;
  333. break;
  334. case AUDIO_RTP_FLAG_RECV_PT:
  335. *(int *)ptr = rtp->m_recv_pt;
  336. break;
  337. case AUDIO_RTP_FLAG_SEND_PT:
  338. *(int*)ptr = rtp->m_send_pt;
  339. break;
  340. case AUDIO_RTP_FLAG_TIMEOUT:
  341. *(int *)ptr = rtp->m_timeout;
  342. break;
  343. case AUDIO_RTP_FLAG_SEND_DTMF:
  344. *(int *)ptr = rtp->m_send_dtmf_pt;
  345. break;
  346. case AUDIO_RTP_FLAG_RECV_DTMF:
  347. *(int *)ptr = rtp->m_recv_dtmf_pt;
  348. break;
  349. case AUDIO_RTP_FLAG_SEND_CLOCK:
  350. *(int*)ptr = rtp->m_send_clock;
  351. break;
  352. case AUDIO_RTP_FLAG_RECV_CLOCK:
  353. *(int*)ptr = rtp->m_recv_clock;
  354. break;
  355. case AUDIO_RTP_FLAG_SEND_HOOK:
  356. *(void (**)(const char*,int,void*))ptr = rtp->m_on_send_hook;
  357. break;
  358. case AUDIO_RTP_FLAG_RECV_HOOK:
  359. *(void (**)(const char*,int,void*))ptr = rtp->m_on_recv_hook;
  360. break;
  361. case AUDIO_RTP_FLAG_HOOK_ARG:
  362. *(void**)ptr = rtp->m_hook_data;
  363. break;
  364. default:
  365. return APR_BADARG;
  366. }
  367. return APR_SUCCESS;
  368. }
  369. apr_status_t audiortp_init(audiortp_t *ar)
  370. {
  371. audiortp_t *audiortp = (audiortp_t *)ar;
  372. audiortp->m_send_samplebit = pt2bitsample(audiortp->m_send_pt);
  373. audiortp->m_recv_samplebit = pt2bitsample(audiortp->m_recv_pt);
  374. audiortp->m_send_psize = audiortp->m_send_ptime * audiortp->m_send_clock / 1000 * audiortp->m_send_samplebit / 8;
  375. audiortp->m_recv_psize = audiortp->m_recv_ptime * audiortp->m_recv_clock / 1000 * audiortp->m_recv_samplebit / 8;
  376. audiortp->m_jbuf_ptime = audiortp->m_recv_ptime;
  377. audiortp->m_jbuf_psize = audiortp->m_jbuf_ptime * audiortp->m_recv_clock / 1000 * audiortp->m_recv_samplebit / 8;
  378. jbuf_create(audiortp->m_jbuf_psize, audiortp->m_jbuf_ptime, JB_MAX, &audiortp->m_jitterbuf);
  379. jbuf_set_adaptive(audiortp->m_jitterbuf, 0, 7, JB_MAX);
  380. DEBUG_TRACE("audiortp init, send_psize:%d, recv_psize:%d", audiortp->m_send_psize, audiortp->m_recv_psize);
  381. return APR_SUCCESS;
  382. }
  383. apr_status_t audiortp_term(audiortp_t *self)
  384. {
  385. audiortp_t *audiortp = (audiortp_t *)self;
  386. jbuf_destroy(audiortp->m_jitterbuf);
  387. audiortp->m_jitterbuf = 0;
  388. return APR_SUCCESS;
  389. }
  390. apr_status_t audiortp_send_dtmf(audiortp_t *self, const char *digits, unsigned cnt)
  391. {
  392. audiortp_t *audiortp = (audiortp_t *)self;
  393. unsigned count = 0;
  394. if (!(audiostream_get_direction(&audiortp->base)&STREAM_DIR_WRITE)) {
  395. AUDIO_LOG_ERROR("audio rtp cannot used for send!");
  396. return APR_EGENERAL;
  397. }
  398. if (!digits || !cnt)
  399. return -1;
  400. if (!(audiostream_get_direction(&audiortp->base)&STREAM_DIR_WRITE))
  401. return -1;
  402. EnterCriticalSection(&audiortp->m_dtmf_lock);
  403. while (CIRC_LEN(audiortp->m_send_dtmf_rd, audiortp->m_send_dtmf_wr, MAX_DTMF) < MAX_DTMF-1 && count < cnt) {
  404. rtp_dtmf_event *dtmf = &audiortp->m_send_dtmf[audiortp->m_send_dtmf_wr];
  405. dtmf->event = (unsigned char)digit2event(digits[count]);
  406. dtmf->e_vol = DTMF_VOLUME;
  407. dtmf->p = 0;
  408. dtmf->e = 0;
  409. dtmf->duration = 0;
  410. audiortp->m_send_dtmf_wr = CIRC_INC(audiortp->m_send_dtmf_wr, MAX_DTMF);
  411. count ++;
  412. }
  413. LeaveCriticalSection(&audiortp->m_dtmf_lock);
  414. return APR_SUCCESS;
  415. }
  416. apr_status_t audiortp_recv_dtmf(audiortp_t *self, char *digits, unsigned *cnt)
  417. {
  418. audiortp_t *audiortp = (audiortp_t *)self;
  419. unsigned count = 0;
  420. int i = 0;
  421. if (!(audiostream_get_direction(&audiortp->base)&STREAM_DIR_READ)) {
  422. AUDIO_LOG_ERROR("audio rtp cannot used for recv!");
  423. return APR_EGENERAL;
  424. }
  425. if (!digits)
  426. return APR_BADARG;
  427. EnterCriticalSection(&audiortp->m_dtmf_lock);
  428. while (count < *cnt && i < audiortp->m_recv_dtmf_cnt) {
  429. digits[count++] = audiortp->m_recv_dtmf[i++];
  430. }
  431. if (i < audiortp->m_recv_dtmf_cnt)
  432. memmove(&audiortp->m_recv_dtmf[0], &audiortp->m_recv_dtmf[i], audiortp->m_recv_dtmf_cnt-i);
  433. audiortp->m_recv_dtmf_cnt -= i;
  434. *cnt = count;
  435. LeaveCriticalSection(&audiortp->m_dtmf_lock);
  436. return APR_SUCCESS;
  437. }
  438. apr_status_t audiortp_reset_jitter(audiortp_t *audiortp)
  439. {
  440. jbuf_reset(audiortp->m_jitterbuf);
  441. return APR_SUCCESS;
  442. }