audio_opensles.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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.h"
  19. #include "audio_opensles_consumer.h"
  20. #include "audio_opensles_producer.h"
  21. #include "audio_opensles_device.h"
  22. #include "audio_opensles_device_impl.h"
  23. #include "tinymedia/tmedia_consumer.h"
  24. #include "tinymedia/tmedia_producer.h"
  25. #include "tsk_list.h"
  26. #include "tsk_safeobj.h"
  27. #include "tsk_debug.h"
  28. typedef enum PLUGIN_INDEX_E {
  29. PLUGIN_INDEX_AUDIO_CONSUMER,
  30. PLUGIN_INDEX_AUDIO_PRODUCER,
  31. PLUGIN_INDEX_COUNT
  32. }
  33. PLUGIN_INDEX_T;
  34. int __plugin_get_def_count()
  35. {
  36. return PLUGIN_INDEX_COUNT;
  37. }
  38. tsk_plugin_def_type_t __plugin_get_def_type_at(int index)
  39. {
  40. switch(index) {
  41. case PLUGIN_INDEX_AUDIO_CONSUMER:
  42. return tsk_plugin_def_type_consumer;
  43. case PLUGIN_INDEX_AUDIO_PRODUCER:
  44. return tsk_plugin_def_type_producer;
  45. default: {
  46. AUDIO_OPENSLES_DEBUG_ERROR("No plugin at index %d", index);
  47. return tsk_plugin_def_type_none;
  48. }
  49. }
  50. }
  51. tsk_plugin_def_media_type_t __plugin_get_def_media_type_at(int index)
  52. {
  53. switch(index) {
  54. case PLUGIN_INDEX_AUDIO_CONSUMER:
  55. case PLUGIN_INDEX_AUDIO_PRODUCER: {
  56. return tsk_plugin_def_media_type_audio;
  57. }
  58. default: {
  59. AUDIO_OPENSLES_DEBUG_ERROR("No plugin at index %d", index);
  60. return tsk_plugin_def_media_type_none;
  61. }
  62. }
  63. }
  64. tsk_plugin_def_ptr_const_t __plugin_get_def_at(int index)
  65. {
  66. switch(index) {
  67. case PLUGIN_INDEX_AUDIO_CONSUMER: {
  68. return audio_consumer_opensles_plugin_def_t;
  69. }
  70. case PLUGIN_INDEX_AUDIO_PRODUCER: {
  71. return audio_producer_opensles_plugin_def_t;
  72. }
  73. default: {
  74. AUDIO_OPENSLES_DEBUG_ERROR("No plugin at index %d", index);
  75. return tsk_null;
  76. }
  77. }
  78. }
  79. //
  80. // SLES AudioInstance
  81. //
  82. typedef struct audio_opensles_instance_s {
  83. TSK_DECLARE_OBJECT;
  84. uint64_t sessionId;
  85. bool isStarted;
  86. bool isConsumerPrepared;
  87. bool isConsumerStarted;
  88. bool isProducerPrepared;
  89. bool isProducerStarted;
  90. bool isSpeakerAvailable;
  91. bool isPlayoutAvailable;
  92. bool isRecordingAvailable;
  93. SLAudioDevice* device;
  94. SLAudioDeviceCallbackImpl* callback;
  95. TSK_DECLARE_SAFEOBJ;
  96. }
  97. audio_opensles_instance_t;
  98. typedef tsk_list_t audio_opensles_instances_L_t;
  99. static audio_opensles_instances_L_t* __audioInstances = tsk_null;
  100. static tsk_object_t* audio_opensles_instance_ctor(tsk_object_t * self, va_list * app)
  101. {
  102. audio_opensles_instance_t* audioInstance = (audio_opensles_instance_t*)self;
  103. if(audioInstance) {
  104. tsk_safeobj_init(audioInstance);
  105. }
  106. return self;
  107. }
  108. static tsk_object_t* audio_opensles_instance_dtor(tsk_object_t * self)
  109. {
  110. AUDIO_OPENSLES_DEBUG_INFO("Audio Instance destroyed");
  111. audio_opensles_instance_t* audioInstance = (audio_opensles_instance_t*)self;
  112. if(audioInstance) {
  113. tsk_safeobj_lock(audioInstance);
  114. if(audioInstance->device) {
  115. audioInstance->device->SetCallback(NULL);
  116. audioInstance->device->Terminate();
  117. delete audioInstance->device;
  118. audioInstance->device = tsk_null;
  119. }
  120. if(audioInstance->callback) {
  121. delete audioInstance->callback;
  122. audioInstance->callback = tsk_null;
  123. }
  124. tsk_safeobj_unlock(audioInstance);
  125. tsk_safeobj_deinit(audioInstance);
  126. }
  127. return self;
  128. }
  129. static int audio_opensles_instance_cmp(const tsk_object_t *_ai1, const tsk_object_t *_ai2)
  130. {
  131. return ((int)_ai1 - (int)_ai2);
  132. }
  133. static const tsk_object_def_t audio_opensles_instance_def_s = {
  134. sizeof(audio_opensles_instance_t),
  135. audio_opensles_instance_ctor,
  136. audio_opensles_instance_dtor,
  137. audio_opensles_instance_cmp,
  138. };
  139. const tsk_object_def_t *audio_opensles_instance_def_t = &audio_opensles_instance_def_s;
  140. audio_opensles_instance_handle_t* audio_opensles_instance_create(uint64_t sessionId)
  141. {
  142. audio_opensles_instance_t* audioInstance = tsk_null;
  143. // create list used to hold instances
  144. if(!__audioInstances && !(__audioInstances = tsk_list_create())) {
  145. AUDIO_OPENSLES_DEBUG_ERROR("Failed to create new list");
  146. return tsk_null;
  147. }
  148. //= lock the list
  149. tsk_list_lock(__audioInstances);
  150. // find the instance from the list
  151. const tsk_list_item_t* item;
  152. tsk_list_foreach(item, __audioInstances) {
  153. if(((audio_opensles_instance_t*)item->data)->sessionId == sessionId) {
  154. audioInstance = (audio_opensles_instance_t*)tsk_object_ref(item->data);
  155. break;
  156. }
  157. }
  158. if(!audioInstance) {
  159. audio_opensles_instance_t* _audioInstance;
  160. if(!(_audioInstance = (audio_opensles_instance_t*)tsk_object_new(&audio_opensles_instance_def_s))) {
  161. AUDIO_OPENSLES_DEBUG_ERROR("Failed to create new audio instance");
  162. goto done;
  163. }
  164. if(!(_audioInstance->device = new SLAudioDevice())) {
  165. AUDIO_OPENSLES_DEBUG_ERROR("Failed to create audio device");
  166. TSK_OBJECT_SAFE_FREE(_audioInstance);
  167. goto done;
  168. }
  169. if(!(_audioInstance->callback = new SLAudioDeviceCallbackImpl())) {
  170. AUDIO_OPENSLES_DEBUG_ERROR("Failed to create audio transport");
  171. TSK_OBJECT_SAFE_FREE(_audioInstance);
  172. goto done;
  173. }
  174. if((_audioInstance->device->SetCallback(_audioInstance->callback))) {
  175. AUDIO_OPENSLES_DEBUG_ERROR("AudioDeviceModule::RegisterAudioCallback() failed");
  176. TSK_OBJECT_SAFE_FREE(_audioInstance);
  177. goto done;
  178. }
  179. if((_audioInstance->device->Init())) {
  180. AUDIO_OPENSLES_DEBUG_ERROR("AudioDeviceModule::Init() failed");
  181. TSK_OBJECT_SAFE_FREE(_audioInstance);
  182. goto done;
  183. }
  184. _audioInstance->sessionId = sessionId;
  185. audioInstance = _audioInstance;
  186. tsk_list_push_back_data(__audioInstances, (void**)&_audioInstance);
  187. }
  188. done:
  189. //= unlock the list
  190. tsk_list_unlock(__audioInstances);
  191. return audioInstance;
  192. }
  193. int audio_opensles_instance_prepare_consumer(audio_opensles_instance_handle_t* _self, tmedia_consumer_t** _consumer)
  194. {
  195. audio_opensles_instance_t* self = (audio_opensles_instance_t*)_self;
  196. const struct audio_consumer_opensles_s* consumer = (const struct audio_consumer_opensles_s*)*_consumer;
  197. if(!self || !self->device || !self->callback || !_consumer || !*_consumer) {
  198. AUDIO_OPENSLES_DEBUG_ERROR("invalid parameter");
  199. return -1;
  200. }
  201. if(self->isConsumerPrepared) {
  202. AUDIO_OPENSLES_DEBUG_WARN("Consumer already prepared");
  203. return 0;
  204. }
  205. int ret;
  206. bool _bool;
  207. tsk_safeobj_lock(self);
  208. self->callback->SetConsumer(consumer);
  209. if((ret = self->device->SpeakerIsAvailable(&_bool))) {
  210. AUDIO_OPENSLES_DEBUG_ERROR("SpeakerIsAvailable() failed with error code=%d", ret);
  211. }
  212. else {
  213. if(!_bool) {
  214. AUDIO_OPENSLES_DEBUG_ERROR("SpeakerIsAvailable() returned false");
  215. }
  216. self->isSpeakerAvailable = _bool;
  217. }
  218. if((ret = self->device->InitSpeaker())) {
  219. AUDIO_OPENSLES_DEBUG_ERROR("InitSpeaker() failed with error code=%d", ret);
  220. }
  221. else if((ret = self->device->SetSpeakerOn(audio_consumer_opensles_is_speakerOn(consumer)))) {
  222. AUDIO_OPENSLES_DEBUG_ERROR("SetSpeakerOn() failed with error code=%d", ret);
  223. }
  224. if((ret = self->device->PlayoutIsAvailable(&_bool))) {
  225. AUDIO_OPENSLES_DEBUG_ERROR("PlayoutIsAvailable() failed with error code =%d", ret);
  226. }
  227. else {
  228. if(!_bool) {
  229. AUDIO_OPENSLES_DEBUG_ERROR("PlayoutIsAvailable() returned false");
  230. }
  231. self->isPlayoutAvailable = _bool;
  232. }
  233. if((ret = self->device->SetStereoPlayout(((*_consumer)->audio.in.channels == 2)))) {
  234. AUDIO_OPENSLES_DEBUG_ERROR("SetStereoPlayout(%d==2) failed with error code=%d", (*_consumer)->audio.in.channels, ret);
  235. }
  236. //if((ret = self->device->SetPlayoutBuffer(AudioDeviceModule::kFixedBufferSize, (*_consumer)->audio.ptime))){
  237. // AUDIO_OPENSLES_DEBUG_ERROR("SetPlayoutBuffer(%d ms) failed with error code=%d", (*_consumer)->audio.ptime, ret);
  238. //}
  239. // always request 10ms buffers
  240. if((ret = self->device->SetPlayoutBuffer(10))) {
  241. AUDIO_OPENSLES_DEBUG_ERROR("SetPlayoutBuffer(%d ms) failed with error code=%d", 10, ret);
  242. }
  243. int playoutSampleRate = (*_consumer)->audio.out.rate ? (*_consumer)->audio.out.rate : (*_consumer)->audio.in.rate;
  244. if((ret = self->device->SetPlayoutSampleRate(playoutSampleRate))) {
  245. AUDIO_OPENSLES_DEBUG_ERROR("SetPlayoutSampleRate(%d) failed with error code=%d", playoutSampleRate, ret);
  246. }
  247. if((ret = self->device->InitPlayout())) {
  248. AUDIO_OPENSLES_DEBUG_ERROR("AudioDeviceModule::InitPlayout() failed with error code = %d", ret);
  249. goto done;
  250. }
  251. // init output parameters
  252. if((ret = self->device->StereoPlayout(&_bool))) {
  253. AUDIO_OPENSLES_DEBUG_ERROR("StereoPlayout() failed with error code=%d", ret);
  254. }
  255. else {
  256. (*_consumer)->audio.out.channels = (_bool ? 2 : 1);
  257. }
  258. if((ret = self->device->PlayoutSampleRate(&playoutSampleRate))) {
  259. AUDIO_OPENSLES_DEBUG_ERROR("PlayoutSampleRate() failed with error code=%d", ret);
  260. }
  261. else {
  262. (*_consumer)->audio.out.rate = playoutSampleRate;
  263. }
  264. done:
  265. tsk_safeobj_unlock(self);
  266. self->isConsumerPrepared = (ret == 0);
  267. return ret;
  268. }
  269. int audio_opensles_instance_prepare_producer(audio_opensles_instance_handle_t* _self, tmedia_producer_t** _producer)
  270. {
  271. audio_opensles_instance_t* self = (audio_opensles_instance_t*)_self;
  272. if(!self || !self->device || !self->callback || !_producer || !*_producer) {
  273. AUDIO_OPENSLES_DEBUG_ERROR("invalid parameter");
  274. return -1;
  275. }
  276. if(self->isProducerPrepared) {
  277. AUDIO_OPENSLES_DEBUG_WARN("Producer already prepared");
  278. return 0;
  279. }
  280. int ret;
  281. bool _bool;
  282. tsk_safeobj_lock(self);
  283. self->callback->SetProducer((const struct audio_producer_opensles_s*)*_producer);
  284. if((ret = self->device->RecordingIsAvailable(&_bool))) {
  285. AUDIO_OPENSLES_DEBUG_ERROR("RecordingIsAvailable() failed with error code =%d", ret);
  286. }
  287. else {
  288. if(!_bool) {
  289. AUDIO_OPENSLES_DEBUG_ERROR("RecordingIsAvailable() returned false");
  290. }
  291. self->isRecordingAvailable = _bool;
  292. }
  293. if((ret = self->device->MicrophoneIsAvailable(&_bool))) {
  294. AUDIO_OPENSLES_DEBUG_ERROR("MicrophoneIsAvailable() failed with error code =%d", ret);
  295. }
  296. else {
  297. if(!_bool) {
  298. AUDIO_OPENSLES_DEBUG_ERROR("MicrophoneIsAvailable() returned false");
  299. }
  300. else {
  301. if((ret = self->device->InitMicrophone())) {
  302. AUDIO_OPENSLES_DEBUG_ERROR("InitMicrophone() failed with error code =%d", ret);
  303. }
  304. }
  305. }
  306. if((ret = self->device->SetStereoRecording(((*_producer)->audio.channels == 2)))) {
  307. AUDIO_OPENSLES_DEBUG_ERROR("SetStereoRecording(%d==2) failed with error code=%d", (*_producer)->audio.channels, ret);
  308. }
  309. int recordingSampleRate = (*_producer)->audio.rate;
  310. if((ret = self->device->SetRecordingSampleRate(recordingSampleRate))) {
  311. AUDIO_OPENSLES_DEBUG_ERROR("SetRecordingSampleRate(%d) failed with error code=%d", recordingSampleRate, ret);
  312. }
  313. if((ret = self->device->InitRecording())) {
  314. AUDIO_OPENSLES_DEBUG_ERROR("AudioDeviceModule::InitRecording() failed with error code = %d", ret);
  315. goto done;
  316. }
  317. // init output parameters
  318. if((ret = self->device->StereoRecording(&_bool))) {
  319. AUDIO_OPENSLES_DEBUG_ERROR("StereoRecording() failed with error code=%d", ret);
  320. }
  321. else {
  322. (*_producer)->audio.channels = (_bool ? 2 : 1);
  323. }
  324. if((ret = self->device->RecordingSampleRate(&recordingSampleRate))) {
  325. AUDIO_OPENSLES_DEBUG_ERROR("RecordingSampleRate() failed with error code=%d", ret);
  326. }
  327. else {
  328. (*_producer)->audio.rate = recordingSampleRate;
  329. }
  330. done:
  331. tsk_safeobj_unlock(self);
  332. self->isProducerPrepared = (ret == 0);
  333. return ret;
  334. }
  335. int audio_opensles_instance_start_consumer(audio_opensles_instance_handle_t* _self)
  336. {
  337. audio_opensles_instance_t* self = (audio_opensles_instance_t*)_self;
  338. if(!self || !self->device || !self->callback) {
  339. AUDIO_OPENSLES_DEBUG_ERROR("invalid parameter");
  340. return -1;
  341. }
  342. tsk_safeobj_lock(self);
  343. if(!self->isConsumerPrepared) {
  344. AUDIO_OPENSLES_DEBUG_ERROR("Consumer not prepared");
  345. goto done;
  346. }
  347. if(self->isConsumerStarted) {
  348. AUDIO_OPENSLES_DEBUG_WARN("Consumer already started");
  349. goto done;
  350. }
  351. if(self->isPlayoutAvailable) {
  352. int ret;
  353. if((ret = self->device->StartPlayout())) {
  354. AUDIO_OPENSLES_DEBUG_ERROR("StartPlayout() failed with error code = %d", ret);
  355. }
  356. self->isConsumerStarted = self->device->Playing();
  357. AUDIO_OPENSLES_DEBUG_INFO("isPlaying=%s", (self->isConsumerPrepared ? "true" : "false"));
  358. }
  359. done:
  360. tsk_safeobj_unlock(self);
  361. return (self->isConsumerStarted ? 0 : -1);
  362. }
  363. int audio_opensles_instance_start_producer(audio_opensles_instance_handle_t* _self)
  364. {
  365. audio_opensles_instance_t* self = (audio_opensles_instance_t*)_self;
  366. if(!self || !self->device || !self->callback) {
  367. AUDIO_OPENSLES_DEBUG_ERROR("invalid parameter");
  368. return -1;
  369. }
  370. tsk_safeobj_lock(self);
  371. if(!self->isProducerPrepared) {
  372. AUDIO_OPENSLES_DEBUG_ERROR("Producer not prepared");
  373. goto done;
  374. }
  375. if(self->isProducerStarted) {
  376. AUDIO_OPENSLES_DEBUG_WARN("Consumer already started");
  377. goto done;
  378. }
  379. if(self->isRecordingAvailable) {
  380. int ret;
  381. if((ret = self->device->StartRecording())) {
  382. AUDIO_OPENSLES_DEBUG_ERROR("StartRecording() failed with error code = %d", ret);
  383. }
  384. self->isProducerStarted = self->device->Recording();
  385. AUDIO_OPENSLES_DEBUG_INFO("isRecording=%s", (self->isProducerStarted ? "true" : "false"));
  386. }
  387. done:
  388. tsk_safeobj_unlock(self);
  389. return (self->isProducerStarted ? 0 : -1);
  390. }
  391. int audio_opensles_instance_stop_consumer(audio_opensles_instance_handle_t* _self)
  392. {
  393. audio_opensles_instance_t* self = (audio_opensles_instance_t*)_self;
  394. if(!self || !self->device || !self->callback) {
  395. AUDIO_OPENSLES_DEBUG_ERROR("invalid parameter");
  396. return -1;
  397. }
  398. tsk_safeobj_lock(self);
  399. if(!self->isConsumerStarted) {
  400. goto done;
  401. }
  402. int ret;
  403. if((ret = self->device->StopPlayout())) {
  404. AUDIO_OPENSLES_DEBUG_ERROR("StopPlayout() failed with error code = %d", ret);
  405. }
  406. else {
  407. self->isConsumerStarted = self->device->Playing();
  408. self->isConsumerPrepared = false;
  409. }
  410. done:
  411. tsk_safeobj_unlock(self);
  412. return (self->isConsumerStarted ? -1 : 0);
  413. }
  414. int audio_opensles_instance_stop_producer(audio_opensles_instance_handle_t* _self)
  415. {
  416. audio_opensles_instance_t* self = (audio_opensles_instance_t*)_self;
  417. if(!self || !self->device || !self->callback) {
  418. AUDIO_OPENSLES_DEBUG_ERROR("invalid parameter");
  419. return -1;
  420. }
  421. tsk_safeobj_lock(self);
  422. if(!self->isProducerStarted) {
  423. goto done;
  424. }
  425. int ret;
  426. if((ret = self->device->StopRecording())) {
  427. AUDIO_OPENSLES_DEBUG_ERROR("StopRecording() failed with error code = %d", ret);
  428. }
  429. else {
  430. self->isProducerStarted = self->device->Recording();
  431. self->isProducerPrepared = false;
  432. }
  433. done:
  434. tsk_safeobj_unlock(self);
  435. return (self->isProducerStarted ? -1 : 0);
  436. }
  437. int audio_opensles_instance_set_speakerOn(audio_opensles_instance_handle_t* _self, bool speakerOn)
  438. {
  439. audio_opensles_instance_t* self = (audio_opensles_instance_t*)_self;
  440. if(!self || !self->device ) {
  441. AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter");
  442. return -1;
  443. }
  444. return self->device->SetSpeakerOn(speakerOn);
  445. }
  446. int audio_opensles_instance_set_microphone_volume(audio_opensles_instance_handle_t* _self, int32_t volume)
  447. {
  448. audio_opensles_instance_t* self = (audio_opensles_instance_t*)_self;
  449. if(!self || !self->device ) {
  450. AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter");
  451. return -1;
  452. }
  453. return self->device->SetMicrophoneVolume(volume);
  454. }
  455. int audio_opensles_instance_destroy(audio_opensles_instance_handle_t** _self)
  456. {
  457. if(!_self || !*_self) {
  458. AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter");
  459. return -1;
  460. }
  461. tsk_list_lock(__audioInstances);
  462. if(tsk_object_get_refcount(*_self)==1) {
  463. tsk_list_remove_item_by_data(__audioInstances, *_self);
  464. }
  465. else {
  466. tsk_object_unref(*_self);
  467. }
  468. tsk_list_unlock(__audioInstances);
  469. *_self = tsk_null;
  470. return 0;
  471. }