audiortp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. #ifdef _WIN32
  123. EnterCriticalSection(&audiortp->m_dtmf_lock);
  124. #else
  125. apr_thread_mutex_lock(audiortp->m_dtmf_mtx);
  126. #endif
  127. if (audiortp->m_recv_dtmf_cnt < MAX_DTMF)
  128. audiortp->m_recv_dtmf[audiortp->m_recv_dtmf_cnt++] = digit[j];
  129. #ifdef _WIN32
  130. LeaveCriticalSection(&audiortp->m_dtmf_lock);
  131. #else
  132. apr_thread_mutex_unlock(audiortp->m_dtmf_mtx);
  133. #endif
  134. }
  135. }
  136. if (digit_cnt > 0)
  137. new_dtmf = digit[digit_cnt-1]; // the last one
  138. }
  139. } else if (pt == audiortp->m_recv_pt) { /* audio frame */
  140. if (ret == audiortp->m_recv_psize) {
  141. int time_span = (audiortp->m_recv_clock / 1000 * audiortp->m_recv_ptime);
  142. jbuf_put_frame2(audiortp->m_jitterbuf, &buf[0], ret, 0, ts / time_span, NULL);
  143. }
  144. } else {
  145. break;
  146. }
  147. audiortp->m_last_rtp_tick = audiortp->m_rtp_tick;
  148. }
  149. if (audiortp->m_timeout > 0) {
  150. unsigned int diffms = (audiortp->m_rtp_tick - audiortp->m_last_rtp_tick) * FRAME_TIME;
  151. if (diffms >= (unsigned)audiortp->m_timeout) {
  152. audiostream_raise_event(self, STREAM_EVT_RTP_TIMEOUT, 0, 0);
  153. }
  154. }
  155. {
  156. char frm_type;
  157. jbuf_get_frame(audiortp->m_jitterbuf, frame->buffer, &frm_type);
  158. if (frm_type == JB_NORMAL_FRAME) {
  159. frame->size = audiortp->m_jbuf_psize;
  160. } else {
  161. frame->size = 0;
  162. }
  163. }
  164. frame->dtmf = new_dtmf;
  165. return APR_SUCCESS;
  166. }
  167. static apr_status_t write_frame(void *self, const audioframe_t *frame)
  168. {
  169. audiortp_t *audiortp = CONTAINING_RECORD(self, audiortp_t, base);
  170. int dtmf = 0;
  171. #ifdef WIN32
  172. EnterCriticalSection(&audiortp->m_dtmf_lock);
  173. #else
  174. apr_thread_mutex_lock(audiortp->m_dtmf_mtx);
  175. #endif
  176. if (frame->dtmf && CIRC_LEN(audiortp->m_send_dtmf_rd, audiortp->m_send_dtmf_wr, MAX_DTMF) < MAX_DTMF - 1) {
  177. rtp_dtmf_event* dtmf = &audiortp->m_send_dtmf[audiortp->m_send_dtmf_wr];
  178. dtmf->event = (unsigned char)digit2event(frame->dtmf);
  179. dtmf->e_vol = DTMF_VOLUME;
  180. dtmf->p = 0;
  181. dtmf->e = 0;
  182. dtmf->duration = 0;
  183. audiortp->m_send_dtmf_wr = CIRC_INC(audiortp->m_send_dtmf_wr, MAX_DTMF);
  184. }
  185. if (!CIRC_IS_EMPTY(audiortp->m_send_dtmf_rd, audiortp->m_send_dtmf_wr)) {
  186. rtp_dtmf_event* digit = &audiortp->m_send_dtmf[audiortp->m_send_dtmf_rd];
  187. send_dtmf_digit(audiortp->m_rtpsession, digit, audiortp->m_send_dtmf_pt);
  188. dtmf = 1;
  189. digit->duration += DTMF_DURATION_STEP;
  190. if (!digit->e) {
  191. if (digit->duration == DTMF_DURATION) {
  192. digit->e = 1;
  193. }
  194. }
  195. else {
  196. audiortp->m_send_dtmf_rd = CIRC_INC(audiortp->m_send_dtmf_rd, MAX_DTMF);
  197. rtp_session_advance_timestamp(audiortp->m_rtpsession, DTMF_DURATION);
  198. }
  199. }
  200. #ifdef _WIN32
  201. LeaveCriticalSection(&audiortp->m_dtmf_lock);
  202. #else
  203. apr_thread_mutex_unlock(audiortp->m_dtmf_mtx);
  204. #endif // WIN32
  205. if (dtmf) {
  206. /* because we send dtmf, so ignore this frame */
  207. return 0;
  208. }
  209. if (frame->size < (unsigned long)audiortp->m_send_psize) {
  210. if (frame->size) {
  211. memcpy(audiortp->m_SendSample.buffer + audiortp->m_SendSample.size, frame->buffer, frame->size);
  212. audiortp->m_SendSample.size += frame->size;
  213. } else {
  214. char tmp[160];
  215. int tmp_cnt = FRAME_TIME * audiortp->m_send_clock / 1000 * audiortp->m_send_samplebit / 8;
  216. memset(tmp, 0, tmp_cnt);
  217. memcpy(audiortp->m_SendSample.buffer + audiortp->m_SendSample.size, tmp, tmp_cnt);
  218. audiortp->m_SendSample.size += tmp_cnt;
  219. }
  220. if (audiortp->m_SendSample.size >= (unsigned long)audiortp->m_send_psize) {
  221. rtp_session_send_hook(audiortp->m_rtpsession, 0,
  222. audiortp->m_send_pt, 0, audiortp->m_send_ptime*audiortp->m_send_clock/1000,
  223. audiortp->m_SendSample.buffer, audiortp->m_send_psize, audiortp->m_on_send_hook, audiortp->m_hook_data);
  224. if (audiortp->m_SendSample.size > (unsigned long)audiortp->m_send_psize) {
  225. audiortp->m_SendSample.size -= audiortp->m_send_psize;
  226. memmove(audiortp->m_SendSample.buffer,
  227. audiortp->m_SendSample.buffer+audiortp->m_send_psize,
  228. audiortp->m_SendSample.size);
  229. } else {
  230. audiortp->m_SendSample.size = 0;
  231. }
  232. }
  233. } else {
  234. int delta_ts = audiortp->m_send_ptime*audiortp->m_send_clock/1000;
  235. rtp_session_send_hook(audiortp->m_rtpsession, 0,
  236. audiortp->m_send_pt, 0, delta_ts, frame->buffer, audiortp->m_send_psize, audiortp->m_on_send_hook, audiortp->m_hook_data);
  237. if (frame->size > (unsigned long)audiortp->m_send_psize) {
  238. audiortp->m_SendSample.size = frame->size-audiortp->m_send_psize;
  239. memcpy(audiortp->m_SendSample.buffer,
  240. frame->buffer+audiortp->m_send_psize,
  241. audiortp->m_SendSample.size);
  242. }
  243. }
  244. return APR_SUCCESS;
  245. }
  246. static audiostream_vtbl_t g_stream_vtbl = {
  247. &read_frame,
  248. &write_frame,
  249. };
  250. void audiortp_destroy(audiortp_t *self)
  251. {
  252. audiortp_t *audiortp = (audiortp_t *)self;
  253. #ifdef _WIN32
  254. DeleteCriticalSection(&audiortp->m_dtmf_lock);
  255. #else
  256. apr_thread_mutex_destroy(audiortp->m_dtmf_mtx);
  257. #endif // _WIN32
  258. }
  259. apr_status_t audiortp_create(apr_pool_t *pool, audioengine_t *engine, rtp_session_t *sess, audiortp_t **p_audiortp)
  260. {
  261. apr_status_t status = -1;
  262. audiortp_t *audiortp;
  263. audiortp = apr_palloc(pool, sizeof(audiortp_t));
  264. memset(audiortp, 0, sizeof(audiortp_t));
  265. audiostream_init(engine, &g_stream_vtbl, &audiortp->base);
  266. audiortp->m_rtpsession = sess;
  267. audiortp->m_SendSample.buffer = apr_palloc(pool, SUGGEST_FRAME_SIZE);
  268. audiortp->m_SendSample.size = 0;
  269. audiortp->m_SendSample.dtmf = 0;
  270. audiortp->m_RecvSample.buffer = apr_palloc(pool, SUGGEST_FRAME_SIZE);
  271. audiortp->m_RecvSample.size = 0;
  272. audiortp->m_RecvSample.dtmf = 0;
  273. audiortp->m_send_ptime = 0;
  274. audiortp->m_recv_ptime = 0;
  275. audiortp->m_send_psize = 0;
  276. audiortp->m_recv_psize = 0;
  277. audiortp->m_send_samplebit = 0;
  278. audiortp->m_recv_samplebit = 0;
  279. audiortp->m_timeout = -1;
  280. audiortp->m_send_pt = -1;
  281. audiortp->m_recv_pt = -1;
  282. audiortp->m_send_clock = 8000;
  283. audiortp->m_recv_clock = 8000;
  284. audiortp->m_send_dtmf_rd = 0;
  285. audiortp->m_send_dtmf_wr = 0;
  286. audiortp->m_recv_dtmf_cnt = 0;
  287. audiortp->m_recving_dtmf_ts = 0;
  288. audiortp->m_recving_dtmf_event.e = 1;
  289. audiortp->m_recving_dtmf_event.event = 0xff;
  290. audiortp->m_rtp_tick = 0;
  291. audiortp->m_last_rtp_tick = 0;
  292. audiortp->m_jitterbuf = 0;
  293. audiortp->m_on_recv_hook = NULL;
  294. audiortp->m_on_send_hook = NULL;
  295. audiortp->m_hook_data = NULL;
  296. #ifdef _WIN32
  297. InitializeCriticalSection(&audiortp->m_dtmf_lock);
  298. status = APR_SUCCESS;
  299. #else
  300. status = apr_thread_mutex_create(&audiortp->m_dtmf_mtx, APR_THREAD_MUTEX_NESTED, pool);
  301. #endif // _WIN32
  302. *p_audiortp = audiortp;
  303. return status;
  304. }
  305. apr_status_t audiortp_set_param(audiortp_t *audiortp, int flag, const void *ptr)
  306. {
  307. audiortp_t *rtp = (audiortp_t *)audiortp;
  308. switch (flag) {
  309. case AUDIO_RTP_FLAG_RECV_PTIME:
  310. rtp->m_recv_ptime = *(const int *)ptr;
  311. break;
  312. case AUDIO_RTP_FLAG_SEND_PTIME:
  313. rtp->m_send_ptime = *(const int *)ptr;
  314. break;
  315. case AUDIO_RTP_FLAG_RECV_PT:
  316. rtp->m_recv_pt = *(const int *)ptr;
  317. break;
  318. case AUDIO_RTP_FLAG_SEND_PT:
  319. rtp->m_send_pt = *(const int*)ptr;
  320. break;
  321. case AUDIO_RTP_FLAG_TIMEOUT:
  322. rtp->m_timeout = *(const int *)ptr;
  323. break;
  324. case AUDIO_RTP_FLAG_SEND_DTMF:
  325. rtp->m_send_dtmf_pt = *(const int *)ptr;
  326. break;
  327. case AUDIO_RTP_FLAG_RECV_DTMF:
  328. rtp->m_recv_dtmf_pt = *(const int *)ptr;
  329. break;
  330. case AUDIO_RTP_FLAG_SEND_CLOCK:
  331. rtp->m_send_clock = *(const int*)ptr;
  332. break;
  333. case AUDIO_RTP_FLAG_RECV_CLOCK:
  334. rtp->m_recv_clock = *(const int*)ptr;
  335. break;
  336. case AUDIO_RTP_FLAG_SEND_HOOK:
  337. rtp->m_on_send_hook = /**(void (**)(const char*,int,void*))*/(void*)ptr;
  338. break;
  339. case AUDIO_RTP_FLAG_RECV_HOOK:
  340. rtp->m_on_recv_hook = /**(void (**)(const char*,int,void*))*/(void*)ptr;
  341. break;
  342. case AUDIO_RTP_FLAG_HOOK_ARG:
  343. rtp->m_hook_data = /**(void**)*/(void*)ptr;
  344. break;
  345. default:
  346. return APR_BADARG;
  347. }
  348. return APR_SUCCESS;
  349. }
  350. apr_status_t audiortp_get_param(audiortp_t *audiortp, int flag, void *ptr)
  351. {
  352. audiortp_t *rtp = (audiortp_t *)audiortp;
  353. switch (flag) {
  354. case AUDIO_RTP_FLAG_RECV_PTIME:
  355. *(int *)ptr = rtp->m_recv_ptime;
  356. break;
  357. case AUDIO_RTP_FLAG_SEND_PTIME:
  358. *(int *)ptr = rtp->m_send_ptime;
  359. break;
  360. case AUDIO_RTP_FLAG_RECV_PT:
  361. *(int *)ptr = rtp->m_recv_pt;
  362. break;
  363. case AUDIO_RTP_FLAG_SEND_PT:
  364. *(int*)ptr = rtp->m_send_pt;
  365. break;
  366. case AUDIO_RTP_FLAG_TIMEOUT:
  367. *(int *)ptr = rtp->m_timeout;
  368. break;
  369. case AUDIO_RTP_FLAG_SEND_DTMF:
  370. *(int *)ptr = rtp->m_send_dtmf_pt;
  371. break;
  372. case AUDIO_RTP_FLAG_RECV_DTMF:
  373. *(int *)ptr = rtp->m_recv_dtmf_pt;
  374. break;
  375. case AUDIO_RTP_FLAG_SEND_CLOCK:
  376. *(int*)ptr = rtp->m_send_clock;
  377. break;
  378. case AUDIO_RTP_FLAG_RECV_CLOCK:
  379. *(int*)ptr = rtp->m_recv_clock;
  380. break;
  381. case AUDIO_RTP_FLAG_SEND_HOOK:
  382. *(void (**)(const char*,int,void*))ptr = rtp->m_on_send_hook;
  383. break;
  384. case AUDIO_RTP_FLAG_RECV_HOOK:
  385. *(void (**)(const char*,int,void*))ptr = rtp->m_on_recv_hook;
  386. break;
  387. case AUDIO_RTP_FLAG_HOOK_ARG:
  388. *(void**)ptr = rtp->m_hook_data;
  389. break;
  390. default:
  391. return APR_BADARG;
  392. }
  393. return APR_SUCCESS;
  394. }
  395. apr_status_t audiortp_init(audiortp_t *ar)
  396. {
  397. audiortp_t *audiortp = (audiortp_t *)ar;
  398. audiortp->m_send_samplebit = pt2bitsample(audiortp->m_send_pt);
  399. audiortp->m_recv_samplebit = pt2bitsample(audiortp->m_recv_pt);
  400. audiortp->m_send_psize = audiortp->m_send_ptime * audiortp->m_send_clock / 1000 * audiortp->m_send_samplebit / 8;
  401. audiortp->m_recv_psize = audiortp->m_recv_ptime * audiortp->m_recv_clock / 1000 * audiortp->m_recv_samplebit / 8;
  402. audiortp->m_jbuf_ptime = audiortp->m_recv_ptime;
  403. audiortp->m_jbuf_psize = audiortp->m_jbuf_ptime * audiortp->m_recv_clock / 1000 * audiortp->m_recv_samplebit / 8;
  404. jbuf_create(audiortp->m_jbuf_psize, audiortp->m_jbuf_ptime, JB_MAX, &audiortp->m_jitterbuf);
  405. jbuf_set_adaptive(audiortp->m_jitterbuf, 0, 7, JB_MAX);
  406. return APR_SUCCESS;
  407. }
  408. apr_status_t audiortp_term(audiortp_t *self)
  409. {
  410. audiortp_t *audiortp = (audiortp_t *)self;
  411. jbuf_destroy(audiortp->m_jitterbuf);
  412. audiortp->m_jitterbuf = 0;
  413. return APR_SUCCESS;
  414. }
  415. apr_status_t audiortp_send_dtmf(audiortp_t *self, const char *digits, unsigned cnt)
  416. {
  417. audiortp_t *audiortp = (audiortp_t *)self;
  418. unsigned count = 0;
  419. if (!(audiostream_get_direction(&audiortp->base)&STREAM_DIR_WRITE)) {
  420. audio_log_v(AUDIO_LOG_LEVEL_ERROR,"audio rtp cannot used for send!");
  421. return APR_EGENERAL;
  422. }
  423. if (!digits || !cnt)
  424. return -1;
  425. if (!(audiostream_get_direction(&audiortp->base)&STREAM_DIR_WRITE))
  426. return -1;
  427. #ifdef _WIN32
  428. EnterCriticalSection(&audiortp->m_dtmf_lock);
  429. #else
  430. apr_thread_mutex_lock(audiortp->m_dtmf_mtx);
  431. #endif
  432. while (CIRC_LEN(audiortp->m_send_dtmf_rd, audiortp->m_send_dtmf_wr, MAX_DTMF) < MAX_DTMF - 1 && count < cnt) {
  433. rtp_dtmf_event* dtmf = &audiortp->m_send_dtmf[audiortp->m_send_dtmf_wr];
  434. dtmf->event = (unsigned char)digit2event(digits[count]);
  435. dtmf->e_vol = DTMF_VOLUME;
  436. dtmf->p = 0;
  437. dtmf->e = 0;
  438. dtmf->duration = 0;
  439. audiortp->m_send_dtmf_wr = CIRC_INC(audiortp->m_send_dtmf_wr, MAX_DTMF);
  440. count++;
  441. }
  442. #ifdef _WIN32
  443. LeaveCriticalSection(&audiortp->m_dtmf_lock);
  444. #else
  445. apr_thread_mutex_unlock(audiortp->m_dtmf_mtx);
  446. #endif // _WIN32
  447. return APR_SUCCESS;
  448. }
  449. apr_status_t audiortp_recv_dtmf(audiortp_t *self, char *digits, unsigned *cnt)
  450. {
  451. audiortp_t *audiortp = (audiortp_t *)self;
  452. unsigned count = 0;
  453. int i = 0;
  454. if (!(audiostream_get_direction(&audiortp->base)&STREAM_DIR_READ)) {
  455. audio_log_v(AUDIO_LOG_LEVEL_ERROR,"audio rtp cannot used for recv!");
  456. return APR_EGENERAL;
  457. }
  458. if (!digits)
  459. return APR_BADARG;
  460. #ifdef _WIN32
  461. EnterCriticalSection(&audiortp->m_dtmf_lock);
  462. #else
  463. apr_thread_mutex_lock(audiortp->m_dtmf_mtx);
  464. #endif
  465. while (count < *cnt && i < audiortp->m_recv_dtmf_cnt) {
  466. digits[count++] = audiortp->m_recv_dtmf[i++];
  467. }
  468. if (i < audiortp->m_recv_dtmf_cnt)
  469. memmove(&audiortp->m_recv_dtmf[0], &audiortp->m_recv_dtmf[i], audiortp->m_recv_dtmf_cnt - i);
  470. audiortp->m_recv_dtmf_cnt -= i;
  471. *cnt = count;
  472. #ifdef _WIN32
  473. LeaveCriticalSection(&audiortp->m_dtmf_lock);
  474. #else
  475. apr_thread_mutex_unlock(audiortp->m_dtmf_mtx);
  476. #endif // _WIN32
  477. return APR_SUCCESS;
  478. }
  479. apr_status_t audiortp_reset_jitter(audiortp_t *audiortp)
  480. {
  481. jbuf_reset(audiortp->m_jitterbuf);
  482. return APR_SUCCESS;
  483. }