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
15namespace 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.
22class CONTENT_EXPORT VideoCapturerDelegate
23    : public base::RefCountedThreadSafe<VideoCapturerDelegate> {
24 public:
25  typedef base::Callback<void(MediaStreamRequestResult result)> RunningCallback;
26
27  explicit VideoCapturerDelegate(const StreamDeviceInfo& device_info);
28
29  // Collects the formats that can currently be used.
30  // |max_requested_height|, |max_requested_width|, and
31  // |max_requested_frame_rate| is used by Tab and Screen capture to decide what
32  // resolution/framerate to generate. |callback| is triggered when the formats
33  // have been collected.
34  virtual void GetCurrentSupportedFormats(
35      int max_requested_width,
36      int max_requested_height,
37      double max_requested_frame_rate,
38      const VideoCaptureDeviceFormatsCB& callback);
39
40  // Starts capturing frames using the resolution in |params|.
41  // |new_frame_callback| is triggered when a new video frame is available.
42  // If capturing is started successfully then |running_callback| will be
43  // called with a parameter of true.
44  // If capturing fails to start or stopped due to an external event then
45  // |running_callback| will be called with a parameter of false.
46  virtual void StartCapture(
47      const media::VideoCaptureParams& params,
48      const VideoCaptureDeliverFrameCB& new_frame_callback,
49      const RunningCallback& running_callback);
50
51  // Stops capturing frames and clears all callbacks including the
52  // SupportedFormatsCallback callback.
53  virtual void StopCapture();
54
55 private:
56  FRIEND_TEST_ALL_PREFIXES(MediaStreamVideoCapturerSourceTest, Ended);
57  friend class base::RefCountedThreadSafe<VideoCapturerDelegate>;
58  friend class MockVideoCapturerDelegate;
59
60  virtual ~VideoCapturerDelegate();
61
62  void OnStateUpdateOnRenderThread(VideoCaptureState state);
63  void OnDeviceFormatsInUseReceived(const media::VideoCaptureFormats& formats);
64  void OnDeviceSupportedFormatsEnumerated(
65      const media::VideoCaptureFormats& formats);
66
67  // The id identifies which video capture device is used for this video
68  // capture session.
69  media::VideoCaptureSessionId session_id_;
70  base::Closure release_device_cb_;
71  base::Closure stop_capture_cb_;
72
73  bool is_screen_cast_;
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.
91class 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      double max_requested_frame_rate,
107      const VideoCaptureDeviceFormatsCB& callback) OVERRIDE;
108
109  virtual void StartSourceImpl(
110      const media::VideoCaptureFormat& format,
111      const VideoCaptureDeliverFrameCB& frame_callback) OVERRIDE;
112
113  virtual void StopSourceImpl() OVERRIDE;
114
115 private:
116  // The delegate that provides video frames.
117  scoped_refptr<VideoCapturerDelegate> delegate_;
118
119  DISALLOW_COPY_AND_ASSIGN(MediaStreamVideoCapturerSource);
120};
121
122}  // namespace content
123
124#endif  // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_VIDEO_CAPTURER_SOURCE_H_
125