Make USB permissions work in the new permission message system
[chromium-blink-merge.git] / content / renderer / media / user_media_client_impl.h
blob78f19e498d541bd5834edd8310b954ee8fcdc82c
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_
8 #include <string>
9 #include <vector>
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"
31 namespace content {
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
41 // render thread.
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) {
47 public:
48 // |render_frame| and |dependency_factory| must outlive this instance.
49 UserMediaClientImpl(
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;
90 protected:
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 void DelayedGetUserMediaRequestSucceeded(
101 const blink::WebMediaStream& stream,
102 blink::WebUserMediaRequest request_info);
103 virtual void GetUserMediaRequestFailed(
104 blink::WebUserMediaRequest request_info,
105 MediaStreamRequestResult result,
106 const blink::WebString& result_name);
107 void DelayedGetUserMediaRequestFailed(
108 blink::WebUserMediaRequest request_info,
109 MediaStreamRequestResult result,
110 const blink::WebString& result_name);
112 virtual void EnumerateDevicesSucceded(
113 blink::WebMediaDevicesRequest* request,
114 blink::WebVector<blink::WebMediaDeviceInfo>& devices);
115 virtual void EnumerateSourcesSucceded(
116 blink::WebMediaStreamTrackSourcesRequest* request,
117 blink::WebVector<blink::WebSourceInfo>& sources);
118 // Creates a MediaStreamVideoSource object.
119 // This is virtual for test purposes.
120 virtual MediaStreamVideoSource* CreateVideoSource(
121 const StreamDeviceInfo& device,
122 const MediaStreamSource::SourceStoppedCallback& stop_callback);
124 private:
125 // Class for storing information about a WebKit request to create a
126 // MediaStream.
127 class UserMediaRequestInfo
128 : public base::SupportsWeakPtr<UserMediaRequestInfo> {
129 public:
130 typedef base::Callback<void(UserMediaRequestInfo* request_info,
131 MediaStreamRequestResult result,
132 const blink::WebString& result_name)>
133 ResourcesReady;
135 UserMediaRequestInfo(int request_id,
136 const blink::WebUserMediaRequest& request,
137 bool enable_automatic_output_device_selection);
138 ~UserMediaRequestInfo();
139 int request_id;
140 // True if MediaStreamDispatcher has generated the stream, see
141 // OnStreamGenerated.
142 bool generated;
143 const bool enable_automatic_output_device_selection;
144 blink::WebMediaStream web_stream;
145 blink::WebUserMediaRequest request;
147 void StartAudioTrack(const blink::WebMediaStreamTrack& track,
148 const blink::WebMediaConstraints& constraints);
150 blink::WebMediaStreamTrack CreateAndStartVideoTrack(
151 const blink::WebMediaStreamSource& source,
152 const blink::WebMediaConstraints& constraints);
154 // Triggers |callback| when all sources used in this request have either
155 // successfully started, or a source has failed to start.
156 void CallbackOnTracksStarted(const ResourcesReady& callback);
158 bool IsSourceUsed(const blink::WebMediaStreamSource& source) const;
159 void RemoveSource(const blink::WebMediaStreamSource& source);
161 bool HasPendingSources() const;
163 private:
164 void OnTrackStarted(
165 MediaStreamSource* source,
166 MediaStreamRequestResult result,
167 const blink::WebString& result_name);
168 void CheckAllTracksStarted();
170 ResourcesReady ready_callback_;
171 MediaStreamRequestResult request_result_;
172 blink::WebString request_result_name_;
173 // Sources used in this request.
174 std::vector<blink::WebMediaStreamSource> sources_;
175 std::vector<MediaStreamSource*> sources_waiting_for_callback_;
177 typedef ScopedVector<UserMediaRequestInfo> UserMediaRequests;
179 typedef std::vector<blink::WebMediaStreamSource> LocalStreamSources;
181 struct MediaDevicesRequestInfo;
182 typedef ScopedVector<MediaDevicesRequestInfo> MediaDevicesRequests;
184 // Creates a WebKit representation of stream sources based on
185 // |devices| from the MediaStreamDispatcher.
186 void InitializeSourceObject(
187 const StreamDeviceInfo& device,
188 blink::WebMediaStreamSource::Type type,
189 const blink::WebMediaConstraints& constraints,
190 blink::WebMediaStreamSource* webkit_source);
192 void CreateVideoTracks(
193 const StreamDeviceInfoArray& devices,
194 const blink::WebMediaConstraints& constraints,
195 blink::WebVector<blink::WebMediaStreamTrack>* webkit_tracks,
196 UserMediaRequestInfo* request);
198 void CreateAudioTracks(
199 const StreamDeviceInfoArray& devices,
200 const blink::WebMediaConstraints& constraints,
201 blink::WebVector<blink::WebMediaStreamTrack>* webkit_tracks,
202 UserMediaRequestInfo* request);
204 // Callback function triggered when all native versions of the
205 // underlying media sources and tracks have been created and started.
206 void OnCreateNativeTracksCompleted(
207 UserMediaRequestInfo* request,
208 MediaStreamRequestResult result,
209 const blink::WebString& result_name);
211 void OnStreamGeneratedForCancelledRequest(
212 const StreamDeviceInfoArray& audio_array,
213 const StreamDeviceInfoArray& video_array);
215 void FinalizeEnumerateDevices(MediaDevicesRequestInfo* request);
216 void FinalizeEnumerateSources(MediaDevicesRequestInfo* request);
218 UserMediaRequestInfo* FindUserMediaRequestInfo(int request_id);
219 UserMediaRequestInfo* FindUserMediaRequestInfo(
220 const blink::WebUserMediaRequest& request);
221 void DeleteUserMediaRequestInfo(UserMediaRequestInfo* request);
222 void DeleteAllUserMediaRequests();
224 MediaDevicesRequestInfo* FindMediaDevicesRequestInfo(int request_id);
225 MediaDevicesRequestInfo* FindMediaDevicesRequestInfo(
226 const blink::WebMediaDevicesRequest& request);
227 void CancelAndDeleteMediaDevicesRequest(MediaDevicesRequestInfo* request);
229 // Returns the source that use a device with |device.session_id|
230 // and |device.device.id|. NULL if such source doesn't exist.
231 const blink::WebMediaStreamSource* FindLocalSource(
232 const StreamDeviceInfo& device) const;
234 void StopLocalSource(const blink::WebMediaStreamSource& source,
235 bool notify_dispatcher);
237 // Weak ref to a PeerConnectionDependencyFactory, owned by the RenderThread.
238 // It's valid for the lifetime of RenderThread.
239 // TODO(xians): Remove this dependency once audio do not need it for local
240 // audio.
241 PeerConnectionDependencyFactory* const dependency_factory_;
243 // UserMediaClientImpl owns MediaStreamDispatcher instead of RenderFrameImpl
244 // (or RenderFrameObserver) to ensure tear-down occurs in the right order.
245 const scoped_ptr<MediaStreamDispatcher> media_stream_dispatcher_;
247 LocalStreamSources local_sources_;
249 UserMediaRequests user_media_requests_;
251 // Requests to enumerate media devices.
252 MediaDevicesRequests media_devices_requests_;
254 // Note: This member must be the last to ensure all outstanding weak pointers
255 // are invalidated first.
256 base::WeakPtrFactory<UserMediaClientImpl> weak_factory_;
258 DISALLOW_COPY_AND_ASSIGN(UserMediaClientImpl);
261 } // namespace content
263 #endif // CONTENT_RENDERER_MEDIA_USER_MEDIA_CLIENT_IMPL_H_