-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_data_source.cc
126 lines (109 loc) · 3.74 KB
/
resource_data_source.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "chromium_media_lib/resource_data_source.h"
#include "base/callback_helpers.h"
#include "net/base/net_errors.h"
namespace media {
const int kBlockSizeShift = 15; // 1<<15 == 32kb
ResourceDataSource::ResourceDataSource(
const GURL& url,
const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner)
: render_task_runner_(task_runner),
io_task_runner_(io_task_runner),
url_(url),
stop_signal_received_(false),
total_bytes_(0),
multibuffer_(this, url, kBlockSizeShift, io_task_runner_),
weak_factory_(this) {
weak_ptr_ = weak_factory_.GetWeakPtr();
}
ResourceDataSource::~ResourceDataSource() {}
void ResourceDataSource::Initialize(const InitializeCB& init_cb) {
DCHECK(render_task_runner_->BelongsToCurrentThread());
DCHECK(!init_cb.is_null());
init_cb_ = init_cb;
multibuffer_.Start();
}
void ResourceDataSource::Stop() {
base::AutoLock auto_lock(lock_);
stop_signal_received_ = true;
}
void ResourceDataSource::Abort() {
base::AutoLock auto_lock(lock_);
stop_signal_received_ = true;
}
void ResourceDataSource::Read(int64_t position,
int size,
uint8_t* data,
const DataSource::ReadCB& read_cb) {
DCHECK(!read_cb.is_null());
DCHECK(!read_op_);
{
base::AutoLock auto_lock(lock_);
if (stop_signal_received_) {
read_cb.Run(kReadError);
return;
}
}
read_op_.reset(new ReadOperation(position, size, data, read_cb));
LOG(INFO) << "ResourceDataSource::Read position=" << read_op_->position()
<< " size=" << read_op_->size();
render_task_runner_->PostTask(
FROM_HERE,
base::Bind(&ResourceDataSource::ReadTask, weak_factory_.GetWeakPtr()));
}
bool ResourceDataSource::GetSize(int64_t* size_out) {
base::AutoLock auto_lock(lock_);
total_bytes_ = multibuffer_.GetSize();
if (total_bytes_ != -1) {
*size_out = total_bytes_;
return true;
}
*size_out = 0;
return false;
}
bool ResourceDataSource::IsStreaming() {
return false;
}
void ResourceDataSource::SetBitrate(int bitrate) {
// Do nothing
}
void ResourceDataSource::ReadTask() {
DCHECK(render_task_runner_->BelongsToCurrentThread());
base::AutoLock auto_lock(lock_);
if (stop_signal_received_ || !read_op_)
return;
DCHECK(read_op_->size());
multibuffer_.Seek(read_op_->position());
int bytes_read = multibuffer_.Fill(read_op_->position(), read_op_->size(),
read_op_->data());
LOG(INFO) << "ResourceDataSource::ReadTask read_op=" << read_op_.get()
<< " position=" << read_op_->position()
<< " id=" << multibuffer_.ToBlockId(read_op_->position())
<< " size=" << read_op_->size() << " bytes_read=" << bytes_read;
if (bytes_read > 0) {
ReadOperation::Run(std::move(read_op_), bytes_read);
} else if (bytes_read == net::ERR_IO_PENDING) {
// wait until OnUpdateState
render_task_runner_->PostDelayedTask(
FROM_HERE,
base::Bind(&ResourceDataSource::ReadTask, weak_factory_.GetWeakPtr()),
base::TimeDelta::FromMilliseconds(1000));
} else {
ReadOperation::Run(std::move(read_op_), kReadError);
}
}
void ResourceDataSource::DidInitialize() {
if (init_cb_.is_null())
return;
DCHECK(io_task_runner_->BelongsToCurrentThread());
base::AutoLock auto_lock(lock_);
render_task_runner_->PostTask(
FROM_HERE, base::Bind(base::ResetAndReturn(&init_cb_), true));
}
void ResourceDataSource::OnUpdateState() {
DCHECK(io_task_runner_->BelongsToCurrentThread());
render_task_runner_->PostTask(
FROM_HERE,
base::Bind(&ResourceDataSource::ReadTask, weak_factory_.GetWeakPtr()));
}
} // namespace media