output_stream.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2018 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 SERVICES_AUDIO_OUTPUT_STREAM_H_
  5. #define SERVICES_AUDIO_OUTPUT_STREAM_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/callback.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "base/sequence_checker.h"
  12. #include "base/strings/string_piece.h"
  13. #include "base/sync_socket.h"
  14. #include "base/timer/timer.h"
  15. #include "media/mojo/mojom/audio_data_pipe.mojom.h"
  16. #include "media/mojo/mojom/audio_logging.mojom.h"
  17. #include "media/mojo/mojom/audio_output_stream.mojom.h"
  18. #include "mojo/public/cpp/bindings/associated_remote.h"
  19. #include "mojo/public/cpp/bindings/pending_receiver.h"
  20. #include "mojo/public/cpp/bindings/pending_remote.h"
  21. #include "mojo/public/cpp/bindings/receiver.h"
  22. #include "mojo/public/cpp/bindings/shared_remote.h"
  23. #include "mojo/public/cpp/system/buffer.h"
  24. #include "mojo/public/cpp/system/handle.h"
  25. #include "mojo/public/cpp/system/platform_handle.h"
  26. #include "services/audio/loopback_coordinator.h"
  27. #include "services/audio/output_controller.h"
  28. #include "services/audio/sync_reader.h"
  29. namespace base {
  30. class UnguessableToken;
  31. } // namespace base
  32. namespace media {
  33. class AudioManager;
  34. class AudioParameters;
  35. } // namespace media
  36. namespace audio {
  37. class OutputStreamActivityMonitor;
  38. class OutputStream final : public media::mojom::AudioOutputStream,
  39. public OutputController::EventHandler {
  40. public:
  41. using DeleteCallback = base::OnceCallback<void(OutputStream*)>;
  42. using CreatedCallback =
  43. base::OnceCallback<void(media::mojom::ReadWriteAudioDataPipePtr)>;
  44. using ManagedDeviceOutputStreamCreateCallback =
  45. OutputController::ManagedDeviceOutputStreamCreateCallback;
  46. OutputStream(
  47. CreatedCallback created_callback,
  48. DeleteCallback delete_callback,
  49. ManagedDeviceOutputStreamCreateCallback
  50. managed_device_output_stream_create_callback,
  51. mojo::PendingReceiver<media::mojom::AudioOutputStream> stream_receiver,
  52. mojo::PendingAssociatedRemote<media::mojom::AudioOutputStreamObserver>
  53. observer,
  54. mojo::PendingRemote<media::mojom::AudioLog> log,
  55. media::AudioManager* audio_manager,
  56. OutputStreamActivityMonitor* activity_monitor,
  57. const std::string& output_device_id,
  58. const media::AudioParameters& params,
  59. LoopbackCoordinator* coordinator,
  60. const base::UnguessableToken& loopback_group_id);
  61. OutputStream(const OutputStream&) = delete;
  62. OutputStream& operator=(const OutputStream&) = delete;
  63. ~OutputStream() final;
  64. // media::mojom::AudioOutputStream implementation.
  65. void Play() final;
  66. void Pause() final;
  67. void Flush() final;
  68. void SetVolume(double volume) final;
  69. // OutputController::EventHandler implementation.
  70. void OnControllerPlaying() final;
  71. void OnControllerPaused() final;
  72. void OnControllerError() final;
  73. void OnLog(base::StringPiece message) final;
  74. private:
  75. void CreateAudioPipe(CreatedCallback created_callback);
  76. void OnError();
  77. void CallDeleter();
  78. void PollAudioLevel();
  79. bool IsAudible();
  80. // Internal helper method for sending logs related to this class to clients
  81. // registered to receive these logs. Prepends each log with "audio::OS" to
  82. // point out its origin. Compare with OutputController::EventHandler::OnLog()
  83. // which only will be called by the |controller_| object. These logs are
  84. // prepended with "AOC::" where AOC corresponds to AudioOutputController.
  85. void SendLogMessage(const char* format, ...) PRINTF_FORMAT(2, 3);
  86. SEQUENCE_CHECKER(owning_sequence_);
  87. base::CancelableSyncSocket foreign_socket_;
  88. DeleteCallback delete_callback_;
  89. mojo::Receiver<AudioOutputStream> receiver_;
  90. mojo::AssociatedRemote<media::mojom::AudioOutputStreamObserver> observer_;
  91. const mojo::SharedRemote<media::mojom::AudioLog> log_;
  92. const raw_ptr<LoopbackCoordinator> coordinator_;
  93. SyncReader reader_;
  94. OutputController controller_;
  95. // A token indicating membership in a group of output controllers/streams.
  96. const base::UnguessableToken loopback_group_id_;
  97. // This flag ensures that we only send OnStreamStateChanged notifications
  98. // and (de)register with the stream monitor when the state actually changes.
  99. bool playing_ = false;
  100. // Calls PollAudioLevel() at regular intervals while |playing_| is true.
  101. base::RepeatingTimer poll_timer_;
  102. bool is_audible_ = false;
  103. base::WeakPtrFactory<OutputStream> weak_factory_{this};
  104. };
  105. } // namespace audio
  106. #endif // SERVICES_AUDIO_OUTPUT_STREAM_H_