| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_OUTPUT_DELEGATE_H_ | 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_OUTPUT_DELEGATE_H_ |
| 6 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_OUTPUT_DELEGATE_H_ | 6 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_OUTPUT_DELEGATE_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 13 #include "content/browser/renderer_host/media/audio_stream_registry.h" |
| 13 #include "content/common/content_export.h" | 14 #include "content/common/content_export.h" |
| 14 #include "media/audio/audio_output_controller.h" | 15 #include "media/audio/audio_output_controller.h" |
| 15 | 16 |
| 16 namespace base { | 17 namespace base { |
| 17 class SharedMemory; | 18 class SharedMemory; |
| 18 class CancelableSyncSocket; | 19 class CancelableSyncSocket; |
| 19 } | 20 } |
| 20 | 21 |
| 21 namespace content { | 22 namespace content { |
| 22 class AudioMirroringManager; | 23 class AudioMirroringManager; |
| 23 class AudioSyncReader; | 24 class AudioSyncReader; |
| 24 class MediaObserver; | 25 class MediaObserver; |
| 25 } | 26 } |
| 26 | 27 |
| 27 namespace media { | 28 namespace media { |
| 28 class AudioLog; | 29 class AudioLog; |
| 29 class AudioParameters; | 30 class AudioParameters; |
| 30 } | 31 } |
| 31 | 32 |
| 32 namespace content { | 33 namespace content { |
| 33 | 34 |
| 34 // This class, except for the AudioOutputDelegate::EventHandler implementation, | 35 // This class, except for the AudioOutputDelegate::EventHandler implementation, |
| 35 // is operated on the IO thread. | 36 // is operated on the IO thread. |
| 36 class CONTENT_EXPORT AudioOutputDelegate | 37 class CONTENT_EXPORT AudioOutputDelegate |
| 37 : public media::AudioOutputController::EventHandler { | 38 : public media::AudioOutputController::EventHandler, |
| 39 public AudioStreamRegistry::Stream { |
| 38 public: | 40 public: |
| 39 class CONTENT_EXPORT EventHandler { | 41 class CONTENT_EXPORT EventHandler { |
| 40 public: | 42 public: |
| 41 virtual ~EventHandler() {} | 43 virtual ~EventHandler() {} |
| 42 | 44 |
| 43 // All these methods are called on the IO thread. | 45 // All these methods are called on the IO thread. |
| 44 | 46 |
| 45 // Called when the state changes between playing and not playing. | |
| 46 virtual void OnStreamStateChanged(bool playing) = 0; | |
| 47 | |
| 48 // Called when construction is finished and the stream is ready for | 47 // Called when construction is finished and the stream is ready for |
| 49 // playout. | 48 // playout. |
| 50 virtual void OnStreamCreated(int stream_id, | 49 virtual void OnStreamCreated(int stream_id, |
| 51 base::SharedMemory* shared_memory, | 50 base::SharedMemory* shared_memory, |
| 52 base::CancelableSyncSocket* socket) = 0; | 51 base::CancelableSyncSocket* socket) = 0; |
| 53 | 52 |
| 54 // Called if stream encounters an error and has become unusable. | 53 // Called if stream encounters an error and has become unusable. |
| 55 virtual void OnStreamError(int stream_id) = 0; | 54 virtual void OnStreamError(int stream_id) = 0; |
| 56 }; | 55 }; |
| 57 | 56 |
| 58 class CONTENT_EXPORT Deleter { | 57 class CONTENT_EXPORT Deleter { |
| 59 public: | 58 public: |
| 60 Deleter() = default; | 59 Deleter() = default; |
| 61 explicit Deleter(AudioMirroringManager* mirroring_manager) | 60 explicit Deleter(AudioMirroringManager* mirroring_manager) |
| 62 : mirroring_manager_(mirroring_manager) {} | 61 : mirroring_manager_(mirroring_manager) {} |
| 63 void operator()(AudioOutputDelegate* delegate); | 62 void operator()(AudioOutputDelegate* delegate); |
| 64 | 63 |
| 65 private: | 64 private: |
| 66 AudioMirroringManager* mirroring_manager_; | 65 AudioMirroringManager* mirroring_manager_; |
| 67 }; | 66 }; |
| 68 | 67 |
| 69 using UniquePtr = std::unique_ptr<AudioOutputDelegate, Deleter>; | 68 using UniquePtr = std::unique_ptr<AudioOutputDelegate, Deleter>; |
| 70 | 69 |
| 71 // The AudioOutputDelegate might issue callbacks until the UniquePtr | 70 // The AudioOutputDelegate might issue callbacks until the UniquePtr |
| 72 // destructor finishes, so calling |handler| must be valid until then. | 71 // destructor finishes, so calling |handler| must be valid until then. |
| 73 static UniquePtr Create(EventHandler* handler, | 72 static UniquePtr Create(EventHandler* handler, |
| 73 AudioStreamRegistry* stream_registry, |
| 74 media::AudioManager* audio_manager, | 74 media::AudioManager* audio_manager, |
| 75 std::unique_ptr<media::AudioLog> audio_log, | 75 std::unique_ptr<media::AudioLog> audio_log, |
| 76 AudioMirroringManager* mirroring_manager, | 76 AudioMirroringManager* mirroring_manager, |
| 77 MediaObserver* media_observer, | 77 MediaObserver* media_observer, |
| 78 int stream_id, | 78 int stream_id, |
| 79 int render_frame_id, | 79 int render_frame_id, |
| 80 int render_process_id, | 80 int render_process_id, |
| 81 const media::AudioParameters& params, | 81 const media::AudioParameters& params, |
| 82 const std::string& output_device_id); | 82 const std::string& output_device_id); |
| 83 |
| 83 ~AudioOutputDelegate() override; | 84 ~AudioOutputDelegate() override; |
| 84 | 85 |
| 85 // TODO(maxmorin): Remove this when crbug.com/647185 is closed. | 86 // TODO(maxmorin): Remove this when crbug.com/647185 is closed. |
| 86 // This function is used to provide control of the audio stream to | 87 // This function is used to provide control of the audio stream to |
| 87 // WebrtcAudioPrivateGetActiveSinkFunction and others in the webrtc extension | 88 // WebrtcAudioPrivateGetActiveSinkFunction and others in the webrtc extension |
| 88 // API. Since the controller is shared, this means that it might outlive the | 89 // API. Since the controller is shared, this means that it might outlive the |
| 89 // AudioOutputDelegate. In this case, it is still safe to call functions on | 90 // AudioOutputDelegate. In this case, it is still safe to call functions on |
| 90 // the controller, but it will not do anything. The controller is also shared | 91 // the controller, but it will not do anything. The controller is also shared |
| 91 // with AudioStreamMonitor. | 92 // with AudioStreamMonitor. |
| 92 scoped_refptr<media::AudioOutputController> controller() const { | 93 scoped_refptr<media::AudioOutputController> controller() const { |
| 93 return controller_; | 94 return controller_; |
| 94 } | 95 } |
| 95 | 96 |
| 96 int stream_id() const { return stream_id_; } | 97 int stream_id() const { return stream_id_; } |
| 97 | 98 |
| 98 // Stream control: | 99 // Stream control: |
| 99 void OnPlayStream(); | 100 void OnPlayStream(); |
| 100 void OnPauseStream(); | 101 void OnPauseStream(); |
| 101 void OnSetVolume(double volume); | 102 void OnSetVolume(double volume); |
| 102 | 103 |
| 103 // AudioOutputController::EventHandler implementation. These methods may | 104 // AudioOutputController::EventHandler implementation. These methods may |
| 104 // be called on any thead. | 105 // be called on any thead. |
| 105 void OnControllerCreated() override; | 106 void OnControllerCreated() override; |
| 106 void OnControllerPlaying() override; | 107 void OnControllerPlaying() override; |
| 107 void OnControllerPaused() override; | 108 void OnControllerPaused() override; |
| 108 void OnControllerError() override; | 109 void OnControllerError() override; |
| 109 | 110 |
| 110 private: | 111 private: |
| 111 AudioOutputDelegate(EventHandler* handler, | 112 AudioOutputDelegate(EventHandler* handler, |
| 113 AudioStreamRegistry* stream_registry, |
| 112 media::AudioManager* audio_manager, | 114 media::AudioManager* audio_manager, |
| 113 std::unique_ptr<media::AudioLog> audio_log, | 115 std::unique_ptr<media::AudioLog> audio_log, |
| 114 int stream_id, | 116 int stream_id, |
| 115 int render_frame_id, | 117 int render_frame_id, |
| 116 int render_process_id, | 118 int render_process_id, |
| 117 const media::AudioParameters& params, | 119 const media::AudioParameters& params, |
| 118 const std::string& output_device_id); | 120 const std::string& output_device_id); |
| 119 | 121 |
| 120 void SendCreatedNotification(); | 122 void SendCreatedNotification(); |
| 121 void OnError(); | 123 void OnError(); |
| 122 void UpdatePlayingState(bool playing); | 124 void UpdatePlayingState(bool playing); |
| 123 | 125 |
| 124 // |handler_| is null if we are in the process of destruction. In this case, | 126 // |handler_| is null if we are in the process of destruction. In this case, |
| 125 // we will ignore events from |controller_|. | 127 // we will ignore events from |controller_|. |
| 126 EventHandler* handler_; | 128 EventHandler* handler_; |
| 129 AudioStreamRegistry* const stream_registry_; |
| 127 std::unique_ptr<media::AudioLog> const audio_log_; | 130 std::unique_ptr<media::AudioLog> const audio_log_; |
| 128 std::unique_ptr<AudioSyncReader> reader_; | 131 std::unique_ptr<AudioSyncReader> reader_; |
| 129 scoped_refptr<media::AudioOutputController> controller_; | 132 scoped_refptr<media::AudioOutputController> controller_; |
| 130 const int stream_id_; | 133 const int stream_id_; |
| 131 const int render_frame_id_; | 134 const int render_frame_id_; |
| 132 const int render_process_id_; | 135 const int render_process_id_; |
| 133 | 136 |
| 134 // This flag ensures that we only send OnStreamStateChanged notifications | 137 // This flag ensures that we only send OnStreamStateChanged notifications |
| 135 // and (de)register with the stream monitor when the state actually changes. | 138 // and (de)register with the stream monitor when the state actually changes. |
| 136 bool playing_ = false; | 139 bool playing_ = false; |
| 137 base::WeakPtr<AudioOutputDelegate> weak_this_; | 140 base::WeakPtr<AudioOutputDelegate> weak_this_; |
| 138 base::WeakPtrFactory<AudioOutputDelegate> weak_factory_; | 141 base::WeakPtrFactory<AudioOutputDelegate> weak_factory_; |
| 139 | 142 |
| 140 DISALLOW_COPY_AND_ASSIGN(AudioOutputDelegate); | 143 DISALLOW_COPY_AND_ASSIGN(AudioOutputDelegate); |
| 141 }; | 144 }; |
| 142 | 145 |
| 143 } // namespace content | 146 } // namespace content |
| 144 | 147 |
| 145 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_OUTPUT_DELEGATE_H_ | 148 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_OUTPUT_DELEGATE_H_ |
| OLD | NEW |