| 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 is_pending); |
| 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 |
| 174 // Cheks if the sources for all tracks have been started and if so, |
| 175 // invoke the |ready_callback_|. Note that the caller should expect |
| 176 // that |this| might be deleted when the function returns. |
| 169 void CheckAllTracksStarted(); | 177 void CheckAllTracksStarted(); |
| 170 | 178 |
| 171 ResourcesReady ready_callback_; | 179 ResourcesReady ready_callback_; |
| 172 MediaStreamRequestResult request_result_; | 180 MediaStreamRequestResult request_result_; |
| 173 blink::WebString request_result_name_; | 181 blink::WebString request_result_name_; |
| 174 // Sources used in this request. | 182 // Sources used in this request. |
| 175 std::vector<blink::WebMediaStreamSource> sources_; | 183 std::vector<blink::WebMediaStreamSource> sources_; |
| 176 std::vector<MediaStreamSource*> sources_waiting_for_callback_; | 184 std::vector<MediaStreamSource*> sources_waiting_for_callback_; |
| 177 }; | 185 }; |
| 178 typedef ScopedVector<UserMediaRequestInfo> UserMediaRequests; | 186 typedef ScopedVector<UserMediaRequestInfo> UserMediaRequests; |
| 179 | 187 |
| 180 protected: | 188 protected: |
| 181 // These methods can be accessed in unit tests. | 189 // These methods can be accessed in unit tests. |
| 182 UserMediaRequestInfo* FindUserMediaRequestInfo(int request_id); | 190 UserMediaRequestInfo* FindUserMediaRequestInfo(int request_id); |
| 183 UserMediaRequestInfo* FindUserMediaRequestInfo( | 191 UserMediaRequestInfo* FindUserMediaRequestInfo( |
| 184 const blink::WebUserMediaRequest& request); | 192 const blink::WebUserMediaRequest& request); |
| 185 | 193 |
| 186 void DeleteUserMediaRequestInfo(UserMediaRequestInfo* request); | 194 void DeleteUserMediaRequestInfo(UserMediaRequestInfo* request); |
| 187 | 195 |
| 188 private: | 196 private: |
| 189 typedef std::vector<blink::WebMediaStreamSource> LocalStreamSources; | 197 typedef std::vector<blink::WebMediaStreamSource> LocalStreamSources; |
| 190 | 198 |
| 191 // RenderFrameObserver implementation. | 199 // RenderFrameObserver implementation. |
| 192 void OnDestruct() override; | 200 void OnDestruct() override; |
| 193 | 201 |
| 194 // Creates a WebKit representation of stream sources based on | 202 // Creates a WebKit representation of stream sources based on |
| 195 // |devices| from the MediaStreamDispatcher. | 203 // |devices| from the MediaStreamDispatcher. |
| 196 void InitializeSourceObject( | 204 blink::WebMediaStreamSource InitializeVideoSourceObject( |
| 197 const StreamDeviceInfo& device, | 205 const StreamDeviceInfo& device, |
| 198 blink::WebMediaStreamSource::Type type, | 206 const blink::WebMediaConstraints& constraints); |
| 207 |
| 208 blink::WebMediaStreamSource InitializeAudioSourceObject( |
| 209 const StreamDeviceInfo& device, |
| 199 const blink::WebMediaConstraints& constraints, | 210 const blink::WebMediaConstraints& constraints, |
| 200 blink::WebMediaStreamSource* webkit_source); | 211 bool* is_pending); |
| 201 | 212 |
| 202 void CreateVideoTracks( | 213 void CreateVideoTracks( |
| 203 const StreamDeviceInfoArray& devices, | 214 const StreamDeviceInfoArray& devices, |
| 204 const blink::WebMediaConstraints& constraints, | 215 const blink::WebMediaConstraints& constraints, |
| 205 blink::WebVector<blink::WebMediaStreamTrack>* webkit_tracks, | 216 blink::WebVector<blink::WebMediaStreamTrack>* webkit_tracks, |
| 206 UserMediaRequestInfo* request); | 217 UserMediaRequestInfo* request); |
| 207 | 218 |
| 208 void CreateAudioTracks( | 219 void CreateAudioTracks( |
| 209 const StreamDeviceInfoArray& devices, | 220 const StreamDeviceInfoArray& devices, |
| 210 const blink::WebMediaConstraints& constraints, | 221 const blink::WebMediaConstraints& constraints, |
| 211 blink::WebVector<blink::WebMediaStreamTrack>* webkit_tracks, | 222 blink::WebVector<blink::WebMediaStreamTrack>* webkit_tracks, |
| 212 UserMediaRequestInfo* request); | 223 UserMediaRequestInfo* request); |
| 213 | 224 |
| 214 // Callback function triggered when all native versions of the | 225 // Callback function triggered when all native versions of the |
| 215 // underlying media sources and tracks have been created and started. | 226 // underlying media sources and tracks have been created and started. |
| 216 void OnCreateNativeTracksCompleted( | 227 void OnCreateNativeTracksCompleted( |
| 217 UserMediaRequestInfo* request, | 228 UserMediaRequestInfo* request, |
| 218 MediaStreamRequestResult result, | 229 MediaStreamRequestResult result, |
| 219 const blink::WebString& result_name); | 230 const blink::WebString& result_name); |
| 220 | 231 |
| 221 void OnStreamGeneratedForCancelledRequest( | 232 void OnStreamGeneratedForCancelledRequest( |
| 222 const StreamDeviceInfoArray& audio_array, | 233 const StreamDeviceInfoArray& audio_array, |
| 223 const StreamDeviceInfoArray& video_array); | 234 const StreamDeviceInfoArray& video_array); |
| 224 | 235 |
| 236 static void OnAudioSourceStartedOnAudioThread( |
| 237 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 238 base::WeakPtr<UserMediaClientImpl> weak_ptr, |
| 239 MediaStreamSource* source, |
| 240 MediaStreamRequestResult result, |
| 241 const blink::WebString& result_name); |
| 242 |
| 243 void OnAudioSourceStarted(MediaStreamSource* source, |
| 244 MediaStreamRequestResult result, |
| 245 const blink::WebString& result_name); |
| 246 |
| 247 void NotifyAllRequestsOfAudioSourceStarted( |
| 248 MediaStreamSource* source, |
| 249 MediaStreamRequestResult result, |
| 250 const blink::WebString& result_name); |
| 251 |
| 225 using EnumerationResult = std::vector<MediaDeviceInfoArray>; | 252 using EnumerationResult = std::vector<MediaDeviceInfoArray>; |
| 226 void FinalizeEnumerateDevices(blink::WebMediaDevicesRequest request, | 253 void FinalizeEnumerateDevices(blink::WebMediaDevicesRequest request, |
| 227 const EnumerationResult& result); | 254 const EnumerationResult& result); |
| 228 | 255 |
| 229 void DeleteAllUserMediaRequests(); | 256 void DeleteAllUserMediaRequests(); |
| 230 | 257 |
| 231 // Returns the source that use a device with |device.session_id| | 258 // Returns the source that use a device with |device.session_id| |
| 232 // and |device.device.id|. NULL if such source doesn't exist. | 259 // and |device.device.id|. NULL if such source doesn't exist. |
| 233 const blink::WebMediaStreamSource* FindLocalSource( | 260 const blink::WebMediaStreamSource* FindLocalSource( |
| 261 const StreamDeviceInfo& device) const { |
| 262 return FindLocalSource(local_sources_, device); |
| 263 } |
| 264 const blink::WebMediaStreamSource* FindPendingLocalSource( |
| 265 const StreamDeviceInfo& device) const { |
| 266 return FindLocalSource(pending_local_sources_, device); |
| 267 } |
| 268 const blink::WebMediaStreamSource* FindLocalSource( |
| 269 const LocalStreamSources& sources, |
| 234 const StreamDeviceInfo& device) const; | 270 const StreamDeviceInfo& device) const; |
| 235 | 271 |
| 272 // Looks up a local source and returns it if found. If not found, prepares |
| 273 // a new WebMediaStreamSource with a NULL extraData pointer. |
| 274 blink::WebMediaStreamSource FindOrInitializeSourceObject( |
| 275 const StreamDeviceInfo& device); |
| 276 |
| 236 // Returns true if we do find and remove the |source|. | 277 // Returns true if we do find and remove the |source|. |
| 237 // Otherwise returns false. | 278 // Otherwise returns false. |
| 238 bool RemoveLocalSource(const blink::WebMediaStreamSource& source); | 279 bool RemoveLocalSource(const blink::WebMediaStreamSource& source); |
| 239 | 280 |
| 240 void StopLocalSource(const blink::WebMediaStreamSource& source, | 281 void StopLocalSource(const blink::WebMediaStreamSource& source, |
| 241 bool notify_dispatcher); | 282 bool notify_dispatcher); |
| 242 | 283 |
| 243 const ::mojom::MediaDevicesDispatcherHostPtr& GetMediaDevicesDispatcher(); | 284 const ::mojom::MediaDevicesDispatcherHostPtr& GetMediaDevicesDispatcher(); |
| 244 | 285 |
| 245 void SelectUserMediaDevice( | 286 void SelectUserMediaDevice( |
| (...skipping 21 matching lines...) Expand all Loading... |
| 267 // audio. | 308 // audio. |
| 268 PeerConnectionDependencyFactory* const dependency_factory_; | 309 PeerConnectionDependencyFactory* const dependency_factory_; |
| 269 | 310 |
| 270 // UserMediaClientImpl owns MediaStreamDispatcher instead of RenderFrameImpl | 311 // UserMediaClientImpl owns MediaStreamDispatcher instead of RenderFrameImpl |
| 271 // (or RenderFrameObserver) to ensure tear-down occurs in the right order. | 312 // (or RenderFrameObserver) to ensure tear-down occurs in the right order. |
| 272 const std::unique_ptr<MediaStreamDispatcher> media_stream_dispatcher_; | 313 const std::unique_ptr<MediaStreamDispatcher> media_stream_dispatcher_; |
| 273 | 314 |
| 274 ::mojom::MediaDevicesDispatcherHostPtr media_devices_dispatcher_; | 315 ::mojom::MediaDevicesDispatcherHostPtr media_devices_dispatcher_; |
| 275 | 316 |
| 276 LocalStreamSources local_sources_; | 317 LocalStreamSources local_sources_; |
| 318 LocalStreamSources pending_local_sources_; |
| 277 | 319 |
| 278 UserMediaRequests user_media_requests_; | 320 UserMediaRequests user_media_requests_; |
| 279 MediaDevicesEventDispatcher::SubscriptionIdList | 321 MediaDevicesEventDispatcher::SubscriptionIdList |
| 280 device_change_subscription_ids_; | 322 device_change_subscription_ids_; |
| 281 | 323 |
| 282 blink::WebMediaDeviceChangeObserver media_device_change_observer_; | 324 blink::WebMediaDeviceChangeObserver media_device_change_observer_; |
| 283 | 325 |
| 284 // Note: This member must be the last to ensure all outstanding weak pointers | 326 // Note: This member must be the last to ensure all outstanding weak pointers |
| 285 // are invalidated first. | 327 // are invalidated first. |
| 286 base::WeakPtrFactory<UserMediaClientImpl> weak_factory_; | 328 base::WeakPtrFactory<UserMediaClientImpl> weak_factory_; |
| 287 | 329 |
| 288 DISALLOW_COPY_AND_ASSIGN(UserMediaClientImpl); | 330 DISALLOW_COPY_AND_ASSIGN(UserMediaClientImpl); |
| 289 }; | 331 }; |
| 290 | 332 |
| 291 } // namespace content | 333 } // namespace content |
| 292 | 334 |
| 293 #endif // CONTENT_RENDERER_MEDIA_USER_MEDIA_CLIENT_IMPL_H_ | 335 #endif // CONTENT_RENDERER_MEDIA_USER_MEDIA_CLIENT_IMPL_H_ |
| OLD | NEW |