-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_data_source.cc
110 lines (94 loc) · 2.84 KB
/
file_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
// Copyright (c) 2017 YuTeh Shen
//
#include "chromium_media_lib/file_data_source.h"
#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/location.h"
#include "base/macros.h"
namespace media {
FileDataSource::FileDataSource(
const base::FilePath& path,
const scoped_refptr<base::SingleThreadTaskRunner>& task_runner)
: render_task_runner_(task_runner),
path_(path),
total_bytes_(-1),
stop_signal_received_(false),
weak_factory_(this) {
weak_ptr_ = weak_factory_.GetWeakPtr();
}
FileDataSource::~FileDataSource() {}
void FileDataSource::Initialize(const InitializeCB& init_cb) {
DCHECK(render_task_runner_->BelongsToCurrentThread());
DCHECK(!init_cb.is_null());
bool success = mapped_file_.Initialize(
base::File(path_, base::File::FLAG_OPEN | base::File::FLAG_READ));
{
base::AutoLock auto_lock(lock_);
if (success)
total_bytes_ = mapped_file_.length();
}
init_cb_ = init_cb;
render_task_runner_->PostTask(
FROM_HERE, base::Bind(base::ResetAndReturn(&init_cb_), success));
}
void FileDataSource::Stop() {
base::AutoLock auto_lock(lock_);
stop_signal_received_ = true;
}
void FileDataSource::Abort() {
base::AutoLock auto_lock(lock_);
stop_signal_received_ = true;
}
void FileDataSource::Read(int64_t position,
int size,
uint8_t* data,
const DataSource::ReadCB& read_cb) {
DCHECK(!read_cb.is_null());
{
base::AutoLock auto_lock(lock_);
DCHECK(!read_op_);
if (stop_signal_received_) {
read_cb.Run(kReadError);
return;
}
read_op_.reset(new ReadOperation(position, size, data, read_cb));
}
render_task_runner_->PostTask(
FROM_HERE,
base::Bind(&FileDataSource::ReadTask, weak_factory_.GetWeakPtr()));
}
bool FileDataSource::GetSize(int64_t* size_out) {
base::AutoLock auto_lock(lock_);
if (total_bytes_ != -1) {
*size_out = total_bytes_;
return true;
}
*size_out = 0;
return false;
}
bool FileDataSource::IsStreaming() {
return false;
}
void FileDataSource::SetBitrate(int bitrate) {
// Do nothing
}
void FileDataSource::ReadTask() {
DCHECK(render_task_runner_->BelongsToCurrentThread());
base::AutoLock auto_lock(lock_);
if (stop_signal_received_ || !read_op_)
return;
DCHECK(read_op_->size());
if (mapped_file_.IsValid()) {
const uint8_t* file_data = mapped_file_.data();
int64_t available = total_bytes_ - read_op_->position();
if (available > 0) {
int bytes_read = static_cast<int>(
std::min<int64_t>(available, static_cast<int64_t>(read_op_->size())));
memcpy(read_op_->data(), file_data + read_op_->position(), bytes_read);
ReadOperation::Run(std::move(read_op_), bytes_read);
return;
}
}
ReadOperation::Run(std::move(read_op_), kReadError);
}
} // namespace media