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

Side by Side Diff: media/gpu/avda_codec_allocator.cc

Issue 2508053002: media: Do a TimedWait() for video surface teardown in AVDA (Closed)
Patch Set: Created 4 years, 1 month 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/gpu/avda_codec_allocator.h" 5 #include "media/gpu/avda_codec_allocator.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 10
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/metrics/histogram.h" 12 #include "base/metrics/histogram.h"
13 #include "base/sys_info.h" 13 #include "base/sys_info.h"
14 #include "base/task_runner_util.h" 14 #include "base/task_runner_util.h"
15 #include "base/threading/thread.h" 15 #include "base/threading/thread.h"
16 #include "base/threading/thread_checker.h" 16 #include "base/threading/thread_checker.h"
17 #include "base/threading/thread_task_runner_handle.h" 17 #include "base/threading/thread_task_runner_handle.h"
18 #include "base/time/default_tick_clock.h" 18 #include "base/time/default_tick_clock.h"
19 #include "base/trace_event/trace_event.h" 19 #include "base/trace_event/trace_event.h"
20 #include "media/base/android/sdk_media_codec_bridge.h"
20 #include "media/base/limits.h" 21 #include "media/base/limits.h"
21 #include "media/base/media.h" 22 #include "media/base/media.h"
22 #include "media/base/timestamp_constants.h" 23 #include "media/base/timestamp_constants.h"
24 #include "media/gpu/android_video_decode_accelerator.h"
23 25
24 namespace media { 26 namespace media {
25 27
26 namespace { 28 namespace {
27 29
28 // Give tasks on the construction thread 800ms before considering them hung. 30 base::LazyInstance<AVDACodecAllocator>::Leaky g_avda_codec_allocator =
29 // MediaCodec.configure() calls typically take 100-200ms on a N5, so 800ms is 31 LAZY_INSTANCE_INITIALIZER;
30 // expected to very rarely result in false positives. Also, false positives have 32
31 // low impact because we resume using the thread if its apparently hung task 33 // Give tasks 800ms before considering them hung. MediaCodec.configure() calls
32 // completes. 34 // typically take 100-200ms on a N5, so 800ms is expected to very rarely result
35 // in false positives. Also, false positives have low impact because we resume
36 // using the thread when the task completes.
33 constexpr base::TimeDelta kHungTaskDetectionTimeout = 37 constexpr base::TimeDelta kHungTaskDetectionTimeout =
34 base::TimeDelta::FromMilliseconds(800); 38 base::TimeDelta::FromMilliseconds(800);
35 39
40 // Delete |codec| and signal |done_event| if it's not null.
41 void DeleteMediaCodecAndSignal(std::unique_ptr<VideoCodecBridge> codec,
42 base::WaitableEvent* done_event) {
43 codec.reset();
44 if (done_event)
45 done_event->Signal();
46 }
47
36 } // namespace 48 } // namespace
37 49
50 CodecConfig::CodecConfig() {}
51 CodecConfig::~CodecConfig() {}
52
38 AVDACodecAllocator::TestInformation::TestInformation() {} 53 AVDACodecAllocator::TestInformation::TestInformation() {}
39 AVDACodecAllocator::TestInformation::~TestInformation() {} 54 AVDACodecAllocator::TestInformation::~TestInformation() {}
40 55
41 AVDACodecAllocator::HangDetector::HangDetector(base::TickClock* tick_clock) 56 AVDACodecAllocator::HangDetector::HangDetector(base::TickClock* tick_clock)
42 : tick_clock_(tick_clock) {} 57 : tick_clock_(tick_clock) {}
43 58
44 void AVDACodecAllocator::HangDetector::WillProcessTask( 59 void AVDACodecAllocator::HangDetector::WillProcessTask(
45 const base::PendingTask& pending_task) { 60 const base::PendingTask& pending_task) {
46 base::AutoLock l(lock_); 61 base::AutoLock l(lock_);
47 task_start_time_ = tick_clock_->NowTicks(); 62 task_start_time_ = tick_clock_->NowTicks();
48 } 63 }
49 64
50 void AVDACodecAllocator::HangDetector::DidProcessTask( 65 void AVDACodecAllocator::HangDetector::DidProcessTask(
51 const base::PendingTask& pending_task) { 66 const base::PendingTask& pending_task) {
52 base::AutoLock l(lock_); 67 base::AutoLock l(lock_);
53 task_start_time_ = base::TimeTicks(); 68 task_start_time_ = base::TimeTicks();
54 } 69 }
55 70
56 bool AVDACodecAllocator::HangDetector::IsThreadLikelyHung() { 71 bool AVDACodecAllocator::HangDetector::IsThreadLikelyHung() {
57 base::AutoLock l(lock_); 72 base::AutoLock l(lock_);
58 if (task_start_time_.is_null()) 73 if (task_start_time_.is_null())
59 return false; 74 return false;
60 75
61 return (tick_clock_->NowTicks() - task_start_time_) > 76 return (tick_clock_->NowTicks() - task_start_time_) >
62 kHungTaskDetectionTimeout; 77 kHungTaskDetectionTimeout;
63 } 78 }
64 79
65 // Make sure the construction threads are started for |avda|. 80 // static
66 bool AVDACodecAllocator::StartThread(AndroidVideoDecodeAccelerator* avda) { 81 AVDACodecAllocator& AVDACodecAllocator::Get() {
82 return g_avda_codec_allocator.Get();
83 }
84
85 // Make sure the construction threads are started for |client|.
86 bool AVDACodecAllocator::StartThread(AVDACodecAllocatorClient* client) {
67 DCHECK(thread_checker_.CalledOnValidThread()); 87 DCHECK(thread_checker_.CalledOnValidThread());
68 88
69 // Cancel any pending StopThreadTask()s because we need the threads now. 89 // Cancel any pending StopThreadTask()s because we need the threads now.
70 weak_this_factory_.InvalidateWeakPtrs(); 90 weak_this_factory_.InvalidateWeakPtrs();
71 91
72 // Try to start all threads if they haven't been started. Remember that 92 // Try to start all threads if they haven't been started. Remember that
73 // threads fail to start fairly often. 93 // threads fail to start fairly often.
74 for (size_t i = 0; i < threads_.size(); i++) { 94 for (size_t i = 0; i < threads_.size(); i++) {
75 if (threads_[i]->thread.IsRunning()) 95 if (threads_[i]->thread.IsRunning())
76 continue; 96 continue;
77 97
78 if (!threads_[i]->thread.Start()) 98 if (!threads_[i]->thread.Start())
79 continue; 99 continue;
80 100
81 // Register the hang detector to observe the thread's MessageLoop. 101 // Register the hang detector to observe the thread's MessageLoop.
82 threads_[i]->thread.task_runner()->PostTask( 102 threads_[i]->thread.task_runner()->PostTask(
83 FROM_HERE, 103 FROM_HERE,
84 base::Bind(&base::MessageLoop::AddTaskObserver, 104 base::Bind(&base::MessageLoop::AddTaskObserver,
85 base::Unretained(threads_[i]->thread.message_loop()), 105 base::Unretained(threads_[i]->thread.message_loop()),
86 &threads_[i]->hang_detector)); 106 &threads_[i]->hang_detector));
87 } 107 }
88 108
89 // Make sure that the construction thread started, else refuse to run. 109 // Make sure that the construction thread started, else refuse to run.
90 // If other threads fail to start, then we'll post to the GPU main thread for 110 // If other threads fail to start, then we'll post to the GPU main thread for
91 // those cases. SW allocation failures are much less rare, so this usually 111 // those cases. SW allocation failures are much less rare, so this usually
92 // just costs us the latency of doing the codec allocation on the main thread. 112 // just costs us the latency of doing the codec allocation on the main thread.
93 if (!threads_[TaskType::AUTO_CODEC]->thread.IsRunning()) 113 if (!threads_[TaskType::AUTO_CODEC]->thread.IsRunning())
94 return false; 114 return false;
95 115
96 thread_avda_instances_.insert(avda); 116 clients_.insert(client);
97 UMA_HISTOGRAM_ENUMERATION("Media.AVDA.NumAVDAInstances", 117 UMA_HISTOGRAM_ENUMERATION("Media.AVDA.NumAVDAInstances", clients_.size(),
98 thread_avda_instances_.size(),
99 31); // PRESUBMIT_IGNORE_UMA_MAX 118 31); // PRESUBMIT_IGNORE_UMA_MAX
100 return true; 119 return true;
101 } 120 }
102 121
103 void AVDACodecAllocator::StopThread(AndroidVideoDecodeAccelerator* avda) { 122 void AVDACodecAllocator::StopThread(AVDACodecAllocatorClient* client) {
104 DCHECK(thread_checker_.CalledOnValidThread()); 123 DCHECK(thread_checker_.CalledOnValidThread());
105 124
106 thread_avda_instances_.erase(avda); 125 clients_.erase(client);
107 // Post a task to stop the thread through the thread's task runner and back 126 // Post a task to stop the thread through the thread's task runner and back
108 // to this thread. This ensures that all pending tasks are run first. If the 127 // to this thread. This ensures that all pending tasks are run first. If the
109 // thread is hung we don't post a task to avoid leaking an unbounded number 128 // thread is hung we don't post a task to avoid leaking an unbounded number
110 // of tasks on its queue. If the thread is not hung, but appears to be, it 129 // of tasks on its queue. If the thread is not hung, but appears to be, it
111 // will stay alive until next time an AVDA tries to stop it. We're 130 // will stay alive until next time an AVDA tries to stop it. We're
112 // guaranteed to not run StopThreadTask() when the thread is hung because if 131 // guaranteed to not run StopThreadTask() when the thread is hung because if
113 // an AVDA queues tasks after DoNothing(), the StopThreadTask() reply will 132 // an AVDA queues tasks after DoNothing(), the StopThreadTask() reply will
114 // be canceled by invalidating its weak pointer. 133 // be canceled by invalidating its weak pointer.
115 base::WaitableEvent* event = 134 base::WaitableEvent* event =
116 (test_info_ ? test_info_->stop_event_.get() : nullptr); 135 (test_info_ ? test_info_->stop_event_.get() : nullptr);
117 if (!thread_avda_instances_.empty()) { 136 if (!clients_.empty()) {
118 // If we aren't stopping, then signal immediately. 137 // If we aren't stopping, then signal immediately.
119 if (event) 138 if (event)
120 event->Signal(); 139 event->Signal();
121 return; 140 return;
122 } 141 }
123 142
124 for (size_t i = 0; i < threads_.size(); i++) { 143 for (size_t i = 0; i < threads_.size(); i++) {
125 if (threads_[i]->thread.IsRunning() && 144 if (threads_[i]->thread.IsRunning() &&
126 !threads_[i]->hang_detector.IsThreadLikelyHung()) { 145 !threads_[i]->hang_detector.IsThreadLikelyHung()) {
127 threads_[i]->thread.task_runner()->PostTaskAndReply( 146 threads_[i]->thread.task_runner()->PostTaskAndReply(
(...skipping 15 matching lines...) Expand all
143 // Fail over to the main thread if this thread failed to start. Note that 162 // Fail over to the main thread if this thread failed to start. Note that
144 // if the AUTO_CODEC thread fails to start, then AVDA init will fail. 163 // if the AUTO_CODEC thread fails to start, then AVDA init will fail.
145 // We won't fall back autodetection to the main thread, even without a 164 // We won't fall back autodetection to the main thread, even without a
146 // special case here. 165 // special case here.
147 if (!thread.IsRunning()) 166 if (!thread.IsRunning())
148 return base::ThreadTaskRunnerHandle::Get(); 167 return base::ThreadTaskRunnerHandle::Get();
149 168
150 return thread.task_runner(); 169 return thread.task_runner();
151 } 170 }
152 171
172 bool AVDACodecAllocator::AllocateSurface(AVDACodecAllocatorClient* client,
173 int surface_id) {
174 DVLOG(1) << __func__ << ": " << surface_id;
175 DCHECK(thread_checker_.CalledOnValidThread());
176
177 if (surface_id == SurfaceManager::kNoSurfaceID)
178 return true;
179
180 if (!surface_owners_.count(surface_id) &&
181 !codec_release_tasks_.count(surface_id)) {
182 surface_owners_[surface_id].owner = client;
183 return true;
184 }
185
186 // The surface is already owned or being released. |client| replaces the
187 // previous waiter if any.
188 OwnerRecord& record = surface_owners_[surface_id];
189 if (record.waiter)
190 record.waiter->OnSurfaceAvailable(false);
191 record.waiter = client;
192 return false;
193 }
194
195 void AVDACodecAllocator::DeallocateSurface(AVDACodecAllocatorClient* client,
196 int surface_id) {
197 DCHECK(thread_checker_.CalledOnValidThread());
198 if (surface_id == SurfaceManager::kNoSurfaceID ||
199 !surface_owners_.count(surface_id)) {
200 return;
201 }
202
203 OwnerRecord& record = surface_owners_[surface_id];
204 if (record.owner == client)
205 record.owner = nullptr;
206 else if (record.waiter == client)
207 record.waiter = nullptr;
208
209 if (record.waiter && !record.owner &&
210 !codec_release_tasks_.count(surface_id)) {
211 record.owner = record.waiter;
212 record.waiter = nullptr;
213 record.owner->OnSurfaceAvailable(true);
214 return;
215 }
216
217 if (!record.owner && !record.waiter)
218 surface_owners_.erase(surface_id);
219 }
220
221 // During surface teardown we have to handle the following cases.
222 // 1) No AVDA has acquired the surface, or the surface has already been
223 // completely released.
224 // 2) No AVDA owns the surface, but a MediaCodec is currently being configured
liberato (no reviews please) 2016/11/17 07:28:19 is this the case where async construction is pendi
watk 2016/11/17 20:37:05 Yeah exactly. Whether the AVDA is alive or not the
225 // with it on another thread. In this case we assume it's safe for the the
226 // AVDA is expected to drop the MediaCodec when it receives it.
227 // 3) An AVDA owns the surface and it responds to OnSurfaceDestroyed() by:
228 // a) Replacing the destroyed surface by calling MediaCodec#setSurface() and
229 // releasing it immediately.
230 // b) Releasing the MediaCodec it's attached to.
231 // 4) No AVDA owns the surface, but the MediaCodec it's attached to is currently
232 // being destroyed on another thread.
233 void AVDACodecAllocator::OnSurfaceDestroyed(int surface_id) {
234 DVLOG(1) << __func__ << ": " << surface_id;
235 DCHECK(thread_checker_.CalledOnValidThread());
236
237 if (surface_owners_.count(surface_id)) {
238 OwnerRecord& record = surface_owners_[surface_id];
239 if (record.waiter) {
240 record.waiter->OnSurfaceAvailable(false);
241 record.waiter = nullptr;
242 }
243
244 if (record.owner)
245 record.owner->OnSurfaceDestroyed();
246
247 surface_owners_.erase(surface_id);
248 }
249
250 if (!codec_release_tasks_.count(surface_id))
251 return;
252
253 // The codec is being released so we have to wait for it here. It's a
254 // TimedWait() because the MediaCodec release may hang, and in that case we
255 // don't want to hang the browser UI thread. Android ANRs occur when the UI
256 // thread is blocked for 5 seconds, so waiting for 4 seconds gives us some
257 // leeway to avoid an ANR. Tested on a Nexus 7.
258 base::WaitableEvent& released =
259 codec_release_tasks_[surface_id].codec_released_event;
260 released.TimedWait(base::TimeDelta::FromSeconds(4));
261 if (!released.IsSignaled())
262 DLOG(WARNING) << __func__ << ": timed out waiting for MediaCodec#release()";
263 }
264
265 std::unique_ptr<VideoCodecBridge> AVDACodecAllocator::CreateMediaCodecSync(
266 scoped_refptr<CodecConfig> codec_config) {
267 TRACE_EVENT0("media", "AVDA::CreateMediaCodecSync");
268
269 jobject media_crypto = codec_config->media_crypto_
270 ? codec_config->media_crypto_->obj()
271 : nullptr;
272
273 // |needs_protected_surface_| implies encrypted stream.
274 DCHECK(!codec_config->needs_protected_surface_ || media_crypto);
275
276 const bool require_software_codec =
277 codec_config->task_type_ == TaskType::SW_CODEC;
278
279 std::unique_ptr<VideoCodecBridge> codec(VideoCodecBridge::CreateDecoder(
280 codec_config->codec_, codec_config->needs_protected_surface_,
281 codec_config->initial_expected_coded_size_,
282 codec_config->surface_.j_surface().obj(), media_crypto,
283 codec_config->csd0_, codec_config->csd1_, true, require_software_codec));
284
285 return codec;
286 }
287
288 void AVDACodecAllocator::CreateMediaCodecAsync(
289 base::WeakPtr<AVDACodecAllocatorClient> client,
290 scoped_refptr<CodecConfig> codec_config) {
291 base::PostTaskAndReplyWithResult(
292 TaskRunnerFor(codec_config->task_type_).get(), FROM_HERE,
293 base::Bind(&AVDACodecAllocator::CreateMediaCodecSync,
294 base::Unretained(this), codec_config),
295 base::Bind(&AVDACodecAllocatorClient::OnCodecConfigured, client));
296 }
297
298 void AVDACodecAllocator::ReleaseMediaCodec(
299 std::unique_ptr<VideoCodecBridge> media_codec,
300 TaskType task_type,
301 int surface_id) {
302 DCHECK(thread_checker_.CalledOnValidThread());
303 DCHECK(media_codec);
304
305 // No need to track the release if it's a SurfaceTexture.
306 if (surface_id == SurfaceManager::kNoSurfaceID) {
307 TaskRunnerFor(task_type)->PostTask(
308 FROM_HERE, base::Bind(&DeleteMediaCodecAndSignal,
309 base::Passed(std::move(media_codec)), nullptr));
310 return;
311 }
312
313 base::WaitableEvent* released =
314 &codec_release_tasks_[surface_id].codec_released_event;
315
316 // TODO(watk): Even if this is the current thread, things will work, but we
317 // should refactor this to not tolerate threads failing to start.
318 TaskRunnerFor(task_type)->PostTaskAndReply(
319 FROM_HERE, base::Bind(&DeleteMediaCodecAndSignal,
320 base::Passed(std::move(media_codec)), released),
321 base::Bind(&AVDACodecAllocator::OnMediaCodecAndSurfaceReleased,
322 base::Unretained(this), surface_id));
323 }
324
325 void AVDACodecAllocator::OnMediaCodecAndSurfaceReleased(int surface_id) {
326 DCHECK(thread_checker_.CalledOnValidThread());
327
328 codec_release_tasks_.erase(surface_id);
329 if (!surface_owners_.count(surface_id))
330 return;
331
332 OwnerRecord& record = surface_owners_[surface_id];
333 if (!record.owner && record.waiter) {
334 record.owner = record.waiter;
335 record.waiter = nullptr;
336 record.owner->OnSurfaceAvailable(true);
337 }
338 }
339
153 // Returns a hint about whether the construction thread has hung for 340 // Returns a hint about whether the construction thread has hung for
154 // |task_type|. Note that if a thread isn't started, then we'll just return 341 // |task_type|. Note that if a thread isn't started, then we'll just return
155 // "not hung", since it'll run on the current thread anyway. The hang 342 // "not hung", since it'll run on the current thread anyway. The hang
156 // detector will see no pending jobs in that case, so it's automatic. 343 // detector will see no pending jobs in that case, so it's automatic.
157 bool AVDACodecAllocator::IsThreadLikelyHung(TaskType task_type) { 344 bool AVDACodecAllocator::IsThreadLikelyHung(TaskType task_type) {
158 DCHECK(thread_checker_.CalledOnValidThread()); 345 DCHECK(thread_checker_.CalledOnValidThread());
159 return threads_[task_type]->hang_detector.IsThreadLikelyHung(); 346 return threads_[task_type]->hang_detector.IsThreadLikelyHung();
160 } 347 }
161 348
162 bool AVDACodecAllocator::IsAnyRegisteredAVDA() { 349 bool AVDACodecAllocator::IsAnyRegisteredAVDA() {
163 return !thread_avda_instances_.empty(); 350 return !clients_.empty();
164 } 351 }
165 352
166 AVDACodecAllocator::TaskType AVDACodecAllocator::TaskTypeForAllocation() { 353 TaskType AVDACodecAllocator::TaskTypeForAllocation() {
167 if (!IsThreadLikelyHung(TaskType::AUTO_CODEC)) 354 if (!IsThreadLikelyHung(TaskType::AUTO_CODEC))
168 return TaskType::AUTO_CODEC; 355 return TaskType::AUTO_CODEC;
169 356
170 if (!IsThreadLikelyHung(TaskType::SW_CODEC)) 357 if (!IsThreadLikelyHung(TaskType::SW_CODEC))
171 return TaskType::SW_CODEC; 358 return TaskType::SW_CODEC;
172 359
173 // If nothing is working, then we can't allocate anyway. 360 // If nothing is working, then we can't allocate anyway.
174 return TaskType::FAILED_CODEC; 361 return TaskType::FAILED_CODEC;
175 } 362 }
176 363
(...skipping 29 matching lines...) Expand all
206 } 393 }
207 394
208 void AVDACodecAllocator::StopThreadTask(size_t index, 395 void AVDACodecAllocator::StopThreadTask(size_t index,
209 base::WaitableEvent* event) { 396 base::WaitableEvent* event) {
210 threads_[index]->thread.Stop(); 397 threads_[index]->thread.Stop();
211 if (event) 398 if (event)
212 event->Signal(); 399 event->Signal();
213 } 400 }
214 401
215 } // namespace media 402 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698