audio_device_template.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*
  2. * Copyright (c) 2013 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. #ifndef MODULES_AUDIO_DEVICE_ANDROID_AUDIO_DEVICE_TEMPLATE_H_
  11. #define MODULES_AUDIO_DEVICE_ANDROID_AUDIO_DEVICE_TEMPLATE_H_
  12. #include "modules/audio_device/android/audio_manager.h"
  13. #include "modules/audio_device/audio_device_generic.h"
  14. #include "rtc_base/checks.h"
  15. #include "rtc_base/logging.h"
  16. #include "rtc_base/thread_checker.h"
  17. namespace webrtc {
  18. // InputType/OutputType can be any class that implements the capturing/rendering
  19. // part of the AudioDeviceGeneric API.
  20. // Construction and destruction must be done on one and the same thread. Each
  21. // internal implementation of InputType and OutputType will RTC_DCHECK if that
  22. // is not the case. All implemented methods must also be called on the same
  23. // thread. See comments in each InputType/OutputType class for more info.
  24. // It is possible to call the two static methods (SetAndroidAudioDeviceObjects
  25. // and ClearAndroidAudioDeviceObjects) from a different thread but both will
  26. // RTC_CHECK that the calling thread is attached to a Java VM.
  27. template <class InputType, class OutputType>
  28. class AudioDeviceTemplate : public AudioDeviceGeneric {
  29. public:
  30. AudioDeviceTemplate(AudioDeviceModule::AudioLayer audio_layer,
  31. AudioManager* audio_manager)
  32. : audio_layer_(audio_layer),
  33. audio_manager_(audio_manager),
  34. output_(audio_manager_),
  35. input_(audio_manager_),
  36. initialized_(false) {
  37. RTC_LOG(INFO) << __FUNCTION__;
  38. RTC_CHECK(audio_manager);
  39. audio_manager_->SetActiveAudioLayer(audio_layer);
  40. }
  41. virtual ~AudioDeviceTemplate() { RTC_LOG(INFO) << __FUNCTION__; }
  42. int32_t ActiveAudioLayer(
  43. AudioDeviceModule::AudioLayer& audioLayer) const override {
  44. RTC_LOG(INFO) << __FUNCTION__;
  45. audioLayer = audio_layer_;
  46. return 0;
  47. }
  48. InitStatus Init() override {
  49. RTC_LOG(INFO) << __FUNCTION__;
  50. RTC_DCHECK(thread_checker_.IsCurrent());
  51. RTC_DCHECK(!initialized_);
  52. if (!audio_manager_->Init()) {
  53. return InitStatus::OTHER_ERROR;
  54. }
  55. if (output_.Init() != 0) {
  56. audio_manager_->Close();
  57. return InitStatus::PLAYOUT_ERROR;
  58. }
  59. if (input_.Init() != 0) {
  60. output_.Terminate();
  61. audio_manager_->Close();
  62. return InitStatus::RECORDING_ERROR;
  63. }
  64. initialized_ = true;
  65. return InitStatus::OK;
  66. }
  67. int32_t Terminate() override {
  68. RTC_LOG(INFO) << __FUNCTION__;
  69. RTC_DCHECK(thread_checker_.IsCurrent());
  70. int32_t err = input_.Terminate();
  71. err |= output_.Terminate();
  72. err |= !audio_manager_->Close();
  73. initialized_ = false;
  74. RTC_DCHECK_EQ(err, 0);
  75. return err;
  76. }
  77. bool Initialized() const override {
  78. RTC_LOG(INFO) << __FUNCTION__;
  79. RTC_DCHECK(thread_checker_.IsCurrent());
  80. return initialized_;
  81. }
  82. int16_t PlayoutDevices() override {
  83. RTC_LOG(INFO) << __FUNCTION__;
  84. return 1;
  85. }
  86. int16_t RecordingDevices() override {
  87. RTC_LOG(INFO) << __FUNCTION__;
  88. return 1;
  89. }
  90. int32_t PlayoutDeviceName(uint16_t index,
  91. char name[kAdmMaxDeviceNameSize],
  92. char guid[kAdmMaxGuidSize]) override {
  93. FATAL() << "Should never be called";
  94. return -1;
  95. }
  96. int32_t RecordingDeviceName(uint16_t index,
  97. char name[kAdmMaxDeviceNameSize],
  98. char guid[kAdmMaxGuidSize]) override {
  99. FATAL() << "Should never be called";
  100. return -1;
  101. }
  102. int32_t SetPlayoutDevice(uint16_t index) override {
  103. // OK to use but it has no effect currently since device selection is
  104. // done using Andoid APIs instead.
  105. RTC_LOG(INFO) << __FUNCTION__;
  106. return 0;
  107. }
  108. int32_t SetPlayoutDevice(
  109. AudioDeviceModule::WindowsDeviceType device) override {
  110. FATAL() << "Should never be called";
  111. return -1;
  112. }
  113. int32_t SetRecordingDevice(uint16_t index) override {
  114. // OK to use but it has no effect currently since device selection is
  115. // done using Andoid APIs instead.
  116. RTC_LOG(INFO) << __FUNCTION__;
  117. return 0;
  118. }
  119. int32_t SetRecordingDevice(
  120. AudioDeviceModule::WindowsDeviceType device) override {
  121. FATAL() << "Should never be called";
  122. return -1;
  123. }
  124. int32_t PlayoutIsAvailable(bool& available) override {
  125. RTC_LOG(INFO) << __FUNCTION__;
  126. available = true;
  127. return 0;
  128. }
  129. int32_t InitPlayout() override {
  130. RTC_LOG(INFO) << __FUNCTION__;
  131. return output_.InitPlayout();
  132. }
  133. bool PlayoutIsInitialized() const override {
  134. RTC_LOG(INFO) << __FUNCTION__;
  135. return output_.PlayoutIsInitialized();
  136. }
  137. int32_t RecordingIsAvailable(bool& available) override {
  138. RTC_LOG(INFO) << __FUNCTION__;
  139. available = true;
  140. return 0;
  141. }
  142. int32_t InitRecording() override {
  143. RTC_LOG(INFO) << __FUNCTION__;
  144. return input_.InitRecording();
  145. }
  146. bool RecordingIsInitialized() const override {
  147. RTC_LOG(INFO) << __FUNCTION__;
  148. return input_.RecordingIsInitialized();
  149. }
  150. int32_t StartPlayout() override {
  151. RTC_LOG(INFO) << __FUNCTION__;
  152. if (!audio_manager_->IsCommunicationModeEnabled()) {
  153. RTC_LOG(WARNING)
  154. << "The application should use MODE_IN_COMMUNICATION audio mode!";
  155. }
  156. return output_.StartPlayout();
  157. }
  158. int32_t StopPlayout() override {
  159. // Avoid using audio manger (JNI/Java cost) if playout was inactive.
  160. if (!Playing())
  161. return 0;
  162. RTC_LOG(INFO) << __FUNCTION__;
  163. int32_t err = output_.StopPlayout();
  164. return err;
  165. }
  166. bool Playing() const override {
  167. RTC_LOG(INFO) << __FUNCTION__;
  168. return output_.Playing();
  169. }
  170. int32_t StartRecording() override {
  171. RTC_LOG(INFO) << __FUNCTION__;
  172. if (!audio_manager_->IsCommunicationModeEnabled()) {
  173. RTC_LOG(WARNING)
  174. << "The application should use MODE_IN_COMMUNICATION audio mode!";
  175. }
  176. return input_.StartRecording();
  177. }
  178. int32_t StopRecording() override {
  179. // Avoid using audio manger (JNI/Java cost) if recording was inactive.
  180. RTC_LOG(INFO) << __FUNCTION__;
  181. if (!Recording())
  182. return 0;
  183. int32_t err = input_.StopRecording();
  184. return err;
  185. }
  186. bool Recording() const override { return input_.Recording(); }
  187. int32_t InitSpeaker() override {
  188. RTC_LOG(INFO) << __FUNCTION__;
  189. return 0;
  190. }
  191. bool SpeakerIsInitialized() const override {
  192. RTC_LOG(INFO) << __FUNCTION__;
  193. return true;
  194. }
  195. int32_t InitMicrophone() override {
  196. RTC_LOG(INFO) << __FUNCTION__;
  197. return 0;
  198. }
  199. bool MicrophoneIsInitialized() const override {
  200. RTC_LOG(INFO) << __FUNCTION__;
  201. return true;
  202. }
  203. int32_t SpeakerVolumeIsAvailable(bool& available) override {
  204. RTC_LOG(INFO) << __FUNCTION__;
  205. return output_.SpeakerVolumeIsAvailable(available);
  206. }
  207. int32_t SetSpeakerVolume(uint32_t volume) override {
  208. RTC_LOG(INFO) << __FUNCTION__;
  209. return output_.SetSpeakerVolume(volume);
  210. }
  211. int32_t SpeakerVolume(uint32_t& volume) const override {
  212. RTC_LOG(INFO) << __FUNCTION__;
  213. return output_.SpeakerVolume(volume);
  214. }
  215. int32_t MaxSpeakerVolume(uint32_t& maxVolume) const override {
  216. RTC_LOG(INFO) << __FUNCTION__;
  217. return output_.MaxSpeakerVolume(maxVolume);
  218. }
  219. int32_t MinSpeakerVolume(uint32_t& minVolume) const override {
  220. RTC_LOG(INFO) << __FUNCTION__;
  221. return output_.MinSpeakerVolume(minVolume);
  222. }
  223. int32_t MicrophoneVolumeIsAvailable(bool& available) override {
  224. available = false;
  225. return -1;
  226. }
  227. int32_t SetMicrophoneVolume(uint32_t volume) override {
  228. FATAL() << "Should never be called";
  229. return -1;
  230. }
  231. int32_t MicrophoneVolume(uint32_t& volume) const override {
  232. FATAL() << "Should never be called";
  233. return -1;
  234. }
  235. int32_t MaxMicrophoneVolume(uint32_t& maxVolume) const override {
  236. FATAL() << "Should never be called";
  237. return -1;
  238. }
  239. int32_t MinMicrophoneVolume(uint32_t& minVolume) const override {
  240. FATAL() << "Should never be called";
  241. return -1;
  242. }
  243. int32_t SpeakerMuteIsAvailable(bool& available) override {
  244. FATAL() << "Should never be called";
  245. return -1;
  246. }
  247. int32_t SetSpeakerMute(bool enable) override {
  248. FATAL() << "Should never be called";
  249. return -1;
  250. }
  251. int32_t SpeakerMute(bool& enabled) const override {
  252. FATAL() << "Should never be called";
  253. return -1;
  254. }
  255. int32_t MicrophoneMuteIsAvailable(bool& available) override {
  256. FATAL() << "Not implemented";
  257. return -1;
  258. }
  259. int32_t SetMicrophoneMute(bool enable) override {
  260. FATAL() << "Not implemented";
  261. return -1;
  262. }
  263. int32_t MicrophoneMute(bool& enabled) const override {
  264. FATAL() << "Not implemented";
  265. return -1;
  266. }
  267. // Returns true if the audio manager has been configured to support stereo
  268. // and false otherwised. Default is mono.
  269. int32_t StereoPlayoutIsAvailable(bool& available) override {
  270. RTC_LOG(INFO) << __FUNCTION__;
  271. available = audio_manager_->IsStereoPlayoutSupported();
  272. return 0;
  273. }
  274. int32_t SetStereoPlayout(bool enable) override {
  275. RTC_LOG(INFO) << __FUNCTION__;
  276. bool available = audio_manager_->IsStereoPlayoutSupported();
  277. // Android does not support changes between mono and stero on the fly.
  278. // Instead, the native audio layer is configured via the audio manager
  279. // to either support mono or stereo. It is allowed to call this method
  280. // if that same state is not modified.
  281. return (enable == available) ? 0 : -1;
  282. }
  283. int32_t StereoPlayout(bool& enabled) const override {
  284. enabled = audio_manager_->IsStereoPlayoutSupported();
  285. return 0;
  286. }
  287. int32_t StereoRecordingIsAvailable(bool& available) override {
  288. RTC_LOG(INFO) << __FUNCTION__;
  289. available = audio_manager_->IsStereoRecordSupported();
  290. return 0;
  291. }
  292. int32_t SetStereoRecording(bool enable) override {
  293. RTC_LOG(INFO) << __FUNCTION__;
  294. bool available = audio_manager_->IsStereoRecordSupported();
  295. // Android does not support changes between mono and stero on the fly.
  296. // Instead, the native audio layer is configured via the audio manager
  297. // to either support mono or stereo. It is allowed to call this method
  298. // if that same state is not modified.
  299. return (enable == available) ? 0 : -1;
  300. }
  301. int32_t StereoRecording(bool& enabled) const override {
  302. RTC_LOG(INFO) << __FUNCTION__;
  303. enabled = audio_manager_->IsStereoRecordSupported();
  304. return 0;
  305. }
  306. int32_t PlayoutDelay(uint16_t& delay_ms) const override {
  307. // Best guess we can do is to use half of the estimated total delay.
  308. delay_ms = audio_manager_->GetDelayEstimateInMilliseconds() / 2;
  309. RTC_DCHECK_GT(delay_ms, 0);
  310. return 0;
  311. }
  312. void AttachAudioBuffer(AudioDeviceBuffer* audioBuffer) override {
  313. RTC_LOG(INFO) << __FUNCTION__;
  314. output_.AttachAudioBuffer(audioBuffer);
  315. input_.AttachAudioBuffer(audioBuffer);
  316. }
  317. // Returns true if the device both supports built in AEC and the device
  318. // is not blacklisted.
  319. // Currently, if OpenSL ES is used in both directions, this method will still
  320. // report the correct value and it has the correct effect. As an example:
  321. // a device supports built in AEC and this method returns true. Libjingle
  322. // will then disable the WebRTC based AEC and that will work for all devices
  323. // (mainly Nexus) even when OpenSL ES is used for input since our current
  324. // implementation will enable built-in AEC by default also for OpenSL ES.
  325. // The only "bad" thing that happens today is that when Libjingle calls
  326. // OpenSLESRecorder::EnableBuiltInAEC() it will not have any real effect and
  327. // a "Not Implemented" log will be filed. This non-perfect state will remain
  328. // until I have added full support for audio effects based on OpenSL ES APIs.
  329. bool BuiltInAECIsAvailable() const override {
  330. RTC_LOG(INFO) << __FUNCTION__;
  331. return audio_manager_->IsAcousticEchoCancelerSupported();
  332. }
  333. // TODO(henrika): add implementation for OpenSL ES based audio as well.
  334. int32_t EnableBuiltInAEC(bool enable) override {
  335. RTC_LOG(INFO) << __FUNCTION__ << "(" << enable << ")";
  336. RTC_CHECK(BuiltInAECIsAvailable()) << "HW AEC is not available";
  337. return input_.EnableBuiltInAEC(enable);
  338. }
  339. // Returns true if the device both supports built in AGC and the device
  340. // is not blacklisted.
  341. // TODO(henrika): add implementation for OpenSL ES based audio as well.
  342. // In addition, see comments for BuiltInAECIsAvailable().
  343. bool BuiltInAGCIsAvailable() const override {
  344. RTC_LOG(INFO) << __FUNCTION__;
  345. return audio_manager_->IsAutomaticGainControlSupported();
  346. }
  347. // TODO(henrika): add implementation for OpenSL ES based audio as well.
  348. int32_t EnableBuiltInAGC(bool enable) override {
  349. RTC_LOG(INFO) << __FUNCTION__ << "(" << enable << ")";
  350. RTC_CHECK(BuiltInAGCIsAvailable()) << "HW AGC is not available";
  351. return input_.EnableBuiltInAGC(enable);
  352. }
  353. // Returns true if the device both supports built in NS and the device
  354. // is not blacklisted.
  355. // TODO(henrika): add implementation for OpenSL ES based audio as well.
  356. // In addition, see comments for BuiltInAECIsAvailable().
  357. bool BuiltInNSIsAvailable() const override {
  358. RTC_LOG(INFO) << __FUNCTION__;
  359. return audio_manager_->IsNoiseSuppressorSupported();
  360. }
  361. // TODO(henrika): add implementation for OpenSL ES based audio as well.
  362. int32_t EnableBuiltInNS(bool enable) override {
  363. RTC_LOG(INFO) << __FUNCTION__ << "(" << enable << ")";
  364. RTC_CHECK(BuiltInNSIsAvailable()) << "HW NS is not available";
  365. return input_.EnableBuiltInNS(enable);
  366. }
  367. private:
  368. rtc::ThreadChecker thread_checker_;
  369. // Local copy of the audio layer set during construction of the
  370. // AudioDeviceModuleImpl instance. Read only value.
  371. const AudioDeviceModule::AudioLayer audio_layer_;
  372. // Non-owning raw pointer to AudioManager instance given to use at
  373. // construction. The real object is owned by AudioDeviceModuleImpl and the
  374. // life time is the same as that of the AudioDeviceModuleImpl, hence there
  375. // is no risk of reading a NULL pointer at any time in this class.
  376. AudioManager* const audio_manager_;
  377. OutputType output_;
  378. InputType input_;
  379. bool initialized_;
  380. };
  381. } // namespace webrtc
  382. #endif // MODULES_AUDIO_DEVICE_ANDROID_AUDIO_DEVICE_TEMPLATE_H_