| 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); |
| 123 virtual MediaStreamVideoSource* CreateVideoSource( | 124 virtual MediaStreamVideoSource* CreateVideoSource( |
| 124 const StreamDeviceInfo& device, | 125 const StreamDeviceInfo& device, |
| 125 const MediaStreamSource::SourceStoppedCallback& stop_callback); | 126 const MediaStreamSource::SourceStoppedCallback& stop_callback); |
| 126 | 127 |
| 127 // Class for storing information about a WebKit request to create a | 128 // Class for storing information about a WebKit request to create a |
| 128 // MediaStream. | 129 // MediaStream. |
| 129 class UserMediaRequestInfo | 130 class UserMediaRequestInfo |
| 130 : public base::SupportsWeakPtr<UserMediaRequestInfo> { | 131 : public base::SupportsWeakPtr<UserMediaRequestInfo> { |
| 131 public: | 132 public: |
| 132 typedef base::Callback<void(UserMediaRequestInfo* request_info, | 133 typedef base::Callback<void(UserMediaRequestInfo* request_info, |
| 133 MediaStreamRequestResult result, | 134 MediaStreamRequestResult result, |
| 134 const blink::WebString& result_name)> | 135 const blink::WebString& result_name)> |
| 135 ResourcesReady; | 136 ResourcesReady; |
| 136 | 137 |
| 137 UserMediaRequestInfo(int request_id, | 138 UserMediaRequestInfo(int request_id, |
| 138 const blink::WebUserMediaRequest& request, | 139 const blink::WebUserMediaRequest& request, |
| 139 bool enable_automatic_output_device_selection); | 140 bool enable_automatic_output_device_selection); |
| 140 ~UserMediaRequestInfo(); | 141 ~UserMediaRequestInfo(); |
| 141 int request_id; | 142 int request_id; |
| 142 // True if MediaStreamDispatcher has generated the stream, see | 143 // True if MediaStreamDispatcher has generated the stream, see |
| 143 // OnStreamGenerated. | 144 // OnStreamGenerated. |
| 144 bool generated; | 145 bool generated; |
| 145 const bool enable_automatic_output_device_selection; | 146 const bool enable_automatic_output_device_selection; |
| 146 blink::WebMediaStream web_stream; | 147 blink::WebMediaStream web_stream; |
| 147 blink::WebUserMediaRequest request; | 148 blink::WebUserMediaRequest request; |
| 148 | 149 |
| 149 void StartAudioTrack(const blink::WebMediaStreamTrack& track); | 150 void StartAudioTrack(const blink::WebMediaStreamTrack& track, |
| 151 bool source_initialized); |
| 150 | 152 |
| 151 blink::WebMediaStreamTrack CreateAndStartVideoTrack( | 153 blink::WebMediaStreamTrack CreateAndStartVideoTrack( |
| 152 const blink::WebMediaStreamSource& source, | 154 const blink::WebMediaStreamSource& source, |
| 153 const blink::WebMediaConstraints& constraints); | 155 const blink::WebMediaConstraints& constraints); |
| 154 | 156 |
| 155 // Triggers |callback| when all sources used in this request have either | 157 // Triggers |callback| when all sources used in this request have either |
| 156 // successfully started, or a source has failed to start. | 158 // successfully started, or a source has failed to start. |
| 157 void CallbackOnTracksStarted(const ResourcesReady& callback); | 159 void CallbackOnTracksStarted(const ResourcesReady& callback); |
| 158 | 160 |
| 159 bool IsSourceUsed(const blink::WebMediaStreamSource& source) const; | 161 bool HasPendingSources() const; |
| 160 void RemoveSource(const blink::WebMediaStreamSource& source); | |
| 161 | 162 |
| 162 bool HasPendingSources() const; | 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); |
| 163 | 167 |
| 164 private: | 168 private: |
| 165 void OnTrackStarted( | 169 void OnTrackStarted( |
| 166 MediaStreamSource* source, | 170 MediaStreamSource* source, |
| 167 MediaStreamRequestResult result, | 171 MediaStreamRequestResult result, |
| 168 const blink::WebString& result_name); | 172 const blink::WebString& result_name); |
| 173 |
| 169 void CheckAllTracksStarted(); | 174 void CheckAllTracksStarted(); |
| 170 | 175 |
| 171 ResourcesReady ready_callback_; | 176 ResourcesReady ready_callback_; |
| 172 MediaStreamRequestResult request_result_; | 177 MediaStreamRequestResult request_result_; |
| 173 blink::WebString request_result_name_; | 178 blink::WebString request_result_name_; |
| 174 // Sources used in this request. | 179 // Sources used in this request. |
| 175 std::vector<blink::WebMediaStreamSource> sources_; | 180 std::vector<blink::WebMediaStreamSource> sources_; |
| 176 std::vector<MediaStreamSource*> sources_waiting_for_callback_; | 181 std::vector<MediaStreamSource*> sources_waiting_for_callback_; |
| 177 }; | 182 }; |
| 178 typedef ScopedVector<UserMediaRequestInfo> UserMediaRequests; | 183 typedef ScopedVector<UserMediaRequestInfo> UserMediaRequests; |
| 179 | 184 |
| 180 protected: | 185 protected: |
| 181 // These methods can be accessed in unit tests. | 186 // These methods can be accessed in unit tests. |
| 182 UserMediaRequestInfo* FindUserMediaRequestInfo(int request_id); | 187 UserMediaRequestInfo* FindUserMediaRequestInfo(int request_id); |
| 183 UserMediaRequestInfo* FindUserMediaRequestInfo( | 188 UserMediaRequestInfo* FindUserMediaRequestInfo( |
| 184 const blink::WebUserMediaRequest& request); | 189 const blink::WebUserMediaRequest& request); |
| 185 | 190 |
| 186 void DeleteUserMediaRequestInfo(UserMediaRequestInfo* request); | 191 void DeleteUserMediaRequestInfo(UserMediaRequestInfo* request); |
| 187 | 192 |
| 188 private: | 193 private: |
| 189 typedef std::vector<blink::WebMediaStreamSource> LocalStreamSources; | 194 typedef std::vector<blink::WebMediaStreamSource> LocalStreamSources; |
| 190 | 195 |
| 191 // RenderFrameObserver implementation. | 196 // RenderFrameObserver implementation. |
| 192 void OnDestruct() override; | 197 void OnDestruct() override; |
| 193 | 198 |
| 194 // Creates a WebKit representation of stream sources based on | 199 // Creates a WebKit representation of stream sources based on |
| 195 // |devices| from the MediaStreamDispatcher. | 200 // |devices| from the MediaStreamDispatcher. |
| 196 void InitializeSourceObject( | 201 void InitializeVideoSourceObject( |
| 197 const StreamDeviceInfo& device, | 202 const StreamDeviceInfo& device, |
| 198 blink::WebMediaStreamSource::Type type, | |
| 199 const blink::WebMediaConstraints& constraints, | 203 const blink::WebMediaConstraints& constraints, |
| 200 blink::WebMediaStreamSource* webkit_source); | 204 blink::WebMediaStreamSource* webkit_source); |
| 201 | 205 |
| 206 void InitializeAudioSourceObject( |
| 207 const StreamDeviceInfo& device, |
| 208 const blink::WebMediaConstraints& constraints, |
| 209 blink::WebMediaStreamSource* webkit_source, |
| 210 bool* source_initialized); |
| 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 |
| 225 using EnumerationResult = std::vector<MediaDeviceInfoArray>; | 246 using EnumerationResult = std::vector<MediaDeviceInfoArray>; |
| 226 void FinalizeEnumerateDevices(blink::WebMediaDevicesRequest request, | 247 void FinalizeEnumerateDevices(blink::WebMediaDevicesRequest request, |
| 227 const EnumerationResult& result); | 248 const EnumerationResult& result); |
| 228 | 249 |
| 229 void DeleteAllUserMediaRequests(); | 250 void DeleteAllUserMediaRequests(); |
| 230 | 251 |
| 231 // Returns the source that use a device with |device.session_id| | 252 // Returns the source that use a device with |device.session_id| |
| 232 // and |device.device.id|. NULL if such source doesn't exist. | 253 // and |device.device.id|. NULL if such source doesn't exist. |
| 233 const blink::WebMediaStreamSource* FindLocalSource( | 254 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, |
| 234 const StreamDeviceInfo& device) const; | 264 const StreamDeviceInfo& device) const; |
| 235 | 265 |
| 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 |
| 236 // Returns true if we do find and remove the |source|. | 271 // Returns true if we do find and remove the |source|. |
| 237 // Otherwise returns false. | 272 // Otherwise returns false. |
| 238 bool RemoveLocalSource(const blink::WebMediaStreamSource& source); | 273 bool RemoveLocalSource(const blink::WebMediaStreamSource& source); |
| 239 | 274 |
| 240 void StopLocalSource(const blink::WebMediaStreamSource& source, | 275 void StopLocalSource(const blink::WebMediaStreamSource& source, |
| 241 bool notify_dispatcher); | 276 bool notify_dispatcher); |
| 242 | 277 |
| 243 const ::mojom::MediaDevicesDispatcherHostPtr& GetMediaDevicesDispatcher(); | 278 const ::mojom::MediaDevicesDispatcherHostPtr& GetMediaDevicesDispatcher(); |
| 244 | 279 |
| 245 void SelectUserMediaDevice( | 280 void SelectUserMediaDevice( |
| (...skipping 21 matching lines...) Expand all Loading... |
| 267 // audio. | 302 // audio. |
| 268 PeerConnectionDependencyFactory* const dependency_factory_; | 303 PeerConnectionDependencyFactory* const dependency_factory_; |
| 269 | 304 |
| 270 // UserMediaClientImpl owns MediaStreamDispatcher instead of RenderFrameImpl | 305 // UserMediaClientImpl owns MediaStreamDispatcher instead of RenderFrameImpl |
| 271 // (or RenderFrameObserver) to ensure tear-down occurs in the right order. | 306 // (or RenderFrameObserver) to ensure tear-down occurs in the right order. |
| 272 const std::unique_ptr<MediaStreamDispatcher> media_stream_dispatcher_; | 307 const std::unique_ptr<MediaStreamDispatcher> media_stream_dispatcher_; |
| 273 | 308 |
| 274 ::mojom::MediaDevicesDispatcherHostPtr media_devices_dispatcher_; | 309 ::mojom::MediaDevicesDispatcherHostPtr media_devices_dispatcher_; |
| 275 | 310 |
| 276 LocalStreamSources local_sources_; | 311 LocalStreamSources local_sources_; |
| 312 LocalStreamSources pending_local_sources_; |
| 277 | 313 |
| 278 UserMediaRequests user_media_requests_; | 314 UserMediaRequests user_media_requests_; |
| 279 MediaDevicesEventDispatcher::SubscriptionIdList | 315 MediaDevicesEventDispatcher::SubscriptionIdList |
| 280 device_change_subscription_ids_; | 316 device_change_subscription_ids_; |
| 281 | 317 |
| 282 blink::WebMediaDeviceChangeObserver media_device_change_observer_; | 318 blink::WebMediaDeviceChangeObserver media_device_change_observer_; |
| 283 | 319 |
| 284 // Note: This member must be the last to ensure all outstanding weak pointers | 320 // Note: This member must be the last to ensure all outstanding weak pointers |
| 285 // are invalidated first. | 321 // are invalidated first. |
| 286 base::WeakPtrFactory<UserMediaClientImpl> weak_factory_; | 322 base::WeakPtrFactory<UserMediaClientImpl> weak_factory_; |
| 287 | 323 |
| 288 DISALLOW_COPY_AND_ASSIGN(UserMediaClientImpl); | 324 DISALLOW_COPY_AND_ASSIGN(UserMediaClientImpl); |
| 289 }; | 325 }; |
| 290 | 326 |
| 291 } // namespace content | 327 } // namespace content |
| 292 | 328 |
| 293 #endif // CONTENT_RENDERER_MEDIA_USER_MEDIA_CLIENT_IMPL_H_ | 329 #endif // CONTENT_RENDERER_MEDIA_USER_MEDIA_CLIENT_IMPL_H_ |
| OLD | NEW |