mcasas | bf728d7 | 2015-09-05 04:46:13 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors. All rights reserved. |
hubbe | a5ed06c | 2015-02-25 00:01:25 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
chfremer | 004926e | 2016-10-24 20:52:55 | [diff] [blame] | 5 | #ifndef MEDIA_CAPTURE_VIDEO_CAPTURER_SOURCE_H_ |
| 6 | #define MEDIA_CAPTURE_VIDEO_CAPTURER_SOURCE_H_ |
hubbe | a5ed06c | 2015-02-25 00:01:25 | [diff] [blame] | 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
mcasas | bf728d7 | 2015-09-05 04:46:13 | [diff] [blame] | 11 | #include "base/callback.h" |
hubbe | a5ed06c | 2015-02-25 00:01:25 | [diff] [blame] | 12 | #include "base/memory/ref_counted.h" |
mcasas | bf728d7 | 2015-09-05 04:46:13 | [diff] [blame] | 13 | #include "base/time/time.h" |
chfremer | 004926e | 2016-10-24 20:52:55 | [diff] [blame] | 14 | #include "media/capture/capture_export.h" |
| 15 | #include "media/capture/video_capture_types.h" |
hubbe | a5ed06c | 2015-02-25 00:01:25 | [diff] [blame] | 16 | |
| 17 | namespace media { |
| 18 | |
mcasas | bf728d7 | 2015-09-05 04:46:13 | [diff] [blame] | 19 | class VideoFrame; |
| 20 | |
| 21 | // VideoCapturerSource is an interface representing the source for captured |
| 22 | // video. An implementation will periodically call the frame callback with new |
| 23 | // video frames. |
chfremer | 004926e | 2016-10-24 20:52:55 | [diff] [blame] | 24 | class CAPTURE_EXPORT VideoCapturerSource { |
hubbe | a5ed06c | 2015-02-25 00:01:25 | [diff] [blame] | 25 | public: |
| 26 | virtual ~VideoCapturerSource(); |
| 27 | |
| 28 | // This callback is used to deliver video frames. |
| 29 | // |
| 30 | // |estimated_capture_time| - The capture time of the delivered video |
| 31 | // frame. This field represents the local time at which either: 1) the frame |
| 32 | // was generated, if it was done so locally; or 2) the targeted play-out time |
| 33 | // of the frame, if it was generated from a remote source. Either way, an |
| 34 | // implementation should not present the frame before this point-in-time. This |
| 35 | // value is NOT a high-resolution timestamp, and so it should not be used as a |
| 36 | // presentation time; but, instead, it should be used for buffering playback |
| 37 | // and for A/V synchronization purposes. NOTE: It is possible for this value |
| 38 | // to be null if the current implementation lacks this timing information. |
| 39 | // |
| 40 | // |video_frame->timestamp()| gives the presentation timestamp of the video |
| 41 | // frame relative to the first frame generated by the corresponding source. |
| 42 | // Because a source can start generating frames before a subscriber is added, |
| 43 | // the first video frame delivered may not have timestamp equal to 0. |
mcasas | bf728d7 | 2015-09-05 04:46:13 | [diff] [blame] | 44 | using VideoCaptureDeliverFrameCB = |
| 45 | base::Callback<void(const scoped_refptr<media::VideoFrame>& video_frame, |
| 46 | base::TimeTicks estimated_capture_time)>; |
hubbe | a5ed06c | 2015-02-25 00:01:25 | [diff] [blame] | 47 | |
mcasas | bf728d7 | 2015-09-05 04:46:13 | [diff] [blame] | 48 | using VideoCaptureDeviceFormatsCB = |
| 49 | base::Callback<void(const media::VideoCaptureFormats&)>; |
hubbe | a5ed06c | 2015-02-25 00:01:25 | [diff] [blame] | 50 | |
mcasas | bf728d7 | 2015-09-05 04:46:13 | [diff] [blame] | 51 | using RunningCallback = base::Callback<void(bool)>; |
hubbe | a5ed06c | 2015-02-25 00:01:25 | [diff] [blame] | 52 | |
guidou | 9f7541c | 2017-04-05 21:29:24 | [diff] [blame] | 53 | // Returns formats that are preferred and can currently be used. May be empty |
| 54 | // if no formats are available or known. |
| 55 | virtual VideoCaptureFormats GetPreferredFormats() = 0; |
hubbe | a5ed06c | 2015-02-25 00:01:25 | [diff] [blame] | 56 | |
mcasas | bf728d7 | 2015-09-05 04:46:13 | [diff] [blame] | 57 | // Starts capturing frames using the capture |params|. |new_frame_callback| is |
| 58 | // triggered when a new video frame is available. |
hubbe | a5ed06c | 2015-02-25 00:01:25 | [diff] [blame] | 59 | // If capturing is started successfully then |running_callback| will be |
| 60 | // called with a parameter of true. Note that some implementations may |
| 61 | // simply reject StartCapture (by calling running_callback with a false |
| 62 | // argument) if called with the wrong task runner. |
| 63 | // If capturing fails to start or stopped due to an external event then |
| 64 | // |running_callback| will be called with a parameter of false. |
| 65 | // |running_callback| will always be called on the same thread as the |
| 66 | // StartCapture. |
| 67 | virtual void StartCapture( |
| 68 | const media::VideoCaptureParams& params, |
| 69 | const VideoCaptureDeliverFrameCB& new_frame_callback, |
hubbe | a5ed06c | 2015-02-25 00:01:25 | [diff] [blame] | 70 | const RunningCallback& running_callback) = 0; |
| 71 | |
emircan | dbe511a | 2016-03-26 00:46:01 | [diff] [blame] | 72 | // Asks source to send a refresh frame. In cases where source does not provide |
| 73 | // a continuous rate of new frames (e.g. canvas capture, screen capture where |
| 74 | // the screen's content has not changed in a while), consumers may request a |
| 75 | // "refresh frame" to be delivered. For instance, this would be needed when |
| 76 | // a new sink is added to a MediaStreamTrack. |
miu | 1c98a2e | 2016-09-29 01:18:35 | [diff] [blame] | 77 | // |
emircan | dbe511a | 2016-03-26 00:46:01 | [diff] [blame] | 78 | // The default implementation is a no-op and implementations are not required |
| 79 | // to honor this request. If they decide to and capturing is started |
| 80 | // successfully, then |new_frame_callback| should be called with a frame. |
miu | 1c98a2e | 2016-09-29 01:18:35 | [diff] [blame] | 81 | // |
| 82 | // Note: This should only be called after StartCapture() and before |
| 83 | // StopCapture(). Otherwise, its behavior is undefined. |
emircan | dbe511a | 2016-03-26 00:46:01 | [diff] [blame] | 84 | virtual void RequestRefreshFrame() {} |
| 85 | |
miu | 1c98a2e | 2016-09-29 01:18:35 | [diff] [blame] | 86 | // Optionally suspends frame delivery. The source may or may not honor this |
| 87 | // request. Thus, the caller cannot assume frame delivery will actually |
| 88 | // stop. Even if frame delivery is suspended, this might not take effect |
| 89 | // immediately. |
| 90 | // |
| 91 | // The purpose of this is to allow minimizing resource usage while |
| 92 | // there are no frame consumers present. |
| 93 | // |
| 94 | // Note: This should only be called after StartCapture() and before |
| 95 | // StopCapture(). Otherwise, its behavior is undefined. |
| 96 | virtual void MaybeSuspend() {} |
| 97 | |
| 98 | // Resumes frame delivery, if it was suspended. If frame delivery was not |
| 99 | // suspended, this is a no-op, and frame delivery will continue. |
| 100 | // |
| 101 | // Note: This should only be called after StartCapture() and before |
| 102 | // StopCapture(). Otherwise, its behavior is undefined. |
| 103 | virtual void Resume() {} |
| 104 | |
hubbe | a5ed06c | 2015-02-25 00:01:25 | [diff] [blame] | 105 | // Stops capturing frames and clears all callbacks including the |
| 106 | // SupportedFormatsCallback callback. Note that queued frame callbacks |
| 107 | // may still occur after this call, so the caller must take care to |
| 108 | // use refcounted or weak references in |new_frame_callback|. |
| 109 | virtual void StopCapture() = 0; |
| 110 | }; |
| 111 | |
| 112 | } // namespace media |
| 113 | |
chfremer | 004926e | 2016-10-24 20:52:55 | [diff] [blame] | 114 | #endif // MEDIA_CAPTURE_VIDEO_CAPTURER_SOURCE_H_ |