AudioStreamOut.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. #ifndef ANDROID_AUDIO_STREAM_OUT_H
  18. #define ANDROID_AUDIO_STREAM_OUT_H
  19. #include <stdint.h>
  20. #include <sys/types.h>
  21. #include <system/audio.h>
  22. namespace android {
  23. class AudioHwDevice;
  24. class DeviceHalInterface;
  25. class StreamOutHalInterface;
  26. /**
  27. * Managed access to a HAL output stream.
  28. */
  29. class AudioStreamOut {
  30. public:
  31. // AudioStreamOut is immutable, so its fields are const.
  32. // For emphasis, we could also make all pointers to them be "const *",
  33. // but that would clutter the code unnecessarily.
  34. AudioHwDevice * const audioHwDev;
  35. sp<StreamOutHalInterface> stream;
  36. const audio_output_flags_t flags;
  37. sp<DeviceHalInterface> hwDev() const;
  38. AudioStreamOut(AudioHwDevice *dev, audio_output_flags_t flags);
  39. virtual status_t open(
  40. audio_io_handle_t handle,
  41. audio_devices_t devices,
  42. struct audio_config *config,
  43. const char *address);
  44. virtual ~AudioStreamOut();
  45. // Get the bottom 32-bits of the 64-bit render position.
  46. status_t getRenderPosition(uint32_t *frames);
  47. virtual status_t getRenderPosition(uint64_t *frames);
  48. virtual status_t getPresentationPosition(uint64_t *frames, struct timespec *timestamp);
  49. /**
  50. * Write audio buffer to driver. Returns number of bytes written, or a
  51. * negative status_t. If at least one frame was written successfully prior to the error,
  52. * it is suggested that the driver return that successful (short) byte count
  53. * and then return an error in the subsequent call.
  54. *
  55. * If set_callback() has previously been called to enable non-blocking mode
  56. * the write() is not allowed to block. It must write only the number of
  57. * bytes that currently fit in the driver/hardware buffer and then return
  58. * this byte count. If this is less than the requested write size the
  59. * callback function must be called when more space is available in the
  60. * driver/hardware buffer.
  61. */
  62. virtual ssize_t write(const void *buffer, size_t bytes);
  63. /**
  64. * @return frame size from the perspective of the application and the AudioFlinger.
  65. */
  66. virtual size_t getFrameSize() const { return mHalFrameSize; }
  67. /**
  68. * @return format from the perspective of the application and the AudioFlinger.
  69. */
  70. virtual audio_format_t getFormat() const;
  71. /**
  72. * The HAL may be running at a higher sample rate if, for example, playing wrapped EAC3.
  73. * @return sample rate from the perspective of the application and the AudioFlinger.
  74. */
  75. virtual uint32_t getSampleRate() const;
  76. /**
  77. * The HAL is in stereo mode when playing multi-channel compressed audio over HDMI.
  78. * @return channel mask from the perspective of the application and the AudioFlinger.
  79. */
  80. virtual audio_channel_mask_t getChannelMask() const;
  81. virtual status_t flush();
  82. virtual status_t standby();
  83. protected:
  84. uint64_t mFramesWritten; // reset by flush
  85. uint64_t mFramesWrittenAtStandby;
  86. uint64_t mRenderPosition; // reset by flush or standby
  87. int mRateMultiplier;
  88. bool mHalFormatHasProportionalFrames;
  89. size_t mHalFrameSize;
  90. };
  91. } // namespace android
  92. #endif // ANDROID_AUDIO_STREAM_OUT_H