audiocontext.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. #include "precompile.h"
  2. #include "audiocontext.h"
  3. #include "audiostream.h"
  4. #include "audiodriver.h"
  5. #include "audiolog.h"
  6. #ifdef _WIN32
  7. #else
  8. #include <fcntl.h>
  9. #include <errno.h>
  10. #endif
  11. typedef struct event_handler_entry
  12. {
  13. APR_RING_ENTRY(event_handler_entry) link;
  14. int priority;
  15. audiocontext_event_handler event_handler;
  16. void *user_data;
  17. }event_handler_entry;
  18. typedef struct event_entry
  19. {
  20. APR_RING_ENTRY(event_entry) link;
  21. void *stream_or_driver;
  22. int bstream;
  23. int evt;
  24. int param1;
  25. int param2;
  26. }event_entry;
  27. struct audiocontext_t
  28. {
  29. audioengine_t *engine;
  30. apr_array_header_t *arr_drv;
  31. APR_RING_HEAD(event_entry_head, event_entry) event_list, event_list_free;
  32. int event_list_cnt, event_list_free_cnt;
  33. APR_RING_HEAD(event_handler_entry_head, event_handler_entry) event_handler_list, event_handler_list_free;
  34. int event_handler_list_cnt, event_handler_list_free_cnt;
  35. apr_thread_mutex_t *mtx;
  36. apr_thread_t *thread_handle;
  37. volatile int bstop;
  38. #ifdef _WIN32
  39. HANDLE sem;
  40. #else
  41. sem_t* sem;
  42. #endif // _WIN32
  43. };
  44. static void *APR_THREAD_FUNC audiocontext_run(apr_thread_t *thread_handle, void *data)
  45. {
  46. audiocontext_t *ctx = (audiocontext_t *)data;
  47. //SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
  48. for (;;) {
  49. #ifdef _WIN32
  50. WaitForSingleObject(ctx->sem, INFINITE);
  51. #else
  52. sem_wait(ctx->sem);
  53. #endif // _WIN32
  54. if (!ctx->bstop) {
  55. int i;
  56. apr_thread_mutex_lock(ctx->mtx);
  57. for (i = 0; i < ctx->arr_drv->nelts; ++i) {
  58. audiodriver_t *drv = APR_ARRAY_IDX(ctx->arr_drv, i, audiodriver_t*);
  59. //暂时屏蔽rtp流发送功能
  60. drv->vtbl->process_frame(drv);
  61. }
  62. while (ctx->event_list_cnt > 0) {
  63. event_entry *e = APR_RING_FIRST(&ctx->event_list);
  64. event_handler_entry *ep1, *ep2;
  65. APR_RING_REMOVE(e, link);
  66. ctx->event_list_cnt--;
  67. APR_RING_FOREACH_SAFE(ep1, ep2, &ctx->event_handler_list, event_handler_entry, link) {
  68. if ((*ep1->event_handler)(ctx, ep1->user_data, e->bstream, e->stream_or_driver, e->evt, e->param1, e->param2) == TRUE) {
  69. break;
  70. }
  71. }
  72. APR_RING_INSERT_TAIL(&ctx->event_list_free, e, event_entry, link);
  73. ctx->event_list_free_cnt++;
  74. }
  75. apr_thread_mutex_unlock(ctx->mtx);
  76. } else {
  77. break;
  78. }
  79. }
  80. apr_thread_exit(thread_handle, APR_SUCCESS);
  81. return NULL;
  82. }
  83. audioengine_t *audiocontext_get_engine(audiocontext_t *ctx)
  84. {
  85. return ctx ? ctx->engine : 0;
  86. }
  87. apr_status_t audiocontext_create(apr_pool_t *pool, audioengine_t *e, audiocontext_t **p_ctx)
  88. {
  89. apr_status_t status;
  90. audiocontext_t *ctx;
  91. ctx = apr_palloc(pool, sizeof(audiocontext_t));
  92. memset(ctx, 0, sizeof(audiocontext_t));
  93. status = apr_thread_mutex_create(&ctx->mtx, APR_THREAD_MUTEX_NESTED, pool);
  94. if (status != APR_SUCCESS) {
  95. return status;
  96. }
  97. #ifdef _WIN32
  98. ctx->sem = CreateSemaphoreA(NULL, 0, 0x7fffffff, NULL);
  99. #else
  100. ctx->sem = apr_palloc(pool, sizeof(sem_t));
  101. sem_init(ctx->sem, 0, 0);
  102. if (ctx->sem){
  103. int ivalue = -1;
  104. sem_getvalue(ctx->sem, &ivalue);
  105. }
  106. #endif // _WIN32
  107. if (!ctx->sem) {
  108. apr_thread_mutex_destroy(ctx->mtx);
  109. return apr_get_os_error();
  110. }
  111. ctx->arr_drv = apr_array_make(pool, 1024, sizeof(audiodriver_t*));
  112. APR_RING_INIT(&ctx->event_list, event_entry, link);
  113. APR_RING_INIT(&ctx->event_list_free, event_entry, link);
  114. APR_RING_INIT(&ctx->event_handler_list, event_handler_entry, link);
  115. APR_RING_INIT(&ctx->event_handler_list_free, event_handler_entry, link);
  116. ctx->event_list_cnt = ctx->event_list_free_cnt = 0;
  117. ctx->event_handler_list_cnt = ctx->event_handler_list_free_cnt = 0;
  118. ctx->engine = e;
  119. status = apr_thread_create(&ctx->thread_handle, NULL, &audiocontext_run, ctx, pool);
  120. if (status != APR_SUCCESS) {
  121. #ifdef _WIN32
  122. CloseHandle(ctx->sem);
  123. #else
  124. sem_destroy(ctx->sem);
  125. #endif // _WIN32
  126. apr_thread_mutex_destroy(ctx->mtx);
  127. return status;
  128. }
  129. *p_ctx = ctx;
  130. return APR_SUCCESS;
  131. }
  132. void audiocontext_destroy(audiocontext_t *ctx)
  133. {
  134. if (ctx && ctx->thread_handle) {
  135. apr_status_t s;
  136. ctx->bstop = 1;
  137. #ifdef _WIN32
  138. ReleaseSemaphore(ctx->sem, 2, NULL);
  139. #else
  140. sem_post(ctx->sem);
  141. #endif // _WIN32
  142. apr_thread_join(&s, ctx->thread_handle);
  143. ctx->thread_handle = 0;
  144. #ifdef _WIN32
  145. CloseHandle(ctx->sem);
  146. #else
  147. sem_destroy(ctx->sem);
  148. #endif // _WIN32
  149. {
  150. event_handler_entry *ep1, *ep2;
  151. APR_RING_FOREACH_SAFE(ep1, ep2, &ctx->event_handler_list, event_handler_entry, link) {
  152. APR_RING_REMOVE(ep1, link);
  153. free(ep1);
  154. }
  155. APR_RING_FOREACH_SAFE(ep1, ep2, &ctx->event_handler_list_free, event_handler_entry, link) {
  156. APR_RING_REMOVE(ep1, link);
  157. free(ep1);
  158. }
  159. }
  160. {
  161. event_entry *ep1, *ep2;
  162. APR_RING_FOREACH_SAFE(ep1, ep2, &ctx->event_list, event_entry, link) {
  163. APR_RING_REMOVE(ep1, link);
  164. free(ep1);
  165. }
  166. APR_RING_FOREACH_SAFE(ep1, ep2, &ctx->event_list_free, event_entry, link) {
  167. APR_RING_REMOVE(ep1, link);
  168. free(ep1);
  169. }
  170. }
  171. apr_thread_mutex_destroy(ctx->mtx);
  172. }
  173. }
  174. void audiocontext_signal(audiocontext_t *ctx)
  175. {
  176. #ifdef _WIN32
  177. ReleaseSemaphore(ctx->sem, 1, NULL);
  178. #else
  179. sem_post(ctx->sem);
  180. #endif // _WIN32
  181. }
  182. apr_status_t audiocontext_add_driver(audiocontext_t *ctx, audiodriver_t *drv)
  183. {
  184. if (!ctx || !drv)
  185. return APR_BADARG;
  186. apr_thread_mutex_lock(ctx->mtx);
  187. drv->ctx = ctx;
  188. APR_ARRAY_PUSH(ctx->arr_drv, audiodriver_t*) = drv;
  189. apr_thread_mutex_unlock(ctx->mtx);
  190. return APR_SUCCESS;
  191. }
  192. apr_status_t audiocontext_remove_driver(audiocontext_t *ctx, audiodriver_t *drv)
  193. {
  194. if (!ctx || !drv)
  195. return APR_BADARG;
  196. apr_thread_mutex_lock(ctx->mtx);
  197. {
  198. int i;
  199. for (i = 0; i < ctx->arr_drv->nelts; ++i) {
  200. audiodriver_t *d = APR_ARRAY_IDX(ctx->arr_drv, i, audiodriver_t*);
  201. if (d == drv) {
  202. drv->ctx = NULL;
  203. if (i < ctx->arr_drv->nelts-1) {
  204. audiodriver_t *tmp = APR_ARRAY_IDX(ctx->arr_drv, i, audiodriver_t*);
  205. APR_ARRAY_IDX(ctx->arr_drv, i, audiodriver_t*) = APR_ARRAY_IDX(ctx->arr_drv, ctx->arr_drv->nelts-1, audiodriver_t*);
  206. APR_ARRAY_IDX(ctx->arr_drv, ctx->arr_drv->nelts-1, audiodriver_t*) = tmp;
  207. }
  208. apr_array_pop(ctx->arr_drv);
  209. apr_thread_mutex_unlock(ctx->mtx);
  210. return APR_SUCCESS;
  211. }
  212. }
  213. }
  214. apr_thread_mutex_unlock(ctx->mtx);
  215. return APR_EGENERAL;
  216. }
  217. apr_status_t audiocontext_add_stream(audiocontext_t *ctx, audiostream_t *stream)
  218. {
  219. stream->ctx = ctx;
  220. return APR_SUCCESS;
  221. }
  222. apr_status_t audiocontext_remove_stream(audiocontext_t *ctx, audiostream_t *stream)
  223. {
  224. stream->ctx = NULL;
  225. return APR_SUCCESS;
  226. }
  227. apr_status_t audiocontext_lock(audiocontext_t *ctx)
  228. {
  229. return ctx ? apr_thread_mutex_lock(ctx->mtx) : APR_BADARG;
  230. }
  231. apr_status_t audiocontext_unlock(audiocontext_t *ctx)
  232. {
  233. return ctx ? apr_thread_mutex_unlock(ctx->mtx) : APR_BADARG;
  234. }
  235. static apr_status_t audiocontext_put_event(audiocontext_t *ctx, int bstream, void *stream_or_driver, int evt, int param1, int param2)
  236. {
  237. event_entry *e;
  238. if (!ctx || !stream_or_driver)
  239. return APR_EINVAL;
  240. apr_thread_mutex_lock(ctx->mtx);
  241. if (ctx->event_list_free_cnt == 0) {
  242. int i;
  243. for (i = 0; i < 10; ++i) {
  244. e = malloc(sizeof(event_entry));
  245. APR_RING_INSERT_TAIL(&ctx->event_list_free, e, event_entry, link);
  246. ctx->event_list_free_cnt ++;
  247. }
  248. }
  249. e = APR_RING_FIRST(&ctx->event_list_free);
  250. APR_RING_REMOVE(e, link);
  251. ctx->event_list_free_cnt --;
  252. e->stream_or_driver = stream_or_driver;
  253. e->evt = evt;
  254. e->bstream = bstream;
  255. e->param1 = param1;
  256. e->param2 = param2;
  257. APR_RING_INSERT_TAIL(&ctx->event_list, e, event_entry, link);
  258. ctx->event_list_cnt++;
  259. apr_thread_mutex_unlock(ctx->mtx);
  260. return APR_SUCCESS;
  261. }
  262. apr_status_t audiocontext_put_stream_event(audiocontext_t *ctx, audiostream_t *stream, int evt, int param1, int param2)
  263. {
  264. return audiocontext_put_event(ctx, TRUE, stream, evt, param1, param2);
  265. }
  266. apr_status_t audiocontext_put_driver_event(audiocontext_t *ctx, audiodriver_t *driver, int evt, int param1, int param2)
  267. {
  268. return audiocontext_put_event(ctx, FALSE, driver, evt, param1, param2);
  269. }
  270. apr_status_t audiocontext_add_event_handler(audiocontext_t *ctx, int priority, audiocontext_event_handler handler, void *user_data)
  271. {
  272. event_handler_entry *e, *ep;
  273. if (!ctx || !handler)
  274. return APR_EINVAL;
  275. apr_thread_mutex_lock(ctx->mtx);
  276. if (ctx->event_handler_list_free_cnt == 0) {
  277. int i;
  278. for (i = 0; i < 10; ++i) {
  279. e = malloc(sizeof(event_handler_entry));
  280. APR_RING_INSERT_TAIL(&ctx->event_handler_list_free, e, event_handler_entry, link);
  281. ctx->event_handler_list_free_cnt ++;
  282. }
  283. }
  284. e = APR_RING_FIRST(&ctx->event_handler_list_free);
  285. APR_RING_REMOVE(e, link);
  286. ctx->event_handler_list_free_cnt --;
  287. e->priority = priority;
  288. e->event_handler = handler;
  289. e->user_data = user_data;
  290. APR_RING_FOREACH(ep, &ctx->event_handler_list, event_handler_entry, link) {
  291. if (priority < ep->priority) {
  292. break;
  293. }
  294. }
  295. APR_RING_INSERT_BEFORE(ep, e, link);
  296. ctx->event_handler_list_cnt ++;
  297. apr_thread_mutex_unlock(ctx->mtx);
  298. return APR_SUCCESS;
  299. }
  300. apr_status_t audiocontext_remove_event_handler(audiocontext_t *ctx, audiocontext_event_handler handler)
  301. {
  302. event_handler_entry *ep;
  303. apr_thread_mutex_lock(ctx->mtx);
  304. APR_RING_FOREACH(ep, &ctx->event_handler_list, event_handler_entry, link) {
  305. if (ep->event_handler == handler) {
  306. APR_RING_REMOVE(ep, link);
  307. ctx->event_handler_list_cnt --;
  308. APR_RING_INSERT_TAIL(&ctx->event_handler_list_free, ep, event_handler_entry, link);
  309. ctx->event_handler_list_free_cnt ++;
  310. apr_thread_mutex_unlock(ctx->mtx);
  311. return APR_SUCCESS;
  312. }
  313. }
  314. apr_thread_mutex_unlock(ctx->mtx);
  315. return APR_NOTFOUND;
  316. }