1 // Copyright 2014 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_VIDEO_CAPTURER_SOURCE_H_
6 #define CONTENT_RENDERER_MEDIA_MEDIA_STREAM_VIDEO_CAPTURER_SOURCE_H_
8 #include "base/callback.h"
9 #include "base/gtest_prod_util.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/message_loop/message_loop_proxy.h"
12 #include "base/threading/thread_checker.h"
13 #include "content/common/media/video_capture.h"
14 #include "content/renderer/media/media_stream_video_source.h"
18 // VideoCapturerDelegate is a delegate used by MediaStreamVideoCapturerSource
19 // for local video capturer. It uses VideoCaptureImplManager to start / stop
20 // and receive I420 frames from Chrome's video capture implementation.
22 // This is a render thread only object.
23 class CONTENT_EXPORT VideoCapturerDelegate
{
25 typedef base::Callback
<void(MediaStreamRequestResult result
)> RunningCallback
;
27 explicit VideoCapturerDelegate(const StreamDeviceInfo
& device_info
);
28 virtual ~VideoCapturerDelegate();
30 // Collects the formats that can currently be used.
31 // |max_requested_height|, |max_requested_width|, and
32 // |max_requested_frame_rate| is used by Tab and Screen capture to decide what
33 // resolution/framerate to generate. |callback| is triggered when the formats
34 // have been collected.
35 virtual void GetCurrentSupportedFormats(
36 int max_requested_width
,
37 int max_requested_height
,
38 double max_requested_frame_rate
,
39 const VideoCaptureDeviceFormatsCB
& callback
);
41 // Starts capturing frames using the resolution in |params|.
42 // |new_frame_callback| is triggered when a new video frame is available.
43 // If capturing is started successfully then |running_callback| will be
44 // called with a parameter of true.
45 // If capturing fails to start or stopped due to an external event then
46 // |running_callback| will be called with a parameter of false.
47 virtual void StartCapture(
48 const media::VideoCaptureParams
& params
,
49 const VideoCaptureDeliverFrameCB
& new_frame_callback
,
50 const RunningCallback
& running_callback
);
52 // Stops capturing frames and clears all callbacks including the
53 // SupportedFormatsCallback callback.
54 virtual void StopCapture();
57 FRIEND_TEST_ALL_PREFIXES(MediaStreamVideoCapturerSourceTest
, Ended
);
58 friend class base::RefCountedThreadSafe
<VideoCapturerDelegate
>;
59 friend class MockVideoCapturerDelegate
;
61 void OnStateUpdateOnRenderThread(VideoCaptureState state
);
62 void OnDeviceFormatsInUseReceived(const media::VideoCaptureFormats
& formats
);
63 void OnDeviceSupportedFormatsEnumerated(
64 const media::VideoCaptureFormats
& formats
);
66 // The id identifies which video capture device is used for this video
68 media::VideoCaptureSessionId session_id_
;
69 base::Closure release_device_cb_
;
70 base::Closure stop_capture_cb_
;
74 // |running_callback| is provided to this class in StartCapture and must be
75 // valid until StopCapture is called.
76 RunningCallback running_callback_
;
78 VideoCaptureDeviceFormatsCB source_formats_callback_
;
80 // Bound to the render thread.
81 base::ThreadChecker thread_checker_
;
83 base::WeakPtrFactory
<VideoCapturerDelegate
> weak_factory_
;
85 DISALLOW_COPY_AND_ASSIGN(VideoCapturerDelegate
);
88 // Owned by WebMediaStreamSource in Blink as a representation of a video
89 // stream coming from a camera.
90 // This is a render thread only object. All methods must be called on the
92 class CONTENT_EXPORT MediaStreamVideoCapturerSource
93 : public MediaStreamVideoSource
{
95 MediaStreamVideoCapturerSource(
96 const StreamDeviceInfo
& device_info
,
97 const SourceStoppedCallback
& stop_callback
,
98 scoped_ptr
<VideoCapturerDelegate
> delegate
);
100 virtual ~MediaStreamVideoCapturerSource();
103 // Implements MediaStreamVideoSource.
104 void GetCurrentSupportedFormats(
105 int max_requested_width
,
106 int max_requested_height
,
107 double max_requested_frame_rate
,
108 const VideoCaptureDeviceFormatsCB
& callback
) override
;
110 void StartSourceImpl(
111 const media::VideoCaptureFormat
& format
,
112 const VideoCaptureDeliverFrameCB
& frame_callback
) override
;
114 void StopSourceImpl() override
;
117 // The delegate that provides video frames.
118 scoped_ptr
<VideoCapturerDelegate
> delegate_
;
120 DISALLOW_COPY_AND_ASSIGN(MediaStreamVideoCapturerSource
);
123 } // namespace content
125 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_VIDEO_CAPTURER_SOURCE_H_