audio_webrtc_producer.cxx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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_producer.h"
  19. #include "audio_webrtc.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_webrtc_s {
  25. TDAV_DECLARE_PRODUCER_AUDIO;
  26. bool isMuted;
  27. audio_webrtc_instance_handle_t* audioInstHandle;
  28. struct {
  29. void* ptr;
  30. int size;
  31. int index;
  32. } buffer;
  33. }
  34. audio_producer_webrtc_t;
  35. int audio_producer_webrtc_handle_data_10ms(const audio_producer_webrtc_t* _self, const void* audioSamples, int nSamples, int nBytesPerSample, int samplesPerSec, int nChannels)
  36. {
  37. if(!_self || !audioSamples || !nSamples) {
  38. DOUBANGO_AUDIO_WEBRTC_DEBUG_ERROR("Invalid parameter");
  39. return -1;
  40. }
  41. if((nSamples != (samplesPerSec / 100))) {
  42. DOUBANGO_AUDIO_WEBRTC_DEBUG_ERROR("Not producing 10ms samples (nSamples=%d, samplesPerSec=%d)", nSamples, samplesPerSec);
  43. return -2;
  44. }
  45. if((nBytesPerSample != (TMEDIA_PRODUCER(_self)->audio.bits_per_sample >> 3))) {
  46. DOUBANGO_AUDIO_WEBRTC_DEBUG_ERROR("%d not valid bytes/samples", nBytesPerSample);
  47. return -3;
  48. }
  49. if((nChannels != TMEDIA_PRODUCER(_self)->audio.channels)) {
  50. DOUBANGO_AUDIO_WEBRTC_DEBUG_ERROR("%d not the expected number of channels", nChannels);
  51. return -4;
  52. }
  53. int nSamplesInBits = (nSamples * nBytesPerSample);
  54. if(_self->buffer.index + nSamplesInBits > _self->buffer.size) {
  55. DOUBANGO_AUDIO_WEBRTC_DEBUG_ERROR("Buffer overflow");
  56. return -5;
  57. }
  58. audio_producer_webrtc_t* self = const_cast<audio_producer_webrtc_t*>(_self);
  59. memcpy((((uint8_t*)self->buffer.ptr) + self->buffer.index), audioSamples, nSamplesInBits);
  60. self->buffer.index += nSamplesInBits;
  61. if(self->buffer.index == self->buffer.size) {
  62. self->buffer.index = 0;
  63. if(TMEDIA_PRODUCER(self)->enc_cb.callback) {
  64. if(self->isMuted) {
  65. memset(self->buffer.ptr, 0, self->buffer.size);
  66. }
  67. TMEDIA_PRODUCER(self)->enc_cb.callback(TMEDIA_PRODUCER(self)->enc_cb.callback_data, self->buffer.ptr, self->buffer.size);
  68. }
  69. }
  70. return 0;
  71. }
  72. /* ============ Media Producer Interface ================= */
  73. static int audio_producer_webrtc_set(tmedia_producer_t* _self, const tmedia_param_t* param)
  74. {
  75. audio_producer_webrtc_t* self = (audio_producer_webrtc_t*)_self;
  76. if(param->plugin_type == tmedia_ppt_producer) {
  77. if(param->value_type == tmedia_pvt_int32) {
  78. if(tsk_striequals(param->key, "mute")) {
  79. self->isMuted = (TSK_TO_INT32((uint8_t*)param->value) != 0);
  80. return 0;
  81. }
  82. }
  83. }
  84. return tdav_producer_audio_set(TDAV_PRODUCER_AUDIO(self), param);
  85. }
  86. static int audio_producer_webrtc_prepare(tmedia_producer_t* _self, const tmedia_codec_t* codec)
  87. {
  88. audio_producer_webrtc_t* self = (audio_producer_webrtc_t*)_self;
  89. if(!self || !codec) {
  90. DOUBANGO_AUDIO_WEBRTC_DEBUG_ERROR("Invalid parameter");
  91. return -1;
  92. }
  93. // create audio instance
  94. if(!(self->audioInstHandle = audio_webrtc_instance_create(TMEDIA_PRODUCER(self)->session_id))) {
  95. DOUBANGO_AUDIO_WEBRTC_DEBUG_ERROR("Failed to create audio instance handle");
  96. return -2;
  97. }
  98. // check that ptime is mutiple of 10
  99. if((codec->plugin->audio.ptime % 10)) {
  100. DOUBANGO_AUDIO_WEBRTC_DEBUG_ERROR("ptime=%d not multiple of 10", codec->plugin->audio.ptime);
  101. return -3;
  102. }
  103. // init input parameters from the codec
  104. TMEDIA_PRODUCER(self)->audio.channels = codec->plugin->audio.channels;
  105. TMEDIA_PRODUCER(self)->audio.rate = codec->plugin->rate;
  106. TMEDIA_PRODUCER(self)->audio.ptime = codec->plugin->audio.ptime;
  107. // prepare playout device and update output parameters
  108. int ret;
  109. ret = audio_webrtc_instance_prepare_producer(self->audioInstHandle, &_self);
  110. // now that the producer is prepared we can initialize internal buffer using device caps
  111. if(ret == 0) {
  112. // allocate buffer
  113. int xsize = ((TMEDIA_PRODUCER(self)->audio.ptime * TMEDIA_PRODUCER(self)->audio.rate) / 1000) * (TMEDIA_PRODUCER(self)->audio.bits_per_sample >> 3);
  114. if(!(self->buffer.ptr = tsk_realloc(self->buffer.ptr, xsize))) {
  115. DOUBANGO_AUDIO_WEBRTC_DEBUG_ERROR("Failed to allocate buffer with size = %d", xsize);
  116. self->buffer.size = 0;
  117. return -1;
  118. }
  119. self->buffer.size = xsize;
  120. self->buffer.index = 0;
  121. }
  122. return ret;
  123. }
  124. static int audio_producer_webrtc_start(tmedia_producer_t* _self)
  125. {
  126. audio_producer_webrtc_t* self = (audio_producer_webrtc_t*)_self;
  127. if(!self) {
  128. DOUBANGO_AUDIO_WEBRTC_DEBUG_ERROR("Invalid parameter");
  129. return -1;
  130. }
  131. return audio_webrtc_instance_start_producer(self->audioInstHandle);
  132. }
  133. static int audio_producer_webrtc_pause(tmedia_producer_t* self)
  134. {
  135. return 0;
  136. }
  137. static int audio_producer_webrtc_stop(tmedia_producer_t* _self)
  138. {
  139. audio_producer_webrtc_t* self = (audio_producer_webrtc_t*)_self;
  140. if(!self) {
  141. DOUBANGO_AUDIO_WEBRTC_DEBUG_ERROR("Invalid parameter");
  142. return -1;
  143. }
  144. return audio_webrtc_instance_stop_producer(self->audioInstHandle);
  145. }
  146. //
  147. // WebRTC audio producer object definition
  148. //
  149. /* constructor */
  150. static tsk_object_t* audio_producer_webrtc_ctor(tsk_object_t *_self, va_list * app)
  151. {
  152. audio_producer_webrtc_t *self = (audio_producer_webrtc_t *)_self;
  153. if(self) {
  154. /* init base */
  155. tdav_producer_audio_init(TDAV_PRODUCER_AUDIO(self));
  156. /* init self */
  157. }
  158. return self;
  159. }
  160. /* destructor */
  161. static tsk_object_t* audio_producer_webrtc_dtor(tsk_object_t *_self)
  162. {
  163. audio_producer_webrtc_t *self = (audio_producer_webrtc_t *)_self;
  164. if(self) {
  165. /* stop */
  166. audio_producer_webrtc_stop(TMEDIA_PRODUCER(self));
  167. /* deinit self */
  168. if(self->audioInstHandle) {
  169. audio_webrtc_instance_destroy(&self->audioInstHandle);
  170. }
  171. TSK_FREE(self->buffer.ptr);
  172. /* deinit base */
  173. tdav_producer_audio_deinit(TDAV_PRODUCER_AUDIO(self));
  174. }
  175. return self;
  176. }
  177. /* object definition */
  178. static const tsk_object_def_t audio_producer_webrtc_def_s = {
  179. sizeof(audio_producer_webrtc_t),
  180. audio_producer_webrtc_ctor,
  181. audio_producer_webrtc_dtor,
  182. tdav_producer_audio_cmp,
  183. };
  184. /* plugin definition*/
  185. static const tmedia_producer_plugin_def_t audio_producer_webrtc_plugin_def_s = {
  186. &audio_producer_webrtc_def_s,
  187. tmedia_audio,
  188. "WebRTC audio producer",
  189. audio_producer_webrtc_set,
  190. audio_producer_webrtc_prepare,
  191. audio_producer_webrtc_start,
  192. audio_producer_webrtc_pause,
  193. audio_producer_webrtc_stop
  194. };
  195. const tmedia_producer_plugin_def_t *audio_producer_webrtc_plugin_def_t = &audio_producer_webrtc_plugin_def_s;