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 MessageLoopProxy; 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), width(0), height(0), frame_rate(0) {} 32 bool started; 33 int width; 34 int height; 35 int frame_rate; 36 }; 37 38 // Called on main thread. 39 VideoCaptureHandlerProxy( 40 VideoCapture::EventHandler* proxied, 41 scoped_refptr<base::MessageLoopProxy> main_message_loop); 42 virtual ~VideoCaptureHandlerProxy(); 43 44 // Retrieves the state of the VideoCapture. Must be called on main thread. 45 const VideoCaptureState& state() const { return state_; } 46 47 // VideoCapture::EventHandler implementation, called on VC thread. 48 virtual void OnStarted(VideoCapture* capture) OVERRIDE; 49 virtual void OnStopped(VideoCapture* capture) OVERRIDE; 50 virtual void OnPaused(VideoCapture* capture) OVERRIDE; 51 virtual void OnError(VideoCapture* capture, int error_code) OVERRIDE; 52 virtual void OnRemoved(VideoCapture* capture) OVERRIDE; 53 virtual void OnBufferReady( 54 VideoCapture* capture, 55 scoped_refptr<VideoCapture::VideoFrameBuffer> buffer) OVERRIDE; 56 virtual void OnDeviceInfoReceived( 57 VideoCapture* capture, 58 const VideoCaptureParams& device_info) OVERRIDE; 59 60 private: 61 // Called on main thread. 62 void OnStartedOnMainThread( 63 VideoCapture* capture, 64 const VideoCaptureState& state); 65 void OnStoppedOnMainThread( 66 VideoCapture* capture, 67 const VideoCaptureState& state); 68 void OnPausedOnMainThread( 69 VideoCapture* capture, 70 const VideoCaptureState& state); 71 void OnErrorOnMainThread( 72 VideoCapture* capture, 73 const VideoCaptureState& state, 74 int error_code); 75 void OnRemovedOnMainThread( 76 VideoCapture* capture, 77 const VideoCaptureState& state); 78 void OnBufferReadyOnMainThread( 79 VideoCapture* capture, 80 const VideoCaptureState& state, 81 scoped_refptr<VideoCapture::VideoFrameBuffer> buffer); 82 void OnDeviceInfoReceivedOnMainThread( 83 VideoCapture* capture, 84 const VideoCaptureState& state, 85 const VideoCaptureParams& device_info); 86 87 // Only accessed from main thread. 88 VideoCapture::EventHandler* proxied_; 89 VideoCaptureState state_; 90 91 scoped_refptr<base::MessageLoopProxy> main_message_loop_; 92 }; 93 94 } // namespace media 95 96 #endif // MEDIA_VIDEO_CAPTURE_VIDEO_CAPTURE_PROXY_H_ 97