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_RTC_VIDEO_CAPTURE_DELEGATE_H_
6 #define CONTENT_RENDERER_MEDIA_RTC_VIDEO_CAPTURE_DELEGATE_H_
8 #include "base/callback.h"
9 #include "base/message_loop_proxy.h"
10 #include "content/common/media/video_capture.h"
11 #include "content/renderer/media/video_capture_impl_manager.h"
12 #include "media/video/capture/video_capture.h"
16 // Implements a simple reference counted video capturer that guarantees that
17 // methods in RtcVideoCaptureDelegateEventHandler is only called from when
18 // StartCapture have been called until after StopCapture have been called.
19 // It uses VideoCaptureImplManager to start / stop and receive I420 frames
20 // from Chrome's video capture implementation.
21 class RtcVideoCaptureDelegate
22 : public base::RefCountedThreadSafe
<RtcVideoCaptureDelegate
>,
23 public media::VideoCapture::EventHandler
{
26 CAPTURE_STOPPED
, // The capturer has been stopped or hasn't started yet.
27 CAPTURE_RUNNING
, // The capturer has been started successfully and is now
29 CAPTURE_FAILED
, // The capturer failed to start.
32 typedef base::Callback
<void(const media::VideoCapture::VideoFrameBuffer
& buf
)>
33 FrameCapturedCallback
;
34 typedef base::Callback
<void(CaptureState state
)> StateChangeCallback
;
36 RtcVideoCaptureDelegate(const media::VideoCaptureSessionId id
,
37 VideoCaptureImplManager
* vc_manager
);
39 void StartCapture(const media::VideoCaptureCapability
& capability
,
40 const FrameCapturedCallback
& captured_callback
,
41 const StateChangeCallback
& state_callback
);
44 // media::VideoCapture::EventHandler implementation.
45 // These functions are called from a thread owned by |vc_manager_|.
46 virtual void OnStarted(media::VideoCapture
* capture
) OVERRIDE
;
47 virtual void OnStopped(media::VideoCapture
* capture
) OVERRIDE
;
48 virtual void OnPaused(media::VideoCapture
* capture
) OVERRIDE
;
49 virtual void OnError(media::VideoCapture
* capture
, int error_code
) OVERRIDE
;
50 virtual void OnRemoved(media::VideoCapture
* capture
) OVERRIDE
;
51 virtual void OnBufferReady(
52 media::VideoCapture
* capture
,
53 scoped_refptr
<media::VideoCapture::VideoFrameBuffer
> buf
) OVERRIDE
;
54 virtual void OnDeviceInfoReceived(
55 media::VideoCapture
* capture
,
56 const media::VideoCaptureParams
& device_info
) OVERRIDE
;
59 friend class base::RefCountedThreadSafe
<RtcVideoCaptureDelegate
>;
61 virtual ~RtcVideoCaptureDelegate();
63 void OnBufferReadyOnCaptureThread(
64 media::VideoCapture
* capture
,
65 scoped_refptr
<media::VideoCapture::VideoFrameBuffer
> buf
);
66 void OnErrorOnCaptureThread(media::VideoCapture
* capture
);
67 void OnRemovedOnCaptureThread(media::VideoCapture
* capture
);
69 // The id identifies which video capture device is used for this video
71 media::VideoCaptureSessionId session_id_
;
72 // The video capture manager handles open/close of video capture devices.
73 scoped_refptr
<VideoCaptureImplManager
> vc_manager_
;
74 media::VideoCapture
* capture_engine_
;
76 // Accessed on the thread where StartCapture is called.
77 bool got_first_frame_
;
80 // |captured_callback_| is provided to this class in StartCapture and must be
81 // valid until StopCapture is called.
82 FrameCapturedCallback captured_callback_
;
83 // |state_callback_| is provided to this class in StartCapture and must be
84 // valid until StopCapture is called.
85 StateChangeCallback state_callback_
;
86 // MessageLoop of the caller of StartCapture.
87 scoped_refptr
<base::MessageLoopProxy
> message_loop_proxy_
;
90 } // namespace content
92 #endif // CONTENT_RENDERER_MEDIA_RTC_VIDEO_CAPTURE_DELEGATE_H_