Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / media / video / capture / video_capture_proxy.h
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.
4
5 #ifndef MEDIA_VIDEO_CAPTURE_VIDEO_CAPTURE_PROXY_H_
6 #define MEDIA_VIDEO_CAPTURE_VIDEO_CAPTURE_PROXY_H_
7
8 #include "base/compiler_specific.h"
9 #include "base/memory/ref_counted.h"
10 #include "media/video/capture/video_capture.h"
11
12 namespace base {
13 class SingleThreadTaskRunner;
14 }
15
16 namespace media {
17
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 {
29  public:
30   struct VideoCaptureState {
31     VideoCaptureState() : started(false), frame_rate(0) {}
32     bool started;
33     int frame_rate;
34   };
35
36   // Called on main thread.
37   VideoCaptureHandlerProxy(
38       VideoCapture::EventHandler* proxied,
39       const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner);
40   virtual ~VideoCaptureHandlerProxy();
41
42   // Retrieves the state of the VideoCapture. Must be called on main thread.
43   const VideoCaptureState& state() const { return state_; }
44
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;
53
54  private:
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,
68       int error_code);
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);
75
76   // Only accessed from main thread.
77   VideoCapture::EventHandler* proxied_;
78   VideoCaptureState state_;
79
80   scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
81 };
82
83 }  // namespace media
84
85 #endif  // MEDIA_VIDEO_CAPTURE_VIDEO_CAPTURE_PROXY_H_