audio_renderer_sink.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef MEDIA_BASE_AUDIO_RENDERER_SINK_H_
  5. #define MEDIA_BASE_AUDIO_RENDERER_SINK_H_
  6. #include <stdint.h>
  7. #include <string>
  8. #include "base/callback.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "media/base/audio_bus.h"
  11. #include "media/base/audio_parameters.h"
  12. #include "media/base/output_device_info.h"
  13. namespace media {
  14. // AudioRendererSink is an interface representing the end-point for
  15. // rendered audio. An implementation is expected to
  16. // periodically call Render() on a callback object.
  17. class AudioRendererSink
  18. : public base::RefCountedThreadSafe<media::AudioRendererSink> {
  19. public:
  20. class RenderCallback {
  21. public:
  22. // Attempts to completely fill all channels of |dest|, returns actual
  23. // number of frames filled. |prior_frames_skipped| contains the number of
  24. // frames
  25. // the consumer has skipped, if any.
  26. // The |delay| argument represents audio device output latency,
  27. // |delay_timestamp| represents the time when |delay| was obtained.
  28. virtual int Render(base::TimeDelta delay,
  29. base::TimeTicks delay_timestamp,
  30. int prior_frames_skipped,
  31. AudioBus* dest) = 0;
  32. // Signals an error has occurred.
  33. virtual void OnRenderError() = 0;
  34. protected:
  35. virtual ~RenderCallback() {}
  36. };
  37. // Sets important information about the audio stream format.
  38. // It must be called before any of the other methods.
  39. virtual void Initialize(const AudioParameters& params,
  40. RenderCallback* callback) = 0;
  41. // Starts audio playback.
  42. virtual void Start() = 0;
  43. // Stops audio playback and performs cleanup. It must be called before
  44. // destruction.
  45. virtual void Stop() = 0;
  46. // Pauses playback.
  47. virtual void Pause() = 0;
  48. // Resumes playback after calling Pause().
  49. virtual void Play() = 0;
  50. // Flushes playback.
  51. // This should only be called if the sink is not playing.
  52. virtual void Flush() = 0;
  53. // Sets the playback volume, with range [0.0, 1.0] inclusive.
  54. // Returns |true| on success.
  55. virtual bool SetVolume(double volume) = 0;
  56. // Returns current output device information. If the information is not
  57. // available yet, this method may block until it becomes available. If the
  58. // sink is not associated with any output device, |device_status| of
  59. // OutputDeviceInfo should be set to OUTPUT_DEVICE_STATUS_ERROR_INTERNAL. Must
  60. // never be called on the IO thread.
  61. //
  62. // Note: Prefer to use GetOutputDeviceInfoAsync instead if possible.
  63. virtual OutputDeviceInfo GetOutputDeviceInfo() = 0;
  64. // Same as the above, but does not block and will execute |info_cb| when the
  65. // OutputDeviceInfo is available. Callback will be executed on the calling
  66. // thread. Prefer this function to the synchronous version, it does not have a
  67. // timeout so will result in less spurious timeout errors.
  68. //
  69. // |info_cb| will always be posted (I.e., executed after this function
  70. // returns), even if OutputDeviceInfo is already available.
  71. //
  72. // Upon destruction if OutputDeviceInfo is still not available, |info_cb| will
  73. // be posted with OUTPUT_DEVICE_STATUS_ERROR_INTERNAL. Note: Because |info_cb|
  74. // is posted it will execute after destruction, so clients must handle
  75. // cancellation of the callback if needed.
  76. using OutputDeviceInfoCB = base::OnceCallback<void(OutputDeviceInfo)>;
  77. virtual void GetOutputDeviceInfoAsync(OutputDeviceInfoCB info_cb) = 0;
  78. // Returns |true| if a source with hardware parameters is preferable.
  79. virtual bool IsOptimizedForHardwareParameters() = 0;
  80. // If DCHECKs are enabled, this function returns true if called on rendering
  81. // thread, otherwise false. With DCHECKs disabled, it returns true. Thus, it
  82. // is intended to be used for DCHECKing.
  83. virtual bool CurrentThreadIsRenderingThread() = 0;
  84. protected:
  85. friend class base::RefCountedThreadSafe<AudioRendererSink>;
  86. virtual ~AudioRendererSink() {}
  87. };
  88. // Same as AudioRendererSink except that Initialize() and Start() can be called
  89. // again after Stop().
  90. // TODO(sandersd): Fold back into AudioRendererSink once all subclasses support
  91. // this.
  92. class RestartableAudioRendererSink : public AudioRendererSink {
  93. protected:
  94. ~RestartableAudioRendererSink() override {}
  95. };
  96. class SwitchableAudioRendererSink : public RestartableAudioRendererSink {
  97. public:
  98. // Attempts to switch the audio output device associated with a sink.
  99. // Once the attempt is finished, |callback| is invoked with the
  100. // result of the operation passed as a parameter. The result is a value from
  101. // the media::OutputDeviceStatus enum.
  102. // There is no guarantee about the thread where |callback| will be invoked.
  103. virtual void SwitchOutputDevice(const std::string& device_id,
  104. OutputDeviceStatusCB callback) = 0;
  105. protected:
  106. ~SwitchableAudioRendererSink() override {}
  107. };
  108. } // namespace media
  109. #endif // MEDIA_BASE_AUDIO_RENDERER_SINK_H_