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
3 // found in the LICENSE file.
5 #ifndef CONTENT_RENDERER_MEDIA_USER_MEDIA_CLIENT_IMPL_H_
6 #define CONTENT_RENDERER_MEDIA_USER_MEDIA_CLIENT_IMPL_H_
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/scoped_vector.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/threading/non_thread_safe.h"
18 #include "content/common/content_export.h"
19 #include "content/public/renderer/render_frame_observer.h"
20 #include "content/renderer/media/media_stream_dispatcher_eventhandler.h"
21 #include "content/renderer/media/media_stream_source.h"
22 #include "third_party/WebKit/public/platform/WebMediaStream.h"
23 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h"
24 #include "third_party/WebKit/public/platform/WebSourceInfo.h"
25 #include "third_party/WebKit/public/platform/WebVector.h"
26 #include "third_party/WebKit/public/web/WebMediaDevicesRequest.h"
27 #include "third_party/WebKit/public/web/WebUserMediaClient.h"
28 #include "third_party/WebKit/public/web/WebUserMediaRequest.h"
29 #include "third_party/libjingle/source/talk/app/webrtc/mediastreaminterface.h"
32 class PeerConnectionDependencyFactory
;
33 class MediaStreamDispatcher
;
34 class MediaStreamVideoSource
;
35 class VideoCapturerDelegate
;
37 // UserMediaClientImpl is a delegate for the Media Stream GetUserMedia API.
38 // It ties together WebKit and MediaStreamManager
39 // (via MediaStreamDispatcher and MediaStreamDispatcherHost)
40 // in the browser process. It must be created, called and destroyed on the
42 class CONTENT_EXPORT UserMediaClientImpl
43 : public RenderFrameObserver
,
44 NON_EXPORTED_BASE(public blink::WebUserMediaClient
),
45 public MediaStreamDispatcherEventHandler
,
46 NON_EXPORTED_BASE(public base::NonThreadSafe
) {
48 // |render_frame| and |dependency_factory| must outlive this instance.
50 RenderFrame
* render_frame
,
51 PeerConnectionDependencyFactory
* dependency_factory
,
52 scoped_ptr
<MediaStreamDispatcher
> media_stream_dispatcher
);
53 virtual ~UserMediaClientImpl();
55 MediaStreamDispatcher
* media_stream_dispatcher() const {
56 return media_stream_dispatcher_
.get();
59 // blink::WebUserMediaClient implementation
60 virtual void requestUserMedia(
61 const blink::WebUserMediaRequest
& user_media_request
);
62 virtual void cancelUserMediaRequest(
63 const blink::WebUserMediaRequest
& user_media_request
);
64 virtual void requestMediaDevices(
65 const blink::WebMediaDevicesRequest
& media_devices_request
) override
;
66 virtual void cancelMediaDevicesRequest(
67 const blink::WebMediaDevicesRequest
& media_devices_request
) override
;
68 virtual void requestSources(
69 const blink::WebMediaStreamTrackSourcesRequest
& sources_request
) override
;
71 // MediaStreamDispatcherEventHandler implementation.
72 void OnStreamGenerated(int request_id
,
73 const std::string
& label
,
74 const StreamDeviceInfoArray
& audio_array
,
75 const StreamDeviceInfoArray
& video_array
) override
;
76 void OnStreamGenerationFailed(int request_id
,
77 MediaStreamRequestResult result
) override
;
78 void OnDeviceStopped(const std::string
& label
,
79 const StreamDeviceInfo
& device_info
) override
;
80 void OnDevicesEnumerated(int request_id
,
81 const StreamDeviceInfoArray
& device_array
) override
;
82 void OnDeviceOpened(int request_id
,
83 const std::string
& label
,
84 const StreamDeviceInfo
& device_info
) override
;
85 void OnDeviceOpenFailed(int request_id
) override
;
87 // RenderFrameObserver override
88 void FrameWillClose() override
;
91 // Called when |source| has been stopped from JavaScript.
92 void OnLocalSourceStopped(const blink::WebMediaStreamSource
& source
);
94 // These methods are virtual for test purposes. A test can override them to
95 // test requesting local media streams. The function notifies WebKit that the
96 // |request| have completed.
97 virtual void GetUserMediaRequestSucceeded(
98 const blink::WebMediaStream
& stream
,
99 blink::WebUserMediaRequest
* request_info
);
100 virtual void GetUserMediaRequestFailed(
101 blink::WebUserMediaRequest
* request_info
,
102 MediaStreamRequestResult result
,
103 const blink::WebString
& result_name
);
104 virtual void EnumerateDevicesSucceded(
105 blink::WebMediaDevicesRequest
* request
,
106 blink::WebVector
<blink::WebMediaDeviceInfo
>& devices
);
107 virtual void EnumerateSourcesSucceded(
108 blink::WebMediaStreamTrackSourcesRequest
* request
,
109 blink::WebVector
<blink::WebSourceInfo
>& sources
);
110 // Creates a MediaStreamVideoSource object.
111 // This is virtual for test purposes.
112 virtual MediaStreamVideoSource
* CreateVideoSource(
113 const StreamDeviceInfo
& device
,
114 const MediaStreamSource::SourceStoppedCallback
& stop_callback
);
117 // Class for storing information about a WebKit request to create a
119 class UserMediaRequestInfo
120 : public base::SupportsWeakPtr
<UserMediaRequestInfo
> {
122 typedef base::Callback
<void(UserMediaRequestInfo
* request_info
,
123 MediaStreamRequestResult result
,
124 const blink::WebString
& result_name
)>
127 UserMediaRequestInfo(int request_id
,
128 const blink::WebUserMediaRequest
& request
,
129 bool enable_automatic_output_device_selection
);
130 ~UserMediaRequestInfo();
132 // True if MediaStreamDispatcher has generated the stream, see
133 // OnStreamGenerated.
135 const bool enable_automatic_output_device_selection
;
136 blink::WebMediaStream web_stream
;
137 blink::WebUserMediaRequest request
;
139 void StartAudioTrack(const blink::WebMediaStreamTrack
& track
,
140 const blink::WebMediaConstraints
& constraints
);
142 blink::WebMediaStreamTrack
CreateAndStartVideoTrack(
143 const blink::WebMediaStreamSource
& source
,
144 const blink::WebMediaConstraints
& constraints
);
146 // Triggers |callback| when all sources used in this request have either
147 // successfully started, or a source has failed to start.
148 void CallbackOnTracksStarted(const ResourcesReady
& callback
);
150 bool IsSourceUsed(const blink::WebMediaStreamSource
& source
) const;
151 void RemoveSource(const blink::WebMediaStreamSource
& source
);
153 bool HasPendingSources() const;
157 MediaStreamSource
* source
,
158 MediaStreamRequestResult result
,
159 const blink::WebString
& result_name
);
160 void CheckAllTracksStarted();
162 ResourcesReady ready_callback_
;
163 MediaStreamRequestResult request_result_
;
164 blink::WebString request_result_name_
;
165 // Sources used in this request.
166 std::vector
<blink::WebMediaStreamSource
> sources_
;
167 std::vector
<MediaStreamSource
*> sources_waiting_for_callback_
;
169 typedef ScopedVector
<UserMediaRequestInfo
> UserMediaRequests
;
171 typedef std::vector
<blink::WebMediaStreamSource
> LocalStreamSources
;
173 struct MediaDevicesRequestInfo
;
174 typedef ScopedVector
<MediaDevicesRequestInfo
> MediaDevicesRequests
;
176 // Creates a WebKit representation of stream sources based on
177 // |devices| from the MediaStreamDispatcher.
178 void InitializeSourceObject(
179 const StreamDeviceInfo
& device
,
180 blink::WebMediaStreamSource::Type type
,
181 const blink::WebMediaConstraints
& constraints
,
182 blink::WebMediaStreamSource
* webkit_source
);
184 void CreateVideoTracks(
185 const StreamDeviceInfoArray
& devices
,
186 const blink::WebMediaConstraints
& constraints
,
187 blink::WebVector
<blink::WebMediaStreamTrack
>* webkit_tracks
,
188 UserMediaRequestInfo
* request
);
190 void CreateAudioTracks(
191 const StreamDeviceInfoArray
& devices
,
192 const blink::WebMediaConstraints
& constraints
,
193 blink::WebVector
<blink::WebMediaStreamTrack
>* webkit_tracks
,
194 UserMediaRequestInfo
* request
);
196 // Callback function triggered when all native versions of the
197 // underlying media sources and tracks have been created and started.
198 void OnCreateNativeTracksCompleted(
199 UserMediaRequestInfo
* request
,
200 MediaStreamRequestResult result
,
201 const blink::WebString
& result_name
);
203 void OnStreamGeneratedForCancelledRequest(
204 const StreamDeviceInfoArray
& audio_array
,
205 const StreamDeviceInfoArray
& video_array
);
207 void FinalizeEnumerateDevices(MediaDevicesRequestInfo
* request
);
208 void FinalizeEnumerateSources(MediaDevicesRequestInfo
* request
);
210 UserMediaRequestInfo
* FindUserMediaRequestInfo(int request_id
);
211 UserMediaRequestInfo
* FindUserMediaRequestInfo(
212 const blink::WebUserMediaRequest
& request
);
213 void DeleteUserMediaRequestInfo(UserMediaRequestInfo
* request
);
214 void DeleteAllUserMediaRequests();
216 MediaDevicesRequestInfo
* FindMediaDevicesRequestInfo(int request_id
);
217 MediaDevicesRequestInfo
* FindMediaDevicesRequestInfo(
218 const blink::WebMediaDevicesRequest
& request
);
219 void CancelAndDeleteMediaDevicesRequest(MediaDevicesRequestInfo
* request
);
221 // Returns the source that use a device with |device.session_id|
222 // and |device.device.id|. NULL if such source doesn't exist.
223 const blink::WebMediaStreamSource
* FindLocalSource(
224 const StreamDeviceInfo
& device
) const;
226 void StopLocalSource(const blink::WebMediaStreamSource
& source
,
227 bool notify_dispatcher
);
229 // Weak ref to a PeerConnectionDependencyFactory, owned by the RenderThread.
230 // It's valid for the lifetime of RenderThread.
231 // TODO(xians): Remove this dependency once audio do not need it for local
233 PeerConnectionDependencyFactory
* const dependency_factory_
;
235 // UserMediaClientImpl owns MediaStreamDispatcher instead of RenderFrameImpl
236 // (or RenderFrameObserver) to ensure tear-down occurs in the right order.
237 const scoped_ptr
<MediaStreamDispatcher
> media_stream_dispatcher_
;
239 LocalStreamSources local_sources_
;
241 UserMediaRequests user_media_requests_
;
243 // Requests to enumerate media devices.
244 MediaDevicesRequests media_devices_requests_
;
246 // Note: This member must be the last to ensure all outstanding weak pointers
247 // are invalidated first.
248 base::WeakPtrFactory
<UserMediaClientImpl
> weak_factory_
;
250 DISALLOW_COPY_AND_ASSIGN(UserMediaClientImpl
);
253 } // namespace content
255 #endif // CONTENT_RENDERER_MEDIA_USER_MEDIA_CLIENT_IMPL_H_