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

Side by Side Diff: media/filters/blocking_url_protocol.cc

Issue 2710133003: Replace FFmpegDemuxer thread per element with base::TaskScheduler. (Closed)
Patch Set: Give WeakPtr to URLProtocol. Created 3 years, 9 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/filters/blocking_url_protocol.h" 5 #include "media/filters/blocking_url_protocol.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "media/base/data_source.h" 11 #include "media/base/data_source.h"
12 #include "media/ffmpeg/ffmpeg_common.h" 12 #include "media/ffmpeg/ffmpeg_common.h"
13 13
14 namespace media { 14 namespace media {
15 15
16 BlockingUrlProtocol::BlockingUrlProtocol(DataSource* data_source, 16 BlockingUrlProtocol::BlockingUrlProtocol(DataSource* data_source,
17 const base::Closure& error_cb) 17 const base::Closure& error_cb)
18 : data_source_(data_source), 18 : data_source_(data_source),
19 error_cb_(error_cb), 19 error_cb_(error_cb),
20 is_streaming_(data_source_->IsStreaming()),
20 aborted_(base::WaitableEvent::ResetPolicy::MANUAL, 21 aborted_(base::WaitableEvent::ResetPolicy::MANUAL,
21 base::WaitableEvent::InitialState::NOT_SIGNALED), // We never 22 base::WaitableEvent::InitialState::NOT_SIGNALED), // We never
22 // want to 23 // want to
23 // reset 24 // reset
24 // |aborted_|. 25 // |aborted_|.
25 read_complete_(base::WaitableEvent::ResetPolicy::AUTOMATIC, 26 read_complete_(base::WaitableEvent::ResetPolicy::AUTOMATIC,
26 base::WaitableEvent::InitialState::NOT_SIGNALED), 27 base::WaitableEvent::InitialState::NOT_SIGNALED),
27 last_read_bytes_(0), 28 last_read_bytes_(0),
28 read_position_(0) {} 29 read_position_(0) {}
29 30
30 BlockingUrlProtocol::~BlockingUrlProtocol() {} 31 BlockingUrlProtocol::~BlockingUrlProtocol() {}
31 32
32 void BlockingUrlProtocol::Abort() { 33 void BlockingUrlProtocol::Abort() {
33 aborted_.Signal(); 34 aborted_.Signal();
35 base::AutoLock lock(data_source_lock_);
36 data_source_ = nullptr;
34 } 37 }
35 38
36 int BlockingUrlProtocol::Read(int size, uint8_t* data) { 39 int BlockingUrlProtocol::Read(int size, uint8_t* data) {
37 // Read errors are unrecoverable. 40 {
38 if (aborted_.IsSignaled()) 41 // Read errors are unrecoverable.
39 return AVERROR(EIO); 42 base::AutoLock lock(data_source_lock_);
43 if (!data_source_) {
44 DCHECK(aborted_.IsSignaled());
45 return AVERROR(EIO);
46 }
40 47
41 // Even though FFmpeg defines AVERROR_EOF, it's not to be used with I/O 48 // Even though FFmpeg defines AVERROR_EOF, it's not to be used with I/O
42 // routines. Instead return 0 for any read at or past EOF. 49 // routines. Instead return 0 for any read at or past EOF.
43 int64_t file_size; 50 int64_t file_size;
44 if (data_source_->GetSize(&file_size) && read_position_ >= file_size) 51 if (data_source_->GetSize(&file_size) && read_position_ >= file_size)
45 return 0; 52 return 0;
46 53
47 // Blocking read from data source until either: 54 // Blocking read from data source until either:
48 // 1) |last_read_bytes_| is set and |read_complete_| is signalled 55 // 1) |last_read_bytes_| is set and |read_complete_| is signalled
49 // 2) |aborted_| is signalled 56 // 2) |aborted_| is signalled
50 data_source_->Read(read_position_, size, data, base::Bind( 57 data_source_->Read(read_position_, size, data,
51 &BlockingUrlProtocol::SignalReadCompleted, base::Unretained(this))); 58 base::Bind(&BlockingUrlProtocol::SignalReadCompleted,
59 base::Unretained(this)));
60 }
52 61
53 base::WaitableEvent* events[] = { &aborted_, &read_complete_ }; 62 base::WaitableEvent* events[] = { &aborted_, &read_complete_ };
54 size_t index = base::WaitableEvent::WaitMany(events, arraysize(events)); 63 size_t index = base::WaitableEvent::WaitMany(events, arraysize(events));
55 64
56 if (events[index] == &aborted_) 65 if (events[index] == &aborted_)
57 return AVERROR(EIO); 66 return AVERROR(EIO);
58 67
59 if (last_read_bytes_ == DataSource::kReadError) { 68 if (last_read_bytes_ == DataSource::kReadError) {
60 aborted_.Signal(); 69 aborted_.Signal();
61 error_cb_.Run(); 70 error_cb_.Run();
62 return AVERROR(EIO); 71 return AVERROR(EIO);
63 } 72 }
64 73
65 if (last_read_bytes_ == DataSource::kAborted) 74 if (last_read_bytes_ == DataSource::kAborted)
66 return AVERROR(EIO); 75 return AVERROR(EIO);
67 76
68 read_position_ += last_read_bytes_; 77 read_position_ += last_read_bytes_;
69 return last_read_bytes_; 78 return last_read_bytes_;
70 } 79 }
71 80
72 bool BlockingUrlProtocol::GetPosition(int64_t* position_out) { 81 bool BlockingUrlProtocol::GetPosition(int64_t* position_out) {
73 *position_out = read_position_; 82 *position_out = read_position_;
74 return true; 83 return true;
75 } 84 }
76 85
77 bool BlockingUrlProtocol::SetPosition(int64_t position) { 86 bool BlockingUrlProtocol::SetPosition(int64_t position) {
87 base::AutoLock lock(data_source_lock_);
78 int64_t file_size; 88 int64_t file_size;
79 if ((data_source_->GetSize(&file_size) && position > file_size) || 89 if (!data_source_ ||
90 (data_source_->GetSize(&file_size) && position > file_size) ||
80 position < 0) { 91 position < 0) {
81 return false; 92 return false;
82 } 93 }
83 94
84 read_position_ = position; 95 read_position_ = position;
85 return true; 96 return true;
86 } 97 }
87 98
88 bool BlockingUrlProtocol::GetSize(int64_t* size_out) { 99 bool BlockingUrlProtocol::GetSize(int64_t* size_out) {
89 return data_source_->GetSize(size_out); 100 base::AutoLock lock(data_source_lock_);
101 return data_source_ ? data_source_->GetSize(size_out) : 0;
90 } 102 }
91 103
92 bool BlockingUrlProtocol::IsStreaming() { 104 bool BlockingUrlProtocol::IsStreaming() {
93 return data_source_->IsStreaming(); 105 return is_streaming_;
94 } 106 }
95 107
96 void BlockingUrlProtocol::SignalReadCompleted(int size) { 108 void BlockingUrlProtocol::SignalReadCompleted(int size) {
97 last_read_bytes_ = size; 109 last_read_bytes_ = size;
98 read_complete_.Signal(); 110 read_complete_.Signal();
99 } 111 }
100 112
101 } // namespace media 113 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698