Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(207)

Side by Side Diff: media/capture/thread_safe_capture_oracle.cc

Issue 1199593005: Automatic resolution throttling for screen capture pipeline. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@resolution_chooser_ITEM13
Patch Set: Addressed hubbe's nit. Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "media/capture/thread_safe_capture_oracle.h" 5 #include "media/capture/thread_safe_capture_oracle.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "base/synchronization/lock.h" 11 #include "base/synchronization/lock.h"
12 #include "base/time/time.h" 12 #include "base/time/time.h"
13 #include "base/trace_event/trace_event.h" 13 #include "base/trace_event/trace_event.h"
14 #include "media/base/video_capture_types.h" 14 #include "media/base/video_capture_types.h"
15 #include "media/base/video_frame.h" 15 #include "media/base/video_frame.h"
16 #include "media/base/video_frame_metadata.h" 16 #include "media/base/video_frame_metadata.h"
17 #include "media/base/video_util.h" 17 #include "media/base/video_util.h"
18 #include "ui/gfx/geometry/rect.h" 18 #include "ui/gfx/geometry/rect.h"
19 19
20 namespace media { 20 namespace media {
21 21
22 namespace {
23
24 // The target maximum amount of the buffer pool to utilize. Actual buffer pool
25 // utilization is attenuated by this amount before being reported to the
26 // VideoCaptureOracle. This value takes into account the maximum number of
27 // buffer pool buffers and a desired safety margin.
28 const int kTargetMaxPoolUtilizationPercent = 60;
29
30 } // namespace
31
22 ThreadSafeCaptureOracle::ThreadSafeCaptureOracle( 32 ThreadSafeCaptureOracle::ThreadSafeCaptureOracle(
23 scoped_ptr<VideoCaptureDevice::Client> client, 33 scoped_ptr<VideoCaptureDevice::Client> client,
24 const VideoCaptureParams& params) 34 const VideoCaptureParams& params,
35 bool enable_auto_throttling)
25 : client_(client.Pass()), 36 : client_(client.Pass()),
26 oracle_(base::TimeDelta::FromMicroseconds( 37 oracle_(base::TimeDelta::FromMicroseconds(
27 static_cast<int64>(1000000.0 / params.requested_format.frame_rate + 38 static_cast<int64>(1000000.0 / params.requested_format.frame_rate +
28 0.5 /* to round to nearest int */))), 39 0.5 /* to round to nearest int */)),
29 params_(params), 40 params.requested_format.frame_size,
30 resolution_chooser_(params.requested_format.frame_size, 41 params.resolution_change_policy,
31 params.resolution_change_policy) {} 42 enable_auto_throttling),
43 params_(params) {}
32 44
33 ThreadSafeCaptureOracle::~ThreadSafeCaptureOracle() {} 45 ThreadSafeCaptureOracle::~ThreadSafeCaptureOracle() {}
34 46
35 bool ThreadSafeCaptureOracle::ObserveEventAndDecideCapture( 47 bool ThreadSafeCaptureOracle::ObserveEventAndDecideCapture(
36 VideoCaptureOracle::Event event, 48 VideoCaptureOracle::Event event,
37 const gfx::Rect& damage_rect, 49 const gfx::Rect& damage_rect,
38 base::TimeTicks event_time, 50 base::TimeTicks event_time,
39 scoped_refptr<VideoFrame>* storage, 51 scoped_refptr<VideoFrame>* storage,
40 CaptureFrameCallback* callback) { 52 CaptureFrameCallback* callback) {
41 // Grab the current time before waiting to acquire the |lock_|. 53 // Grab the current time before waiting to acquire the |lock_|.
42 const base::TimeTicks capture_begin_time = base::TimeTicks::Now(); 54 const base::TimeTicks capture_begin_time = base::TimeTicks::Now();
43 55
44 base::AutoLock guard(lock_); 56 base::AutoLock guard(lock_);
45 57
46 if (!client_) 58 if (!client_)
47 return false; // Capture is stopped. 59 return false; // Capture is stopped.
48 60
49 const gfx::Size visible_size = resolution_chooser_.capture_size(); 61 const bool should_capture =
62 oracle_.ObserveEventAndDecideCapture(event, damage_rect, event_time);
63 const gfx::Size visible_size = oracle_.capture_size();
50 // Always round up the coded size to multiple of 16 pixels. 64 // Always round up the coded size to multiple of 16 pixels.
51 // See http://crbug.com/402151. 65 // See http://crbug.com/402151.
52 const gfx::Size coded_size((visible_size.width() + 15) & ~15, 66 const gfx::Size coded_size((visible_size.width() + 15) & ~15,
53 (visible_size.height() + 15) & ~15); 67 (visible_size.height() + 15) & ~15);
54 68
55 scoped_ptr<media::VideoCaptureDevice::Client::Buffer> output_buffer( 69 scoped_ptr<media::VideoCaptureDevice::Client::Buffer> output_buffer(
56 client_->ReserveOutputBuffer(coded_size, 70 client_->ReserveOutputBuffer(coded_size,
57 (params_.requested_format.pixel_storage != 71 (params_.requested_format.pixel_storage !=
58 media::PIXEL_STORAGE_TEXTURE) 72 media::PIXEL_STORAGE_TEXTURE)
59 ? media::PIXEL_FORMAT_I420 73 ? media::PIXEL_FORMAT_I420
60 : media::PIXEL_FORMAT_ARGB, 74 : media::PIXEL_FORMAT_ARGB,
61 params_.requested_format.pixel_storage)); 75 params_.requested_format.pixel_storage));
62 // TODO(miu): Use current buffer pool utilization to drive automatic video 76 // Get the current buffer pool utilization and attenuate it: The utilization
63 // resolution changes. http://crbug.com/156767. 77 // reported to the oracle is in terms of a maximum sustainable amount (not the
64 VLOG(2) << "Current buffer pool utilization is " 78 // absolute maximum).
65 << (client_->GetBufferPoolUtilization() * 100.0) << '%'; 79 const double attenuated_utilization = client_->GetBufferPoolUtilization() *
66 80 (100.0 / kTargetMaxPoolUtilizationPercent);
67 const bool should_capture =
68 oracle_.ObserveEventAndDecideCapture(event, damage_rect, event_time);
69 81
70 const char* event_name = 82 const char* event_name =
71 (event == VideoCaptureOracle::kTimerPoll ? "poll" : 83 (event == VideoCaptureOracle::kTimerPoll ? "poll" :
72 (event == VideoCaptureOracle::kCompositorUpdate ? "gpu" : 84 (event == VideoCaptureOracle::kCompositorUpdate ? "gpu" :
73 "unknown")); 85 "unknown"));
74 86
75 // Consider the various reasons not to initiate a capture. 87 // Consider the various reasons not to initiate a capture.
76 if (should_capture && !output_buffer.get()) { 88 if (should_capture && !output_buffer.get()) {
77 TRACE_EVENT_INSTANT1("gpu.capture", 89 TRACE_EVENT_INSTANT1("gpu.capture",
78 "PipelineLimited", 90 "PipelineLimited",
79 TRACE_EVENT_SCOPE_THREAD, 91 TRACE_EVENT_SCOPE_THREAD,
80 "trigger", 92 "trigger",
81 event_name); 93 event_name);
94 oracle_.RecordWillNotCapture(attenuated_utilization);
82 return false; 95 return false;
83 } else if (!should_capture && output_buffer.get()) { 96 } else if (!should_capture && output_buffer.get()) {
84 if (event == VideoCaptureOracle::kCompositorUpdate) { 97 if (event == VideoCaptureOracle::kCompositorUpdate) {
85 // This is a normal and acceptable way to drop a frame. We've hit our 98 // This is a normal and acceptable way to drop a frame. We've hit our
86 // capture rate limit: for example, the content is animating at 60fps but 99 // capture rate limit: for example, the content is animating at 60fps but
87 // we're capturing at 30fps. 100 // we're capturing at 30fps.
88 TRACE_EVENT_INSTANT1("gpu.capture", "FpsRateLimited", 101 TRACE_EVENT_INSTANT1("gpu.capture", "FpsRateLimited",
89 TRACE_EVENT_SCOPE_THREAD, 102 TRACE_EVENT_SCOPE_THREAD,
90 "trigger", event_name); 103 "trigger", event_name);
91 } 104 }
92 return false; 105 return false;
93 } else if (!should_capture && !output_buffer.get()) { 106 } else if (!should_capture && !output_buffer.get()) {
94 // We decided not to capture, but we wouldn't have been able to if we wanted 107 // We decided not to capture, but we wouldn't have been able to if we wanted
95 // to because no output buffer was available. 108 // to because no output buffer was available.
96 TRACE_EVENT_INSTANT1("gpu.capture", "NearlyPipelineLimited", 109 TRACE_EVENT_INSTANT1("gpu.capture", "NearlyPipelineLimited",
97 TRACE_EVENT_SCOPE_THREAD, 110 TRACE_EVENT_SCOPE_THREAD,
98 "trigger", event_name); 111 "trigger", event_name);
99 return false; 112 return false;
100 } 113 }
101 int frame_number = oracle_.RecordCapture(); 114 const int frame_number = oracle_.RecordCapture(attenuated_utilization);
102 TRACE_EVENT_ASYNC_BEGIN2("gpu.capture", "Capture", output_buffer.get(), 115 TRACE_EVENT_ASYNC_BEGIN2("gpu.capture", "Capture", output_buffer.get(),
103 "frame_number", frame_number, 116 "frame_number", frame_number,
104 "trigger", event_name); 117 "trigger", event_name);
105 // Texture frames wrap a texture mailbox, which we don't have at the moment. 118 // Texture frames wrap a texture mailbox, which we don't have at the moment.
106 // We do not construct those frames. 119 // We do not construct those frames.
107 if (params_.requested_format.pixel_storage != media::PIXEL_STORAGE_TEXTURE) { 120 if (params_.requested_format.pixel_storage != media::PIXEL_STORAGE_TEXTURE) {
108 *storage = VideoFrame::WrapExternalData( 121 *storage = VideoFrame::WrapExternalData(
109 VideoFrame::I420, 122 VideoFrame::I420,
110 coded_size, 123 coded_size,
111 gfx::Rect(visible_size), 124 gfx::Rect(visible_size),
112 visible_size, 125 visible_size,
113 static_cast<uint8*>(output_buffer->data()), 126 static_cast<uint8*>(output_buffer->data()),
114 output_buffer->size(), 127 output_buffer->size(),
115 base::TimeDelta()); 128 base::TimeDelta());
116 DCHECK(*storage); 129 DCHECK(*storage);
117 } 130 }
118 *callback = base::Bind(&ThreadSafeCaptureOracle::DidCaptureFrame, 131 *callback = base::Bind(&ThreadSafeCaptureOracle::DidCaptureFrame,
119 this, 132 this,
120 frame_number, 133 frame_number,
121 base::Passed(&output_buffer), 134 base::Passed(&output_buffer),
122 capture_begin_time, 135 capture_begin_time,
123 oracle_.estimated_frame_duration()); 136 oracle_.estimated_frame_duration());
124 return true; 137 return true;
125 } 138 }
126 139
127 gfx::Size ThreadSafeCaptureOracle::GetCaptureSize() const { 140 gfx::Size ThreadSafeCaptureOracle::GetCaptureSize() const {
128 base::AutoLock guard(lock_); 141 base::AutoLock guard(lock_);
129 return resolution_chooser_.capture_size(); 142 return oracle_.capture_size();
130 } 143 }
131 144
132 void ThreadSafeCaptureOracle::UpdateCaptureSize(const gfx::Size& source_size) { 145 void ThreadSafeCaptureOracle::UpdateCaptureSize(const gfx::Size& source_size) {
133 base::AutoLock guard(lock_); 146 base::AutoLock guard(lock_);
134 resolution_chooser_.SetSourceSize(source_size); 147 VLOG(1) << "Source size changed to " << source_size.ToString();
135 VLOG(1) << "Source size changed to " << source_size.ToString() 148 oracle_.SetSourceSize(source_size);
136 << " --> Capture size is now "
137 << resolution_chooser_.capture_size().ToString();
138 } 149 }
139 150
140 void ThreadSafeCaptureOracle::Stop() { 151 void ThreadSafeCaptureOracle::Stop() {
141 base::AutoLock guard(lock_); 152 base::AutoLock guard(lock_);
142 client_.reset(); 153 client_.reset();
143 } 154 }
144 155
145 void ThreadSafeCaptureOracle::ReportError(const std::string& reason) { 156 void ThreadSafeCaptureOracle::ReportError(const std::string& reason) {
146 base::AutoLock guard(lock_); 157 base::AutoLock guard(lock_);
147 if (client_) 158 if (client_)
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 } 198 }
188 } 199 }
189 200
190 void ThreadSafeCaptureOracle::DidConsumeFrame( 201 void ThreadSafeCaptureOracle::DidConsumeFrame(
191 int frame_number, 202 int frame_number,
192 const media::VideoFrameMetadata* metadata) { 203 const media::VideoFrameMetadata* metadata) {
193 // Note: This function may be called on any thread by the VideoFrame 204 // Note: This function may be called on any thread by the VideoFrame
194 // destructor. |metadata| is still valid for read-access at this point. 205 // destructor. |metadata| is still valid for read-access at this point.
195 double utilization = -1.0; 206 double utilization = -1.0;
196 if (metadata->GetDouble(media::VideoFrameMetadata::RESOURCE_UTILIZATION, 207 if (metadata->GetDouble(media::VideoFrameMetadata::RESOURCE_UTILIZATION,
197 &utilization) && 208 &utilization)) {
198 utilization >= 0.0) { 209 base::AutoLock guard(lock_);
199 VLOG(2) << "Consumer resource utilization for frame " << frame_number 210 oracle_.RecordConsumerFeedback(frame_number, utilization);
200 << ": " << utilization;
201 } 211 }
202
203 // TODO(miu): Use |utilization| to drive automatic video resolution changes.
204 // http://crbug.com/156767.
205 } 212 }
206 213
207 } // namespace media 214 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698