| OLD | NEW |
| (Empty) |
| 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 MEDIA_CAPTURE_CONTENT_THREAD_SAFE_CAPTURE_ORACLE_H_ | |
| 6 #define MEDIA_CAPTURE_CONTENT_THREAD_SAFE_CAPTURE_ORACLE_H_ | |
| 7 | |
| 8 #include <memory> | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/memory/ref_counted.h" | |
| 12 #include "media/base/video_frame.h" | |
| 13 #include "media/capture/capture_export.h" | |
| 14 #include "media/capture/content/video_capture_oracle.h" | |
| 15 #include "media/capture/video/video_capture_device.h" | |
| 16 | |
| 17 namespace tracked_objects { | |
| 18 class Location; | |
| 19 } // namespace tracked_objects | |
| 20 | |
| 21 namespace media { | |
| 22 | |
| 23 struct VideoCaptureParams; | |
| 24 class VideoFrame; | |
| 25 | |
| 26 // Thread-safe, refcounted proxy to the VideoCaptureOracle. This proxy wraps | |
| 27 // the VideoCaptureOracle, which decides which frames to capture, and a | |
| 28 // VideoCaptureDevice::Client, which allocates and receives the captured | |
| 29 // frames, in a lock to synchronize state between the two. | |
| 30 class CAPTURE_EXPORT ThreadSafeCaptureOracle | |
| 31 : public base::RefCountedThreadSafe<ThreadSafeCaptureOracle> { | |
| 32 public: | |
| 33 ThreadSafeCaptureOracle(std::unique_ptr<VideoCaptureDevice::Client> client, | |
| 34 const VideoCaptureParams& params, | |
| 35 bool enable_auto_throttling); | |
| 36 | |
| 37 // Called when a captured frame is available or an error has occurred. | |
| 38 // If |success| is true then |frame| is valid and |timestamp| indicates when | |
| 39 // the frame was painted. | |
| 40 // If |success| is false, all other parameters are invalid. | |
| 41 typedef base::Callback<void(const scoped_refptr<VideoFrame>& frame, | |
| 42 base::TimeTicks timestamp, | |
| 43 bool success)> CaptureFrameCallback; | |
| 44 | |
| 45 // Record a change |event| along with its |damage_rect| and |event_time|, and | |
| 46 // then make a decision whether to proceed with capture. The decision is based | |
| 47 // on recent event history, capture activity, and the availability of | |
| 48 // resources. | |
| 49 // | |
| 50 // If this method returns false, the caller should take no further action. | |
| 51 // Otherwise, |storage| is set to the destination for the video frame capture | |
| 52 // and the caller should initiate capture. Then, once the video frame has | |
| 53 // been populated with its content, or if capture failed, the |callback| | |
| 54 // should be run. | |
| 55 bool ObserveEventAndDecideCapture(VideoCaptureOracle::Event event, | |
| 56 const gfx::Rect& damage_rect, | |
| 57 base::TimeTicks event_time, | |
| 58 scoped_refptr<VideoFrame>* storage, | |
| 59 CaptureFrameCallback* callback); | |
| 60 | |
| 61 // Attempt to re-send the last frame to the VideoCaptureDevice::Client. | |
| 62 // Returns true if successful. This can fail if the last frame is no longer | |
| 63 // available in the buffer pool, or if the VideoCaptureOracle decides to | |
| 64 // reject the "passive" refresh. | |
| 65 bool AttemptPassiveRefresh(); | |
| 66 | |
| 67 base::TimeDelta min_capture_period() const { | |
| 68 return oracle_.min_capture_period(); | |
| 69 } | |
| 70 | |
| 71 base::TimeTicks last_time_animation_was_detected() const { | |
| 72 return oracle_.last_time_animation_was_detected(); | |
| 73 } | |
| 74 | |
| 75 gfx::Size max_frame_size() const { | |
| 76 return params_.requested_format.frame_size; | |
| 77 } | |
| 78 | |
| 79 // Returns the current capture resolution. | |
| 80 gfx::Size GetCaptureSize() const; | |
| 81 | |
| 82 // Updates capture resolution based on the supplied source size and the | |
| 83 // maximum frame size. | |
| 84 void UpdateCaptureSize(const gfx::Size& source_size); | |
| 85 | |
| 86 // Stop new captures from happening (but doesn't forget the client). | |
| 87 void Stop(); | |
| 88 | |
| 89 // Signal an error to the client. | |
| 90 void ReportError(const tracked_objects::Location& from_here, | |
| 91 const std::string& reason); | |
| 92 | |
| 93 private: | |
| 94 friend class base::RefCountedThreadSafe<ThreadSafeCaptureOracle>; | |
| 95 virtual ~ThreadSafeCaptureOracle(); | |
| 96 | |
| 97 // Callback invoked on completion of all captures. | |
| 98 void DidCaptureFrame( | |
| 99 int frame_number, | |
| 100 std::unique_ptr<VideoCaptureDevice::Client::Buffer> buffer, | |
| 101 base::TimeTicks capture_begin_time, | |
| 102 base::TimeDelta estimated_frame_duration, | |
| 103 const scoped_refptr<VideoFrame>& frame, | |
| 104 base::TimeTicks reference_time, | |
| 105 bool success); | |
| 106 | |
| 107 // Callback invoked once all consumers have finished with a delivered video | |
| 108 // frame. Consumer feedback signals are scanned from the frame's |metadata|. | |
| 109 void DidConsumeFrame(int frame_number, | |
| 110 const media::VideoFrameMetadata* metadata); | |
| 111 | |
| 112 // Protects everything below it. | |
| 113 mutable base::Lock lock_; | |
| 114 | |
| 115 // Recipient of our capture activity. | |
| 116 std::unique_ptr<VideoCaptureDevice::Client> client_; | |
| 117 | |
| 118 // Makes the decision to capture a frame. | |
| 119 VideoCaptureOracle oracle_; | |
| 120 | |
| 121 // The video capture parameters used to construct the oracle proxy. | |
| 122 const VideoCaptureParams params_; | |
| 123 }; | |
| 124 | |
| 125 } // namespace media | |
| 126 | |
| 127 #endif // MEDIA_CAPTURE_CONTENT_THREAD_SAFE_CAPTURE_ORACLE_H_ | |
| OLD | NEW |