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