audio_opensles_device.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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_config.h"
  19. #ifndef _DOUBANGO_AUDIO_OPENSLES_SLDEVICE_H
  20. #define _DOUBANGO_AUDIO_OPENSLES_SLDEVICE_H
  21. #if AUDIO_OPENSLES_UNDER_ANDROID
  22. # include <SLES/OpenSLES.h>
  23. # include <SLES/OpenSLES_Android.h>
  24. # include <SLES/OpenSLES_AndroidConfiguration.h>
  25. #endif
  26. #define BYTES_PER_SAMPLE 2
  27. #define BYTES_PER_SAMPLE_LOG2 1
  28. #define PLAYOUT_BUFFER_SIZE 10 // millis
  29. #define PLAYOUT_SAMPLE_RATE 16000
  30. #define RECORDING_BUFFER_SIZE 10
  31. #define RECORDING_SAMPLE_RATE 16000
  32. // max buffer size = 10 ms @ 48 kHz
  33. #define REC_BUF_SIZE_IN_SAMPLES 480
  34. #define PLAY_BUF_SIZE_IN_SAMPLES 480
  35. #define N_MAX_INTERFACES 4
  36. #define N_PLAY_QUEUE_BUFFERS 5
  37. #define N_REC_QUEUE_BUFFERS 5
  38. #define N_REC_BUFFERS 20
  39. class SLAudioDeviceCallback
  40. {
  41. public:
  42. virtual int32_t RecordedDataIsAvailable(const void* audioSamples,
  43. const uint32_t nSamples,
  44. const uint8_t nBytesPerSample,
  45. const uint8_t nChannels,
  46. const uint32_t samplesPerSec) = 0;
  47. virtual int32_t NeedMorePlayData(const uint32_t nSamples,
  48. const uint8_t nBytesPerSample,
  49. const uint8_t nChannels,
  50. const uint32_t samplesPerSec,
  51. void* audioSamples,
  52. uint32_t& nSamplesOut) = 0;
  53. protected:
  54. virtual ~SLAudioDeviceCallback() {}
  55. };
  56. class SLAudioDevice
  57. {
  58. public:
  59. SLAudioDevice(const SLAudioDeviceCallback* pCallback = NULL);
  60. virtual ~SLAudioDevice();
  61. public:
  62. int SetCallback(const SLAudioDeviceCallback* pCallback);
  63. int Init();
  64. bool Initialized();
  65. int SpeakerIsAvailable(bool *pAvailable);
  66. int InitSpeaker();
  67. int SetMaxSpeakerVolume(int nMaxSpeakerVolume);
  68. int SetMinSpeakerVolume(int nMinSpeakerVolume);
  69. int SetSpeakerVolume(int nSpeakerVolume);
  70. int SetSpeakerOn(bool bSpeakerOn);
  71. int PlayoutIsAvailable(bool *pAvailable);
  72. int SetStereoPlayout(bool bEnabled);
  73. int SetPlayoutBuffer(int nPlayoutBufferSize);
  74. int SetPlayoutSampleRate(int nPlayoutSampleRate);
  75. int InitPlayout();
  76. int StereoPlayout(bool *pEnabled);
  77. int PlayoutSampleRate(int *pPlayoutSampleRate);
  78. int StartPlayout();
  79. bool Playing();
  80. int StopPlayout();
  81. int RecordingIsAvailable(bool *pAvailable);
  82. int MicrophoneIsAvailable(bool *pAvailable);
  83. int InitMicrophone();
  84. int SetMicrophoneVolume(int nMicrophoneVolume);
  85. int SetStereoRecording(bool bEnabled);
  86. int SetRecordingSampleRate(int nRecordingSampleRate);
  87. int InitRecording();
  88. int StereoRecording(bool *pEnabled);
  89. int RecordingSampleRate(int *pRecordingSampleRate);
  90. int StartRecording();
  91. bool Recording();
  92. int StopRecording();
  93. int Terminate();
  94. private:
  95. int PlayoutApplyNewConfig();
  96. uint32_t PullPlayoutData(void* pAudioSamples, const uint32_t nSamples);
  97. void PushRecordingData(void* pAudioSamples, const uint32_t nSamples);
  98. #if AUDIO_OPENSLES_UNDER_ANDROID
  99. static void PlayerSimpleBufferQueueCallback(SLAndroidSimpleBufferQueueItf queueItf, void *pContext);
  100. static void RecorderSimpleBufferQueueCallback(SLAndroidSimpleBufferQueueItf queueItf, void *pContext);
  101. #endif
  102. private:
  103. const SLAudioDeviceCallback* m_pCallback;
  104. int m_nMicDeviceId;
  105. bool m_bInitialized;
  106. bool m_bSpeakerInitialized;
  107. bool m_bPlayoutInitialized;
  108. bool m_bRecordingInitialized;
  109. bool m_bMicrophoneInitialized;
  110. bool m_bSpeakerOn;
  111. bool m_bStereoPlayout;
  112. bool m_bStereoRecording;
  113. int m_nPlayoutBufferSize;
  114. int m_nRecordingBufferSize;
  115. int m_nPlayoutSampleRate;
  116. int m_nRecordingSampleRate;
  117. bool m_bPlaying;
  118. bool m_bRecording;
  119. int m_nSpeakerVolume;
  120. int m_nMaxSpeakerVolume;
  121. int m_nMinSpeakerVolume;
  122. #if AUDIO_OPENSLES_UNDER_ANDROID
  123. // audio unit
  124. SLObjectItf m_slEngineObject;
  125. // playout device
  126. SLObjectItf m_slPlayer;
  127. SLEngineItf m_slEngine;
  128. SLPlayItf m_slPlayerPlay;
  129. SLAndroidSimpleBufferQueueItf m_slPlayerSimpleBufferQueue;
  130. SLObjectItf m_slOutputMixObject;
  131. SLVolumeItf m_slSpeakerVolume;
  132. // recording device
  133. SLObjectItf m_slRecorder;
  134. SLRecordItf m_slRecorderRecord;
  135. SLAudioIODeviceCapabilitiesItf m_slAudioIODeviceCapabilities;
  136. SLAndroidSimpleBufferQueueItf m_slRecorderSimpleBufferQueue;
  137. SLDeviceVolumeItf m_slMicVolume;
  138. int _recQueueSeq;
  139. // Playout buffer
  140. uint8_t _playQueueBuffer[N_PLAY_QUEUE_BUFFERS][PLAY_BUF_SIZE_IN_SAMPLES << BYTES_PER_SAMPLE_LOG2];
  141. int _playQueueSeq;
  142. // Recording buffer
  143. uint8_t _recQueueBuffer[N_REC_QUEUE_BUFFERS][REC_BUF_SIZE_IN_SAMPLES << BYTES_PER_SAMPLE_LOG2];
  144. uint8_t _recBuffer[N_REC_BUFFERS][REC_BUF_SIZE_IN_SAMPLES << BYTES_PER_SAMPLE_LOG2];
  145. int _recLength[N_REC_BUFFERS];
  146. int _recSeqNumber[N_REC_BUFFERS];
  147. int _recCurrentSeq;
  148. // Current total size all data in buffers, used for delay estimate
  149. int _recBufferTotalSize;
  150. #endif
  151. };
  152. #endif /* _DOUBANGO_AUDIO_OPENSLES_SLDEVICE_H */