platform_audio.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (C) 2017 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "chre/platform/platform_audio.h"
  17. #include <cinttypes>
  18. #include "chre/core/audio_request_manager.h"
  19. #include "chre/core/event_loop_manager.h"
  20. #include "chre/platform/fatal_error.h"
  21. #include "chre/platform/log.h"
  22. #include "chre/platform/system_time.h"
  23. namespace chre {
  24. namespace {
  25. //! The fixed sampling rate for audio data when running CHRE on Android.
  26. constexpr uint32_t kAndroidAudioSampleRate = 16000;
  27. //! The minimum buffer size in samples.
  28. constexpr uint32_t kAndroidAudioMinBufferSize = kAndroidAudioSampleRate / 10;
  29. //! The maximum buffer size in samples.
  30. constexpr uint32_t kAndroidAudioMaxBufferSize = kAndroidAudioSampleRate * 10;
  31. } // namespace
  32. void PlatformAudioBase::audioReadCallback(void *cookie) {
  33. auto *platformAudio = static_cast<PlatformAudio *>(cookie);
  34. auto& dataEvent = platformAudio->mDataEvent;
  35. Nanoseconds samplingTime =
  36. AudioRequestManager::getDurationFromSampleCountAndRate(
  37. platformAudio->mNumSamples, kAndroidAudioSampleRate);
  38. dataEvent.timestamp = (SystemTime::getMonotonicTime() - samplingTime)
  39. .toRawNanoseconds();
  40. if (dataEvent.format == CHRE_AUDIO_DATA_FORMAT_16_BIT_SIGNED_PCM) {
  41. uint32_t intervalNumSamples =
  42. AudioRequestManager::getSampleCountFromRateAndDuration(
  43. kAndroidAudioSampleRate, platformAudio->mEventDelay);
  44. // Determine how much new audio data is required to be read from the device.
  45. // Samples that are already buffered by this implementation may be reused.
  46. int16_t *audioBuffer = platformAudio->mBuffer.data();
  47. uint32_t readAmount = platformAudio->mNumSamples;
  48. if (intervalNumSamples > platformAudio->mNumSamples) {
  49. uint32_t seekAmount = intervalNumSamples - platformAudio->mNumSamples;
  50. audioBuffer = &platformAudio->mBuffer.data()[seekAmount];
  51. readAmount = platformAudio->mNumSamples - seekAmount;
  52. }
  53. // Perform a blocking read. A timeout of 1 nanoasecond is passed here to
  54. // ensure that we read exactly the amount of requested audio frames. The
  55. // timer ensures that we wait approximately long enough to read the
  56. // requested number of samples and the timeout ensures that they match
  57. // exactly.
  58. int32_t framesRead = AAudioStream_read(
  59. platformAudio->mStream, audioBuffer, readAmount, 1);
  60. if (framesRead != static_cast<int32_t>(platformAudio->mNumSamples)) {
  61. FATAL_ERROR("Failed to read requested number of audio samples");
  62. } else {
  63. EventLoopManagerSingleton::get()->getAudioRequestManager()
  64. .handleAudioDataEvent(&dataEvent);
  65. }
  66. } else {
  67. FATAL_ERROR("Unimplemented data format");
  68. }
  69. }
  70. PlatformAudio::PlatformAudio() {
  71. if (!mTimer.init()) {
  72. FATAL_ERROR("Failed to initialize audio timer");
  73. }
  74. aaudio_result_t result = AAudio_createStreamBuilder(&mStreamBuilder);
  75. if (result != AAUDIO_OK) {
  76. FATAL_ERROR("Failed to create audio stream builder with %" PRId32, result);
  77. }
  78. AAudioStreamBuilder_setDirection(mStreamBuilder, AAUDIO_DIRECTION_INPUT);
  79. AAudioStreamBuilder_setSharingMode(mStreamBuilder,
  80. AAUDIO_SHARING_MODE_SHARED);
  81. AAudioStreamBuilder_setSampleRate(mStreamBuilder, kAndroidAudioSampleRate);
  82. AAudioStreamBuilder_setChannelCount(mStreamBuilder, 1);
  83. AAudioStreamBuilder_setFormat(mStreamBuilder, AAUDIO_FORMAT_PCM_I16);
  84. AAudioStreamBuilder_setBufferCapacityInFrames(mStreamBuilder,
  85. kAndroidAudioMaxBufferSize);
  86. result = AAudioStreamBuilder_openStream(mStreamBuilder, &mStream);
  87. if (result != AAUDIO_OK) {
  88. FATAL_ERROR("Failed to create audio stream with %" PRId32, result);
  89. }
  90. int32_t bufferSize = AAudioStream_getBufferCapacityInFrames(mStream);
  91. LOGD("Created audio stream with %" PRId32 " frames buffer size",
  92. bufferSize);
  93. mMinBufferDuration = AudioRequestManager::getDurationFromSampleCountAndRate(
  94. kAndroidAudioMinBufferSize, kAndroidAudioSampleRate).toRawNanoseconds();
  95. mMaxBufferDuration = AudioRequestManager::getDurationFromSampleCountAndRate(
  96. bufferSize, kAndroidAudioSampleRate).toRawNanoseconds();
  97. result = AAudioStream_requestStart(mStream);
  98. if (result != AAUDIO_OK) {
  99. FATAL_ERROR("Failed to start audio stream with %" PRId32, result);
  100. }
  101. mBuffer.resize(bufferSize);
  102. initAudioDataEvent();
  103. }
  104. PlatformAudio::~PlatformAudio() {
  105. AAudioStream_close(mStream);
  106. AAudioStreamBuilder_delete(mStreamBuilder);
  107. }
  108. void PlatformAudio::init() {
  109. // TODO: Implement this.
  110. }
  111. void PlatformAudio::setHandleEnabled(uint32_t handle, bool enabled) {
  112. // TODO: Implement this.
  113. }
  114. bool PlatformAudio::requestAudioDataEvent(uint32_t handle,
  115. uint32_t numSamples,
  116. Nanoseconds eventDelay) {
  117. mNumSamples = numSamples;
  118. mEventDelay = eventDelay;
  119. mDataEvent.sampleCount = numSamples;
  120. return mTimer.set(audioReadCallback, this, eventDelay);
  121. }
  122. void PlatformAudio::cancelAudioDataEventRequest(uint32_t handle) {
  123. mTimer.cancel();
  124. }
  125. void PlatformAudio::releaseAudioDataEvent(struct chreAudioDataEvent *event) {
  126. }
  127. size_t PlatformAudio::getSourceCount() {
  128. // Hardcoded at one as the Android platform only surfaces the default mic.
  129. return 1;
  130. }
  131. bool PlatformAudio::getAudioSource(uint32_t handle,
  132. chreAudioSource *audioSource) const {
  133. bool success = false;
  134. if (handle == 0) {
  135. audioSource->name = "Default Android Audio Input";
  136. audioSource->sampleRate = kAndroidAudioSampleRate;
  137. audioSource->minBufferDuration = mMinBufferDuration;
  138. audioSource->maxBufferDuration = mMaxBufferDuration;
  139. audioSource->format = mDataEvent.format;
  140. success = true;
  141. }
  142. return success;
  143. }
  144. void PlatformAudioBase::initAudioDataEvent() {
  145. mDataEvent.version = CHRE_AUDIO_DATA_EVENT_VERSION;
  146. memset(mDataEvent.reserved, 0, sizeof(mDataEvent.reserved));
  147. mDataEvent.handle = 0;
  148. mDataEvent.sampleRate = kAndroidAudioSampleRate;
  149. mDataEvent.format = CHRE_AUDIO_DATA_FORMAT_16_BIT_SIGNED_PCM;
  150. mDataEvent.samplesS16 = mBuffer.data();
  151. }
  152. } // namespace chre