SpdifStreamOut.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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_SPDIF_STREAM_OUT_H
  18. #define ANDROID_SPDIF_STREAM_OUT_H
  19. #include <stdint.h>
  20. #include <sys/types.h>
  21. #include <system/audio.h>
  22. #include "AudioStreamOut.h"
  23. #include <audio_utils/spdif/SPDIFEncoder.h>
  24. namespace android {
  25. /**
  26. * Stream that is a PCM data burst in the HAL but looks like an encoded stream
  27. * to the AudioFlinger. Wraps encoded data in an SPDIF wrapper per IEC61973-3.
  28. */
  29. class SpdifStreamOut : public AudioStreamOut {
  30. public:
  31. SpdifStreamOut(AudioHwDevice *dev, audio_output_flags_t flags,
  32. audio_format_t format);
  33. virtual ~SpdifStreamOut() { }
  34. virtual status_t open(
  35. audio_io_handle_t handle,
  36. audio_devices_t devices,
  37. struct audio_config *config,
  38. const char *address);
  39. /**
  40. * Write audio buffer to driver. Returns number of bytes written, or a
  41. * negative status_t. If at least one frame was written successfully prior to the error,
  42. * it is suggested that the driver return that successful (short) byte count
  43. * and then return an error in the subsequent call.
  44. *
  45. * If set_callback() has previously been called to enable non-blocking mode
  46. * the write() is not allowed to block. It must write only the number of
  47. * bytes that currently fit in the driver/hardware buffer and then return
  48. * this byte count. If this is less than the requested write size the
  49. * callback function must be called when more space is available in the
  50. * driver/hardware buffer.
  51. */
  52. virtual ssize_t write(const void* buffer, size_t bytes);
  53. /**
  54. * @return frame size from the perspective of the application and the AudioFlinger.
  55. */
  56. virtual size_t getFrameSize() const { return sizeof(int8_t); }
  57. /**
  58. * @return format from the perspective of the application and the AudioFlinger.
  59. */
  60. virtual audio_format_t getFormat() const { return mApplicationFormat; }
  61. /**
  62. * The HAL may be running at a higher sample rate if, for example, playing wrapped EAC3.
  63. * @return sample rate from the perspective of the application and the AudioFlinger.
  64. */
  65. virtual uint32_t getSampleRate() const { return mApplicationSampleRate; }
  66. /**
  67. * The HAL is in stereo mode when playing multi-channel compressed audio over HDMI.
  68. * @return channel mask from the perspective of the application and the AudioFlinger.
  69. */
  70. virtual audio_channel_mask_t getChannelMask() const { return mApplicationChannelMask; }
  71. virtual status_t flush();
  72. virtual status_t standby();
  73. private:
  74. class MySPDIFEncoder : public SPDIFEncoder
  75. {
  76. public:
  77. MySPDIFEncoder(SpdifStreamOut *spdifStreamOut, audio_format_t format)
  78. : SPDIFEncoder(format)
  79. , mSpdifStreamOut(spdifStreamOut)
  80. {
  81. }
  82. virtual ssize_t writeOutput(const void* buffer, size_t bytes)
  83. {
  84. return mSpdifStreamOut->writeDataBurst(buffer, bytes);
  85. }
  86. protected:
  87. SpdifStreamOut * const mSpdifStreamOut;
  88. };
  89. MySPDIFEncoder mSpdifEncoder;
  90. audio_format_t mApplicationFormat;
  91. uint32_t mApplicationSampleRate;
  92. audio_channel_mask_t mApplicationChannelMask;
  93. ssize_t writeDataBurst(const void* data, size_t bytes);
  94. ssize_t writeInternal(const void* buffer, size_t bytes);
  95. };
  96. } // namespace android
  97. #endif // ANDROID_SPDIF_STREAM_OUT_H