audio_push_fifo.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2016 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_PUSH_FIFO_H_
  5. #define MEDIA_BASE_AUDIO_PUSH_FIFO_H_
  6. #include <memory>
  7. #include "base/callback.h"
  8. #include "media/base/audio_bus.h"
  9. #include "media/base/media_export.h"
  10. namespace media {
  11. // Yet another FIFO for audio data that re-buffers audio to a desired buffer
  12. // size. Unlike AudioFifo and AudioBlockFifo, this FIFO cannot overflow: The
  13. // client is required to provide a callback that is called synchronously during
  14. // a push whenever enough data becomes available. This implementation
  15. // eliminates redundant memory copies when the input buffer size always matches
  16. // the desired buffer size.
  17. class MEDIA_EXPORT AudioPushFifo final {
  18. public:
  19. // Called synchronously zero, one, or multiple times during a call to Push()
  20. // to deliver re-buffered audio. |frame_delay| refers to the position of the
  21. // first frame in |output| relative to the first frame in the Push() call. If
  22. // negative, this indicates the output contains some data from a prior call to
  23. // Push(). If zero or positive, the output contains data from the current
  24. // call to Push(). Clients can use this to adjust timestamps.
  25. using OutputCallback =
  26. base::RepeatingCallback<void(const AudioBus& output_bus,
  27. int frame_delay)>;
  28. // Creates a new AudioPushFifo which delivers re-buffered audio by running
  29. // |callback|.
  30. explicit AudioPushFifo(const OutputCallback& callback);
  31. AudioPushFifo(const AudioPushFifo&) = delete;
  32. AudioPushFifo& operator=(const AudioPushFifo&) = delete;
  33. ~AudioPushFifo();
  34. // Returns the number of frames in each AudioBus delivered to the
  35. // OutputCallback.
  36. int frames_per_buffer() const { return frames_per_buffer_; }
  37. // The number of frames currently queued in this FIFO.
  38. int queued_frames() const { return queued_frames_; }
  39. // Must be called at least once before the first call to Push(). May be
  40. // called later (e.g., to support an audio format change).
  41. void Reset(int frames_per_buffer);
  42. // Pushes all audio channel data from |input_bus| through the FIFO. This will
  43. // result in zero, one, or multiple synchronous calls to the OutputCallback
  44. // provided in the constructor. If the |input_bus| has a different number of
  45. // channels than the prior Push() call, any currently-queued frames will be
  46. // dropped.
  47. void Push(const AudioBus& input_bus);
  48. // Flushes any enqueued frames by invoking the OutputCallback with those
  49. // frames plus padded zero samples. If there are no frames currently
  50. // enqueued, OutputCallback is not run.
  51. void Flush();
  52. private:
  53. const OutputCallback callback_;
  54. int frames_per_buffer_;
  55. // Queue of frames pending for delivery.
  56. std::unique_ptr<AudioBus> audio_queue_;
  57. int queued_frames_;
  58. };
  59. } // namespace media
  60. #endif // MEDIA_BASE_AUDIO_PUSH_FIFO_H_