audio_opensles_device.cxx 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. /* Copyright (C) 2012 Doubango Telecom <http://www.doubango.org>. All Rights Reserved.
  2. * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "audio_opensles_device.h"
  11. #include <stdio.h>
  12. #include <string.h>
  13. #define CHECK_TRUE(_bool, _text) { if(!_bool){ AUDIO_OPENSLES_DEBUG_ERROR(_text); return -1; } }
  14. #define CHECK_FALSE(_bool, _text) { if(_bool){ AUDIO_OPENSLES_DEBUG_ERROR(_text); return -1; } }
  15. #define CHECK_PLAYOUT_INITIALIZED() CHECK_TRUE(m_bPlayoutInitialized, "Playout not initialized")
  16. #define CHECK_PLAYOUT_NOT_INITIALIZED() CHECK_FALSE(m_bPlayoutInitialized, "Playout initialized")
  17. #define CHECK_RECORDING_INITIALIZED() CHECK_TRUE(m_bRecordingInitialized, "Recording not initialized")
  18. #define CHECK_RECORDING_NOT_INITIALIZED() CHECK_FALSE(m_bRecordingInitialized, "Recording initialized")
  19. #define CHECK_MICROPHONE_INITIALIZED() CHECK_TRUE(m_bMicrophoneInitialized, "Microphone not initialized")
  20. #define CHECK_MICROPHONE_NOT_INITIALIZED() CHECK_FALSE(m_bMicrophoneInitialized, "Microphone initialized")
  21. #if AUDIO_OPENSLES_UNDER_ANDROID
  22. static inline SLuint32 SL_SAMPLING_RATE(int RATE_INT)
  23. {
  24. switch(RATE_INT) {
  25. case 8000:
  26. return SL_SAMPLINGRATE_8;
  27. case 11025:
  28. return SL_SAMPLINGRATE_11_025;
  29. default:
  30. case 16000:
  31. return SL_SAMPLINGRATE_16;
  32. case 22050:
  33. return SL_SAMPLINGRATE_22_05;
  34. case 24000:
  35. return SL_SAMPLINGRATE_24;
  36. case 32000:
  37. return SL_SAMPLINGRATE_32;
  38. case 44100:
  39. return SL_SAMPLINGRATE_44_1;
  40. case 64000:
  41. return SL_SAMPLINGRATE_64;
  42. case 88200:
  43. return SL_SAMPLINGRATE_88_2;
  44. case 96000:
  45. return SL_SAMPLINGRATE_96;
  46. case 192000:
  47. return SL_SAMPLINGRATE_192;
  48. }
  49. }
  50. #endif
  51. SLAudioDevice::SLAudioDevice(const SLAudioDeviceCallback* pCallback):
  52. #if AUDIO_OPENSLES_UNDER_ANDROID
  53. m_slEngineObject(NULL),
  54. m_slPlayer(NULL),
  55. m_slEngine(NULL),
  56. m_slPlayerPlay(NULL),
  57. m_slPlayerSimpleBufferQueue(NULL),
  58. m_slOutputMixObject(NULL),
  59. m_slSpeakerVolume(NULL),
  60. m_slRecorder(NULL),
  61. m_slRecorderRecord(NULL),
  62. m_slAudioIODeviceCapabilities(NULL),
  63. m_slRecorderSimpleBufferQueue(NULL),
  64. m_slMicVolume(NULL),
  65. _playQueueSeq(0),
  66. _recCurrentSeq(0),
  67. _recBufferTotalSize(0),
  68. _recQueueSeq(0),
  69. #endif
  70. m_nMicDeviceId(0),
  71. m_pCallback(pCallback),
  72. m_bInitialized(false),
  73. m_bSpeakerInitialized(false),
  74. m_bSpeakerOn(false),
  75. m_bPlayoutInitialized(false),
  76. m_bRecordingInitialized(false),
  77. m_bMicrophoneInitialized(false),
  78. m_bStereoPlayout(false),
  79. m_bStereoRecording(false),
  80. m_nPlayoutSampleRate(PLAYOUT_SAMPLE_RATE),
  81. m_nRecordingSampleRate(RECORDING_SAMPLE_RATE),
  82. m_nRecordingBufferSize(RECORDING_BUFFER_SIZE),
  83. m_nPlayoutBufferSize(PLAYOUT_BUFFER_SIZE),
  84. m_bPlaying(false),
  85. m_bRecording(false),
  86. m_nSpeakerVolume(0),
  87. m_nMinSpeakerVolume(0),
  88. m_nMaxSpeakerVolume(0)
  89. {
  90. #if AUDIO_OPENSLES_UNDER_ANDROID
  91. memset(_playQueueBuffer, 0, sizeof(_playQueueBuffer));
  92. memset(_recQueueBuffer, 0, sizeof(_recQueueBuffer));
  93. memset(_recBuffer, 0, sizeof(_recBuffer));
  94. memset(_recLength, 0, sizeof(_recLength));
  95. memset(_recSeqNumber, 0, sizeof(_recSeqNumber));
  96. #endif
  97. }
  98. SLAudioDevice::~SLAudioDevice()
  99. {
  100. }
  101. int SLAudioDevice::SetCallback(const SLAudioDeviceCallback* pCallback)
  102. {
  103. m_pCallback = pCallback;
  104. return 0;
  105. }
  106. int SLAudioDevice::Init()
  107. {
  108. CHECK_FALSE(m_bInitialized, "Already initialized");
  109. AUDIO_OPENSLES_DEBUG_INFO("SLAudioDevice::Init()");
  110. #if AUDIO_OPENSLES_UNDER_ANDROID
  111. SLresult slResult;
  112. SLEngineOption EngineOption[] = {
  113. { (SLuint32) SL_ENGINEOPTION_THREADSAFE, (SLuint32) SL_BOOLEAN_TRUE },
  114. };
  115. slResult = slCreateEngine(&m_slEngineObject, 1, EngineOption, 0, NULL, NULL);
  116. if (slResult != SL_RESULT_SUCCESS) {
  117. AUDIO_OPENSLES_DEBUG_ERROR("Failed to create Engine with error code = %d", slResult);
  118. return -1;
  119. }
  120. if ((slResult = (*m_slEngineObject)->Realize(m_slEngineObject, SL_BOOLEAN_FALSE)) != SL_RESULT_SUCCESS) {
  121. AUDIO_OPENSLES_DEBUG_ERROR("Failed to Realize SL Engine with erro code = %d", slResult);
  122. return -1;
  123. }
  124. if ((slResult = (*m_slEngineObject)->GetInterface(m_slEngineObject, SL_IID_ENGINE, (void*) &m_slEngine)) != SL_RESULT_SUCCESS) {
  125. AUDIO_OPENSLES_DEBUG_ERROR("Failed to get SL Engine interface with error code = %d", slResult);
  126. return -1;
  127. }
  128. #endif /* AUDIO_OPENSLES_UNDER_ANDROID */
  129. m_bInitialized = true;
  130. AUDIO_OPENSLES_DEBUG_INFO("SL engine initialized");
  131. return 0;
  132. }
  133. bool SLAudioDevice::Initialized()
  134. {
  135. return m_bInitialized;
  136. }
  137. int SLAudioDevice::SpeakerIsAvailable(bool *pAvailable)
  138. {
  139. CHECK_TRUE(m_bInitialized, "Not initialized");
  140. if(!pAvailable) {
  141. AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter");
  142. return -1;
  143. }
  144. *pAvailable = true;
  145. return 0;
  146. }
  147. int SLAudioDevice::InitSpeaker()
  148. {
  149. AUDIO_OPENSLES_DEBUG_INFO("SLAudioDevice::InitSpeaker()");
  150. CHECK_TRUE(m_bInitialized, "Not initialized");
  151. if(m_bSpeakerInitialized) {
  152. return 0;
  153. }
  154. m_bSpeakerInitialized = true;
  155. return 0;
  156. }
  157. int SLAudioDevice::SetMaxSpeakerVolume(int nMaxSpeakerVolume)
  158. {
  159. CHECK_TRUE(m_bSpeakerInitialized, "Speaker not initialized");
  160. AUDIO_OPENSLES_DEBUG_INFO("SetMaxSpeakerVolume(%d)", nMaxSpeakerVolume);
  161. m_nMaxSpeakerVolume = nMaxSpeakerVolume;
  162. return 0;
  163. }
  164. int SLAudioDevice::SetMinSpeakerVolume(int nMinSpeakerVolume)
  165. {
  166. CHECK_TRUE(m_bSpeakerInitialized, "Speaker not initialized");
  167. AUDIO_OPENSLES_DEBUG_INFO("SetMinSpeakerVolume(%d)", nMinSpeakerVolume);
  168. m_nMinSpeakerVolume = nMinSpeakerVolume;
  169. return 0;
  170. }
  171. int SLAudioDevice::SetSpeakerVolume(int nSpeakerVolume)
  172. {
  173. CHECK_TRUE(m_bSpeakerInitialized, "Speaker not initialized");
  174. AUDIO_OPENSLES_DEBUG_INFO("SetSpeakerVolume(%d)", nSpeakerVolume);
  175. m_nSpeakerVolume = nSpeakerVolume;
  176. return 0;
  177. }
  178. int SLAudioDevice::SetSpeakerOn(bool bSpeakerOn)
  179. {
  180. CHECK_TRUE(m_bSpeakerInitialized, "Speaker not initialized");
  181. AUDIO_OPENSLES_DEBUG_INFO("SetSpeakerOn(%s -> %s)", (m_bSpeakerOn ? "true" : "false"), (bSpeakerOn ? "true" : "false"));
  182. int ret = 0;
  183. bool oldValue = m_bSpeakerOn;
  184. m_bSpeakerOn = bSpeakerOn; // update value beacause use in PlayoutApplyNewConfig();
  185. if(m_bPlayoutInitialized && (oldValue != bSpeakerOn)) {
  186. ret = PlayoutApplyNewConfig();
  187. }
  188. if(ret != 0) {
  189. m_bSpeakerOn = oldValue;
  190. }
  191. return ret;
  192. }
  193. int SLAudioDevice::PlayoutIsAvailable(bool *pAvailable)
  194. {
  195. CHECK_TRUE(m_bInitialized, "Not initialized");
  196. CHECK_PLAYOUT_NOT_INITIALIZED();
  197. if(!pAvailable) {
  198. AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter");
  199. return -1;
  200. }
  201. *pAvailable = true;
  202. return 0;
  203. }
  204. int SLAudioDevice::SetStereoPlayout(bool bEnabled)
  205. {
  206. CHECK_TRUE(m_bInitialized, "Not initialized");
  207. CHECK_PLAYOUT_NOT_INITIALIZED();
  208. m_bStereoPlayout = bEnabled;
  209. return 0;
  210. }
  211. int SLAudioDevice::SetPlayoutBuffer(int nPlayoutBufferSize)
  212. {
  213. CHECK_TRUE(m_bInitialized, "Not initialized");
  214. CHECK_PLAYOUT_NOT_INITIALIZED();
  215. if(PLAYOUT_BUFFER_SIZE != nPlayoutBufferSize) {
  216. AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter");
  217. return -1;
  218. }
  219. m_nPlayoutBufferSize = nPlayoutBufferSize;
  220. return 0;
  221. }
  222. int SLAudioDevice::SetPlayoutSampleRate(int nPlayoutSampleRate)
  223. {
  224. CHECK_TRUE(m_bInitialized, "Not initialized");
  225. CHECK_PLAYOUT_NOT_INITIALIZED();
  226. AUDIO_OPENSLES_DEBUG_INFO("SetPlayoutSampleRate(%d)", nPlayoutSampleRate);
  227. switch(nPlayoutSampleRate) {
  228. case 8000:
  229. case 11025:
  230. case 16000:
  231. case 22050:
  232. case 24000:
  233. case 32000:
  234. case 44100:
  235. case 64000:
  236. case 88200:
  237. case 96000:
  238. case 192000: {
  239. m_nPlayoutSampleRate = nPlayoutSampleRate;
  240. return 0;
  241. }
  242. default: {
  243. AUDIO_OPENSLES_DEBUG_ERROR("%d not valid sampling rate", nPlayoutSampleRate);
  244. return -1;
  245. }
  246. }
  247. }
  248. int SLAudioDevice::InitPlayout()
  249. {
  250. CHECK_TRUE(m_bInitialized, "Not initialized");
  251. AUDIO_OPENSLES_DEBUG_INFO("SLAudioDevice::InitPlayout()");
  252. if(m_bPlayoutInitialized) {
  253. return 0;
  254. }
  255. if (m_bPlaying) {
  256. AUDIO_OPENSLES_DEBUG_ERROR("Playout already started");
  257. return -1;
  258. }
  259. // Initialize the speaker
  260. if (InitSpeaker()) {
  261. AUDIO_OPENSLES_DEBUG_ERROR("InitSpeaker() failed");
  262. }
  263. #if AUDIO_OPENSLES_UNDER_ANDROID
  264. if (m_slEngineObject == NULL || m_slEngine == NULL) {
  265. AUDIO_OPENSLES_DEBUG_ERROR("SLObject or Engiine is NULL");
  266. return -1;
  267. }
  268. SLresult slResult;
  269. SLDataFormat_PCM pcm;
  270. SLDataSource audioSource;
  271. SLDataLocator_AndroidSimpleBufferQueue simpleBufferQueue;
  272. SLDataSink audioSink;
  273. SLDataLocator_OutputMix locator_outputmix;
  274. // Create Output Mix object to be used by player
  275. SLInterfaceID ids[N_MAX_INTERFACES];
  276. SLboolean req[N_MAX_INTERFACES];
  277. for (unsigned int i = 0; i < N_MAX_INTERFACES; i++) {
  278. ids[i] = SL_IID_NULL;
  279. req[i] = SL_BOOLEAN_FALSE;
  280. }
  281. ids[0] = SL_IID_ENVIRONMENTALREVERB;
  282. if ((slResult = (*m_slEngine)->CreateOutputMix(m_slEngine, &m_slOutputMixObject, 1, ids, req)) != SL_RESULT_SUCCESS) {
  283. AUDIO_OPENSLES_DEBUG_ERROR("CreateOutputMix() for playout failed with error code = %d", slResult);
  284. return -1;
  285. }
  286. if ((slResult = (*m_slOutputMixObject)->Realize(m_slOutputMixObject, SL_BOOLEAN_FALSE)) != SL_RESULT_SUCCESS) {
  287. AUDIO_OPENSLES_DEBUG_ERROR("Failed to realize SL Output Mix object for playout with error code = %d", slResult);
  288. return -1;
  289. }
  290. simpleBufferQueue.locatorType = SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE;
  291. simpleBufferQueue.numBuffers = N_PLAY_QUEUE_BUFFERS;
  292. pcm.formatType = SL_DATAFORMAT_PCM;
  293. pcm.numChannels = m_bStereoPlayout ? 2 : 1;
  294. pcm.samplesPerSec = SL_SAMPLING_RATE(m_nPlayoutSampleRate);
  295. pcm.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16;
  296. pcm.containerSize = SL_PCMSAMPLEFORMAT_FIXED_16;
  297. pcm.channelMask = m_bStereoRecording ? (SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT) : SL_SPEAKER_FRONT_CENTER;
  298. pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;
  299. audioSource.pFormat = (void *) &pcm;
  300. audioSource.pLocator = (void *) &simpleBufferQueue;
  301. locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
  302. locator_outputmix.outputMix = m_slOutputMixObject;
  303. audioSink.pLocator = (void *) &locator_outputmix;
  304. audioSink.pFormat = NULL;
  305. ids[0] = SL_IID_ANDROIDSIMPLEBUFFERQUEUE;
  306. ids[1] = SL_IID_EFFECTSEND;
  307. ids[2] = SL_IID_ANDROIDCONFIGURATION;
  308. ids[3] = SL_IID_VOLUME;
  309. req[0] = SL_BOOLEAN_TRUE;
  310. req[1] = SL_BOOLEAN_TRUE;
  311. req[2] = SL_BOOLEAN_TRUE;
  312. req[3] = SL_BOOLEAN_TRUE;
  313. // Create the player
  314. if ((slResult = (*m_slEngine)->CreateAudioPlayer(m_slEngine, &m_slPlayer, &audioSource, &audioSink, 3, ids, req)) != SL_RESULT_SUCCESS) {
  315. AUDIO_OPENSLES_DEBUG_ERROR("Failed to create Audio Player with error code = %d", slResult);
  316. return -1;
  317. }
  318. // set stream type
  319. if(!m_bSpeakerOn) { // only set if speaker OFF, otherwise default is ON. "SL_ANDROID_STREAM_MEDIA" doen't look to work on all devices
  320. static SLAndroidConfigurationItf _playerStreamConfig;
  321. if((slResult = (*m_slPlayer)->GetInterface(m_slPlayer, SL_IID_ANDROIDCONFIGURATION, &_playerStreamConfig)) != SL_RESULT_SUCCESS) {
  322. AUDIO_OPENSLES_DEBUG_ERROR("Failed to get player configuration with error code = %d", slResult);
  323. return -1;
  324. }
  325. else {
  326. static SLint32 _playerStreamType = m_bSpeakerOn ? SL_ANDROID_STREAM_MEDIA : SL_ANDROID_STREAM_VOICE;
  327. static SLint32 _playerStreamTypeSize = sizeof(SLint32);
  328. AUDIO_OPENSLES_DEBUG_INFO("_playerStreamType=%d", _playerStreamType);
  329. if((slResult = (*_playerStreamConfig)->SetConfiguration(_playerStreamConfig, SL_ANDROID_KEY_STREAM_TYPE, &_playerStreamType, _playerStreamTypeSize))) {
  330. AUDIO_OPENSLES_DEBUG_ERROR("Failed to set player stream type with error code = %d", slResult);
  331. return -2;
  332. }
  333. }
  334. }
  335. // Realizing the player in synchronous mode
  336. if ((slResult = (*m_slPlayer)->Realize(m_slPlayer, SL_BOOLEAN_FALSE)) != SL_RESULT_SUCCESS) {
  337. AUDIO_OPENSLES_DEBUG_ERROR("Failed to realize the player with error code = %d", slResult);
  338. return -1;
  339. }
  340. // Get seek and play interfaces
  341. if ((slResult = (*m_slPlayer)->GetInterface(m_slPlayer, SL_IID_PLAY, (void*) &m_slPlayerPlay)) != SL_RESULT_SUCCESS) {
  342. AUDIO_OPENSLES_DEBUG_ERROR("Failed to get Player interface with error code = %d", slResult);
  343. return -1;
  344. }
  345. if ((slResult = (*m_slPlayer)->GetInterface(m_slPlayer, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, (void*) &m_slPlayerSimpleBufferQueue)) != SL_RESULT_SUCCESS) {
  346. AUDIO_OPENSLES_DEBUG_ERROR("Failed to get Player Simple Buffer Queue interface with error code = %d", slResult);
  347. return -1;
  348. }
  349. // Setup to receive buffer queue event callbacks
  350. if ((slResult = (*m_slPlayerSimpleBufferQueue)->RegisterCallback(m_slPlayerSimpleBufferQueue, PlayerSimpleBufferQueueCallback, this)) != SL_RESULT_SUCCESS) {
  351. AUDIO_OPENSLES_DEBUG_ERROR("Failed to register Player Callback");
  352. return -1;
  353. }
  354. #endif /* AUDIO_OPENSLES_UNDER_ANDROID */
  355. m_bPlayoutInitialized = true;
  356. AUDIO_OPENSLES_DEBUG_INFO("Playout initialized");
  357. return 0;
  358. }
  359. int SLAudioDevice::StereoPlayout(bool *pEnabled)
  360. {
  361. CHECK_TRUE(m_bInitialized, "Not initialized");
  362. CHECK_PLAYOUT_INITIALIZED();
  363. if(!pEnabled) {
  364. AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter");
  365. return -1;
  366. }
  367. *pEnabled = m_bStereoPlayout;
  368. return 0;
  369. }
  370. int SLAudioDevice::PlayoutSampleRate(int *pPlayoutSampleRate)
  371. {
  372. CHECK_TRUE(m_bInitialized, "Not initialized");
  373. CHECK_PLAYOUT_INITIALIZED();
  374. if(!pPlayoutSampleRate) {
  375. AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter");
  376. return -1;
  377. }
  378. *pPlayoutSampleRate = m_nPlayoutSampleRate;
  379. return 0;
  380. }
  381. int SLAudioDevice::StartPlayout()
  382. {
  383. AUDIO_OPENSLES_DEBUG_INFO("SLAudioDevice::StartPlayout()");
  384. CHECK_TRUE(m_bInitialized, "Not initialized");
  385. CHECK_PLAYOUT_INITIALIZED();
  386. if (m_bPlaying) {
  387. return 0;
  388. }
  389. #if AUDIO_OPENSLES_UNDER_ANDROID
  390. if (m_slPlayerPlay == NULL) {
  391. AUDIO_OPENSLES_DEBUG_ERROR("PlayItf is NULL");
  392. return -1;
  393. }
  394. if (m_slPlayerSimpleBufferQueue == NULL) {
  395. AUDIO_OPENSLES_DEBUG_ERROR("PlayerSimpleBufferQueue is NULL");
  396. return -1;
  397. }
  398. _recQueueSeq = 0;
  399. SLresult slResult;
  400. /* Enqueue a set of zero buffers to get the ball rolling */
  401. uint32_t nSample10ms = m_nPlayoutSampleRate / 100;
  402. uint8_t playBuffer[nSample10ms << BYTES_PER_SAMPLE_LOG2];
  403. uint32_t noSamplesOut(0);
  404. {
  405. // get data from jitter buffer
  406. noSamplesOut = SLAudioDevice::PullPlayoutData(playBuffer, nSample10ms);
  407. if(noSamplesOut != nSample10ms) {
  408. AUDIO_OPENSLES_DEBUG_WARN("%d not expected as samples output count value", noSamplesOut);
  409. noSamplesOut = nSample10ms;
  410. memset(_playQueueBuffer[_playQueueSeq], 0, (noSamplesOut << BYTES_PER_SAMPLE_LOG2));
  411. }
  412. else {
  413. memcpy(_playQueueBuffer[_playQueueSeq], playBuffer, (noSamplesOut << BYTES_PER_SAMPLE_LOG2));
  414. }
  415. // write the buffer data we into the device
  416. if ((slResult = (*m_slPlayerSimpleBufferQueue)->Enqueue(m_slPlayerSimpleBufferQueue, (void*) _playQueueBuffer[_playQueueSeq], (noSamplesOut << 1))) != SL_RESULT_SUCCESS) {
  417. AUDIO_OPENSLES_DEBUG_ERROR("Player simpler buffer queue Enqueue failed with error code = %d and noSamplesOut = %d", slResult, noSamplesOut);
  418. }
  419. _playQueueSeq = (_playQueueSeq + 1) % N_PLAY_QUEUE_BUFFERS;
  420. }
  421. // Play the PCM samples using a buffer queue
  422. m_bPlaying = true;
  423. if ((slResult = (*m_slPlayerPlay)->SetPlayState(m_slPlayerPlay, SL_PLAYSTATE_PLAYING)) != SL_RESULT_SUCCESS) {
  424. AUDIO_OPENSLES_DEBUG_ERROR("Failed to start playout with error code = %d", slResult);
  425. m_bPlaying = false;
  426. return -1;
  427. }
  428. #else
  429. m_bPlaying = true;
  430. #endif /* AUDIO_OPENSLES_UNDER_ANDROID */
  431. AUDIO_OPENSLES_DEBUG_INFO("Payout started - rate=%d", m_nPlayoutSampleRate);
  432. return 0;
  433. }
  434. bool SLAudioDevice::Playing()
  435. {
  436. return m_bPlaying;
  437. }
  438. int SLAudioDevice::StopPlayout()
  439. {
  440. AUDIO_OPENSLES_DEBUG_INFO("SLAudioDevice::StopPlayout()");
  441. if(!m_bPlaying) {
  442. return 0;
  443. }
  444. #if AUDIO_OPENSLES_UNDER_ANDROID
  445. if ((m_slPlayerPlay != NULL) && (m_slOutputMixObject != NULL) && (m_slPlayer != NULL)) {
  446. SLresult slResult;
  447. if ((slResult = (*m_slPlayerPlay)->SetPlayState(m_slPlayerPlay, SL_PLAYSTATE_STOPPED)) != SL_RESULT_SUCCESS) {
  448. AUDIO_OPENSLES_DEBUG_ERROR("Failed to stop playout with error code = %d", slResult);
  449. return -1;
  450. }
  451. if ((slResult = (*m_slPlayerSimpleBufferQueue)->Clear(m_slPlayerSimpleBufferQueue)) != SL_RESULT_SUCCESS) {
  452. AUDIO_OPENSLES_DEBUG_ERROR("Failed to clear recorder buffer queue");
  453. return -1;
  454. }
  455. // Destroy the player
  456. (*m_slPlayer)->Destroy(m_slPlayer);
  457. // Destroy Output Mix object
  458. (*m_slOutputMixObject)->Destroy(m_slOutputMixObject);
  459. m_slPlayer = NULL;
  460. m_slPlayerPlay = NULL;
  461. m_slPlayerSimpleBufferQueue = NULL;
  462. m_slOutputMixObject = NULL;
  463. }
  464. #endif
  465. AUDIO_OPENSLES_DEBUG_INFO("Playout stopped");
  466. m_bPlayoutInitialized = false;
  467. m_bPlaying = false;
  468. return 0;
  469. }
  470. int SLAudioDevice::RecordingIsAvailable(bool *pAvailable)
  471. {
  472. CHECK_TRUE(m_bInitialized, "Device not initialized");
  473. CHECK_RECORDING_NOT_INITIALIZED();
  474. if(!pAvailable) {
  475. AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter");
  476. return -1;
  477. }
  478. *pAvailable = true;
  479. return 0;
  480. }
  481. int SLAudioDevice::MicrophoneIsAvailable(bool *pAvailable)
  482. {
  483. CHECK_TRUE(m_bInitialized, "Device not initialized");
  484. CHECK_RECORDING_NOT_INITIALIZED();
  485. if(!pAvailable) {
  486. AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter");
  487. return -1;
  488. }
  489. *pAvailable = true;
  490. return 0;
  491. }
  492. int SLAudioDevice::InitMicrophone()
  493. {
  494. AUDIO_OPENSLES_DEBUG_INFO("SLAudioDevice::InitMicrophone()");
  495. CHECK_TRUE(m_bInitialized, "Device not initialized");
  496. if(m_bMicrophoneInitialized) {
  497. return 0;
  498. }
  499. m_bMicrophoneInitialized = true;
  500. return 0;
  501. }
  502. int SLAudioDevice::SetMicrophoneVolume(int nMicrophoneVolume)
  503. {
  504. CHECK_MICROPHONE_INITIALIZED();
  505. AUDIO_OPENSLES_DEBUG_INFO("SetMicrophoneVolume(%d)", nMicrophoneVolume);
  506. #if AUDIO_OPENSLES_UNDER_ANDROID
  507. if (m_slMicVolume == NULL) {
  508. SLresult slResult;
  509. if ((slResult = (*m_slEngineObject)->GetInterface(m_slEngineObject, SL_IID_DEVICEVOLUME, (void*) &m_slMicVolume)) != SL_RESULT_SUCCESS) {
  510. AUDIO_OPENSLES_DEBUG_ERROR("Failed to get 'SL_IID_DEVICEVOLUME' interface with error code = %d", slResult);
  511. return -1;
  512. }
  513. }
  514. if (m_slMicVolume != NULL) {
  515. SLresult slResult;
  516. int vol(0);
  517. vol = ((nMicrophoneVolume * (m_nMaxSpeakerVolume - m_nMinSpeakerVolume) + (int) (255 / 2)) / (255)) + m_nMinSpeakerVolume;
  518. if ((slResult = (*m_slMicVolume)->SetVolume(m_slMicVolume, m_nMicDeviceId, vol)) != SL_RESULT_SUCCESS) {
  519. AUDIO_OPENSLES_DEBUG_ERROR("SetVolume() failed with error code = %d", slResult);
  520. return -1;
  521. }
  522. }
  523. #endif /* AUDIO_OPENSLES_UNDER_ANDROID */
  524. return 0;
  525. }
  526. int SLAudioDevice::SetStereoRecording(bool bEnabled)
  527. {
  528. CHECK_TRUE(m_bInitialized, "Not initialized");
  529. CHECK_RECORDING_NOT_INITIALIZED();
  530. AUDIO_OPENSLES_DEBUG_INFO("SetStereoRecording(%s)", bEnabled ? "True" : "False");
  531. m_bStereoRecording = bEnabled;
  532. return 0;
  533. }
  534. int SLAudioDevice::SetRecordingSampleRate(int nRecordingSampleRate)
  535. {
  536. CHECK_TRUE(m_bInitialized, "Not initialized");
  537. CHECK_RECORDING_NOT_INITIALIZED();
  538. AUDIO_OPENSLES_DEBUG_INFO("SetRecordingSampleRate(%d)", nRecordingSampleRate);
  539. switch(nRecordingSampleRate) {
  540. case 8000:
  541. case 11025:
  542. case 16000:
  543. case 22050:
  544. case 24000:
  545. case 32000:
  546. case 44100:
  547. case 64000:
  548. case 88200:
  549. case 96000:
  550. case 192000: {
  551. m_nRecordingSampleRate = nRecordingSampleRate;
  552. return 0;
  553. }
  554. default: {
  555. AUDIO_OPENSLES_DEBUG_ERROR("%d not valid sampling rate", nRecordingSampleRate);
  556. return -1;
  557. }
  558. }
  559. }
  560. int SLAudioDevice::InitRecording()
  561. {
  562. CHECK_TRUE(m_bInitialized, "Not initialized");
  563. AUDIO_OPENSLES_DEBUG_INFO("SLAudioDevice::InitRecording()");
  564. if (m_bRecording) {
  565. AUDIO_OPENSLES_DEBUG_ERROR("Recording already started");
  566. return -1;
  567. }
  568. if (m_bRecordingInitialized) {
  569. return 0;
  570. }
  571. // Initialize the microphone
  572. if (InitMicrophone() == -1) {
  573. AUDIO_OPENSLES_DEBUG_ERROR("InitMicrophone() failed");
  574. }
  575. #if AUDIO_OPENSLES_UNDER_ANDROID
  576. if (m_slEngineObject == NULL || m_slEngine == NULL) {
  577. AUDIO_OPENSLES_DEBUG_ERROR("Recording object is NULL");
  578. return -1;
  579. }
  580. SLresult slResult;
  581. SLDataSource audioSource;
  582. SLDataLocator_IODevice micLocator;
  583. SLDataSink audioSink;
  584. SLDataFormat_PCM pcm;
  585. SLDataLocator_AndroidSimpleBufferQueue simpleBufferQueue;
  586. // Setup the data source structure
  587. micLocator.locatorType = SL_DATALOCATOR_IODEVICE;
  588. micLocator.deviceType = SL_IODEVICE_AUDIOINPUT;
  589. micLocator.deviceID = SL_DEFAULTDEVICEID_AUDIOINPUT; //micDeviceID;
  590. micLocator.device = NULL;
  591. audioSource.pLocator = (void *) &micLocator;
  592. audioSource.pFormat = NULL;
  593. /* Setup the data source structure for the buffer queue */
  594. simpleBufferQueue.locatorType = SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE;
  595. simpleBufferQueue.numBuffers = N_REC_QUEUE_BUFFERS;
  596. /* Setup the format of the content in the buffer queue */
  597. pcm.formatType = SL_DATAFORMAT_PCM;
  598. pcm.numChannels = 1;
  599. // _samplingRateIn is initialized in initSampleRate()
  600. pcm.samplesPerSec = SL_SAMPLING_RATE(m_nRecordingSampleRate);
  601. pcm.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16;
  602. pcm.containerSize = 16;
  603. pcm.channelMask = SL_SPEAKER_FRONT_CENTER;
  604. pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;
  605. audioSink.pFormat = (void *) &pcm;
  606. audioSink.pLocator = (void *) &simpleBufferQueue;
  607. const SLInterfaceID id[2] = { SL_IID_ANDROIDSIMPLEBUFFERQUEUE, SL_IID_ANDROIDCONFIGURATION };
  608. const SLboolean req[2] = { SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE };
  609. slResult = (*m_slEngine)->CreateAudioRecorder(m_slEngine, &m_slRecorder, &audioSource, &audioSink, 2, id, req);
  610. if (slResult != SL_RESULT_SUCCESS) {
  611. AUDIO_OPENSLES_DEBUG_ERROR("Failed to create Recorder with error code = %d", slResult);
  612. return -1;
  613. }
  614. // Set stream type
  615. SLAndroidConfigurationItf slRecorderConfig;
  616. SLint32 slStreamType = SL_ANDROID_RECORDING_PRESET_GENERIC;
  617. slResult = (*m_slRecorder)->GetInterface(m_slRecorder, SL_IID_ANDROIDCONFIGURATION, &slRecorderConfig);
  618. if(slResult != SL_RESULT_SUCCESS) {
  619. AUDIO_OPENSLES_DEBUG_ERROR("GetInterface(SL_IID_ANDROIDCONFIGURATION) failed with error code = %d", slResult);
  620. return -1;
  621. }
  622. AUDIO_OPENSLES_DEBUG_INFO("Recording stream type = %d", slStreamType);
  623. slResult = (*slRecorderConfig)->SetConfiguration(slRecorderConfig, SL_ANDROID_KEY_RECORDING_PRESET, &slStreamType, sizeof(SLint32));
  624. if(slResult != SL_RESULT_SUCCESS) {
  625. AUDIO_OPENSLES_DEBUG_ERROR("SetConfiguration(SL_ANDROID_KEY_RECORDING_PRESET) failed with error code = %d", slResult);
  626. return -1;
  627. }
  628. // Realizing the recorder in synchronous mode.
  629. slResult = (*m_slRecorder)->Realize(m_slRecorder, SL_BOOLEAN_FALSE);
  630. if (slResult != SL_RESULT_SUCCESS) {
  631. AUDIO_OPENSLES_DEBUG_ERROR("Failed to realize Recorder with error code = %d", slResult);
  632. return -1;
  633. }
  634. // Get the RECORD interface - it is an implicit interface
  635. slResult = (*m_slRecorder)->GetInterface(m_slRecorder, SL_IID_RECORD, (void*) &m_slRecorderRecord);
  636. if (slResult != SL_RESULT_SUCCESS) {
  637. AUDIO_OPENSLES_DEBUG_ERROR("Failed to get Recorder interface with error code = %d", slResult);
  638. return -1;
  639. }
  640. // Get the simpleBufferQueue interface
  641. slResult = (*m_slRecorder)->GetInterface(m_slRecorder, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, (void*) &m_slRecorderSimpleBufferQueue);
  642. if (slResult != SL_RESULT_SUCCESS) {
  643. AUDIO_OPENSLES_DEBUG_ERROR("Failed to get Recorder Simple Buffer Queue with error code = %d", slResult);
  644. return -1;
  645. }
  646. // Setup to receive buffer queue event callbacks
  647. slResult = (*m_slRecorderSimpleBufferQueue)->RegisterCallback(m_slRecorderSimpleBufferQueue, RecorderSimpleBufferQueueCallback, this);
  648. if (slResult != SL_RESULT_SUCCESS) {
  649. AUDIO_OPENSLES_DEBUG_ERROR("Failed to register Recorder Callback with error code = %d", slResult);
  650. return -1;
  651. }
  652. #endif /* AUDIO_OPENSLES_UNDER_ANDROID */
  653. AUDIO_OPENSLES_DEBUG_INFO("Recording initialized");
  654. m_bRecordingInitialized = true;
  655. return 0;
  656. }
  657. int SLAudioDevice::StereoRecording(bool *pEnabled)
  658. {
  659. CHECK_TRUE(m_bInitialized, "Not initialized");
  660. CHECK_RECORDING_INITIALIZED();
  661. if(!pEnabled) {
  662. AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter");
  663. return -1;
  664. }
  665. *pEnabled = m_bStereoRecording;
  666. return 0;
  667. }
  668. int SLAudioDevice::RecordingSampleRate(int *pRecordingSampleRate)
  669. {
  670. CHECK_TRUE(m_bInitialized, "Not initialized");
  671. CHECK_RECORDING_INITIALIZED();
  672. if(!pRecordingSampleRate) {
  673. AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter");
  674. return -1;
  675. }
  676. *pRecordingSampleRate = m_nRecordingSampleRate;
  677. return 0;
  678. }
  679. int SLAudioDevice::StartRecording()
  680. {
  681. AUDIO_OPENSLES_DEBUG_INFO("SLAudioDevice::StartRecording()");
  682. CHECK_TRUE(m_bInitialized, "Not initialized");
  683. CHECK_RECORDING_INITIALIZED();
  684. if (m_bRecording) {
  685. return 0;
  686. }
  687. #if AUDIO_OPENSLES_UNDER_ANDROID
  688. if (m_slRecorderRecord == NULL) {
  689. AUDIO_OPENSLES_DEBUG_ERROR("RecordITF is NULL");
  690. return -1;
  691. }
  692. if (m_slRecorderSimpleBufferQueue == NULL) {
  693. AUDIO_OPENSLES_DEBUG_ERROR("Recorder Simple Buffer Queue is NULL");
  694. return -1;
  695. }
  696. // Reset recording buffer
  697. memset(_recQueueBuffer, 0, sizeof(_recQueueBuffer)); // empty the queue
  698. _recQueueSeq = 0;
  699. memset(_recBuffer, 0, sizeof(_recBuffer));
  700. memset(_recLength, 0, sizeof(_recLength));
  701. memset(_recSeqNumber, 0, sizeof(_recSeqNumber));
  702. // Enqueue N_REC_QUEUE_BUFFERS -1 zero buffers to get the ball rolling
  703. // find out how it behaves when the sample rate is 44100
  704. SLresult slResult;
  705. int nSample10ms = m_nRecordingSampleRate / 100;
  706. for (int i = 0; i < (N_REC_QUEUE_BUFFERS - 1); i++) {
  707. // We assign 10ms buffer to each queue, size given in bytes.
  708. slResult = (*m_slRecorderSimpleBufferQueue)->Enqueue(m_slRecorderSimpleBufferQueue, (void*) _recQueueBuffer[_recQueueSeq], (nSample10ms << BYTES_PER_SAMPLE_LOG2));
  709. if (slResult != SL_RESULT_SUCCESS) {
  710. AUDIO_OPENSLES_DEBUG_ERROR("Failed to Enqueue Empty Buffer to recorder with error code = %d", slResult);
  711. return -1;
  712. }
  713. _recQueueSeq++;
  714. }
  715. // Record the audio
  716. m_bRecording = true;
  717. slResult = (*m_slRecorderRecord)->SetRecordState(m_slRecorderRecord, SL_RECORDSTATE_RECORDING);
  718. if (slResult != SL_RESULT_SUCCESS) {
  719. AUDIO_OPENSLES_DEBUG_ERROR("Failed to start recording with error code = %d", slResult);
  720. m_bRecording = false;
  721. return -1;
  722. }
  723. #else
  724. m_bRecording = true;
  725. #endif /* AUDIO_OPENSLES_UNDER_ANDROID */
  726. AUDIO_OPENSLES_DEBUG_INFO("Recording started - rate = %d", m_nRecordingSampleRate);
  727. return 0;
  728. }
  729. bool SLAudioDevice::Recording()
  730. {
  731. return m_bRecording;
  732. }
  733. int SLAudioDevice::StopRecording()
  734. {
  735. AUDIO_OPENSLES_DEBUG_INFO("SLAudioDevice::StopRecording()");
  736. if (!m_bRecording) {
  737. return 0;
  738. }
  739. #if AUDIO_OPENSLES_UNDER_ANDROID
  740. if ((m_slRecorderRecord != NULL) && (m_slRecorder != NULL)) {
  741. SLresult slResult = (*m_slRecorderRecord)->SetRecordState(m_slRecorderRecord, SL_RECORDSTATE_STOPPED);
  742. if (slResult != SL_RESULT_SUCCESS) {
  743. AUDIO_OPENSLES_DEBUG_ERROR("Failed to stop recording with error code = %d", slResult);
  744. return -1;
  745. }
  746. slResult = (*m_slRecorderSimpleBufferQueue)->Clear(m_slRecorderSimpleBufferQueue);
  747. if (slResult != SL_RESULT_SUCCESS) {
  748. AUDIO_OPENSLES_DEBUG_ERROR("Failed to clear recorder buffer queue with error code = %d", slResult);
  749. return -1;
  750. }
  751. // Destroy the recorder object
  752. (*m_slRecorder)->Destroy(m_slRecorder);
  753. m_slRecorder = NULL;
  754. m_slRecorderRecord = NULL;
  755. m_slRecorderRecord = NULL;
  756. }
  757. #endif
  758. AUDIO_OPENSLES_DEBUG_INFO("Recording stopped");
  759. m_bRecording = false;
  760. m_bRecordingInitialized = false;
  761. return 0;
  762. }
  763. int SLAudioDevice::Terminate()
  764. {
  765. if (!m_bInitialized) {
  766. return 0;
  767. }
  768. if(Recording()) {
  769. StopRecording();
  770. }
  771. if(Playing()) {
  772. StopPlayout();
  773. }
  774. #if AUDIO_OPENSLES_UNDER_ANDROID
  775. if(m_slPlayer) {
  776. (*m_slPlayer)->Destroy(m_slPlayer);
  777. m_slPlayer = NULL;
  778. m_slPlayerPlay = NULL;
  779. m_slPlayerSimpleBufferQueue = NULL;
  780. }
  781. if(m_slRecorder) {
  782. (*m_slRecorder)->Destroy(m_slRecorder);
  783. m_slRecorder = NULL;
  784. m_slRecorderRecord = NULL;
  785. m_slRecorderSimpleBufferQueue = NULL;
  786. m_slAudioIODeviceCapabilities = NULL;
  787. }
  788. if(m_slOutputMixObject) {
  789. (*m_slOutputMixObject)->Destroy(m_slOutputMixObject);
  790. m_slOutputMixObject = NULL;
  791. }
  792. if (m_slEngineObject) {
  793. (*m_slEngineObject)->Destroy(m_slEngineObject);
  794. m_slEngineObject = NULL;
  795. m_slEngine = NULL;
  796. }
  797. #endif
  798. m_bSpeakerInitialized = false;
  799. m_bPlayoutInitialized = false;
  800. m_bRecordingInitialized = false;
  801. m_bInitialized = false;
  802. return 0;
  803. }
  804. int SLAudioDevice::PlayoutApplyNewConfig()
  805. {
  806. AUDIO_OPENSLES_DEBUG_INFO("SLAudioDevice::PlayoutApplyNewConfig()");
  807. #if AUDIO_OPENSLES_UNDER_ANDROID
  808. if(m_slPlayer) {
  809. SLresult slResult;
  810. int ret;
  811. bool wasPlaying = Playing();
  812. if(wasPlaying) {
  813. if ((ret = StopPlayout())) {
  814. AUDIO_OPENSLES_DEBUG_ERROR("Failed to stop playout for reconf");
  815. return ret;
  816. }
  817. if((ret = InitPlayout())) {
  818. AUDIO_OPENSLES_DEBUG_ERROR("Failed to init() playout after reconf");
  819. return ret;
  820. }
  821. }
  822. if(wasPlaying) {
  823. if((ret = StartPlayout())) {
  824. AUDIO_OPENSLES_DEBUG_ERROR("Failed to start() playout after reconf");
  825. return ret;
  826. }
  827. }
  828. }
  829. #endif /* AUDIO_OPENSLES_UNDER_ANDROID */
  830. return 0;
  831. }
  832. uint32_t SLAudioDevice::PullPlayoutData(void* pAudioSamples, const uint32_t nSamples)
  833. {
  834. if(!pAudioSamples || !nSamples) {
  835. AUDIO_OPENSLES_DEBUG_ERROR("PullPlayoutData() - Invalid parameter");
  836. return 0;
  837. }
  838. if(!m_pCallback) {
  839. memset(pAudioSamples, 0, (nSamples << BYTES_PER_SAMPLE_LOG2));
  840. return nSamples;
  841. }
  842. uint32_t nSamplesOut = 0;
  843. const_cast<SLAudioDeviceCallback*>(m_pCallback)->NeedMorePlayData(nSamples,
  844. BYTES_PER_SAMPLE,
  845. m_bStereoPlayout ? 2 : 1,
  846. m_nPlayoutSampleRate,
  847. pAudioSamples,
  848. nSamplesOut);
  849. return nSamplesOut;
  850. }
  851. void SLAudioDevice::PushRecordingData(void* pAudioSamples, const uint32_t nSamples)
  852. {
  853. if(!pAudioSamples || !nSamples) {
  854. AUDIO_OPENSLES_DEBUG_ERROR("PushRecordingData() - Invalid parameter");
  855. return;
  856. }
  857. if(m_pCallback) {
  858. const_cast<SLAudioDeviceCallback*>(m_pCallback)->RecordedDataIsAvailable(pAudioSamples,
  859. nSamples,
  860. BYTES_PER_SAMPLE,
  861. m_bStereoRecording ? 2 : 1,
  862. m_nRecordingSampleRate);
  863. }
  864. }
  865. #if AUDIO_OPENSLES_UNDER_ANDROID
  866. void SLAudioDevice::PlayerSimpleBufferQueueCallback(SLAndroidSimpleBufferQueueItf queueItf, void *pContext)
  867. {
  868. SLAudioDevice* This = static_cast<SLAudioDevice*> (pContext);
  869. // AUDIO_OPENSLES_DEBUG_INFO("PlayerSimpleBufferQueueCallback(playing=%s, _playQueueSeq=%d)", (This->m_bPlaying ? "true" : "false"), This->_playQueueSeq);
  870. if (This->m_bPlaying && (This->_playQueueSeq < N_PLAY_QUEUE_BUFFERS)) {
  871. unsigned int noSamp10ms = This->m_nPlayoutSampleRate / 100;
  872. uint8_t playBuffer[noSamp10ms << BYTES_PER_SAMPLE_LOG2];
  873. uint32_t noSamplesOut = This->PullPlayoutData(playBuffer, noSamp10ms);
  874. if (noSamp10ms != noSamplesOut) {
  875. if(noSamplesOut) { // (noSamplesOut==0) -> jitter buffer cannot provide data
  876. AUDIO_OPENSLES_DEBUG_ERROR("noSamp10ms (%u) != noSamplesOut (%d)", noSamp10ms, noSamplesOut);
  877. }
  878. noSamplesOut = noSamp10ms;
  879. memset(This->_playQueueBuffer[This->_playQueueSeq], 0, (noSamplesOut << BYTES_PER_SAMPLE_LOG2));
  880. }
  881. else {
  882. memcpy(This->_playQueueBuffer[This->_playQueueSeq], playBuffer, (noSamplesOut << BYTES_PER_SAMPLE_LOG2));
  883. }
  884. SLresult slResult = (*This->m_slPlayerSimpleBufferQueue)->Enqueue(This->m_slPlayerSimpleBufferQueue, This->_playQueueBuffer[This->_playQueueSeq], (noSamplesOut << BYTES_PER_SAMPLE_LOG2));
  885. if (slResult != SL_RESULT_SUCCESS) {
  886. AUDIO_OPENSLES_DEBUG_ERROR("Player simpler buffer queue Enqueue failed, noSamplesOut=%d, ret=%d", noSamplesOut, slResult);
  887. return;
  888. }
  889. // update the play buffer sequency
  890. This->_playQueueSeq = (This->_playQueueSeq + 1) % N_PLAY_QUEUE_BUFFERS;
  891. }
  892. }
  893. void SLAudioDevice::RecorderSimpleBufferQueueCallback(SLAndroidSimpleBufferQueueItf queueItf, void *pContext)
  894. {
  895. // AUDIO_OPENSLES_DEBUG_INFO("RecorderSimpleBufferQueueCallback()");
  896. SLAudioDevice* This = static_cast<SLAudioDevice*> (pContext);
  897. if (This->m_bRecording) {
  898. const unsigned int noSamp10ms = This->m_nRecordingSampleRate / 100;
  899. #if 1 // not using async thread
  900. // push data
  901. This->PushRecordingData(This->_recQueueBuffer[0], noSamp10ms);
  902. // enqueue new buffer
  903. SLresult slResult = (*This->m_slRecorderSimpleBufferQueue)->Enqueue(
  904. This->m_slRecorderSimpleBufferQueue,
  905. (void*) This->_recQueueBuffer[0],
  906. (noSamp10ms << BYTES_PER_SAMPLE_LOG2));
  907. if (slResult != SL_RESULT_SUCCESS) {
  908. AUDIO_OPENSLES_DEBUG_WARN("Failed to enqueue recording buffer with error code = %d", slResult);
  909. return;
  910. }
  911. #else
  912. unsigned int dataPos = 0;
  913. uint16_t bufPos = 0;
  914. int16_t insertPos = -1;
  915. unsigned int nCopy = 0; // Number of samples to copy
  916. while (dataPos < noSamp10ms) {
  917. // Loop over all recording buffers or until we find the partially
  918. // full buffer
  919. // First choice is to insert into partially full buffer,
  920. // second choice is to insert into empty buffer
  921. bufPos = 0;
  922. insertPos = -1;
  923. nCopy = 0;
  924. while (bufPos < N_REC_BUFFERS) {
  925. if ((This->_recLength[bufPos] > 0) && (This->_recLength[bufPos] < noSamp10ms)) {
  926. // Found the partially full buffer
  927. insertPos = static_cast<int16_t> (bufPos);
  928. bufPos = N_REC_BUFFERS; // Don't need to search more
  929. }
  930. else if ((-1 == insertPos) && (0 == This->_recLength[bufPos])) {
  931. // Found an empty buffer
  932. insertPos = static_cast<int16_t> (bufPos);
  933. }
  934. ++bufPos;
  935. }
  936. if (insertPos > -1) {
  937. // We found a non-full buffer, copy data from the buffer queue
  938. // o recBuffer
  939. unsigned int dataToCopy = noSamp10ms - dataPos;
  940. unsigned int currentRecLen = _recLength[insertPos];
  941. unsigned int roomInBuffer = noSamp10ms - currentRecLen;
  942. nCopy = (dataToCopy < roomInBuffer ? dataToCopy : roomInBuffer);
  943. memcpy(&This->_recBuffer[insertPos][currentRecLen], &This->_recQueueBuffer[This->_recQueueSeq][dataPos], nCopy * sizeof(short));
  944. if (0 == currentRecLen) {
  945. _recSeqNumber[insertPos] = This->_recCurrentSeq;
  946. ++_recCurrentSeq;
  947. }
  948. This->_recBufferTotalSize += nCopy;
  949. // Has to be done last to avoid interrupt problems
  950. // between threads
  951. This->_recLength[insertPos] += nCopy;
  952. dataPos += nCopy;
  953. }
  954. else {
  955. // Didn't find a non-full buffer
  956. AUDIO_OPENSLES_DEBUG_WARN("Could not insert into recording buffer");
  957. dataPos = noSamp10ms; // Don't try to insert more
  958. }
  959. }
  960. // clean the queue buffer
  961. // Start with empty buffer
  962. memset(This->_recQueueBuffer[This->_recQueueSeq], 0, (REC_BUF_SIZE_IN_SAMPLES << BYTES_PER_SAMPLE_LOG2));
  963. // write the empty buffer to the queue
  964. SLresult slResult = (*This->m_slRecorderSimpleBufferQueue)->Enqueue(
  965. This->m_slRecorderSimpleBufferQueue,
  966. (void*) This->_recQueueBuffer[This->_recQueueSeq],
  967. (noSamp10ms << BYTES_PER_SAMPLE_LOG2));
  968. if (slResult != SL_RESULT_SUCCESS) {
  969. AUDIO_OPENSLES_DEBUG_WARN("Failed to enqueue recording buffer with error code = %d", slResult);
  970. return;
  971. }
  972. // update the rec queue seq
  973. This->_recQueueSeq = (This->_recQueueSeq + 1) % N_REC_QUEUE_BUFFERS;
  974. // alert thread
  975. // TODO
  976. #endif
  977. }
  978. }
  979. #endif /* AUDIO_OPENSLES_UNDER_ANDROID */