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

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: 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<VideoCaptureDevice::Client::Buffer> output_buffer( 67 scoped_ptr<VideoCaptureDevice::Client::Buffer> output_buffer(
56 client_->ReserveOutputBuffer(params_.requested_format.pixel_format, 68 client_->ReserveOutputBuffer(params_.requested_format.pixel_format,
57 coded_size)); 69 coded_size));
58 // TODO(miu): Use current buffer pool utilization to drive automatic video 70 // Get the current buffer pool utilization and attenuate it: The utilization
59 // resolution changes. http://crbug.com/156767. 71 // reported to the oracle is in terms of a maximum sustainable amount (not the
60 VLOG(2) << "Current buffer pool utilization is " 72 // absolute maximum).
61 << (client_->GetBufferPoolUtilization() * 100.0) << '%'; 73 const double attenuated_utilization = client_->GetBufferPoolUtilization() /
hubbe 2015/06/23 20:04:30 Multiplication is potentially faster than division
miu 2015/06/25 20:48:59 Eliminated one of the divisions. If C++11 constex
62 74 (kTargetMaxPoolUtilizationPercent / 100.0);
63 const bool should_capture =
64 oracle_.ObserveEventAndDecideCapture(event, damage_rect, event_time);
65 75
66 const char* event_name = 76 const char* event_name =
67 (event == VideoCaptureOracle::kTimerPoll ? "poll" : 77 (event == VideoCaptureOracle::kTimerPoll ? "poll" :
68 (event == VideoCaptureOracle::kCompositorUpdate ? "gpu" : 78 (event == VideoCaptureOracle::kCompositorUpdate ? "gpu" :
69 "unknown")); 79 "unknown"));
70 80
71 // Consider the various reasons not to initiate a capture. 81 // Consider the various reasons not to initiate a capture.
72 if (should_capture && !output_buffer.get()) { 82 if (should_capture && !output_buffer.get()) {
73 TRACE_EVENT_INSTANT1("gpu.capture", 83 TRACE_EVENT_INSTANT1("gpu.capture",
74 "PipelineLimited", 84 "PipelineLimited",
75 TRACE_EVENT_SCOPE_THREAD, 85 TRACE_EVENT_SCOPE_THREAD,
76 "trigger", 86 "trigger",
77 event_name); 87 event_name);
88 oracle_.RecordWillNotCapture(attenuated_utilization);
78 return false; 89 return false;
79 } else if (!should_capture && output_buffer.get()) { 90 } else if (!should_capture && output_buffer.get()) {
80 if (event == VideoCaptureOracle::kCompositorUpdate) { 91 if (event == VideoCaptureOracle::kCompositorUpdate) {
81 // This is a normal and acceptable way to drop a frame. We've hit our 92 // This is a normal and acceptable way to drop a frame. We've hit our
82 // capture rate limit: for example, the content is animating at 60fps but 93 // capture rate limit: for example, the content is animating at 60fps but
83 // we're capturing at 30fps. 94 // we're capturing at 30fps.
84 TRACE_EVENT_INSTANT1("gpu.capture", "FpsRateLimited", 95 TRACE_EVENT_INSTANT1("gpu.capture", "FpsRateLimited",
85 TRACE_EVENT_SCOPE_THREAD, 96 TRACE_EVENT_SCOPE_THREAD,
86 "trigger", event_name); 97 "trigger", event_name);
87 } 98 }
88 return false; 99 return false;
89 } else if (!should_capture && !output_buffer.get()) { 100 } else if (!should_capture && !output_buffer.get()) {
90 // We decided not to capture, but we wouldn't have been able to if we wanted 101 // We decided not to capture, but we wouldn't have been able to if we wanted
91 // to because no output buffer was available. 102 // to because no output buffer was available.
92 TRACE_EVENT_INSTANT1("gpu.capture", "NearlyPipelineLimited", 103 TRACE_EVENT_INSTANT1("gpu.capture", "NearlyPipelineLimited",
93 TRACE_EVENT_SCOPE_THREAD, 104 TRACE_EVENT_SCOPE_THREAD,
94 "trigger", event_name); 105 "trigger", event_name);
95 return false; 106 return false;
96 } 107 }
97 int frame_number = oracle_.RecordCapture(); 108 const int frame_number = oracle_.RecordCapture(attenuated_utilization);
98 TRACE_EVENT_ASYNC_BEGIN2("gpu.capture", "Capture", output_buffer.get(), 109 TRACE_EVENT_ASYNC_BEGIN2("gpu.capture", "Capture", output_buffer.get(),
99 "frame_number", frame_number, 110 "frame_number", frame_number,
100 "trigger", event_name); 111 "trigger", event_name);
101 // NATIVE_TEXTURE frames wrap a texture mailbox, which we don't have at the 112 // NATIVE_TEXTURE frames wrap a texture mailbox, which we don't have at the
102 // moment. We do not construct those frames. 113 // moment. We do not construct those frames.
103 if (params_.requested_format.pixel_format != PIXEL_FORMAT_TEXTURE) { 114 if (params_.requested_format.pixel_format != PIXEL_FORMAT_TEXTURE) {
104 *storage = VideoFrame::WrapExternalData( 115 *storage = VideoFrame::WrapExternalData(
105 VideoFrame::I420, 116 VideoFrame::I420,
106 coded_size, 117 coded_size,
107 gfx::Rect(visible_size), 118 gfx::Rect(visible_size),
108 visible_size, 119 visible_size,
109 static_cast<uint8*>(output_buffer->data()), 120 static_cast<uint8*>(output_buffer->data()),
110 output_buffer->size(), 121 output_buffer->size(),
111 base::TimeDelta()); 122 base::TimeDelta());
112 DCHECK(*storage); 123 DCHECK(*storage);
113 } 124 }
114 *callback = base::Bind(&ThreadSafeCaptureOracle::DidCaptureFrame, 125 *callback = base::Bind(&ThreadSafeCaptureOracle::DidCaptureFrame,
115 this, 126 this,
116 frame_number, 127 frame_number,
117 base::Passed(&output_buffer), 128 base::Passed(&output_buffer),
118 capture_begin_time, 129 capture_begin_time,
119 oracle_.estimated_frame_duration()); 130 oracle_.estimated_frame_duration());
120 return true; 131 return true;
121 } 132 }
122 133
123 gfx::Size ThreadSafeCaptureOracle::GetCaptureSize() const { 134 gfx::Size ThreadSafeCaptureOracle::GetCaptureSize() const {
124 base::AutoLock guard(lock_); 135 base::AutoLock guard(lock_);
125 return resolution_chooser_.capture_size(); 136 return oracle_.capture_size();
126 } 137 }
127 138
128 void ThreadSafeCaptureOracle::UpdateCaptureSize(const gfx::Size& source_size) { 139 void ThreadSafeCaptureOracle::UpdateCaptureSize(const gfx::Size& source_size) {
129 base::AutoLock guard(lock_); 140 base::AutoLock guard(lock_);
130 resolution_chooser_.SetSourceSize(source_size); 141 VLOG(1) << "Source size changed to " << source_size.ToString();
131 VLOG(1) << "Source size changed to " << source_size.ToString() 142 oracle_.SetSourceSize(source_size);
132 << " --> Capture size is now "
133 << resolution_chooser_.capture_size().ToString();
134 } 143 }
135 144
136 void ThreadSafeCaptureOracle::Stop() { 145 void ThreadSafeCaptureOracle::Stop() {
137 base::AutoLock guard(lock_); 146 base::AutoLock guard(lock_);
138 client_.reset(); 147 client_.reset();
139 } 148 }
140 149
141 void ThreadSafeCaptureOracle::ReportError(const std::string& reason) { 150 void ThreadSafeCaptureOracle::ReportError(const std::string& reason) {
142 base::AutoLock guard(lock_); 151 base::AutoLock guard(lock_);
143 if (client_) 152 if (client_)
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 int frame_number, 196 int frame_number,
188 const media::VideoFrameMetadata* metadata) { 197 const media::VideoFrameMetadata* metadata) {
189 // Note: This function may be called on any thread by the VideoFrame 198 // Note: This function may be called on any thread by the VideoFrame
190 // destructor. |metadata| is still valid for read-access at this point. 199 // destructor. |metadata| is still valid for read-access at this point.
191 double utilization = -1.0; 200 double utilization = -1.0;
192 if (metadata->GetDouble(media::VideoFrameMetadata::RESOURCE_UTILIZATION, 201 if (metadata->GetDouble(media::VideoFrameMetadata::RESOURCE_UTILIZATION,
193 &utilization) && 202 &utilization) &&
194 utilization >= 0.0) { 203 utilization >= 0.0) {
195 VLOG(2) << "Consumer resource utilization for frame " << frame_number 204 VLOG(2) << "Consumer resource utilization for frame " << frame_number
196 << ": " << utilization; 205 << ": " << utilization;
206
207 base::AutoLock guard(lock_);
208 oracle_.RecordConsumerFeedback(frame_number, utilization);
197 } 209 }
198
199 // TODO(miu): Use |utilization| to drive automatic video resolution changes.
200 // http://crbug.com/156767.
201 } 210 }
202 211
203 } // namespace media 212 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698