OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_RENDERER_MEDIA_USER_MEDIA_CLIENT_IMPL_H_ | 5 #ifndef CONTENT_RENDERER_MEDIA_USER_MEDIA_CLIENT_IMPL_H_ |
6 #define CONTENT_RENDERER_MEDIA_USER_MEDIA_CLIENT_IMPL_H_ | 6 #define CONTENT_RENDERER_MEDIA_USER_MEDIA_CLIENT_IMPL_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 const blink::WebString& result_name); | 112 const blink::WebString& result_name); |
113 | 113 |
114 virtual void EnumerateDevicesSucceded( | 114 virtual void EnumerateDevicesSucceded( |
115 blink::WebMediaDevicesRequest* request, | 115 blink::WebMediaDevicesRequest* request, |
116 blink::WebVector<blink::WebMediaDeviceInfo>& devices); | 116 blink::WebVector<blink::WebMediaDeviceInfo>& devices); |
117 | 117 |
118 // Creates a MediaStreamAudioSource/MediaStreamVideoSource objects. | 118 // Creates a MediaStreamAudioSource/MediaStreamVideoSource objects. |
119 // These are virtual for test purposes. | 119 // These are virtual for test purposes. |
120 virtual MediaStreamAudioSource* CreateAudioSource( | 120 virtual MediaStreamAudioSource* CreateAudioSource( |
121 const StreamDeviceInfo& device, | 121 const StreamDeviceInfo& device, |
122 const blink::WebMediaConstraints& constraints, | 122 const blink::WebMediaConstraints& constraints); |
123 const MediaStreamSource::ConstraintsCallback& source_ready); | |
124 virtual MediaStreamVideoSource* CreateVideoSource( | 123 virtual MediaStreamVideoSource* CreateVideoSource( |
125 const StreamDeviceInfo& device, | 124 const StreamDeviceInfo& device, |
126 const MediaStreamSource::SourceStoppedCallback& stop_callback); | 125 const MediaStreamSource::SourceStoppedCallback& stop_callback); |
127 | 126 |
128 // Class for storing information about a WebKit request to create a | 127 // Class for storing information about a WebKit request to create a |
129 // MediaStream. | 128 // MediaStream. |
130 class UserMediaRequestInfo | 129 class UserMediaRequestInfo |
131 : public base::SupportsWeakPtr<UserMediaRequestInfo> { | 130 : public base::SupportsWeakPtr<UserMediaRequestInfo> { |
132 public: | 131 public: |
133 typedef base::Callback<void(UserMediaRequestInfo* request_info, | 132 typedef base::Callback<void(UserMediaRequestInfo* request_info, |
134 MediaStreamRequestResult result, | 133 MediaStreamRequestResult result, |
135 const blink::WebString& result_name)> | 134 const blink::WebString& result_name)> |
136 ResourcesReady; | 135 ResourcesReady; |
137 | 136 |
138 UserMediaRequestInfo(int request_id, | 137 UserMediaRequestInfo(int request_id, |
139 const blink::WebUserMediaRequest& request, | 138 const blink::WebUserMediaRequest& request, |
140 bool enable_automatic_output_device_selection); | 139 bool enable_automatic_output_device_selection); |
141 ~UserMediaRequestInfo(); | 140 ~UserMediaRequestInfo(); |
142 int request_id; | 141 int request_id; |
143 // True if MediaStreamDispatcher has generated the stream, see | 142 // True if MediaStreamDispatcher has generated the stream, see |
144 // OnStreamGenerated. | 143 // OnStreamGenerated. |
145 bool generated; | 144 bool generated; |
146 const bool enable_automatic_output_device_selection; | 145 const bool enable_automatic_output_device_selection; |
147 blink::WebMediaStream web_stream; | 146 blink::WebMediaStream web_stream; |
148 blink::WebUserMediaRequest request; | 147 blink::WebUserMediaRequest request; |
149 | 148 |
150 void StartAudioTrack(const blink::WebMediaStreamTrack& track, | 149 void StartAudioTrack(const blink::WebMediaStreamTrack& track); |
151 bool source_initialized); | |
152 | 150 |
153 blink::WebMediaStreamTrack CreateAndStartVideoTrack( | 151 blink::WebMediaStreamTrack CreateAndStartVideoTrack( |
154 const blink::WebMediaStreamSource& source, | 152 const blink::WebMediaStreamSource& source, |
155 const blink::WebMediaConstraints& constraints); | 153 const blink::WebMediaConstraints& constraints); |
156 | 154 |
157 // Triggers |callback| when all sources used in this request have either | 155 // Triggers |callback| when all sources used in this request have either |
158 // successfully started, or a source has failed to start. | 156 // successfully started, or a source has failed to start. |
159 void CallbackOnTracksStarted(const ResourcesReady& callback); | 157 void CallbackOnTracksStarted(const ResourcesReady& callback); |
160 | 158 |
| 159 bool IsSourceUsed(const blink::WebMediaStreamSource& source) const; |
| 160 void RemoveSource(const blink::WebMediaStreamSource& source); |
| 161 |
161 bool HasPendingSources() const; | 162 bool HasPendingSources() const; |
162 | 163 |
163 // Called when a local audio source has finished (or failed) initializing. | |
164 void OnAudioSourceStarted(MediaStreamSource* source, | |
165 MediaStreamRequestResult result, | |
166 const blink::WebString& result_name); | |
167 | |
168 private: | 164 private: |
169 void OnTrackStarted( | 165 void OnTrackStarted( |
170 MediaStreamSource* source, | 166 MediaStreamSource* source, |
171 MediaStreamRequestResult result, | 167 MediaStreamRequestResult result, |
172 const blink::WebString& result_name); | 168 const blink::WebString& result_name); |
173 | |
174 void CheckAllTracksStarted(); | 169 void CheckAllTracksStarted(); |
175 | 170 |
176 ResourcesReady ready_callback_; | 171 ResourcesReady ready_callback_; |
177 MediaStreamRequestResult request_result_; | 172 MediaStreamRequestResult request_result_; |
178 blink::WebString request_result_name_; | 173 blink::WebString request_result_name_; |
179 // Sources used in this request. | 174 // Sources used in this request. |
180 std::vector<blink::WebMediaStreamSource> sources_; | 175 std::vector<blink::WebMediaStreamSource> sources_; |
181 std::vector<MediaStreamSource*> sources_waiting_for_callback_; | 176 std::vector<MediaStreamSource*> sources_waiting_for_callback_; |
182 }; | 177 }; |
183 typedef ScopedVector<UserMediaRequestInfo> UserMediaRequests; | 178 typedef ScopedVector<UserMediaRequestInfo> UserMediaRequests; |
184 | 179 |
185 protected: | 180 protected: |
186 // These methods can be accessed in unit tests. | 181 // These methods can be accessed in unit tests. |
187 UserMediaRequestInfo* FindUserMediaRequestInfo(int request_id); | 182 UserMediaRequestInfo* FindUserMediaRequestInfo(int request_id); |
188 UserMediaRequestInfo* FindUserMediaRequestInfo( | 183 UserMediaRequestInfo* FindUserMediaRequestInfo( |
189 const blink::WebUserMediaRequest& request); | 184 const blink::WebUserMediaRequest& request); |
190 | 185 |
191 void DeleteUserMediaRequestInfo(UserMediaRequestInfo* request); | 186 void DeleteUserMediaRequestInfo(UserMediaRequestInfo* request); |
192 | 187 |
193 private: | 188 private: |
194 typedef std::vector<blink::WebMediaStreamSource> LocalStreamSources; | 189 typedef std::vector<blink::WebMediaStreamSource> LocalStreamSources; |
195 | 190 |
196 // RenderFrameObserver implementation. | 191 // RenderFrameObserver implementation. |
197 void OnDestruct() override; | 192 void OnDestruct() override; |
198 | 193 |
199 // Creates a WebKit representation of stream sources based on | 194 // Creates a WebKit representation of stream sources based on |
200 // |devices| from the MediaStreamDispatcher. | 195 // |devices| from the MediaStreamDispatcher. |
201 void InitializeVideoSourceObject( | 196 void InitializeSourceObject( |
202 const StreamDeviceInfo& device, | 197 const StreamDeviceInfo& device, |
| 198 blink::WebMediaStreamSource::Type type, |
203 const blink::WebMediaConstraints& constraints, | 199 const blink::WebMediaConstraints& constraints, |
204 blink::WebMediaStreamSource* webkit_source); | 200 blink::WebMediaStreamSource* webkit_source); |
205 | 201 |
206 void InitializeAudioSourceObject( | |
207 const StreamDeviceInfo& device, | |
208 const blink::WebMediaConstraints& constraints, | |
209 blink::WebMediaStreamSource* webkit_source, | |
210 bool* source_initialized); | |
211 | |
212 void CreateVideoTracks( | 202 void CreateVideoTracks( |
213 const StreamDeviceInfoArray& devices, | 203 const StreamDeviceInfoArray& devices, |
214 const blink::WebMediaConstraints& constraints, | 204 const blink::WebMediaConstraints& constraints, |
215 blink::WebVector<blink::WebMediaStreamTrack>* webkit_tracks, | 205 blink::WebVector<blink::WebMediaStreamTrack>* webkit_tracks, |
216 UserMediaRequestInfo* request); | 206 UserMediaRequestInfo* request); |
217 | 207 |
218 void CreateAudioTracks( | 208 void CreateAudioTracks( |
219 const StreamDeviceInfoArray& devices, | 209 const StreamDeviceInfoArray& devices, |
220 const blink::WebMediaConstraints& constraints, | 210 const blink::WebMediaConstraints& constraints, |
221 blink::WebVector<blink::WebMediaStreamTrack>* webkit_tracks, | 211 blink::WebVector<blink::WebMediaStreamTrack>* webkit_tracks, |
222 UserMediaRequestInfo* request); | 212 UserMediaRequestInfo* request); |
223 | 213 |
224 // Callback function triggered when all native versions of the | 214 // Callback function triggered when all native versions of the |
225 // underlying media sources and tracks have been created and started. | 215 // underlying media sources and tracks have been created and started. |
226 void OnCreateNativeTracksCompleted( | 216 void OnCreateNativeTracksCompleted( |
227 UserMediaRequestInfo* request, | 217 UserMediaRequestInfo* request, |
228 MediaStreamRequestResult result, | 218 MediaStreamRequestResult result, |
229 const blink::WebString& result_name); | 219 const blink::WebString& result_name); |
230 | 220 |
231 void OnStreamGeneratedForCancelledRequest( | 221 void OnStreamGeneratedForCancelledRequest( |
232 const StreamDeviceInfoArray& audio_array, | 222 const StreamDeviceInfoArray& audio_array, |
233 const StreamDeviceInfoArray& video_array); | 223 const StreamDeviceInfoArray& video_array); |
234 | 224 |
235 static void OnAudioSourceStartedOnAudioThread( | |
236 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | |
237 base::WeakPtr<UserMediaClientImpl> weak_ptr, | |
238 MediaStreamSource* source, | |
239 MediaStreamRequestResult result, | |
240 const blink::WebString& result_name); | |
241 | |
242 void OnAudioSourceStarted(MediaStreamSource* source, | |
243 MediaStreamRequestResult result, | |
244 const blink::WebString& result_name); | |
245 | |
246 using EnumerationResult = std::vector<MediaDeviceInfoArray>; | 225 using EnumerationResult = std::vector<MediaDeviceInfoArray>; |
247 void FinalizeEnumerateDevices(blink::WebMediaDevicesRequest request, | 226 void FinalizeEnumerateDevices(blink::WebMediaDevicesRequest request, |
248 const EnumerationResult& result); | 227 const EnumerationResult& result); |
249 | 228 |
250 void DeleteAllUserMediaRequests(); | 229 void DeleteAllUserMediaRequests(); |
251 | 230 |
252 // Returns the source that use a device with |device.session_id| | 231 // Returns the source that use a device with |device.session_id| |
253 // and |device.device.id|. NULL if such source doesn't exist. | 232 // and |device.device.id|. NULL if such source doesn't exist. |
254 const blink::WebMediaStreamSource* FindLocalSource( | 233 const blink::WebMediaStreamSource* FindLocalSource( |
255 const StreamDeviceInfo& device) const { | |
256 return FindLocalSource(local_sources_, device); | |
257 } | |
258 const blink::WebMediaStreamSource* FindPendingLocalSource( | |
259 const StreamDeviceInfo& device) const { | |
260 return FindLocalSource(pending_local_sources_, device); | |
261 } | |
262 const blink::WebMediaStreamSource* FindLocalSource( | |
263 const LocalStreamSources& sources, | |
264 const StreamDeviceInfo& device) const; | 234 const StreamDeviceInfo& device) const; |
265 | 235 |
266 // Looks up a local source and returns it if found. If not found, prepares | |
267 // a new WebMediaStreamSource with a NULL extraData pointer. | |
268 blink::WebMediaStreamSource FindOrInitializeSourceObject( | |
269 const StreamDeviceInfo& device); | |
270 | |
271 // Returns true if we do find and remove the |source|. | 236 // Returns true if we do find and remove the |source|. |
272 // Otherwise returns false. | 237 // Otherwise returns false. |
273 bool RemoveLocalSource(const blink::WebMediaStreamSource& source); | 238 bool RemoveLocalSource(const blink::WebMediaStreamSource& source); |
274 | 239 |
275 void StopLocalSource(const blink::WebMediaStreamSource& source, | 240 void StopLocalSource(const blink::WebMediaStreamSource& source, |
276 bool notify_dispatcher); | 241 bool notify_dispatcher); |
277 | 242 |
278 const ::mojom::MediaDevicesDispatcherHostPtr& GetMediaDevicesDispatcher(); | 243 const ::mojom::MediaDevicesDispatcherHostPtr& GetMediaDevicesDispatcher(); |
279 | 244 |
280 void SelectUserMediaDevice( | 245 void SelectUserMediaDevice( |
(...skipping 21 matching lines...) Expand all Loading... |
302 // audio. | 267 // audio. |
303 PeerConnectionDependencyFactory* const dependency_factory_; | 268 PeerConnectionDependencyFactory* const dependency_factory_; |
304 | 269 |
305 // UserMediaClientImpl owns MediaStreamDispatcher instead of RenderFrameImpl | 270 // UserMediaClientImpl owns MediaStreamDispatcher instead of RenderFrameImpl |
306 // (or RenderFrameObserver) to ensure tear-down occurs in the right order. | 271 // (or RenderFrameObserver) to ensure tear-down occurs in the right order. |
307 const std::unique_ptr<MediaStreamDispatcher> media_stream_dispatcher_; | 272 const std::unique_ptr<MediaStreamDispatcher> media_stream_dispatcher_; |
308 | 273 |
309 ::mojom::MediaDevicesDispatcherHostPtr media_devices_dispatcher_; | 274 ::mojom::MediaDevicesDispatcherHostPtr media_devices_dispatcher_; |
310 | 275 |
311 LocalStreamSources local_sources_; | 276 LocalStreamSources local_sources_; |
312 LocalStreamSources pending_local_sources_; | |
313 | 277 |
314 UserMediaRequests user_media_requests_; | 278 UserMediaRequests user_media_requests_; |
315 MediaDevicesEventDispatcher::SubscriptionIdList | 279 MediaDevicesEventDispatcher::SubscriptionIdList |
316 device_change_subscription_ids_; | 280 device_change_subscription_ids_; |
317 | 281 |
318 blink::WebMediaDeviceChangeObserver media_device_change_observer_; | 282 blink::WebMediaDeviceChangeObserver media_device_change_observer_; |
319 | 283 |
320 // Note: This member must be the last to ensure all outstanding weak pointers | 284 // Note: This member must be the last to ensure all outstanding weak pointers |
321 // are invalidated first. | 285 // are invalidated first. |
322 base::WeakPtrFactory<UserMediaClientImpl> weak_factory_; | 286 base::WeakPtrFactory<UserMediaClientImpl> weak_factory_; |
323 | 287 |
324 DISALLOW_COPY_AND_ASSIGN(UserMediaClientImpl); | 288 DISALLOW_COPY_AND_ASSIGN(UserMediaClientImpl); |
325 }; | 289 }; |
326 | 290 |
327 } // namespace content | 291 } // namespace content |
328 | 292 |
329 #endif // CONTENT_RENDERER_MEDIA_USER_MEDIA_CLIENT_IMPL_H_ | 293 #endif // CONTENT_RENDERER_MEDIA_USER_MEDIA_CLIENT_IMPL_H_ |
OLD | NEW |