• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef CONTENT_RENDERER_MEDIA_MEDIA_STREAM_VIDEO_CAPTURER_SOURCE_H_
6 #define CONTENT_RENDERER_MEDIA_MEDIA_STREAM_VIDEO_CAPTURER_SOURCE_H_
7 
8 #include "base/callback.h"
9 #include "base/gtest_prod_util.h"
10 #include "base/message_loop/message_loop_proxy.h"
11 #include "base/threading/thread_checker.h"
12 #include "content/common/media/video_capture.h"
13 #include "content/renderer/media/media_stream_video_source.h"
14 
15 namespace content {
16 
17 // VideoCapturerDelegate is a delegate used by MediaStreamVideoCapturerSource
18 // for local video capturer. It uses VideoCaptureImplManager to start / stop
19 // and receive I420 frames from Chrome's video capture implementation.
20 //
21 // This is a render thread only object.
22 class CONTENT_EXPORT VideoCapturerDelegate
23     : public base::RefCountedThreadSafe<VideoCapturerDelegate> {
24  public:
25   typedef base::Callback<void(bool running)> RunningCallback;
26 
27   explicit VideoCapturerDelegate(
28       const StreamDeviceInfo& device_info);
29 
30   // Collects the formats that can currently be used.
31   // |max_requested_height| and |max_requested_width| is used by Tab and Screen
32   // capture to decide what resolution to generate.
33   // |callback| is triggered when the formats have been collected.
34   virtual void GetCurrentSupportedFormats(
35       int max_requested_width,
36       int max_requested_height,
37       const VideoCaptureDeviceFormatsCB& callback);
38 
39   // Starts capturing frames using the resolution in |params|.
40   // |new_frame_callback| is triggered when a new video frame is available.
41   // If capturing is started successfully then |running_callback| will be
42   // called with a parameter of true.
43   // If capturing fails to start or stopped due to an external event then
44   // |running_callback| will be called with a parameter of false.
45   virtual void StartCapture(
46       const media::VideoCaptureParams& params,
47       const VideoCaptureDeliverFrameCB& new_frame_callback,
48       const RunningCallback& running_callback);
49 
50   // Stops capturing frames and clears all callbacks including the
51   // SupportedFormatsCallback callback.
52   virtual void StopCapture();
53 
54  private:
55   FRIEND_TEST_ALL_PREFIXES(MediaStreamVideoCapturerSourceTest, Ended);
56   friend class base::RefCountedThreadSafe<VideoCapturerDelegate>;
57   friend class MockVideoCapturerDelegate;
58 
59   virtual ~VideoCapturerDelegate();
60 
61   void OnStateUpdateOnRenderThread(VideoCaptureState state);
62   void OnDeviceFormatsInUseReceived(const media::VideoCaptureFormats& formats);
63   void OnDeviceSupportedFormatsEnumerated(
64       const media::VideoCaptureFormats& formats);
65 
66   // The id identifies which video capture device is used for this video
67   // capture session.
68   media::VideoCaptureSessionId session_id_;
69   base::Closure release_device_cb_;
70   base::Closure stop_capture_cb_;
71 
72   bool is_screen_cast_;
73   bool got_first_frame_;
74 
75   // |running_callback| is provided to this class in StartCapture and must be
76   // valid until StopCapture is called.
77   RunningCallback running_callback_;
78 
79   VideoCaptureDeviceFormatsCB source_formats_callback_;
80 
81   // Bound to the render thread.
82   base::ThreadChecker thread_checker_;
83 
84   DISALLOW_COPY_AND_ASSIGN(VideoCapturerDelegate);
85 };
86 
87 // Owned by WebMediaStreamSource in Blink as a representation of a video
88 // stream coming from a camera.
89 // This is a render thread only object. All methods must be called on the
90 // render thread.
91 class CONTENT_EXPORT MediaStreamVideoCapturerSource
92     : public MediaStreamVideoSource {
93  public:
94   MediaStreamVideoCapturerSource(
95       const StreamDeviceInfo& device_info,
96       const SourceStoppedCallback& stop_callback,
97       const scoped_refptr<VideoCapturerDelegate>& delegate);
98 
99   virtual ~MediaStreamVideoCapturerSource();
100 
101  protected:
102   // Implements MediaStreamVideoSource.
103   virtual void GetCurrentSupportedFormats(
104       int max_requested_width,
105       int max_requested_height,
106       const VideoCaptureDeviceFormatsCB& callback) OVERRIDE;
107 
108   virtual void StartSourceImpl(
109       const media::VideoCaptureParams& params,
110       const VideoCaptureDeliverFrameCB& frame_callback) OVERRIDE;
111 
112   virtual void StopSourceImpl() OVERRIDE;
113 
114  private:
115   // The delegate that provides video frames.
116   scoped_refptr<VideoCapturerDelegate> delegate_;
117 
118   DISALLOW_COPY_AND_ASSIGN(MediaStreamVideoCapturerSource);
119 };
120 
121 }  // namespace content
122 
123 #endif  // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_VIDEO_CAPTURER_SOURCE_H_
124