legacy_dev.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <pjmedia-audiodev/audiodev_imp.h>
  19. #include <pjmedia/sound.h>
  20. #include <pj/assert.h>
  21. #if PJMEDIA_AUDIO_DEV_HAS_LEGACY_DEVICE
  22. #define THIS_FILE "legacy_dev.c"
  23. /* Legacy devices factory */
  24. struct legacy_factory
  25. {
  26. pjmedia_aud_dev_factory base;
  27. pj_pool_t *pool;
  28. pj_pool_factory *pf;
  29. };
  30. struct legacy_stream
  31. {
  32. pjmedia_aud_stream base;
  33. pj_pool_t *pool;
  34. pjmedia_aud_param param;
  35. pjmedia_snd_stream *snd_strm;
  36. pjmedia_aud_play_cb user_play_cb;
  37. pjmedia_aud_rec_cb user_rec_cb;
  38. void *user_user_data;
  39. unsigned input_latency;
  40. unsigned output_latency;
  41. };
  42. /* Prototypes */
  43. static pj_status_t factory_init(pjmedia_aud_dev_factory *f);
  44. static pj_status_t factory_destroy(pjmedia_aud_dev_factory *f);
  45. static pj_status_t factory_refresh(pjmedia_aud_dev_factory *f);
  46. static unsigned factory_get_dev_count(pjmedia_aud_dev_factory *f);
  47. static pj_status_t factory_get_dev_info(pjmedia_aud_dev_factory *f,
  48. unsigned index,
  49. pjmedia_aud_dev_info *info);
  50. static pj_status_t factory_default_param(pjmedia_aud_dev_factory *f,
  51. unsigned index,
  52. pjmedia_aud_param *param);
  53. static pj_status_t factory_create_stream(pjmedia_aud_dev_factory *f,
  54. const pjmedia_aud_param *param,
  55. pjmedia_aud_rec_cb rec_cb,
  56. pjmedia_aud_play_cb play_cb,
  57. void *user_data,
  58. pjmedia_aud_stream **p_aud_strm);
  59. static pj_status_t stream_get_param(pjmedia_aud_stream *strm,
  60. pjmedia_aud_param *param);
  61. static pj_status_t stream_get_cap(pjmedia_aud_stream *strm,
  62. pjmedia_aud_dev_cap cap,
  63. void *value);
  64. static pj_status_t stream_set_cap(pjmedia_aud_stream *strm,
  65. pjmedia_aud_dev_cap cap,
  66. const void *value);
  67. static pj_status_t stream_start(pjmedia_aud_stream *strm);
  68. static pj_status_t stream_stop(pjmedia_aud_stream *strm);
  69. static pj_status_t stream_destroy(pjmedia_aud_stream *strm);
  70. /* Operations */
  71. static pjmedia_aud_dev_factory_op factory_op =
  72. {
  73. &factory_init,
  74. &factory_destroy,
  75. &factory_get_dev_count,
  76. &factory_get_dev_info,
  77. &factory_default_param,
  78. &factory_create_stream,
  79. &factory_refresh
  80. };
  81. static pjmedia_aud_stream_op stream_op =
  82. {
  83. &stream_get_param,
  84. &stream_get_cap,
  85. &stream_set_cap,
  86. &stream_start,
  87. &stream_stop,
  88. &stream_destroy
  89. };
  90. /****************************************************************************
  91. * Factory operations
  92. */
  93. /*
  94. * Init legacy audio driver.
  95. */
  96. pjmedia_aud_dev_factory* pjmedia_legacy_factory(pj_pool_factory *pf)
  97. {
  98. struct legacy_factory *f;
  99. pj_pool_t *pool;
  100. pool = pj_pool_create(pf, "legacy-snd", 512, 512, NULL);
  101. f = PJ_POOL_ZALLOC_T(pool, struct legacy_factory);
  102. f->pf = pf;
  103. f->pool = pool;
  104. f->base.op = &factory_op;
  105. return &f->base;
  106. }
  107. /* API: init factory */
  108. static pj_status_t factory_init(pjmedia_aud_dev_factory *f)
  109. {
  110. struct legacy_factory *wf = (struct legacy_factory*)f;
  111. return pjmedia_snd_init(wf->pf);
  112. }
  113. /* API: destroy factory */
  114. static pj_status_t factory_destroy(pjmedia_aud_dev_factory *f)
  115. {
  116. struct legacy_factory *wf = (struct legacy_factory*)f;
  117. pj_status_t status;
  118. status = pjmedia_snd_deinit();
  119. if (status == PJ_SUCCESS) {
  120. pj_pool_safe_release(&wf->pool);
  121. }
  122. return status;
  123. }
  124. /* API: refresh the list of devices */
  125. static pj_status_t factory_refresh(pjmedia_aud_dev_factory *f)
  126. {
  127. PJ_UNUSED_ARG(f);
  128. return PJ_ENOTSUP;
  129. }
  130. /* API: get number of devices */
  131. static unsigned factory_get_dev_count(pjmedia_aud_dev_factory *f)
  132. {
  133. PJ_UNUSED_ARG(f);
  134. return pjmedia_snd_get_dev_count();
  135. }
  136. /* API: get device info */
  137. static pj_status_t factory_get_dev_info(pjmedia_aud_dev_factory *f,
  138. unsigned index,
  139. pjmedia_aud_dev_info *info)
  140. {
  141. const pjmedia_snd_dev_info *si =
  142. pjmedia_snd_get_dev_info(index);;
  143. PJ_UNUSED_ARG(f);
  144. if (si == NULL)
  145. return PJMEDIA_EAUD_INVDEV;
  146. pj_bzero(info, sizeof(*info));
  147. pj_ansi_strxcpy(info->name, si->name, sizeof(info->name));
  148. info->input_count = si->input_count;
  149. info->output_count = si->output_count;
  150. info->default_samples_per_sec = si->default_samples_per_sec;
  151. pj_ansi_strxcpy(info->driver, "legacy", sizeof(info->driver));
  152. info->caps = PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY |
  153. PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY;
  154. return PJ_SUCCESS;
  155. }
  156. /* API: create default device parameter */
  157. static pj_status_t factory_default_param(pjmedia_aud_dev_factory *f,
  158. unsigned index,
  159. pjmedia_aud_param *param)
  160. {
  161. pjmedia_aud_dev_info di;
  162. pj_status_t status;
  163. status = factory_get_dev_info(f, index, &di);
  164. if (status != PJ_SUCCESS)
  165. return status;
  166. pj_bzero(param, sizeof(*param));
  167. if (di.input_count && di.output_count) {
  168. param->dir = PJMEDIA_DIR_CAPTURE_PLAYBACK;
  169. param->rec_id = index;
  170. param->play_id = index;
  171. } else if (di.input_count) {
  172. param->dir = PJMEDIA_DIR_CAPTURE;
  173. param->rec_id = index;
  174. param->play_id = PJMEDIA_AUD_INVALID_DEV;
  175. } else if (di.output_count) {
  176. param->dir = PJMEDIA_DIR_PLAYBACK;
  177. param->play_id = index;
  178. param->rec_id = PJMEDIA_AUD_INVALID_DEV;
  179. } else {
  180. return PJMEDIA_EAUD_INVDEV;
  181. }
  182. param->clock_rate = di.default_samples_per_sec;
  183. param->channel_count = 1;
  184. param->samples_per_frame = di.default_samples_per_sec * 20 / 1000;
  185. param->bits_per_sample = 16;
  186. param->flags = di.caps;
  187. param->input_latency_ms = PJMEDIA_SND_DEFAULT_REC_LATENCY;
  188. param->output_latency_ms = PJMEDIA_SND_DEFAULT_PLAY_LATENCY;
  189. return PJ_SUCCESS;
  190. }
  191. /* Callback from legacy sound device */
  192. static pj_status_t snd_play_cb(/* in */ void *user_data,
  193. /* in */ pj_uint32_t timestamp,
  194. /* out */ void *output,
  195. /* out */ unsigned size)
  196. {
  197. struct legacy_stream *strm = (struct legacy_stream*)user_data;
  198. pjmedia_frame frame;
  199. pj_status_t status;
  200. frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
  201. frame.buf = output;
  202. frame.size = size;
  203. frame.timestamp.u64 = timestamp;
  204. status = strm->user_play_cb(strm->user_user_data, &frame);
  205. if (status != PJ_SUCCESS)
  206. return status;
  207. if (frame.type != PJMEDIA_FRAME_TYPE_AUDIO) {
  208. pj_bzero(output, size);
  209. }
  210. return PJ_SUCCESS;
  211. }
  212. /* Callback from legacy sound device */
  213. static pj_status_t snd_rec_cb( /* in */ void *user_data,
  214. /* in */ pj_uint32_t timestamp,
  215. /* in */ void *input,
  216. /* in*/ unsigned size)
  217. {
  218. struct legacy_stream *strm = (struct legacy_stream*)user_data;
  219. pjmedia_frame frame;
  220. frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
  221. frame.buf = input;
  222. frame.size = size;
  223. frame.timestamp.u64 = timestamp;
  224. return strm->user_rec_cb(strm->user_user_data, &frame);
  225. }
  226. /* API: create stream */
  227. static pj_status_t factory_create_stream(pjmedia_aud_dev_factory *f,
  228. const pjmedia_aud_param *param,
  229. pjmedia_aud_rec_cb rec_cb,
  230. pjmedia_aud_play_cb play_cb,
  231. void *user_data,
  232. pjmedia_aud_stream **p_aud_strm)
  233. {
  234. struct legacy_factory *wf = (struct legacy_factory*)f;
  235. pj_pool_t *pool;
  236. struct legacy_stream *strm;
  237. pj_status_t status;
  238. /* Initialize our stream data */
  239. pool = pj_pool_create(wf->pf, "legacy-snd", 512, 512, NULL);
  240. strm = PJ_POOL_ZALLOC_T(pool, struct legacy_stream);
  241. strm->pool = pool;
  242. strm->user_rec_cb = rec_cb;
  243. strm->user_play_cb = play_cb;
  244. strm->user_user_data = user_data;
  245. pj_memcpy(&strm->param, param, sizeof(*param));
  246. /* Set the latency if wanted */
  247. if (param->dir==PJMEDIA_DIR_CAPTURE_PLAYBACK &&
  248. param->flags & (PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY |
  249. PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY))
  250. {
  251. PJ_ASSERT_RETURN(param->input_latency_ms &&
  252. param->output_latency_ms,
  253. PJMEDIA_EAUD_BADLATENCY);
  254. strm->input_latency = param->input_latency_ms;
  255. strm->output_latency = param->output_latency_ms;
  256. status = pjmedia_snd_set_latency(param->input_latency_ms,
  257. param->output_latency_ms);
  258. if (status != PJ_SUCCESS) {
  259. pj_pool_release(pool);
  260. return status;
  261. }
  262. }
  263. /* Open the stream */
  264. if (param->dir == PJMEDIA_DIR_CAPTURE) {
  265. status = pjmedia_snd_open_rec(param->rec_id,
  266. param->clock_rate,
  267. param->channel_count,
  268. param->samples_per_frame,
  269. param->bits_per_sample,
  270. &snd_rec_cb,
  271. strm,
  272. &strm->snd_strm);
  273. } else if (param->dir == PJMEDIA_DIR_PLAYBACK) {
  274. status = pjmedia_snd_open_player(param->play_id,
  275. param->clock_rate,
  276. param->channel_count,
  277. param->samples_per_frame,
  278. param->bits_per_sample,
  279. &snd_play_cb,
  280. strm,
  281. &strm->snd_strm);
  282. } else if (param->dir == PJMEDIA_DIR_CAPTURE_PLAYBACK) {
  283. status = pjmedia_snd_open(param->rec_id,
  284. param->play_id,
  285. param->clock_rate,
  286. param->channel_count,
  287. param->samples_per_frame,
  288. param->bits_per_sample,
  289. &snd_rec_cb,
  290. &snd_play_cb,
  291. strm,
  292. &strm->snd_strm);
  293. } else {
  294. pj_assert(!"Invalid direction!");
  295. return PJ_EINVAL;
  296. }
  297. if (status != PJ_SUCCESS) {
  298. pj_pool_release(pool);
  299. return status;
  300. }
  301. *p_aud_strm = &strm->base;
  302. return PJ_SUCCESS;
  303. }
  304. /* API: Get stream info. */
  305. static pj_status_t stream_get_param(pjmedia_aud_stream *s,
  306. pjmedia_aud_param *pi)
  307. {
  308. struct legacy_stream *strm = (struct legacy_stream*)s;
  309. PJ_ASSERT_RETURN(strm && pi, PJ_EINVAL);
  310. pj_memcpy(pi, &strm->param, sizeof(*pi));
  311. if (strm->input_latency) {
  312. pi->flags |= PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY;
  313. pi->input_latency_ms = strm->input_latency;
  314. } else {
  315. pi->flags &= ~PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY;
  316. }
  317. if (strm->output_latency) {
  318. pi->flags |= PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY;
  319. pi->output_latency_ms = strm->output_latency;
  320. } else {
  321. pi->flags &= ~PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY;
  322. }
  323. return PJ_SUCCESS;
  324. }
  325. /* API: get capability */
  326. static pj_status_t stream_get_cap(pjmedia_aud_stream *s,
  327. pjmedia_aud_dev_cap cap,
  328. void *pval)
  329. {
  330. struct legacy_stream *strm = (struct legacy_stream*)s;
  331. PJ_ASSERT_RETURN(strm && pval, PJ_EINVAL);
  332. if (cap==PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY &&
  333. (strm->param.dir & PJMEDIA_DIR_CAPTURE))
  334. {
  335. /* Recording latency */
  336. if (strm->input_latency) {
  337. *(unsigned*)pval = strm->input_latency;
  338. return PJ_SUCCESS;
  339. } else {
  340. return PJMEDIA_EAUD_INVCAP;
  341. }
  342. } else if (cap==PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY &&
  343. (strm->param.dir & PJMEDIA_DIR_PLAYBACK))
  344. {
  345. /* Playback latency */
  346. if (strm->output_latency) {
  347. *(unsigned*)pval = strm->output_latency;
  348. return PJ_SUCCESS;
  349. } else {
  350. return PJMEDIA_EAUD_INVCAP;
  351. }
  352. } else {
  353. return PJMEDIA_EAUD_INVCAP;
  354. }
  355. }
  356. /* API: set capability */
  357. static pj_status_t stream_set_cap(pjmedia_aud_stream *s,
  358. pjmedia_aud_dev_cap cap,
  359. const void *pval)
  360. {
  361. PJ_UNUSED_ARG(s);
  362. PJ_UNUSED_ARG(cap);
  363. PJ_UNUSED_ARG(pval);
  364. return PJMEDIA_EAUD_INVCAP;
  365. }
  366. /* API: Start stream. */
  367. static pj_status_t stream_start(pjmedia_aud_stream *s)
  368. {
  369. struct legacy_stream *strm = (struct legacy_stream*)s;
  370. return pjmedia_snd_stream_start(strm->snd_strm);
  371. }
  372. /* API: Stop stream. */
  373. static pj_status_t stream_stop(pjmedia_aud_stream *s)
  374. {
  375. struct legacy_stream *strm = (struct legacy_stream*)s;
  376. return pjmedia_snd_stream_stop(strm->snd_strm);
  377. }
  378. /* API: Destroy stream. */
  379. static pj_status_t stream_destroy(pjmedia_aud_stream *s)
  380. {
  381. struct legacy_stream *strm = (struct legacy_stream*)s;
  382. pj_status_t status;
  383. status = pjmedia_snd_stream_close(strm->snd_strm);
  384. if (status == PJ_SUCCESS) {
  385. pj_pool_safe_release(&strm->pool);
  386. }
  387. return status;
  388. }
  389. #endif /* PJMEDIA_AUDIO_DEV_HAS_LEGACY_DEVICE */