audio_opensles_consumer.cxx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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_consumer.h"
  19. #include "audio_opensles.h"
  20. #include "tinydav/audio/tdav_consumer_audio.h"
  21. #include "tsk_string.h"
  22. #include "tsk_string.h"
  23. #include "tsk_memory.h"
  24. #include "tsk_debug.h"
  25. typedef struct audio_consumer_opensles_s {
  26. TDAV_DECLARE_CONSUMER_AUDIO;
  27. audio_opensles_instance_handle_t* audioInstHandle;
  28. bool isSpeakerOn;
  29. struct {
  30. void* ptr;
  31. bool isFull;
  32. int size;
  33. int index;
  34. } buffer;
  35. }
  36. audio_consumer_opensles_t;
  37. int audio_consumer_opensles_get_data_10ms(const audio_consumer_opensles_t* _self, void* audioSamples, int nSamples, int nBytesPerSample, int nChannels, int samplesPerSec, uint32_t &nSamplesOut)
  38. {
  39. nSamplesOut = 0;
  40. if(!_self || !audioSamples || !nSamples) {
  41. AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter");
  42. return -1;
  43. }
  44. if((nSamples != (samplesPerSec / 100))) {
  45. AUDIO_OPENSLES_DEBUG_ERROR("Not producing 10ms samples (nSamples=%d, samplesPerSec=%d)", nSamples, samplesPerSec);
  46. return -2;
  47. }
  48. if((nBytesPerSample != (TMEDIA_CONSUMER(_self)->audio.bits_per_sample >> 3))) {
  49. AUDIO_OPENSLES_DEBUG_ERROR("%d not valid bytes/samples", nBytesPerSample);
  50. return -3;
  51. }
  52. if((nChannels != TMEDIA_CONSUMER(_self)->audio.out.channels)) {
  53. AUDIO_OPENSLES_DEBUG_ERROR("Playout - %d not the expected number of channels but should be %d", nChannels, TMEDIA_CONSUMER(_self)->audio.out.channels);
  54. return -4;
  55. }
  56. audio_consumer_opensles_t* self = const_cast<audio_consumer_opensles_t*>(_self);
  57. if(self->buffer.index == self->buffer.size) {
  58. if((tdav_consumer_audio_get(TDAV_CONSUMER_AUDIO(self), self->buffer.ptr, self->buffer.size)) != self->buffer.size) {
  59. nSamplesOut = 0;
  60. self->buffer.index = self->buffer.size;
  61. return 0;
  62. }
  63. self->buffer.index = 0;
  64. tdav_consumer_audio_tick(TDAV_CONSUMER_AUDIO(self));
  65. }
  66. int nSamplesInBits = (nSamples * nBytesPerSample);
  67. if(_self->buffer.index + nSamplesInBits <= _self->buffer.size) {
  68. memcpy(audioSamples, (((uint8_t*)self->buffer.ptr) + self->buffer.index), nSamplesInBits);
  69. }
  70. self->buffer.index += nSamplesInBits;
  71. TSK_CLAMP(0, self->buffer.index, self->buffer.size);
  72. nSamplesOut = nSamples;
  73. return 0;
  74. }
  75. bool audio_consumer_opensles_is_speakerOn(const audio_consumer_opensles_t* self)
  76. {
  77. if(!self) {
  78. AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter");
  79. return false;
  80. }
  81. return self->isSpeakerOn;
  82. }
  83. /* ============ Media Consumer Interface ================= */
  84. static int audio_consumer_opensles_set(tmedia_consumer_t* _self, const tmedia_param_t* param)
  85. {
  86. audio_consumer_opensles_t* self = (audio_consumer_opensles_t*)_self;
  87. int ret = tdav_consumer_audio_set(TDAV_CONSUMER_AUDIO(self), param);
  88. if(ret == 0) {
  89. if(tsk_striequals(param->key, "volume")) {
  90. }
  91. else if(tsk_striequals(param->key, "speaker-on")) {
  92. self->isSpeakerOn = (TSK_TO_INT32((uint8_t*)param->value) != 0);
  93. if(self->audioInstHandle) {
  94. return audio_opensles_instance_set_speakerOn(self->audioInstHandle, self->isSpeakerOn);
  95. }
  96. else {
  97. return 0; // will be set when instance is initialized
  98. }
  99. }
  100. }
  101. return ret;
  102. }
  103. static int audio_consumer_opensles_prepare(tmedia_consumer_t* _self, const tmedia_codec_t* codec)
  104. {
  105. audio_consumer_opensles_t* self = (audio_consumer_opensles_t*)_self;
  106. if(!self) {
  107. AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter");
  108. return -1;
  109. }
  110. // create audio instance
  111. if(!(self->audioInstHandle = audio_opensles_instance_create(TMEDIA_CONSUMER(self)->session_id))) {
  112. AUDIO_OPENSLES_DEBUG_ERROR("Failed to create audio instance handle");
  113. return -1;
  114. }
  115. // initialize input parameters from the codec information
  116. TMEDIA_CONSUMER(self)->audio.ptime = TMEDIA_CODEC_PTIME_AUDIO_DECODING(codec);
  117. TMEDIA_CONSUMER(self)->audio.in.channels = TMEDIA_CODEC_CHANNELS_AUDIO_DECODING(codec);
  118. TMEDIA_CONSUMER(self)->audio.in.rate = TMEDIA_CODEC_RATE_DECODING(codec);
  119. AUDIO_OPENSLES_DEBUG_INFO("audio_consumer_opensles_prepare(channels=%d, rate=%d, ptime=%d)", codec->plugin->audio.channels, codec->plugin->rate, codec->plugin->audio.ptime);
  120. // prepare playout device and update output parameters
  121. int ret = audio_opensles_instance_prepare_consumer(self->audioInstHandle, &_self);
  122. // now that the producer is prepared we can initialize internal buffer using device caps
  123. if(ret == 0) {
  124. // allocate buffer
  125. int xsize = ((TMEDIA_CONSUMER(self)->audio.ptime * TMEDIA_CONSUMER(self)->audio.out.rate) / 1000) * (TMEDIA_CONSUMER(self)->audio.bits_per_sample >> 3);
  126. if(!(self->buffer.ptr = tsk_realloc(self->buffer.ptr, xsize))) {
  127. AUDIO_OPENSLES_DEBUG_ERROR("Failed to allocate buffer with size = %d", xsize);
  128. self->buffer.size = 0;
  129. return -1;
  130. }
  131. memset(self->buffer.ptr, 0, xsize);
  132. self->buffer.size = xsize;
  133. self->buffer.index = 0;
  134. self->buffer.isFull = false;
  135. }
  136. return ret;
  137. }
  138. static int audio_consumer_opensles_start(tmedia_consumer_t* _self)
  139. {
  140. audio_consumer_opensles_t* self = (audio_consumer_opensles_t*)_self;
  141. if(!self) {
  142. AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter");
  143. return -1;
  144. }
  145. return audio_opensles_instance_start_consumer(self->audioInstHandle);
  146. }
  147. static int audio_consumer_opensles_consume(tmedia_consumer_t* _self, const void* data, tsk_size_t data_size, const tsk_object_t* proto_hdr)
  148. {
  149. audio_consumer_opensles_t* self = (audio_consumer_opensles_t*)_self;
  150. if(!self || !data || !data_size) {
  151. AUDIO_OPENSLES_DEBUG_ERROR("1Invalid parameter");
  152. return -1;
  153. }
  154. /* buffer is already decoded */
  155. return tdav_consumer_audio_put(TDAV_CONSUMER_AUDIO(self), data, data_size, proto_hdr);
  156. }
  157. static int audio_consumer_opensles_pause(tmedia_consumer_t* self)
  158. {
  159. return 0;
  160. }
  161. static int audio_consumer_opensles_stop(tmedia_consumer_t* _self)
  162. {
  163. audio_consumer_opensles_t* self = (audio_consumer_opensles_t*)_self;
  164. if(!self) {
  165. AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter");
  166. return -1;
  167. }
  168. return audio_opensles_instance_stop_consumer(self->audioInstHandle);
  169. }
  170. //
  171. // SLES audio consumer object definition
  172. //
  173. /* constructor */
  174. static tsk_object_t* audio_consumer_opensles_ctor(tsk_object_t *_self, va_list * app)
  175. {
  176. audio_consumer_opensles_t *self = (audio_consumer_opensles_t *)_self;
  177. if(self) {
  178. /* init base */
  179. tdav_consumer_audio_init(TDAV_CONSUMER_AUDIO(self));
  180. /* init self */
  181. }
  182. return self;
  183. }
  184. /* destructor */
  185. static tsk_object_t* audio_consumer_opensles_dtor(tsk_object_t *_self)
  186. {
  187. audio_consumer_opensles_t *self = (audio_consumer_opensles_t *)_self;
  188. if(self) {
  189. /* stop */
  190. audio_consumer_opensles_stop(TMEDIA_CONSUMER(self));
  191. /* deinit self */
  192. if(self->audioInstHandle) {
  193. audio_opensles_instance_destroy(&self->audioInstHandle);
  194. }
  195. TSK_FREE(self->buffer.ptr);
  196. /* deinit base */
  197. tdav_consumer_audio_deinit(TDAV_CONSUMER_AUDIO(self));
  198. }
  199. return self;
  200. }
  201. /* object definition */
  202. static const tsk_object_def_t audio_consumer_opensles_def_s = {
  203. sizeof(audio_consumer_opensles_t),
  204. audio_consumer_opensles_ctor,
  205. audio_consumer_opensles_dtor,
  206. tdav_consumer_audio_cmp,
  207. };
  208. /* plugin definition*/
  209. static const tmedia_consumer_plugin_def_t audio_consumer_opensles_plugin_def_s = {
  210. &audio_consumer_opensles_def_s,
  211. tmedia_audio,
  212. "SLES audio consumer",
  213. audio_consumer_opensles_set,
  214. audio_consumer_opensles_prepare,
  215. audio_consumer_opensles_start,
  216. audio_consumer_opensles_consume,
  217. audio_consumer_opensles_pause,
  218. audio_consumer_opensles_stop
  219. };
  220. const tmedia_consumer_plugin_def_t *audio_consumer_opensles_plugin_def_t = &audio_consumer_opensles_plugin_def_s;