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_
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_view_observer.h"
20 #include "content/renderer/media/media_stream_dispatcher_eventhandler.h"
21 #include "third_party/WebKit/Source/Platform/chromium/public/WebMediaStream.h"
22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebUserMediaClient.h"
23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebUserMediaRequest.h"
24 #include "third_party/libjingle/source/talk/app/webrtc/mediastreaminterface.h"
25 #include "webkit/media/media_stream_client.h"
27 namespace webkit_media
{
28 class MediaStreamAudioRenderer
;
32 class MediaStreamDependencyFactory
;
33 class MediaStreamDispatcher
;
34 class VideoCaptureImplManager
;
35 class WebRtcAudioRenderer
;
36 class WebRtcLocalAudioRenderer
;
38 // MediaStreamImpl is a delegate for the Media Stream API messages used by
39 // WebKit. It ties together WebKit, native PeerConnection in libjingle and
40 // MediaStreamManager (via MediaStreamDispatcher and MediaStreamDispatcherHost)
41 // in the browser process. It must be created, called and destroyed on the
43 // MediaStreamImpl have weak pointers to a MediaStreamDispatcher.
44 class CONTENT_EXPORT MediaStreamImpl
45 : public RenderViewObserver
,
46 NON_EXPORTED_BASE(public WebKit::WebUserMediaClient
),
47 NON_EXPORTED_BASE(public webkit_media::MediaStreamClient
),
48 public MediaStreamDispatcherEventHandler
,
49 public base::SupportsWeakPtr
<MediaStreamImpl
>,
50 NON_EXPORTED_BASE(public base::NonThreadSafe
) {
53 RenderView
* render_view
,
54 MediaStreamDispatcher
* media_stream_dispatcher
,
55 VideoCaptureImplManager
* vc_manager
,
56 MediaStreamDependencyFactory
* dependency_factory
);
57 virtual ~MediaStreamImpl();
59 // Return true when the |url| is media stream.
60 // This static function has the same functionalilty as IsMediaStream
61 // except that it doesn't require an instance of this class.
62 // This can save some overhead time when the |url| is not media stream.
63 static bool CheckMediaStream(const GURL
& url
);
65 // WebKit::WebUserMediaClient implementation
66 virtual void requestUserMedia(
67 const WebKit::WebUserMediaRequest
& user_media_request
,
68 const WebKit::WebVector
<WebKit::WebMediaStreamSource
>& audio_sources
,
69 const WebKit::WebVector
<WebKit::WebMediaStreamSource
>& video_sources
)
71 virtual void cancelUserMediaRequest(
72 const WebKit::WebUserMediaRequest
& user_media_request
) OVERRIDE
;
74 // webkit_media::MediaStreamClient implementation.
75 virtual bool IsMediaStream(const GURL
& url
) OVERRIDE
;
76 virtual scoped_refptr
<webkit_media::VideoFrameProvider
> GetVideoFrameProvider(
78 const base::Closure
& error_cb
,
79 const webkit_media::VideoFrameProvider::RepaintCB
& repaint_cb
) OVERRIDE
;
80 virtual scoped_refptr
<webkit_media::MediaStreamAudioRenderer
>
81 GetAudioRenderer(const GURL
& url
) OVERRIDE
;
83 // MediaStreamDispatcherEventHandler implementation.
84 virtual void OnStreamGenerated(
86 const std::string
& label
,
87 const StreamDeviceInfoArray
& audio_array
,
88 const StreamDeviceInfoArray
& video_array
) OVERRIDE
;
89 virtual void OnStreamGenerationFailed(int request_id
) OVERRIDE
;
90 virtual void OnDevicesEnumerated(
92 const StreamDeviceInfoArray
& device_array
) OVERRIDE
;
93 virtual void OnDevicesEnumerationFailed(int request_id
) OVERRIDE
;
94 virtual void OnDeviceOpened(
96 const std::string
& label
,
97 const StreamDeviceInfo
& device_info
) OVERRIDE
;
98 virtual void OnDeviceOpenFailed(int request_id
) OVERRIDE
;
100 // RenderViewObserver OVERRIDE
101 virtual void FrameWillClose(WebKit::WebFrame
* frame
) OVERRIDE
;
104 // Stops a local MediaStream by notifying the MediaStreamDispatcher that the
105 // stream no longer may be used.
106 void OnLocalMediaStreamStop(const std::string
& label
);
108 // Callback function triggered when all native (libjingle) versions of the
109 // underlying media sources have been created and started.
110 // |description| is a raw pointer to the description in
111 // UserMediaRequests::description for which the underlying sources have been
113 void OnCreateNativeSourcesComplete(
114 WebKit::WebMediaStream
* description
,
115 bool request_succeeded
);
117 // This function is virtual for test purposes. A test can override this to
118 // test requesting local media streams. The function notifies WebKit that the
119 // |request| have completed and generated the MediaStream |stream|.
120 virtual void CompleteGetUserMediaRequest(
121 const WebKit::WebMediaStream
& stream
,
122 WebKit::WebUserMediaRequest
* request_info
,
123 bool request_succeeded
);
125 // Returns the WebKit representation of a MediaStream given an URL.
126 // This is virtual for test purposes.
127 virtual WebKit::WebMediaStream
GetMediaStream(const GURL
& url
);
130 // Structure for storing information about a WebKit request to create a
132 struct UserMediaRequestInfo
{
133 UserMediaRequestInfo()
134 : request_id(0), generated(false), frame(NULL
), request() {
136 UserMediaRequestInfo(int request_id
,
137 WebKit::WebFrame
* frame
,
138 const WebKit::WebUserMediaRequest
& request
)
139 : request_id(request_id
), generated(false), frame(frame
),
143 // True if MediaStreamDispatcher has generated the stream, see
144 // OnStreamGenerated.
146 WebKit::WebFrame
* frame
; // WebFrame that requested the MediaStream.
147 WebKit::WebMediaStream descriptor
;
148 WebKit::WebUserMediaRequest request
;
150 typedef ScopedVector
<UserMediaRequestInfo
> UserMediaRequests
;
152 UserMediaRequestInfo
* FindUserMediaRequestInfo(int request_id
);
153 UserMediaRequestInfo
* FindUserMediaRequestInfo(
154 WebKit::WebMediaStream
* descriptor
);
155 UserMediaRequestInfo
* FindUserMediaRequestInfo(
156 const WebKit::WebUserMediaRequest
& request
);
157 UserMediaRequestInfo
* FindUserMediaRequestInfo(const std::string
& label
);
158 void DeleteUserMediaRequestInfo(UserMediaRequestInfo
* request
);
160 scoped_refptr
<webkit_media::VideoFrameProvider
>
161 CreateVideoFrameProvider(
162 webrtc::MediaStreamInterface
* stream
,
163 const base::Closure
& error_cb
,
164 const webkit_media::VideoFrameProvider::RepaintCB
& repaint_cb
);
165 scoped_refptr
<WebRtcAudioRenderer
> CreateRemoteAudioRenderer(
166 webrtc::MediaStreamInterface
* stream
);
167 scoped_refptr
<WebRtcLocalAudioRenderer
> CreateLocalAudioRenderer(
168 webrtc::MediaStreamInterface
* stream
);
170 // Weak ref to a MediaStreamDependencyFactory, owned by the RenderThread.
171 // It's valid for the lifetime of RenderThread.
172 MediaStreamDependencyFactory
* dependency_factory_
;
174 // media_stream_dispatcher_ is a weak reference, owned by RenderView. It's
175 // valid for the lifetime of RenderView.
176 MediaStreamDispatcher
* media_stream_dispatcher_
;
178 scoped_refptr
<VideoCaptureImplManager
> vc_manager_
;
180 UserMediaRequests user_media_requests_
;
182 DISALLOW_COPY_AND_ASSIGN(MediaStreamImpl
);
185 } // namespace content
187 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_IMPL_H_