linuxrec.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * @file
  3. * @brief a record demo in linux
  4. *
  5. * a simple record code. using alsa-lib APIs.
  6. * keep the function same as winrec.h
  7. *
  8. * Common steps:
  9. * create_recorder,
  10. * open_recorder,
  11. * start_record,
  12. * stop_record,
  13. * close_recorder,
  14. * destroy_recorder
  15. *
  16. * @author taozhang9
  17. * @date 2016/06/01
  18. */
  19. #ifndef __IFLY_WINREC_H__
  20. #define __IFLY_WINREC_H__
  21. #include "formats.h"
  22. /* error code */
  23. enum {
  24. RECORD_ERR_BASE = 0,
  25. RECORD_ERR_GENERAL,
  26. RECORD_ERR_MEMFAIL,
  27. RECORD_ERR_INVAL,
  28. RECORD_ERR_NOT_READY
  29. };
  30. typedef struct {
  31. union {
  32. char * name;
  33. int index;
  34. void * resv;
  35. }u;
  36. }record_dev_id;
  37. /* recorder object. */
  38. struct recorder {
  39. void (*on_data_ind)(char *data, unsigned long len, void *user_para);
  40. void * user_cb_para;
  41. volatile int state; /* internal record state */
  42. void * wavein_hdl;
  43. /* thread id may be a struct. by implementation
  44. * void * will not be ported!! */
  45. pthread_t rec_thread;
  46. /*void * rec_thread_hdl;*/
  47. void * bufheader;
  48. unsigned int bufcount;
  49. char *audiobuf;
  50. int bits_per_frame;
  51. unsigned int buffer_time;
  52. unsigned int period_time;
  53. size_t period_frames;
  54. size_t buffer_frames;
  55. };
  56. #ifdef __cplusplus
  57. extern "C" {
  58. #endif /* C++ */
  59. /**
  60. * @fn
  61. * @brief Get the default input device ID
  62. *
  63. * @return returns "default" in linux.
  64. *
  65. */
  66. record_dev_id get_default_input_dev();
  67. /**
  68. * @fn
  69. * @brief Get the total number of active input devices.
  70. * @return
  71. */
  72. int get_input_dev_num();
  73. /**
  74. * @fn
  75. * @brief Create a recorder object.
  76. *
  77. * Never call the close_recorder in the callback function. as close
  78. * action will wait for the callback thread to quit.
  79. *
  80. * @return int - Return 0 in success, otherwise return error code.
  81. * @param out_rec - [out] recorder object holder
  82. * @param on_data_ind - [in] callback. called when data coming.
  83. * @param user_cb_para - [in] user params for the callback.
  84. * @see
  85. */
  86. int create_recorder(struct recorder ** out_rec,
  87. void (*on_data_ind)(char *data, unsigned long len, void *user_para),
  88. void* user_cb_para);
  89. /**
  90. * @fn
  91. * @brief Destroy recorder object. free memory.
  92. * @param rec - [in]recorder object
  93. */
  94. void destroy_recorder(struct recorder *rec);
  95. /**
  96. * @fn
  97. * @brief open the device.
  98. * @return int - Return 0 in success, otherwise return error code.
  99. * @param rec - [in] recorder object
  100. * @param dev - [in] device id, from 0.
  101. * @param fmt - [in] record format.
  102. * @see
  103. * get_default_input_dev()
  104. */
  105. int open_recorder(struct recorder * rec, record_dev_id dev, WAVEFORMATEX * fmt);
  106. /**
  107. * @fn
  108. * @brief close the device.
  109. * @param rec - [in] recorder object
  110. */
  111. void close_recorder(struct recorder *rec);
  112. /**
  113. * @fn
  114. * @brief start record.
  115. * @return int - Return 0 in success, otherwise return error code.
  116. * @param rec - [in] recorder object
  117. */
  118. int start_record(struct recorder * rec);
  119. /**
  120. * @fn
  121. * @brief stop record.
  122. * @return int - Return 0 in success, otherwise return error code.
  123. * @param rec - [in] recorder object
  124. */
  125. int stop_record(struct recorder * rec);
  126. /**
  127. * @fn
  128. * @brief test if the recording has been stopped.
  129. * @return int - 1: stopped. 0 : recording.
  130. * @param rec - [in] recorder object
  131. */
  132. int is_record_stopped(struct recorder *rec);
  133. #ifdef __cplusplus
  134. } /* extern "C" */
  135. #endif /* C++ */
  136. #endif