audio_opensles_producer.cxx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* Copyright (C) 2012 Doubango Telecom <http://www.doubango.org>
  2. *
  3. * This file is part of Open Source Doubango Framework.
  4. *
  5. * DOUBANGO is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * DOUBANGO is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with DOUBANGO.
  17. */
  18. #include "audio_opensles_producer.h"
  19. #include "audio_opensles.h"
  20. #include "tinydav/audio/tdav_producer_audio.h"
  21. #include "tsk_string.h"
  22. #include "tsk_memory.h"
  23. #include "tsk_debug.h"
  24. typedef struct audio_producer_opensles_s {
  25. TDAV_DECLARE_PRODUCER_AUDIO;
  26. bool isMuted;
  27. audio_opensles_instance_handle_t* audioInstHandle;
  28. struct {
  29. void* ptr;
  30. int size;
  31. int index;
  32. } buffer;
  33. }
  34. audio_producer_opensles_t;
  35. int audio_producer_opensles_handle_data_10ms(const audio_producer_opensles_t* _self, const void* audioSamples, int nSamples, int nBytesPerSample, int samplesPerSec, int nChannels)
  36. {
  37. if(!_self || !audioSamples || !nSamples) {
  38. AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter");
  39. return -1;
  40. }
  41. if(!TMEDIA_PRODUCER(_self)->enc_cb.callback) {
  42. AUDIO_OPENSLES_DEBUG_WARN("No callback function is registered for the producer");
  43. return 0;
  44. }
  45. if((nSamples != (samplesPerSec / 100))) {
  46. AUDIO_OPENSLES_DEBUG_ERROR("Not producing 10ms samples (nSamples=%d, samplesPerSec=%d)", nSamples, samplesPerSec);
  47. return -2;
  48. }
  49. if((nBytesPerSample != (TMEDIA_PRODUCER(_self)->audio.bits_per_sample >> 3))) {
  50. AUDIO_OPENSLES_DEBUG_ERROR("%d not valid bytes/samples", nBytesPerSample);
  51. return -3;
  52. }
  53. if((nChannels != TMEDIA_PRODUCER(_self)->audio.channels)) {
  54. AUDIO_OPENSLES_DEBUG_ERROR("Recording - %d not the expected number of channels but should be %d", nChannels, TMEDIA_PRODUCER(_self)->audio.channels);
  55. return -4;
  56. }
  57. int nSamplesInBits = (nSamples * nBytesPerSample);
  58. if(_self->buffer.index + nSamplesInBits > _self->buffer.size) {
  59. AUDIO_OPENSLES_DEBUG_ERROR("Buffer overflow");
  60. return -5;
  61. }
  62. audio_producer_opensles_t* self = const_cast<audio_producer_opensles_t*>(_self);
  63. if(self->isMuted) {
  64. memset((((uint8_t*)self->buffer.ptr) + self->buffer.index), 0, nSamplesInBits);
  65. }
  66. else {
  67. memcpy((((uint8_t*)self->buffer.ptr) + self->buffer.index), audioSamples, nSamplesInBits);
  68. }
  69. self->buffer.index += nSamplesInBits;
  70. if(self->buffer.index == self->buffer.size) {
  71. self->buffer.index = 0;
  72. TMEDIA_PRODUCER(self)->enc_cb.callback(TMEDIA_PRODUCER(self)->enc_cb.callback_data, self->buffer.ptr, self->buffer.size);
  73. }
  74. return 0;
  75. }
  76. /* ============ Media Producer Interface ================= */
  77. static int audio_producer_opensles_set(tmedia_producer_t* _self, const tmedia_param_t* param)
  78. {
  79. audio_producer_opensles_t* self = (audio_producer_opensles_t*)_self;
  80. if(param->plugin_type == tmedia_ppt_producer) {
  81. if(param->value_type == tmedia_pvt_int32) {
  82. if(tsk_striequals(param->key, "mute")) {
  83. self->isMuted = (*((int32_t*)param->value) != 0);
  84. // Mute not supported on android -> send silence when needed
  85. return 0;
  86. }
  87. else if(tsk_striequals(param->key, "volume")) {
  88. return audio_opensles_instance_set_microphone_volume(self->audioInstHandle, *((int32_t*)param->value));
  89. }
  90. }
  91. }
  92. return tdav_producer_audio_set(TDAV_PRODUCER_AUDIO(self), param);
  93. }
  94. static int audio_producer_opensles_prepare(tmedia_producer_t* _self, const tmedia_codec_t* codec)
  95. {
  96. audio_producer_opensles_t* self = (audio_producer_opensles_t*)_self;
  97. if(!self || !codec) {
  98. AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter");
  99. return -1;
  100. }
  101. // create audio instance
  102. if(!(self->audioInstHandle = audio_opensles_instance_create(TMEDIA_PRODUCER(self)->session_id))) {
  103. AUDIO_OPENSLES_DEBUG_ERROR("Failed to create audio instance handle");
  104. return -2;
  105. }
  106. // check that ptime is mutiple of 10
  107. if((codec->plugin->audio.ptime % 10)) {
  108. AUDIO_OPENSLES_DEBUG_ERROR("ptime=%d not multiple of 10", codec->plugin->audio.ptime);
  109. return -3;
  110. }
  111. // init input parameters from the codec
  112. TMEDIA_PRODUCER(self)->audio.channels = TMEDIA_CODEC_CHANNELS_AUDIO_ENCODING(codec);
  113. TMEDIA_PRODUCER(self)->audio.rate = TMEDIA_CODEC_RATE_ENCODING(codec);
  114. TMEDIA_PRODUCER(self)->audio.ptime = TMEDIA_CODEC_PTIME_AUDIO_ENCODING(codec);
  115. AUDIO_OPENSLES_DEBUG_INFO("audio_producer_opensles_prepare(channels=%d, rate=%d, ptime=%d)", codec->plugin->audio.channels, codec->plugin->rate, codec->plugin->audio.ptime);
  116. // prepare playout device and update output parameters
  117. int ret;
  118. ret = audio_opensles_instance_prepare_producer(self->audioInstHandle, &_self);
  119. // now that the producer is prepared we can initialize internal buffer using device caps
  120. if(ret == 0) {
  121. // allocate buffer
  122. int xsize = ((TMEDIA_PRODUCER(self)->audio.ptime * TMEDIA_PRODUCER(self)->audio.rate) / 1000) * (TMEDIA_PRODUCER(self)->audio.bits_per_sample >> 3);
  123. AUDIO_OPENSLES_DEBUG_INFO("producer buffer xsize = %d", xsize);
  124. if(!(self->buffer.ptr = tsk_realloc(self->buffer.ptr, xsize))) {
  125. AUDIO_OPENSLES_DEBUG_ERROR("Failed to allocate buffer with size = %d", xsize);
  126. self->buffer.size = 0;
  127. return -1;
  128. }
  129. self->buffer.size = xsize;
  130. self->buffer.index = 0;
  131. }
  132. return ret;
  133. }
  134. static int audio_producer_opensles_start(tmedia_producer_t* _self)
  135. {
  136. audio_producer_opensles_t* self = (audio_producer_opensles_t*)_self;
  137. if(!self) {
  138. AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter");
  139. return -1;
  140. }
  141. AUDIO_OPENSLES_DEBUG_INFO("audio_producer_opensles_start");
  142. return audio_opensles_instance_start_producer(self->audioInstHandle);
  143. }
  144. static int audio_producer_opensles_pause(tmedia_producer_t* self)
  145. {
  146. return 0;
  147. }
  148. static int audio_producer_opensles_stop(tmedia_producer_t* _self)
  149. {
  150. audio_producer_opensles_t* self = (audio_producer_opensles_t*)_self;
  151. if(!self) {
  152. AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter");
  153. return -1;
  154. }
  155. return audio_opensles_instance_stop_producer(self->audioInstHandle);
  156. }
  157. //
  158. // SLES audio producer object definition
  159. //
  160. /* constructor */
  161. static tsk_object_t* audio_producer_opensles_ctor(tsk_object_t *_self, va_list * app)
  162. {
  163. audio_producer_opensles_t *self = (audio_producer_opensles_t *)_self;
  164. if(self) {
  165. /* init base */
  166. tdav_producer_audio_init(TDAV_PRODUCER_AUDIO(self));
  167. /* init self */
  168. }
  169. return self;
  170. }
  171. /* destructor */
  172. static tsk_object_t* audio_producer_opensles_dtor(tsk_object_t *_self)
  173. {
  174. audio_producer_opensles_t *self = (audio_producer_opensles_t *)_self;
  175. if(self) {
  176. /* stop */
  177. audio_producer_opensles_stop(TMEDIA_PRODUCER(self));
  178. /* deinit self */
  179. if(self->audioInstHandle) {
  180. audio_opensles_instance_destroy(&self->audioInstHandle);
  181. }
  182. TSK_FREE(self->buffer.ptr);
  183. /* deinit base */
  184. tdav_producer_audio_deinit(TDAV_PRODUCER_AUDIO(self));
  185. }
  186. return self;
  187. }
  188. /* object definition */
  189. static const tsk_object_def_t audio_producer_opensles_def_s = {
  190. sizeof(audio_producer_opensles_t),
  191. audio_producer_opensles_ctor,
  192. audio_producer_opensles_dtor,
  193. tdav_producer_audio_cmp,
  194. };
  195. /* plugin definition*/
  196. static const tmedia_producer_plugin_def_t audio_producer_opensles_plugin_def_s = {
  197. &audio_producer_opensles_def_s,
  198. tmedia_audio,
  199. "SLES audio producer",
  200. audio_producer_opensles_set,
  201. audio_producer_opensles_prepare,
  202. audio_producer_opensles_start,
  203. audio_producer_opensles_pause,
  204. audio_producer_opensles_stop
  205. };
  206. const tmedia_producer_plugin_def_t *audio_producer_opensles_plugin_def_t = &audio_producer_opensles_plugin_def_s;