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 MEDIA_VIDEO_CAPTURE_VIDEO_CAPTURE_PROXY_H_
6 #define MEDIA_VIDEO_CAPTURE_VIDEO_CAPTURE_PROXY_H_
8 #include "base/compiler_specific.h"
9 #include "base/memory/ref_counted.h"
10 #include "media/video/capture/video_capture.h"
13 class SingleThreadTaskRunner;
18 // This is a helper class to proxy a VideoCapture::EventHandler. In the renderer
19 // process, the VideoCaptureImpl calls its handler on a "Video Capture" thread,
20 // this class allows seamless proxying to another thread ("main thread"), which
21 // would be the thread where the instance of this class is created. The
22 // "proxied" handler is then called on that thread.
23 // Since the VideoCapture is living on the "Video Capture" thread, querying its
24 // state from the "main thread" is fundamentally racy. Instead this class keeps
25 // track of the state every time it is called by the VideoCapture (on the VC
26 // thread), and forwards that information to the main thread.
27 class MEDIA_EXPORT VideoCaptureHandlerProxy
28 : public VideoCapture::EventHandler {
30 struct VideoCaptureState {
31 VideoCaptureState() : started(false), frame_rate(0) {}
36 // Called on main thread.
37 VideoCaptureHandlerProxy(
38 VideoCapture::EventHandler* proxied,
39 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner);
40 virtual ~VideoCaptureHandlerProxy();
42 // Retrieves the state of the VideoCapture. Must be called on main thread.
43 const VideoCaptureState& state() const { return state_; }
45 // VideoCapture::EventHandler implementation, called on VC thread.
46 virtual void OnStarted(VideoCapture* capture) OVERRIDE;
47 virtual void OnStopped(VideoCapture* capture) OVERRIDE;
48 virtual void OnPaused(VideoCapture* capture) OVERRIDE;
49 virtual void OnError(VideoCapture* capture, int error_code) OVERRIDE;
50 virtual void OnRemoved(VideoCapture* capture) OVERRIDE;
51 virtual void OnFrameReady(VideoCapture* capture,
52 const scoped_refptr<VideoFrame>& frame) OVERRIDE;
55 // Called on main thread.
56 void OnStartedOnMainThread(
57 VideoCapture* capture,
58 const VideoCaptureState& state);
59 void OnStoppedOnMainThread(
60 VideoCapture* capture,
61 const VideoCaptureState& state);
62 void OnPausedOnMainThread(
63 VideoCapture* capture,
64 const VideoCaptureState& state);
65 void OnErrorOnMainThread(
66 VideoCapture* capture,
67 const VideoCaptureState& state,
69 void OnRemovedOnMainThread(
70 VideoCapture* capture,
71 const VideoCaptureState& state);
72 void OnFrameReadyOnMainThread(VideoCapture* capture,
73 const VideoCaptureState& state,
74 const scoped_refptr<VideoFrame>& frame);
76 // Only accessed from main thread.
77 VideoCapture::EventHandler* proxied_;
78 VideoCaptureState state_;
80 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
85 #endif // MEDIA_VIDEO_CAPTURE_VIDEO_CAPTURE_PROXY_H_