audio_webrtc_consumer.cxx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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_webrtc_consumer.h"
  19. #include "audio_webrtc.h"
  20. #include "tinydav/audio/tdav_consumer_audio.h"
  21. #include "tsk_string.h"
  22. #include "tsk_memory.h"
  23. #include "tsk_debug.h"
  24. typedef struct audio_consumer_webrtc_s {
  25. TDAV_DECLARE_CONSUMER_AUDIO;
  26. audio_webrtc_instance_handle_t* audioInstHandle;
  27. struct {
  28. void* ptr;
  29. bool isFull;
  30. int size;
  31. int index;
  32. } buffer;
  33. }
  34. audio_consumer_webrtc_t;
  35. int audio_consumer_webrtc_get_data_10ms(const audio_consumer_webrtc_t* _self, void* audioSamples, int nSamples, int nBytesPerSample, int nChannels, int samplesPerSec, uint32_t &nSamplesOut)
  36. {
  37. nSamplesOut = 0;
  38. if(!_self || !audioSamples || !nSamples) {
  39. DOUBANGO_AUDIO_WEBRTC_DEBUG_ERROR("Invalid parameter");
  40. return -1;
  41. }
  42. if((nSamples != (samplesPerSec / 100))) {
  43. DOUBANGO_AUDIO_WEBRTC_DEBUG_ERROR("Not producing 10ms samples (nSamples=%d, samplesPerSec=%d)", nSamples, samplesPerSec);
  44. return -2;
  45. }
  46. if((nBytesPerSample != (TMEDIA_CONSUMER(_self)->audio.bits_per_sample >> 3))) {
  47. DOUBANGO_AUDIO_WEBRTC_DEBUG_ERROR("%d not valid bytes/samples", nBytesPerSample);
  48. return -3;
  49. }
  50. if((nChannels != TMEDIA_CONSUMER(_self)->audio.out.channels)) {
  51. DOUBANGO_AUDIO_WEBRTC_DEBUG_ERROR("%d not the expected number of channels", nChannels);
  52. return -4;
  53. }
  54. audio_consumer_webrtc_t* self = const_cast<audio_consumer_webrtc_t*>(_self);
  55. if(self->buffer.index == self->buffer.size) {
  56. tdav_consumer_audio_tick(TDAV_CONSUMER_AUDIO(self));
  57. self->buffer.index = 0;
  58. if((tdav_consumer_audio_get(TDAV_CONSUMER_AUDIO(self), self->buffer.ptr, self->buffer.size)) != self->buffer.size) {
  59. nSamplesOut = 0;
  60. return 0;
  61. }
  62. }
  63. int nSamplesInBits = (nSamples * nBytesPerSample);
  64. if(_self->buffer.index + nSamplesInBits <= _self->buffer.size) {
  65. memcpy(audioSamples, (((uint8_t*)self->buffer.ptr) + self->buffer.index), nSamplesInBits);
  66. }
  67. self->buffer.index += nSamplesInBits;
  68. TSK_CLAMP(0, self->buffer.index, self->buffer.size);
  69. nSamplesOut = nSamples;
  70. return 0;
  71. }
  72. /* ============ Media Consumer Interface ================= */
  73. static int audio_consumer_webrtc_set(tmedia_consumer_t* self, const tmedia_param_t* param)
  74. {
  75. audio_consumer_webrtc_t* webrtc = (audio_consumer_webrtc_t*)self;
  76. int ret = tdav_consumer_audio_set(TDAV_CONSUMER_AUDIO(self), param);
  77. if(ret == 0) {
  78. if(tsk_striequals(param->key, "volume")) {
  79. }
  80. }
  81. return ret;
  82. }
  83. static int audio_consumer_webrtc_prepare(tmedia_consumer_t* _self, const tmedia_codec_t* codec)
  84. {
  85. audio_consumer_webrtc_t* self = (audio_consumer_webrtc_t*)_self;
  86. if(!self) {
  87. DOUBANGO_AUDIO_WEBRTC_DEBUG_ERROR("Invalid parameter");
  88. return -1;
  89. }
  90. // create audio instance
  91. if(!(self->audioInstHandle = audio_webrtc_instance_create(TMEDIA_CONSUMER(self)->session_id))) {
  92. DOUBANGO_AUDIO_WEBRTC_DEBUG_ERROR("Failed to create audio instance handle");
  93. return -1;
  94. }
  95. // initialize input parameters from the codec information
  96. TMEDIA_CONSUMER(self)->audio.ptime = codec->plugin->audio.ptime;
  97. TMEDIA_CONSUMER(self)->audio.in.channels = codec->plugin->audio.channels;
  98. TMEDIA_CONSUMER(self)->audio.in.rate = codec->plugin->rate;
  99. // prepare playout device and update output parameters
  100. int ret = audio_webrtc_instance_prepare_consumer(self->audioInstHandle, &_self);
  101. // now that the producer is prepared we can initialize internal buffer using device caps
  102. if(ret == 0) {
  103. // allocate buffer
  104. int xsize = ((TMEDIA_CONSUMER(self)->audio.ptime * TMEDIA_CONSUMER(self)->audio.out.rate) / 1000) * (TMEDIA_CONSUMER(self)->audio.bits_per_sample >> 3);
  105. if(!(self->buffer.ptr = tsk_realloc(self->buffer.ptr, xsize))) {
  106. DOUBANGO_AUDIO_WEBRTC_DEBUG_ERROR("Failed to allocate buffer with size = %d", xsize);
  107. self->buffer.size = 0;
  108. return -1;
  109. }
  110. memset(self->buffer.ptr, 0, xsize);
  111. self->buffer.size = xsize;
  112. self->buffer.index = 0;
  113. self->buffer.isFull = false;
  114. }
  115. return ret;
  116. }
  117. static int audio_consumer_webrtc_start(tmedia_consumer_t* _self)
  118. {
  119. audio_consumer_webrtc_t* self = (audio_consumer_webrtc_t*)_self;
  120. if(!self) {
  121. DOUBANGO_AUDIO_WEBRTC_DEBUG_ERROR("Invalid parameter");
  122. return -1;
  123. }
  124. return audio_webrtc_instance_start_consumer(self->audioInstHandle);
  125. }
  126. static int audio_consumer_webrtc_consume(tmedia_consumer_t* _self, const void* buffer, tsk_size_t size, const tsk_object_t* proto_hdr)
  127. {
  128. audio_consumer_webrtc_t* self = (audio_consumer_webrtc_t*)_self;
  129. if(!self || !buffer || !size) {
  130. DOUBANGO_AUDIO_WEBRTC_DEBUG_ERROR("1Invalid parameter");
  131. return -1;
  132. }
  133. /* buffer is already decoded */
  134. return tdav_consumer_audio_put(TDAV_CONSUMER_AUDIO(self), buffer, size, proto_hdr);
  135. }
  136. static int audio_consumer_webrtc_pause(tmedia_consumer_t* self)
  137. {
  138. return 0;
  139. }
  140. static int audio_consumer_webrtc_stop(tmedia_consumer_t* _self)
  141. {
  142. audio_consumer_webrtc_t* self = (audio_consumer_webrtc_t*)_self;
  143. if(!self) {
  144. DOUBANGO_AUDIO_WEBRTC_DEBUG_ERROR("Invalid parameter");
  145. return -1;
  146. }
  147. return audio_webrtc_instance_stop_consumer(self->audioInstHandle);
  148. }
  149. //
  150. // WebRTC audio consumer object definition
  151. //
  152. /* constructor */
  153. static tsk_object_t* audio_consumer_webrtc_ctor(tsk_object_t *_self, va_list * app)
  154. {
  155. audio_consumer_webrtc_t *self = (audio_consumer_webrtc_t *)_self;
  156. if(self) {
  157. /* init base */
  158. tdav_consumer_audio_init(TDAV_CONSUMER_AUDIO(self));
  159. /* init self */
  160. }
  161. return self;
  162. }
  163. /* destructor */
  164. static tsk_object_t* audio_consumer_webrtc_dtor(tsk_object_t *_self)
  165. {
  166. audio_consumer_webrtc_t *self = (audio_consumer_webrtc_t *)_self;
  167. if(self) {
  168. /* stop */
  169. audio_consumer_webrtc_stop(TMEDIA_CONSUMER(self));
  170. /* deinit self */
  171. if(self->audioInstHandle) {
  172. audio_webrtc_instance_destroy(&self->audioInstHandle);
  173. }
  174. TSK_FREE(self->buffer.ptr);
  175. /* deinit base */
  176. tdav_consumer_audio_deinit(TDAV_CONSUMER_AUDIO(self));
  177. }
  178. return self;
  179. }
  180. /* object definition */
  181. static const tsk_object_def_t audio_consumer_webrtc_def_s = {
  182. sizeof(audio_consumer_webrtc_t),
  183. audio_consumer_webrtc_ctor,
  184. audio_consumer_webrtc_dtor,
  185. tdav_consumer_audio_cmp,
  186. };
  187. /* plugin definition*/
  188. static const tmedia_consumer_plugin_def_t audio_consumer_webrtc_plugin_def_s = {
  189. &audio_consumer_webrtc_def_s,
  190. tmedia_audio,
  191. "WebRTC audio consumer",
  192. audio_consumer_webrtc_set,
  193. audio_consumer_webrtc_prepare,
  194. audio_consumer_webrtc_start,
  195. audio_consumer_webrtc_consume,
  196. audio_consumer_webrtc_pause,
  197. audio_consumer_webrtc_stop
  198. };
  199. const tmedia_consumer_plugin_def_t *audio_consumer_webrtc_plugin_def_t = &audio_consumer_webrtc_plugin_def_s;