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

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 comments, and replaced pessimistic accumulators with simple proving-period logic. REBASE Created 5 years, 6 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)
25 : client_(client.Pass()), 35 : client_(client.Pass()),
26 oracle_(base::TimeDelta::FromMicroseconds( 36 oracle_(base::TimeDelta::FromMicroseconds(
27 static_cast<int64>(1000000.0 / params.requested_format.frame_rate + 37 static_cast<int64>(1000000.0 / params.requested_format.frame_rate +
28 0.5 /* to round to nearest int */))), 38 0.5 /* to round to nearest int */)),
29 params_(params), 39 params.requested_format.frame_size,
30 resolution_chooser_(params.requested_format.frame_size, 40 params.resolution_change_policy),
31 params.resolution_change_policy) {} 41 params_(params) {}
32 42
33 ThreadSafeCaptureOracle::~ThreadSafeCaptureOracle() {} 43 ThreadSafeCaptureOracle::~ThreadSafeCaptureOracle() {}
34 44
35 bool ThreadSafeCaptureOracle::ObserveEventAndDecideCapture( 45 bool ThreadSafeCaptureOracle::ObserveEventAndDecideCapture(
36 VideoCaptureOracle::Event event, 46 VideoCaptureOracle::Event event,
37 const gfx::Rect& damage_rect, 47 const gfx::Rect& damage_rect,
38 base::TimeTicks event_time, 48 base::TimeTicks event_time,
39 scoped_refptr<VideoFrame>* storage, 49 scoped_refptr<VideoFrame>* storage,
40 CaptureFrameCallback* callback) { 50 CaptureFrameCallback* callback) {
41 // Grab the current time before waiting to acquire the |lock_|. 51 // Grab the current time before waiting to acquire the |lock_|.
42 const base::TimeTicks capture_begin_time = base::TimeTicks::Now(); 52 const base::TimeTicks capture_begin_time = base::TimeTicks::Now();
43 53
44 base::AutoLock guard(lock_); 54 base::AutoLock guard(lock_);
45 55
46 if (!client_) 56 if (!client_)
47 return false; // Capture is stopped. 57 return false; // Capture is stopped.
48 58
49 const gfx::Size visible_size = resolution_chooser_.capture_size(); 59 const bool should_capture =
60 oracle_.ObserveEventAndDecideCapture(event, damage_rect, event_time);
61 const gfx::Size visible_size = oracle_.capture_size();
50 // Always round up the coded size to multiple of 16 pixels. 62 // Always round up the coded size to multiple of 16 pixels.
51 // See http://crbug.com/402151. 63 // See http://crbug.com/402151.
52 const gfx::Size coded_size((visible_size.width() + 15) & ~15, 64 const gfx::Size coded_size((visible_size.width() + 15) & ~15,
53 (visible_size.height() + 15) & ~15); 65 (visible_size.height() + 15) & ~15);
54 66
55 scoped_ptr<media::VideoCaptureDevice::Client::Buffer> output_buffer( 67 scoped_ptr<media::VideoCaptureDevice::Client::Buffer> output_buffer(
56 client_->ReserveOutputBuffer(coded_size, 68 client_->ReserveOutputBuffer(coded_size,
57 (params_.requested_format.pixel_storage != 69 (params_.requested_format.pixel_storage !=
58 media::PIXEL_STORAGE_TEXTURE) 70 media::PIXEL_STORAGE_TEXTURE)
59 ? media::PIXEL_FORMAT_I420 71 ? media::PIXEL_FORMAT_I420
60 : media::PIXEL_FORMAT_ARGB, 72 : media::PIXEL_FORMAT_ARGB,
61 params_.requested_format.pixel_storage)); 73 params_.requested_format.pixel_storage));
62 // TODO(miu): Use current buffer pool utilization to drive automatic video 74 // Get the current buffer pool utilization and attenuate it: The utilization
63 // resolution changes. http://crbug.com/156767. 75 // reported to the oracle is in terms of a maximum sustainable amount (not the
64 VLOG(2) << "Current buffer pool utilization is " 76 // absolute maximum).
65 << (client_->GetBufferPoolUtilization() * 100.0) << '%'; 77 const double attenuated_utilization = client_->GetBufferPoolUtilization() *
66 78 (100.0 / kTargetMaxPoolUtilizationPercent);
67 const bool should_capture =
68 oracle_.ObserveEventAndDecideCapture(event, damage_rect, event_time);
69 79
70 const char* event_name = 80 const char* event_name =
71 (event == VideoCaptureOracle::kTimerPoll ? "poll" : 81 (event == VideoCaptureOracle::kTimerPoll ? "poll" :
72 (event == VideoCaptureOracle::kCompositorUpdate ? "gpu" : 82 (event == VideoCaptureOracle::kCompositorUpdate ? "gpu" :
73 "unknown")); 83 "unknown"));
74 84
75 // Consider the various reasons not to initiate a capture. 85 // Consider the various reasons not to initiate a capture.
76 if (should_capture && !output_buffer.get()) { 86 if (should_capture && !output_buffer.get()) {
77 TRACE_EVENT_INSTANT1("gpu.capture", 87 TRACE_EVENT_INSTANT1("gpu.capture",
78 "PipelineLimited", 88 "PipelineLimited",
79 TRACE_EVENT_SCOPE_THREAD, 89 TRACE_EVENT_SCOPE_THREAD,
80 "trigger", 90 "trigger",
81 event_name); 91 event_name);
92 oracle_.RecordWillNotCapture(attenuated_utilization);
82 return false; 93 return false;
83 } else if (!should_capture && output_buffer.get()) { 94 } else if (!should_capture && output_buffer.get()) {
84 if (event == VideoCaptureOracle::kCompositorUpdate) { 95 if (event == VideoCaptureOracle::kCompositorUpdate) {
85 // This is a normal and acceptable way to drop a frame. We've hit our 96 // 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 97 // capture rate limit: for example, the content is animating at 60fps but
87 // we're capturing at 30fps. 98 // we're capturing at 30fps.
88 TRACE_EVENT_INSTANT1("gpu.capture", "FpsRateLimited", 99 TRACE_EVENT_INSTANT1("gpu.capture", "FpsRateLimited",
89 TRACE_EVENT_SCOPE_THREAD, 100 TRACE_EVENT_SCOPE_THREAD,
90 "trigger", event_name); 101 "trigger", event_name);
91 } 102 }
92 return false; 103 return false;
93 } else if (!should_capture && !output_buffer.get()) { 104 } else if (!should_capture && !output_buffer.get()) {
94 // We decided not to capture, but we wouldn't have been able to if we wanted 105 // We decided not to capture, but we wouldn't have been able to if we wanted
95 // to because no output buffer was available. 106 // to because no output buffer was available.
96 TRACE_EVENT_INSTANT1("gpu.capture", "NearlyPipelineLimited", 107 TRACE_EVENT_INSTANT1("gpu.capture", "NearlyPipelineLimited",
97 TRACE_EVENT_SCOPE_THREAD, 108 TRACE_EVENT_SCOPE_THREAD,
98 "trigger", event_name); 109 "trigger", event_name);
99 return false; 110 return false;
100 } 111 }
101 int frame_number = oracle_.RecordCapture(); 112 const int frame_number = oracle_.RecordCapture(attenuated_utilization);
102 TRACE_EVENT_ASYNC_BEGIN2("gpu.capture", "Capture", output_buffer.get(), 113 TRACE_EVENT_ASYNC_BEGIN2("gpu.capture", "Capture", output_buffer.get(),
103 "frame_number", frame_number, 114 "frame_number", frame_number,
104 "trigger", event_name); 115 "trigger", event_name);
105 // Texture frames wrap a texture mailbox, which we don't have at the moment. 116 // Texture frames wrap a texture mailbox, which we don't have at the moment.
106 // We do not construct those frames. 117 // We do not construct those frames.
107 if (params_.requested_format.pixel_storage != media::PIXEL_STORAGE_TEXTURE) { 118 if (params_.requested_format.pixel_storage != media::PIXEL_STORAGE_TEXTURE) {
108 *storage = VideoFrame::WrapExternalData( 119 *storage = VideoFrame::WrapExternalData(
109 VideoFrame::I420, 120 VideoFrame::I420,
110 coded_size, 121 coded_size,
111 gfx::Rect(visible_size), 122 gfx::Rect(visible_size),
112 visible_size, 123 visible_size,
113 static_cast<uint8*>(output_buffer->data()), 124 static_cast<uint8*>(output_buffer->data()),
114 output_buffer->size(), 125 output_buffer->size(),
115 base::TimeDelta()); 126 base::TimeDelta());
116 DCHECK(*storage); 127 DCHECK(*storage);
117 } 128 }
118 *callback = base::Bind(&ThreadSafeCaptureOracle::DidCaptureFrame, 129 *callback = base::Bind(&ThreadSafeCaptureOracle::DidCaptureFrame,
119 this, 130 this,
120 frame_number, 131 frame_number,
121 base::Passed(&output_buffer), 132 base::Passed(&output_buffer),
122 capture_begin_time, 133 capture_begin_time,
123 oracle_.estimated_frame_duration()); 134 oracle_.estimated_frame_duration());
124 return true; 135 return true;
125 } 136 }
126 137
127 gfx::Size ThreadSafeCaptureOracle::GetCaptureSize() const { 138 gfx::Size ThreadSafeCaptureOracle::GetCaptureSize() const {
128 base::AutoLock guard(lock_); 139 base::AutoLock guard(lock_);
129 return resolution_chooser_.capture_size(); 140 return oracle_.capture_size();
130 } 141 }
131 142
132 void ThreadSafeCaptureOracle::UpdateCaptureSize(const gfx::Size& source_size) { 143 void ThreadSafeCaptureOracle::UpdateCaptureSize(const gfx::Size& source_size) {
133 base::AutoLock guard(lock_); 144 base::AutoLock guard(lock_);
134 resolution_chooser_.SetSourceSize(source_size); 145 VLOG(1) << "Source size changed to " << source_size.ToString();
135 VLOG(1) << "Source size changed to " << source_size.ToString() 146 oracle_.SetSourceSize(source_size);
136 << " --> Capture size is now "
137 << resolution_chooser_.capture_size().ToString();
138 } 147 }
139 148
140 void ThreadSafeCaptureOracle::Stop() { 149 void ThreadSafeCaptureOracle::Stop() {
141 base::AutoLock guard(lock_); 150 base::AutoLock guard(lock_);
142 client_.reset(); 151 client_.reset();
143 } 152 }
144 153
145 void ThreadSafeCaptureOracle::ReportError(const std::string& reason) { 154 void ThreadSafeCaptureOracle::ReportError(const std::string& reason) {
146 base::AutoLock guard(lock_); 155 base::AutoLock guard(lock_);
147 if (client_) 156 if (client_)
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 } 196 }
188 } 197 }
189 198
190 void ThreadSafeCaptureOracle::DidConsumeFrame( 199 void ThreadSafeCaptureOracle::DidConsumeFrame(
191 int frame_number, 200 int frame_number,
192 const media::VideoFrameMetadata* metadata) { 201 const media::VideoFrameMetadata* metadata) {
193 // Note: This function may be called on any thread by the VideoFrame 202 // Note: This function may be called on any thread by the VideoFrame
194 // destructor. |metadata| is still valid for read-access at this point. 203 // destructor. |metadata| is still valid for read-access at this point.
195 double utilization = -1.0; 204 double utilization = -1.0;
196 if (metadata->GetDouble(media::VideoFrameMetadata::RESOURCE_UTILIZATION, 205 if (metadata->GetDouble(media::VideoFrameMetadata::RESOURCE_UTILIZATION,
197 &utilization) && 206 &utilization)) {
198 utilization >= 0.0) { 207 base::AutoLock guard(lock_);
199 VLOG(2) << "Consumer resource utilization for frame " << frame_number 208 oracle_.RecordConsumerFeedback(frame_number, utilization);
200 << ": " << utilization;
201 } 209 }
202
203 // TODO(miu): Use |utilization| to drive automatic video resolution changes.
204 // http://crbug.com/156767.
205 } 210 }
206 211
207 } // namespace media 212 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698