audio_player.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /* Copyright (C) 2018 RDA Technologies Limited and/or its affiliates("RDA").
  2. * All rights reserved.
  3. *
  4. * This software is supplied "AS IS" without any warranties.
  5. * RDA assumes no responsibility or liability for the use of the software,
  6. * conveys no license or title under any patent, copyright, or mask work
  7. * right to the product. RDA reserves the right to make changes in the
  8. * software without notification. RDA also make no representation or
  9. * warranty that such application will be suitable for the specified use
  10. * without further testing or modification.
  11. */
  12. #ifndef _AUDIO_PLAYER_H_
  13. #define _AUDIO_PLAYER_H_
  14. #include "osi_compiler.h"
  15. #include "audio_types.h"
  16. #include "audio_decoder.h"
  17. OSI_EXTERN_C_BEGIN
  18. /**
  19. * \brief forward declaration
  20. */
  21. struct auReader;
  22. /**
  23. * \brief forward declaration
  24. */
  25. struct osiPipe;
  26. /**
  27. * \brief audio player status
  28. */
  29. typedef enum
  30. {
  31. AUPLAYER_STATUS_IDLE, ///< playback not started
  32. AUPLAYER_STATUS_PLAY, ///< playback is started
  33. AUPLAYER_STATUS_PAUSE, ///< playback is paused
  34. AUPLAYER_STATUS_FINISHED, ///< playback is finished
  35. } auPlayerStatus_t;
  36. /**
  37. * \brief audio player event
  38. *
  39. * There are no separated event for error. When fatal errors occur, audio
  40. * player will be stopped automatically.
  41. */
  42. typedef enum
  43. {
  44. AUPLAYER_EVENT_FINISHED = 1, ///< playback finished, or fatal error
  45. AUPLAYER_EVENT_PLAY_ERR = 2, ///< parse or play error
  46. } auPlayerEvent_t;
  47. /**
  48. * \brief opaque data struct for audio player
  49. */
  50. typedef struct auPlayer auPlayer_t;
  51. /**
  52. * \brief audio player callback prototype
  53. *
  54. * Audio player callback will be invoked in audio thread. <em>Don't call
  55. * audio player APIs, especially auPlayerDelete inside the callvack.</em>
  56. * Usually, it should just notify another thread to handle the event.
  57. *
  58. * \param param audio player callback context
  59. * \param event audio player event
  60. */
  61. typedef void (*auPlayerEventCallback_t)(void *param, auPlayerEvent_t event);
  62. /**
  63. * \brief create an audio player
  64. *
  65. * \return
  66. * - audio player
  67. * - NULL if out of memory
  68. */
  69. auPlayer_t *auPlayerCreate(void);
  70. /**
  71. * \brief delete the audio player
  72. *
  73. * It is recommended to call \p auPlayerStop before. In case playing
  74. * is started, it will try to stop playing in audio device.
  75. *
  76. * \param d the audio player
  77. */
  78. void auPlayerDelete(auPlayer_t *d);
  79. /**
  80. * \brief set audio player event callback
  81. *
  82. * If there are callback already registered, the previous one will be
  83. * replaced.
  84. *
  85. * \param d the audio player
  86. * \param cb event callback
  87. * \param cb_ctx event callback context
  88. */
  89. void auPlayerSetEventCallback(auPlayer_t *d, auPlayerEventCallback_t cb, void *cb_ctx);
  90. /**
  91. * \brief start audio player
  92. *
  93. * Before calling this, \p decoder should be ready for decoding audio
  94. * frame.
  95. *
  96. * \param d audio player instance
  97. * \param decoder decoder for the audio player
  98. * \return
  99. * - true on success
  100. * - false on failed
  101. */
  102. bool auPlayerStartDecoder(auPlayer_t *d, auDecoder_t *decoder);
  103. /**
  104. * \brief start audio player
  105. *
  106. * Before calling this, \p decoder should be ready for decoding audio
  107. * frame.
  108. *
  109. * \param d audio player instance
  110. * \param type audio playing type
  111. * \param decoder decoder for the audio player
  112. * \return
  113. * - true on success
  114. * - false on failed
  115. */
  116. bool auPlayerStartDecoderV2(auPlayer_t *d, audevPlayType_t type, auDecoder_t *decoder);
  117. /**
  118. * \brief start file play
  119. *
  120. * For simplicity, the stream format shall be explicitly specified.
  121. *
  122. * \param d the audio player
  123. * \param format stream format
  124. * \param params decoder parameters
  125. * \param fname file name to be played
  126. * \return
  127. * - true on player start success
  128. * - false on failed
  129. */
  130. bool auPlayerStartFile(auPlayer_t *d, auStreamFormat_t format,
  131. const auDecoderParamSet_t *params,
  132. const char *fname);
  133. /**
  134. * \brief start file play
  135. *
  136. * For simplicity, the stream format shall be explicitly specified.
  137. *
  138. * \param d the audio player
  139. * \param type audio playing type
  140. * \param format stream format
  141. * \param params audio deoder parameters
  142. * \param fname file name to be played
  143. * \return
  144. * - true on player start success
  145. * - false on failed
  146. */
  147. bool auPlayerStartFileV2(auPlayer_t *d, audevPlayType_t type, auStreamFormat_t format,
  148. const auDecoderParamSet_t *params,
  149. const char *fname);
  150. /**
  151. * \brief start memory play
  152. *
  153. * Before player start, the whole stream should be located in memory. And
  154. * the content shouldn't be changed during playback.
  155. *
  156. * \param d the audio player
  157. * \param format stream format
  158. * \param params audio decoder parameters
  159. * \param buf memory to be played
  160. * \param size memory size
  161. * \return
  162. * - true on player start success
  163. * - false on failed
  164. */
  165. bool auPlayerStartMem(auPlayer_t *d, auStreamFormat_t format,
  166. const auDecoderParamSet_t *params,
  167. const void *buf, unsigned size);
  168. /**
  169. * \brief start memory play
  170. *
  171. * Before player start, the whole stream should be located in memory. And
  172. * the content shouldn't be changed during playback.
  173. *
  174. * \param d the audio player
  175. * \param type audio playing type
  176. * \param format stream format
  177. * \param params audio decoder parameters
  178. * \param buf memory to be played
  179. * \param size memory size
  180. * \return
  181. * - true on player start success
  182. * - false on failed
  183. */
  184. bool auPlayerStartMemV2(auPlayer_t *d, audevPlayType_t type, auStreamFormat_t format,
  185. const auDecoderParamSet_t *params,
  186. const void *buf, unsigned size);
  187. /**
  188. * \brief start pipe play
  189. *
  190. * \param d the audio player
  191. * \param format stream format
  192. * \param params audio decoder parameters
  193. * \param pipe the pipe to be read from
  194. * \return
  195. * - true on player start success
  196. * - false on failed
  197. */
  198. bool auPlayerStartPipe(auPlayer_t *d, auStreamFormat_t format,
  199. const auDecoderParamSet_t *params,
  200. struct osiPipe *pipe);
  201. /**
  202. * \brief start pipe play
  203. *
  204. * \param d the audio player
  205. * \param type audio playing type
  206. * \param format stream format
  207. * \param params audio decoder parameters
  208. * \param pipe the pipe to be read from
  209. * \return
  210. * - true on player start success
  211. * - false on failed
  212. */
  213. bool auPlayerStartPipeV2(auPlayer_t *d, audevPlayType_t type, auStreamFormat_t format,
  214. const auDecoderParamSet_t *params,
  215. struct osiPipe *pipe);
  216. /**
  217. * \brief start play from an external audio reader
  218. *
  219. * It is used for playback from neither file, nor memory. The procedure to
  220. * get stream data is implemented externally.
  221. *
  222. * Player will just use \p reader, and the life-cycle should be handled
  223. * eternally.
  224. *
  225. * \param d the audio player
  226. * \param format stream format
  227. * \param params audio decoder parameters
  228. * \param reader audio stream reader
  229. * \return
  230. * - true on player start success
  231. * - false on failed
  232. */
  233. bool auPlayerStartReader(auPlayer_t *d, auStreamFormat_t format,
  234. const auDecoderParamSet_t *params,
  235. struct auReader *reader);
  236. /**
  237. * \brief start play from an external audio reader
  238. *
  239. * It is used for playback from neither file, nor memory. The procedure to
  240. * get stream data is implemented externally.
  241. *
  242. * Player will just use \p reader, and the life-cycle should be handled
  243. * eternally.
  244. *
  245. * \param d the audio player
  246. * \param type audio playing type
  247. * \param format stream format
  248. * \param params audio decoder parameters
  249. * \param reader audio stream reader
  250. * \return
  251. * - true on player start success
  252. * - false on failed
  253. */
  254. bool auPlayerStartReaderV2(auPlayer_t *d, audevPlayType_t type, auStreamFormat_t format,
  255. const auDecoderParamSet_t *params,
  256. struct auReader *reader);
  257. /**
  258. * \brief playback pause
  259. *
  260. * At pause, the audio output device will be releases, and the audio device
  261. * can be used for other purpose.
  262. *
  263. * \param d the audio player
  264. * \return
  265. * - true on success
  266. * - false on invalid parameter
  267. */
  268. bool auPlayerPause(auPlayer_t *d);
  269. /**
  270. * \brief playback resume
  271. *
  272. * \param d the audio player
  273. * \return
  274. * - true on success
  275. * - false on invalid parameter
  276. */
  277. bool auPlayerResume(auPlayer_t *d);
  278. /**
  279. * \brief playback stop
  280. *
  281. * After stop, internal resources of audio player may be freed.
  282. *
  283. * \param d the audio player
  284. * \return
  285. * - true on success
  286. * - false on invalid parameter
  287. */
  288. bool auPlayerStop(auPlayer_t *d);
  289. /**
  290. * \brief wait playback finish
  291. *
  292. * \p timeout can be \p OSI_WAIT_FOREVER for wait without timeout.
  293. * If audio player is already in \p AUPLAYER_STATUS_IDLE status, it will
  294. * return immediately.
  295. *
  296. * Pay attention to call this in \p AUPLAYER_STATUS_PAUSE status. It may
  297. * cause caller to wait forever (or timeout), if there are no other thread
  298. * to call \p auPlayerResume.
  299. *
  300. * \param d the audio player
  301. * \param timeout wait timeout
  302. * \return
  303. * - true on playback finished
  304. * - false on timeout
  305. */
  306. bool auPlayerWaitFinish(auPlayer_t *d, unsigned timeout);
  307. /**
  308. * \brief playback elapsed time in milliseconds
  309. *
  310. * When player is in \p AUPLAYER_STATUS_IDLE status, it will return 0.
  311. *
  312. * \param d the audio player
  313. * \return
  314. * - playback elapsed time in milliseconds
  315. * - 0 on error
  316. */
  317. unsigned auPlayerPlayTime(auPlayer_t *d);
  318. /**
  319. * \brief get audio player playing type
  320. *
  321. * When audio player is not started, return \p AUDEV_PLAY_TYPE_NONE.
  322. *
  323. * \param d audio player instance
  324. * \return
  325. * - playing type
  326. */
  327. audevPlayType_t auPlayerGetType(auPlayer_t *d);
  328. /**
  329. * \brief check whether player is running
  330. *
  331. * Player will be paused implicitly. The typical case is: when there is a MT
  332. * call, active player will be paused implicitly at accepting the MT call.
  333. *
  334. * \return
  335. * - true if player is running
  336. * - false on otherwise
  337. */
  338. auPlayerStatus_t auPlayerGetStatus(auPlayer_t *d);
  339. /**
  340. * \brief get the audio decoder in using
  341. *
  342. * When audio player is not started, it may return NULL.
  343. *
  344. * \param d the audio player
  345. * \return
  346. * - audio decoder handler
  347. * - NULL if audio player not started
  348. */
  349. auDecoder_t *auPlayerGetDecoder(auPlayer_t *d);
  350. /**
  351. * \brief get the audio reader in using
  352. *
  353. * When audio player is not started, or audio reader is not managed
  354. * by audio player, it may return NULL.
  355. *
  356. * \param d the audio player
  357. * \return
  358. * - audio reader handler
  359. * - NULL if audio player not started, or not managed
  360. */
  361. struct auReader *auPlayerGetReader(auPlayer_t *d);
  362. bool auPlaySbcConfig(auPlayer_t *d, struct osiPipe *pipe, const void *conf, size_t conf_len);
  363. OSI_EXTERN_C_END
  364. #endif