audio_reader.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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_READER_H_
  13. #define _AUDIO_READER_H_
  14. #include "osi_compiler.h"
  15. OSI_EXTERN_C_BEGIN
  16. /**
  17. * \brief forward declaration
  18. */
  19. struct auReader;
  20. struct osiPipe;
  21. /**
  22. * \brief audio reader operations
  23. */
  24. typedef struct
  25. {
  26. /** delete the audio reader */
  27. void (*destroy)(struct auReader *d);
  28. /** refer to \p auReaderRead */
  29. int (*read)(struct auReader *d, void *buf, unsigned size);
  30. /** refer to \p auReaderSeek */
  31. int (*seek)(struct auReader *d, int offset, int whence);
  32. /** refer to \p auReaderIsEof */
  33. bool (*is_eof)(struct auReader *d);
  34. } auReaderOps_t;
  35. /**
  36. * \brief audio reader base
  37. *
  38. * Audio reader is an abstraction of file. The basic operation is to read
  39. * from the reader. And the reader doen't care about the content.
  40. *
  41. * When implement an audio reader, the first field should be
  42. * <tt>auReaderOps_t</tt>.
  43. */
  44. typedef struct auReader
  45. {
  46. auReaderOps_t ops; ///< audio reader operations
  47. } auReader_t;
  48. /**
  49. * \brief delete the audio reader
  50. *
  51. * When \p d is NULL, nothing will be done.
  52. *
  53. * \param d audio reader
  54. */
  55. void auReaderDelete(auReader_t *d);
  56. /**
  57. * \brief read data from audio reader
  58. *
  59. * When the reader reaches end of file, it will return 0.
  60. *
  61. * \param d audio reader
  62. * \param buf buf to be fetched, can't be NULL if \p size is not 0.
  63. * \param size buf size
  64. * \return
  65. * - the size fetched from audio reader, which may be less than \p size.
  66. * - -1 for invalid parameters, or file read error.
  67. */
  68. int auReaderRead(auReader_t *d, void *buf, unsigned size);
  69. /**
  70. * \brief read data from audio reader, at specified offset
  71. *
  72. * This is the same as \p auReaderSeek followed by \p auReaderRead.
  73. *
  74. * \param d audio reader
  75. * \param offset specified offset
  76. * \param buf buf to be fetched, can't be NULL if \p size is not 0.
  77. * \param size buf size
  78. * \return
  79. * - the size fetched from audio reader, which may be less than \p size.
  80. * - -1 for invalid parameters, or file read error.
  81. */
  82. int auReaderReadAt(auReader_t *d, int offset, void *buf, unsigned size);
  83. /**
  84. * \brief whether the audio reader support seek
  85. *
  86. * When \p seek function pointer is NULL, it is not seekable.
  87. *
  88. * \param d audio reader
  89. * \return
  90. * - true if the audio reader supports seek
  91. * - false if the audio reader doesn't support seek
  92. */
  93. bool auReaderIsSeekable(auReader_t *d);
  94. /**
  95. * \brief audio reader seek
  96. *
  97. * Macros for \p whence are defined in <tt>stdio.h</tt>.
  98. *
  99. * \param d audio reader
  100. * \param offset offset according to the directive whence
  101. * \param whence SEEK_SET, SEEK_CUR, SEEK_END
  102. * \return
  103. * - offset location from the beginning of reader on success.
  104. * - -1 on error.
  105. */
  106. int auReaderSeek(auReader_t *d, int offset, int whence);
  107. /**
  108. * \brief audio reader drop data
  109. *
  110. * When audio reader supports seek, it is the same to seek forward.
  111. * Otherwise, it will try to read-and-drop specified size.
  112. *
  113. * \param d audio reader
  114. * \param size byte count to be dropped
  115. * \return
  116. * - 0 on success.
  117. * - -1 on error.
  118. */
  119. int auReaderDrop(auReader_t *d, unsigned size);
  120. /**
  121. * \brief whether the reader reaches end of file
  122. *
  123. * \param d audio reader
  124. * \return
  125. * - true when the reader reaches end of file
  126. * - false if not
  127. */
  128. bool auReaderIsEof(auReader_t *d);
  129. /**
  130. * \brief opaque data strcture of file based audio reader
  131. */
  132. typedef struct auFileReader auFileReader_t;
  133. /**
  134. * \brief create a file based audio reader
  135. *
  136. * \param fname file name
  137. * \return
  138. * - the created audio reader
  139. * - NULL if invalid parameter, or out of memory
  140. */
  141. auFileReader_t *auFileReaderCreate(const char *fname);
  142. /**
  143. * \brief opaque data structure of meory based audio reader
  144. */
  145. typedef struct auMemReader auMemReader_t;
  146. /**
  147. * \brief create a memory based audio reader
  148. *
  149. * When \p buf is NULL or \p size is 0, it will create an empty audio reader.
  150. *
  151. * \param buf buffer
  152. * \param size buffer size
  153. * \return
  154. * - the created audio reader
  155. * - NULL if out of memory
  156. */
  157. auMemReader_t *auMemReaderCreate(const void *buf, unsigned size);
  158. /**
  159. * \brief opaque data structure for pip based audio reader
  160. */
  161. typedef struct auPipeReader auPipeReader_t;
  162. /**
  163. * \brief create a pipe based audio reader
  164. *
  165. * Pipe base audio reader will decouple audio data producing and consuming.
  166. * Seek is unsupported in pipe based audio reader. Audio reader will just
  167. * access the reader side of pipe.
  168. *
  169. * \param pipe the pipe to be read
  170. * \return
  171. * - the created audio reader
  172. * - NULL if out of memory, or invalid parameter
  173. */
  174. auPipeReader_t *auPipeReaderCreate(struct osiPipe *pipe);
  175. /**
  176. * \brief set wait timeout for audio pipe reader
  177. *
  178. * By default, audio pipe read will wait a while when the pipe is empty, or
  179. * not enough for the requested byte count. This can set the wait timeout,
  180. * 0 for not waiting and \p OSI_WAIT_FOREVER for infinite wait until the
  181. * requested data are read.
  182. *
  183. * When \p timeout is set to 0, the pipe will be read repeatedly. In most
  184. * cases, it is not wanted.
  185. *
  186. * \param d the audio pipe reader
  187. * \param timeout wait timeout
  188. */
  189. void auPipeReaderSetWait(auPipeReader_t *d, unsigned timeout);
  190. /**
  191. * \brief helper to manage input buffer for audio reader
  192. *
  193. * The struct definition is just to make it easier to embed data struct.
  194. * DON'T acess members directly.
  195. */
  196. typedef struct
  197. {
  198. uint8_t *buf; ///< the real input buffer
  199. unsigned size; ///< byte count in the buffer
  200. unsigned pos; ///< read position in the buffer
  201. unsigned file_pos; ///< file position for the read position
  202. unsigned data_start; ///< valid data start in file
  203. unsigned data_end; ///< valid data end in file
  204. } auReadBuf_t;
  205. /**
  206. * \brief initialize read buffer
  207. *
  208. * \param d read buffer
  209. * \param buf real input buffer pointer
  210. */
  211. void auReadBufInit(auReadBuf_t *d, void *buf);
  212. /**
  213. * \brief clear read buffer, and set file position
  214. *
  215. * \param d read buffer
  216. * \param file_pos file position
  217. */
  218. void auReadBufReset(auReadBuf_t *d, unsigned file_pos);
  219. /**
  220. * \brief fetch from reader
  221. *
  222. * When needed, it will read from audio reader, and try to ensure
  223. * valid data size is greater than \p size.
  224. *
  225. * \param d read buffer
  226. * \param reader audio reader
  227. * \param size requested valid data size
  228. * \return
  229. * - valid data size in read buffer
  230. */
  231. unsigned auReadBufFetch(auReadBuf_t *d, auReader_t *reader, unsigned size);
  232. /**
  233. * \brief skip bytes in reader buffer
  234. *
  235. * \p size may be larger than valid data size in read buffer. In this
  236. * case, it will call \p auReaderDrop.
  237. *
  238. * \param d read buffer
  239. * \param reader audio reader
  240. * \param size requested byte count to be skipped
  241. * \return
  242. * - true on success.
  243. * - false on error.
  244. */
  245. bool auReadBufSkip(auReadBuf_t *d, auReader_t *reader, unsigned size);
  246. /**
  247. * \brief set pipe reader timeout
  248. *
  249. *
  250. * \param d pipe reader
  251. * \param timeout timeout
  252. * \return
  253. * - NULL.
  254. */
  255. void auPipeReaderSetWait(auPipeReader_t *d, unsigned timeout);
  256. /**
  257. * \brief valid data pointer
  258. *
  259. * \param d read buffer
  260. * \return
  261. * - valid data pointer
  262. */
  263. static inline uint8_t *auReadBufData(auReadBuf_t *d) { return &d->buf[d->pos]; }
  264. /**
  265. * \brief valid data size
  266. *
  267. * \param d read buffer
  268. * \return
  269. * - valid data size
  270. */
  271. static inline unsigned auReadBufSize(auReadBuf_t *d) { return d->size - d->pos; }
  272. /**
  273. * \brief file position
  274. *
  275. * \param d read buffer
  276. * \return
  277. * - file position
  278. */
  279. static inline unsigned auReadBufFilePos(auReadBuf_t *d) { return d->file_pos; }
  280. /**
  281. * \brief data start position in file
  282. *
  283. * \param d read buffer
  284. * \return
  285. * - data start position in file
  286. */
  287. static inline unsigned auReadBufDataStart(auReadBuf_t *d) { return d->data_start; }
  288. /**
  289. * \brief data end position in file
  290. *
  291. * \param d read buffer
  292. * \return
  293. * - data end position in file
  294. */
  295. static inline unsigned auReadBufDataEnd(auReadBuf_t *d) { return d->data_end; }
  296. /**
  297. * \brief set data start position in file
  298. *
  299. * \param d read buffer
  300. * \param data_start data start position in file
  301. */
  302. static inline void auReadBufSetDataStart(auReadBuf_t *d, unsigned data_start) { d->data_start = data_start; }
  303. /**
  304. * \brief set data end position in file
  305. *
  306. * \param d read buffer
  307. * \param data_end data end position in file
  308. */
  309. static inline void auReadBufSetDataEnd(auReadBuf_t *d, unsigned data_end) { d->data_end = data_end; }
  310. /**
  311. * \brief data read is finish
  312. *
  313. * \param d read buffer
  314. * \return
  315. * - true finish
  316. * - false not finish
  317. */
  318. static inline bool auReadBufFetchAllData(auReadBuf_t *d) { return (d->file_pos >= d->data_end); }
  319. OSI_EXTERN_C_END
  320. #endif