Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / content / renderer / media / media_stream_impl.h
blobd96ef27416720bf06f1e215d9cef63099a2a9e54
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_MEDIA_STREAM_IMPL_H_
6 #define CONTENT_RENDERER_MEDIA_MEDIA_STREAM_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/WebVector.h"
25 #include "third_party/WebKit/public/web/WebMediaDevicesRequest.h"
26 #include "third_party/WebKit/public/web/WebUserMediaClient.h"
27 #include "third_party/WebKit/public/web/WebUserMediaRequest.h"
28 #include "third_party/libjingle/source/talk/app/webrtc/mediastreaminterface.h"
30 namespace content {
31 class PeerConnectionDependencyFactory;
32 class MediaStreamDispatcher;
33 class MediaStreamVideoSource;
34 class VideoCapturerDelegate;
36 // MediaStreamImpl is a delegate for the Media Stream GetUserMedia API.
37 // It ties together WebKit and MediaStreamManager
38 // (via MediaStreamDispatcher and MediaStreamDispatcherHost)
39 // in the browser process. It must be created, called and destroyed on the
40 // render thread.
41 class CONTENT_EXPORT MediaStreamImpl
42 : public RenderFrameObserver,
43 NON_EXPORTED_BASE(public blink::WebUserMediaClient),
44 public MediaStreamDispatcherEventHandler,
45 NON_EXPORTED_BASE(public base::NonThreadSafe) {
46 public:
47 // |render_frame| and |dependency_factory| must outlive this instance.
48 MediaStreamImpl(
49 RenderFrame* render_frame,
50 PeerConnectionDependencyFactory* dependency_factory,
51 scoped_ptr<MediaStreamDispatcher> media_stream_dispatcher);
52 virtual ~MediaStreamImpl();
54 MediaStreamDispatcher* media_stream_dispatcher() const {
55 return media_stream_dispatcher_.get();
58 // blink::WebUserMediaClient implementation
59 virtual void requestUserMedia(
60 const blink::WebUserMediaRequest& user_media_request);
61 virtual void cancelUserMediaRequest(
62 const blink::WebUserMediaRequest& user_media_request);
63 virtual void requestMediaDevices(
64 const blink::WebMediaDevicesRequest& media_devices_request) OVERRIDE;
65 virtual void cancelMediaDevicesRequest(
66 const blink::WebMediaDevicesRequest& media_devices_request) OVERRIDE;
68 // MediaStreamDispatcherEventHandler implementation.
69 virtual void OnStreamGenerated(
70 int request_id,
71 const std::string& label,
72 const StreamDeviceInfoArray& audio_array,
73 const StreamDeviceInfoArray& video_array) OVERRIDE;
74 virtual void OnStreamGenerationFailed(
75 int request_id,
76 MediaStreamRequestResult result) OVERRIDE;
77 virtual void OnDeviceStopped(const std::string& label,
78 const StreamDeviceInfo& device_info) OVERRIDE;
79 virtual void OnDevicesEnumerated(
80 int request_id,
81 const StreamDeviceInfoArray& device_array) OVERRIDE;
82 virtual void OnDeviceOpened(
83 int request_id,
84 const std::string& label,
85 const StreamDeviceInfo& device_info) OVERRIDE;
86 virtual void OnDeviceOpenFailed(int request_id) OVERRIDE;
88 // RenderFrameObserver OVERRIDE
89 virtual void FrameWillClose() OVERRIDE;
91 protected:
92 // Called when |source| has been stopped from JavaScript.
93 void OnLocalSourceStopped(const blink::WebMediaStreamSource& source);
95 // These methods are virtual for test purposes. A test can override them to
96 // test requesting local media streams. The function notifies WebKit that the
97 // |request| have completed.
98 virtual void GetUserMediaRequestSucceeded(
99 const blink::WebMediaStream& stream,
100 blink::WebUserMediaRequest* request_info);
101 virtual void GetUserMediaRequestFailed(
102 blink::WebUserMediaRequest* request_info,
103 MediaStreamRequestResult result);
104 virtual void GetUserMediaRequestTrackStartedFailed(
105 blink::WebUserMediaRequest* request_info,
106 MediaStreamRequestResult result,
107 const blink::WebString& result_name);
108 virtual void EnumerateDevicesSucceded(
109 blink::WebMediaDevicesRequest* request,
110 blink::WebVector<blink::WebMediaDeviceInfo>& devices);
111 // Creates a MediaStreamVideoSource object.
112 // This is virtual for test purposes.
113 virtual MediaStreamVideoSource* CreateVideoSource(
114 const StreamDeviceInfo& device,
115 const MediaStreamSource::SourceStoppedCallback& stop_callback);
117 private:
118 // Class for storing information about a WebKit request to create a
119 // MediaStream.
120 class UserMediaRequestInfo
121 : public base::SupportsWeakPtr<UserMediaRequestInfo> {
122 public:
123 typedef base::Callback<void(UserMediaRequestInfo* request_info,
124 MediaStreamRequestResult result,
125 const blink::WebString& result_name)>
126 ResourcesReady;
128 UserMediaRequestInfo(int request_id,
129 const blink::WebUserMediaRequest& request,
130 bool enable_automatic_output_device_selection);
131 ~UserMediaRequestInfo();
132 int request_id;
133 // True if MediaStreamDispatcher has generated the stream, see
134 // OnStreamGenerated.
135 bool generated;
136 const bool enable_automatic_output_device_selection;
137 blink::WebMediaStream web_stream;
138 blink::WebUserMediaRequest request;
140 void StartAudioTrack(const blink::WebMediaStreamTrack& track,
141 const blink::WebMediaConstraints& constraints);
143 blink::WebMediaStreamTrack CreateAndStartVideoTrack(
144 const blink::WebMediaStreamSource& source,
145 const blink::WebMediaConstraints& constraints);
147 // Triggers |callback| when all sources used in this request have either
148 // successfully started, or a source has failed to start.
149 void CallbackOnTracksStarted(const ResourcesReady& callback);
151 bool IsSourceUsed(const blink::WebMediaStreamSource& source) const;
152 void RemoveSource(const blink::WebMediaStreamSource& source);
154 bool HasPendingSources() const;
156 private:
157 void OnTrackStarted(
158 MediaStreamSource* source,
159 MediaStreamRequestResult result,
160 const blink::WebString& result_name);
161 void CheckAllTracksStarted();
163 ResourcesReady ready_callback_;
164 MediaStreamRequestResult request_result_;
165 blink::WebString request_result_name_;
166 // Sources used in this request.
167 std::vector<blink::WebMediaStreamSource> sources_;
168 std::vector<MediaStreamSource*> sources_waiting_for_callback_;
170 typedef ScopedVector<UserMediaRequestInfo> UserMediaRequests;
172 typedef std::vector<blink::WebMediaStreamSource> LocalStreamSources;
174 struct MediaDevicesRequestInfo;
175 typedef ScopedVector<MediaDevicesRequestInfo> MediaDevicesRequests;
177 // Creates a WebKit representation of stream sources based on
178 // |devices| from the MediaStreamDispatcher.
179 void InitializeSourceObject(
180 const StreamDeviceInfo& device,
181 blink::WebMediaStreamSource::Type type,
182 const blink::WebMediaConstraints& constraints,
183 blink::WebMediaStreamSource* webkit_source);
185 void CreateVideoTracks(
186 const StreamDeviceInfoArray& devices,
187 const blink::WebMediaConstraints& constraints,
188 blink::WebVector<blink::WebMediaStreamTrack>* webkit_tracks,
189 UserMediaRequestInfo* request);
191 void CreateAudioTracks(
192 const StreamDeviceInfoArray& devices,
193 const blink::WebMediaConstraints& constraints,
194 blink::WebVector<blink::WebMediaStreamTrack>* webkit_tracks,
195 UserMediaRequestInfo* request);
197 // Callback function triggered when all native versions of the
198 // underlying media sources and tracks have been created and started.
199 void OnCreateNativeTracksCompleted(
200 UserMediaRequestInfo* request,
201 MediaStreamRequestResult result,
202 const blink::WebString& result_name);
204 void OnStreamGeneratedForCancelledRequest(
205 const StreamDeviceInfoArray& audio_array,
206 const StreamDeviceInfoArray& video_array);
208 UserMediaRequestInfo* FindUserMediaRequestInfo(int request_id);
209 UserMediaRequestInfo* FindUserMediaRequestInfo(
210 const blink::WebUserMediaRequest& request);
211 void DeleteUserMediaRequestInfo(UserMediaRequestInfo* request);
212 void DeleteAllUserMediaRequests();
214 MediaDevicesRequestInfo* FindMediaDevicesRequestInfo(int request_id);
215 MediaDevicesRequestInfo* FindMediaDevicesRequestInfo(
216 const blink::WebMediaDevicesRequest& request);
217 void CancelAndDeleteMediaDevicesRequest(MediaDevicesRequestInfo* request);
219 // Returns the source that use a device with |device.session_id|
220 // and |device.device.id|. NULL if such source doesn't exist.
221 const blink::WebMediaStreamSource* FindLocalSource(
222 const StreamDeviceInfo& device) const;
224 void StopLocalSource(const blink::WebMediaStreamSource& source,
225 bool notify_dispatcher);
227 // Weak ref to a PeerConnectionDependencyFactory, owned by the RenderThread.
228 // It's valid for the lifetime of RenderThread.
229 // TODO(xians): Remove this dependency once audio do not need it for local
230 // audio.
231 PeerConnectionDependencyFactory* const dependency_factory_;
233 // MediaStreamImpl owns MediaStreamDispatcher instead of RenderFrameImpl
234 // (or RenderFrameObserver) to ensure tear-down occurs in the right order.
235 const scoped_ptr<MediaStreamDispatcher> media_stream_dispatcher_;
237 LocalStreamSources local_sources_;
239 UserMediaRequests user_media_requests_;
241 // Requests to enumerate media devices.
242 MediaDevicesRequests media_devices_requests_;
244 // Note: This member must be the last to ensure all outstanding weak pointers
245 // are invalidated first.
246 base::WeakPtrFactory<MediaStreamImpl> weak_factory_;
248 DISALLOW_COPY_AND_ASSIGN(MediaStreamImpl);
251 } // namespace content
253 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_IMPL_H_