audio_recorder.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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_RECORDER_H_
  13. #define _AUDIO_RECORDER_H_
  14. #include "osi_compiler.h"
  15. #include "audio_types.h"
  16. #include "audio_encoder.h"
  17. OSI_EXTERN_C_BEGIN
  18. /**
  19. * \brief forward declaration
  20. */
  21. struct auWriter;
  22. /**
  23. * \brief forward declaration
  24. */
  25. struct osiPipe;
  26. /**
  27. * \brief audio recorder status
  28. */
  29. typedef enum
  30. {
  31. AURECORDER_STATUS_IDLE, ///< recorder not started
  32. AURECORDER_STATUS_RECORD, ///< recorder is started
  33. AURECORDER_STATUS_PAUSE, ///< recorder is paused
  34. AURECORDER_STATUS_FINISHED, ///< recorder is finished
  35. } auRecorderStatus_t;
  36. /**
  37. * \brief audio recorder event
  38. *
  39. * There are no separated event for error. When fatal errors occur, audio
  40. * recorder will be stopped automatically.
  41. */
  42. typedef enum
  43. {
  44. AURECORDER_EVENT_FINISHED = 1, ///< voice call finished, or fatal error
  45. } auRecorderEvent_t;
  46. /**
  47. * \brief opaque data struct of audio recorder
  48. */
  49. typedef struct auRecorder auRecorder_t;
  50. /**
  51. * \brief audio recorder callback prototype
  52. *
  53. * Audio recorder callback will be invoked in audio thread. <em>Don't call
  54. * audio recorder APIs, especially auRecorderDelete inside the callvack.</em>
  55. * Usually, it should just notify another thread to handle the event.
  56. *
  57. * \param param audio recorder callback context
  58. * \param event audio recorder event
  59. */
  60. typedef void (*auRecorderEventCallback_t)(void *param, auRecorderEvent_t event);
  61. /**
  62. * \brief create an audio recorder
  63. *
  64. * \return
  65. * - created audio recorder
  66. * - NULL if out of memory
  67. */
  68. auRecorder_t *auRecorderCreate(void);
  69. /**
  70. * \brief delete the audio recorder
  71. *
  72. * It is recommended to call \p auRecorderStop before. In case recording
  73. * is started, it will try to stop recording in audio device.
  74. *
  75. * \param d audio recoder instance
  76. */
  77. void auRecorderDelete(auRecorder_t *d);
  78. /**
  79. * \brief set audio recorder event callback
  80. *
  81. * If there are callback already registered, the previous one will be
  82. * replaced.
  83. *
  84. * \param d the audio recorder
  85. * \param cb event callback
  86. * \param cb_ctx event callback context
  87. */
  88. void auRecorderSetEventCallback(auRecorder_t *d, auRecorderEventCallback_t cb, void *cb_ctx);
  89. /**
  90. * \brief get audio recorder recording type
  91. *
  92. * When audio player is not started, return \p AUDEV_RECORD_TYPE_NONE.
  93. *
  94. * \param d audio recoder instance
  95. * \return
  96. * - recording type
  97. */
  98. audevRecordType_t auRecorderGetType(auRecorder_t *d);
  99. /**
  100. * \brief check whether recorder is running
  101. *
  102. * Recorder will be paused implicitly.
  103. *
  104. * \return
  105. * - 0 if recorder is stop
  106. * - 1 if recorder is running
  107. * - 2 if recorder is pause
  108. * - 3 if recorder is finish
  109. */
  110. auRecorderStatus_t auRecorderGetStatus(auRecorder_t *d);
  111. /**
  112. * \brief start audio recoder
  113. *
  114. * Before calling this, \p encoder should be ready for encoding audio
  115. * frame.
  116. *
  117. * Audio recoder will call \p auEncoderEncode inside the audio device
  118. * thread. It should be fast enough. When it takes too long, it is
  119. * possible lose input audio samples, and even worse, may trigger
  120. * assert. The audio writer should be considered also. For example,
  121. * when encoded data will be write to SFFS (which is very slow), it
  122. * is suggest to use \p auPipeWriter_t.
  123. *
  124. * \param d audio recorder instance
  125. * \param type audio recording type
  126. * \param encoder encoder for the audio recorder
  127. * \return
  128. * - true on success
  129. * - false on failed
  130. */
  131. bool auRecorderStartEncoder(auRecorder_t *d, audevRecordType_t type, auEncoder_t *encoder);
  132. /**
  133. * \brief start audio recoder, and write to file
  134. *
  135. * Inside, recorder will manage encoder and writer. And it is provided
  136. * for simpler usage.
  137. *
  138. * The write performace of file system on NOR flash may be not enough for
  139. * audio recorder, especially for PCM or WAV. It will cause underflow,
  140. * and even assert. So, don't write recorded PCM to NOR flash file system
  141. * directly.
  142. *
  143. * When \p params is not NULL, it should be an array. And the \p id of
  144. * the last element must be zero to indicate the end.
  145. *
  146. * \param d audio recorder instance
  147. * \param type audio recording type
  148. * \param format recorded stream format
  149. * \param params encoder parameters
  150. * \param fname file name
  151. * \return
  152. * - true on success
  153. * - false on failed
  154. */
  155. bool auRecorderStartFile(auRecorder_t *d, audevRecordType_t type, auStreamFormat_t format,
  156. const auEncoderParamSet_t *params, const char *fname);
  157. /**
  158. * \brief start audio recoder, and write to pipe
  159. *
  160. * Inside, recorder will manage encoder and writer. And it is provided
  161. * for simpler usage.
  162. *
  163. * When \p params is not NULL, it should be an array. And the \p id of
  164. * the last element must be zero to indicate the end.
  165. *
  166. * \param d audio recorder instance
  167. * \param type audio recording type
  168. * \param format recorded stream format
  169. * \param params encoder parameters
  170. * \param pipe pipe for audio pipe write
  171. * \return
  172. * - true on success
  173. * - false on failed
  174. */
  175. bool auRecorderStartPipe(auRecorder_t *d, audevRecordType_t type, auStreamFormat_t format,
  176. const auEncoderParamSet_t *params, struct osiPipe *pipe);
  177. /**
  178. * \brief start audio recoder, and write to memory
  179. *
  180. * Inside, recorder will manage encoder and writer. And it is provided
  181. * for simpler usage.
  182. *
  183. * When \p params is not NULL, it should be an array. And the \p id of
  184. * the last element must be zero to indicate the end.
  185. *
  186. * \param d audio recorder instance
  187. * \param type audio recording type
  188. * \param format recorded stream format
  189. * \param params encoder parameters
  190. * \param max_size maximum size for audio memory write
  191. * \return
  192. * - true on success
  193. * - false on failed
  194. */
  195. bool auRecorderStartMem(auRecorder_t *d, audevRecordType_t type, auStreamFormat_t format,
  196. const auEncoderParamSet_t *params, unsigned max_size);
  197. /**
  198. * \brief start audio recoder, and write to external writer
  199. *
  200. * Inside, recorder will manage encoder. Writer should be managed externally,
  201. * It is provided for simpler usage.
  202. *
  203. * When \p params is not NULL, it should be an array. And the \p id of
  204. * the last element must be zero to indicate the end.
  205. *
  206. * \param d audio recorder instance
  207. * \param type audio recording type
  208. * \param format recorded stream format
  209. * \param params encoder parameters
  210. * \param writer external writer to be used
  211. * \return
  212. * - true on success
  213. * - false on failed
  214. */
  215. bool auRecorderStartWriter(auRecorder_t *d, audevRecordType_t type, auStreamFormat_t format,
  216. const auEncoderParamSet_t *params, struct auWriter *writer);
  217. /**
  218. * \brief audio recorder pause
  219. *
  220. * At pause, the audio input device will be releases, and the audio device
  221. * can be used for other purpose.
  222. *
  223. * \param d the audio recorder
  224. * \return
  225. * - true on success
  226. * - false on invalid parameter
  227. */
  228. bool auRecorderPause(auRecorder_t *d);
  229. /**
  230. * \brief audio recorder resume
  231. *
  232. * \param d the audio recorder
  233. * \return
  234. * - true on success
  235. * - false on invalid parameter
  236. */
  237. bool auRecorderResume(auRecorder_t *d);
  238. /**
  239. * \brief stop audio recorder
  240. *
  241. * After stop, internal resources of audio recorder may be freed.
  242. *
  243. * \param d audio recorder instance
  244. * \return
  245. * - true on success
  246. * - false on invalid parameter
  247. */
  248. bool auRecorderStop(auRecorder_t *d);
  249. /**
  250. * \brief get the audio encoder in using
  251. *
  252. * When audio recorder is not started, it may return NULL.
  253. *
  254. * \param d audio recorder instance
  255. * \return
  256. * - audio encoder handler
  257. * - NULL if audio recorder not started
  258. */
  259. auEncoder_t *auRecorderGetEncoder(auRecorder_t *d);
  260. /**
  261. * \brief get the audio writer in using
  262. *
  263. * When audio recorder is not started, or audio writer is not managed
  264. * by audio recorder, it may return NULL.
  265. *
  266. * \param d audio recorder instance
  267. * \return
  268. * - audio writer handler
  269. * - NULL if audio recorder not started, or not managed
  270. */
  271. struct auWriter *auRecorderGetWriter(auRecorder_t *d);
  272. /**
  273. * \brief fix wav header by file name
  274. *
  275. * In the wav file header, there are information about the data size.
  276. * In some cases, such as recording with pipe, seek is not supported.
  277. * So, the header of wav file is incorrect. This will try to fix the
  278. * header size information.
  279. *
  280. * It will check the wav header, it will change the data size base on
  281. * file size. And it requires there are only RIFF and DAT in the
  282. * beginning, and assuming all of the rest are data.
  283. *
  284. * \param fname file name to be fixed
  285. * \return
  286. * - true on success
  287. * - false on fail, RIFF and DAT check fail
  288. */
  289. bool auFixWavHeaderByName(const char *fname);
  290. /**
  291. * \brief fix wav header by file descriptor
  292. *
  293. * It is the same as \p auFixWavHeaderByName, just the parameter is file
  294. * descriptor rather than file name.
  295. *
  296. * \param fd file descriptor to be fixed
  297. * \return
  298. * - true on success
  299. * - false on fail, RIFF and DAT check fail
  300. */
  301. bool auFixWavHeaderByDesc(int fd);
  302. OSI_EXTERN_C_END
  303. #endif