AudioStreamOut.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. **
  3. ** Copyright 2015, The Android Open Source Project
  4. **
  5. ** Licensed under the Apache License, Version 2.0 (the "License");
  6. ** you may not use this file except in compliance with the License.
  7. ** You may obtain a copy of the License at
  8. **
  9. ** http://www.apache.org/licenses/LICENSE-2.0
  10. **
  11. ** Unless required by applicable law or agreed to in writing, software
  12. ** distributed under the License is distributed on an "AS IS" BASIS,
  13. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. ** See the License for the specific language governing permissions and
  15. ** limitations under the License.
  16. */
  17. #define LOG_TAG "AudioFlinger"
  18. //#define LOG_NDEBUG 0
  19. #include <media/audiohal/DeviceHalInterface.h>
  20. #include <media/audiohal/StreamHalInterface.h>
  21. #include <system/audio.h>
  22. #include <utils/Log.h>
  23. #include "AudioHwDevice.h"
  24. #include "AudioStreamOut.h"
  25. namespace android {
  26. // ----------------------------------------------------------------------------
  27. AudioStreamOut::AudioStreamOut(AudioHwDevice *dev, audio_output_flags_t flags)
  28. : audioHwDev(dev)
  29. , stream(NULL)
  30. , flags(flags)
  31. , mFramesWritten(0)
  32. , mFramesWrittenAtStandby(0)
  33. , mRenderPosition(0)
  34. , mRateMultiplier(1)
  35. , mHalFormatHasProportionalFrames(false)
  36. , mHalFrameSize(0)
  37. {
  38. }
  39. AudioStreamOut::~AudioStreamOut()
  40. {
  41. }
  42. sp<DeviceHalInterface> AudioStreamOut::hwDev() const
  43. {
  44. return audioHwDev->hwDevice();
  45. }
  46. status_t AudioStreamOut::getRenderPosition(uint64_t *frames)
  47. {
  48. if (stream == 0) {
  49. return NO_INIT;
  50. }
  51. uint32_t halPosition = 0;
  52. status_t status = stream->getRenderPosition(&halPosition);
  53. if (status != NO_ERROR) {
  54. return status;
  55. }
  56. // Maintain a 64-bit render position using the 32-bit result from the HAL.
  57. // This delta calculation relies on the arithmetic overflow behavior
  58. // of integers. For example (100 - 0xFFFFFFF0) = 116.
  59. const uint32_t truncatedPosition = (uint32_t)mRenderPosition;
  60. int32_t deltaHalPosition; // initialization not needed, overwitten by __builtin_sub_overflow()
  61. (void) __builtin_sub_overflow(halPosition, truncatedPosition, &deltaHalPosition);
  62. if (deltaHalPosition > 0) {
  63. mRenderPosition += deltaHalPosition;
  64. }
  65. // Scale from HAL sample rate to application rate.
  66. *frames = mRenderPosition / mRateMultiplier;
  67. return status;
  68. }
  69. // return bottom 32-bits of the render position
  70. status_t AudioStreamOut::getRenderPosition(uint32_t *frames)
  71. {
  72. uint64_t position64 = 0;
  73. status_t status = getRenderPosition(&position64);
  74. if (status == NO_ERROR) {
  75. *frames = (uint32_t)position64;
  76. }
  77. return status;
  78. }
  79. status_t AudioStreamOut::getPresentationPosition(uint64_t *frames, struct timespec *timestamp)
  80. {
  81. if (stream == 0) {
  82. return NO_INIT;
  83. }
  84. uint64_t halPosition = 0;
  85. status_t status = stream->getPresentationPosition(&halPosition, timestamp);
  86. if (status != NO_ERROR) {
  87. return status;
  88. }
  89. // Adjust for standby using HAL rate frames.
  90. // Only apply this correction if the HAL is getting PCM frames.
  91. if (mHalFormatHasProportionalFrames) {
  92. uint64_t adjustedPosition = (halPosition <= mFramesWrittenAtStandby) ?
  93. 0 : (halPosition - mFramesWrittenAtStandby);
  94. // Scale from HAL sample rate to application rate.
  95. *frames = adjustedPosition / mRateMultiplier;
  96. } else {
  97. // For offloaded MP3 and other compressed formats.
  98. *frames = halPosition;
  99. }
  100. return status;
  101. }
  102. status_t AudioStreamOut::open(
  103. audio_io_handle_t handle,
  104. audio_devices_t devices,
  105. struct audio_config *config,
  106. const char *address)
  107. {
  108. sp<StreamOutHalInterface> outStream;
  109. audio_output_flags_t customFlags = (config->format == AUDIO_FORMAT_IEC61937)
  110. ? (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO)
  111. : flags;
  112. int status = hwDev()->openOutputStream(
  113. handle,
  114. devices,
  115. customFlags,
  116. config,
  117. address,
  118. &outStream);
  119. ALOGV("AudioStreamOut::open(), HAL returned "
  120. " stream %p, sampleRate %d, Format %#x, "
  121. "channelMask %#x, status %d",
  122. outStream.get(),
  123. config->sample_rate,
  124. config->format,
  125. config->channel_mask,
  126. status);
  127. // Some HALs may not recognize AUDIO_FORMAT_IEC61937. But if we declare
  128. // it as PCM then it will probably work.
  129. if (status != NO_ERROR && config->format == AUDIO_FORMAT_IEC61937) {
  130. struct audio_config customConfig = *config;
  131. customConfig.format = AUDIO_FORMAT_PCM_16_BIT;
  132. status = hwDev()->openOutputStream(
  133. handle,
  134. devices,
  135. customFlags,
  136. &customConfig,
  137. address,
  138. &outStream);
  139. ALOGV("AudioStreamOut::open(), treat IEC61937 as PCM, status = %d", status);
  140. }
  141. if (status == NO_ERROR) {
  142. stream = outStream;
  143. mHalFormatHasProportionalFrames = audio_has_proportional_frames(config->format);
  144. status = stream->getFrameSize(&mHalFrameSize);
  145. }
  146. return status;
  147. }
  148. audio_format_t AudioStreamOut::getFormat() const
  149. {
  150. audio_format_t result;
  151. return stream->getFormat(&result) == OK ? result : AUDIO_FORMAT_INVALID;
  152. }
  153. uint32_t AudioStreamOut::getSampleRate() const
  154. {
  155. uint32_t result;
  156. return stream->getSampleRate(&result) == OK ? result : 0;
  157. }
  158. audio_channel_mask_t AudioStreamOut::getChannelMask() const
  159. {
  160. audio_channel_mask_t result;
  161. return stream->getChannelMask(&result) == OK ? result : AUDIO_CHANNEL_INVALID;
  162. }
  163. int AudioStreamOut::flush()
  164. {
  165. mRenderPosition = 0;
  166. mFramesWritten = 0;
  167. mFramesWrittenAtStandby = 0;
  168. status_t result = stream->flush();
  169. return result != INVALID_OPERATION ? result : NO_ERROR;
  170. }
  171. int AudioStreamOut::standby()
  172. {
  173. mRenderPosition = 0;
  174. mFramesWrittenAtStandby = mFramesWritten;
  175. return stream->standby();
  176. }
  177. ssize_t AudioStreamOut::write(const void *buffer, size_t numBytes)
  178. {
  179. size_t bytesWritten;
  180. status_t result = stream->write(buffer, numBytes, &bytesWritten);
  181. if (result == OK && bytesWritten > 0 && mHalFrameSize > 0) {
  182. mFramesWritten += bytesWritten / mHalFrameSize;
  183. }
  184. return result == OK ? bytesWritten : result;
  185. }
  186. } // namespace android