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