7 #include "packager/media/file/threaded_io_file.h"
9 #include "packager/base/bind.h"
10 #include "packager/base/bind_helpers.h"
11 #include "packager/base/threading/platform_thread.h"
12 #include "packager/media/base/closure_thread.h"
14 namespace edash_packager {
17 ThreadedIoFile::ThreadedIoFile(scoped_ptr<File, FileCloser> internal_file,
19 uint64_t io_cache_size,
20 uint64_t io_block_size)
21 : File(internal_file->file_name()),
22 internal_file_(internal_file.Pass()),
24 cache_(io_cache_size),
25 io_buffer_(io_block_size),
28 internal_file_error_(0) {
29 DCHECK(internal_file_);
32 ThreadedIoFile::~ThreadedIoFile() {}
35 DCHECK(internal_file_);
37 if (!internal_file_->Open())
40 size_ = internal_file_->Size();
43 base::Bind(mode_ == kInputMode ?
44 &ThreadedIoFile::RunInInputMode :
45 &ThreadedIoFile::RunInOutputMode,
46 base::Unretained(
this))));
52 DCHECK(internal_file_);
55 if (mode_ == kOutputMode)
61 bool result = internal_file_.release()->Close();
67 DCHECK(internal_file_);
69 DCHECK_EQ(kInputMode, mode_);
71 if (internal_file_error_)
72 return internal_file_error_;
77 return cache_.
Read(buffer, length);
81 DCHECK(internal_file_);
83 DCHECK_EQ(kOutputMode, mode_);
85 if (internal_file_error_)
86 return internal_file_error_;
89 return cache_.
Write(buffer, length);
93 DCHECK(internal_file_);
100 DCHECK(internal_file_);
102 DCHECK_EQ(kOutputMode, mode_);
105 return internal_file_->Flush();
108 void ThreadedIoFile::RunInInputMode() {
109 DCHECK(internal_file_);
111 DCHECK_EQ(kInputMode, mode_);
114 int64_t read_result = internal_file_->Read(&io_buffer_[0],
116 if (read_result <= 0) {
117 eof_ = read_result == 0;
118 internal_file_error_ = read_result;
122 cache_.
Write(&io_buffer_[0], read_result);
136 void ThreadedIoFile::RunInOutputMode() {
137 DCHECK(internal_file_);
139 DCHECK_EQ(kOutputMode, mode_);
142 uint64_t write_bytes = cache_.
Read(&io_buffer_[0], io_buffer_.size());
143 if (write_bytes == 0)
146 int64_t write_result = internal_file_->Write(&io_buffer_[0], write_bytes);
147 if (write_result < 0) {
148 internal_file_error_ = write_result;
152 CHECK_EQ(write_result, static_cast<int64_t>(write_bytes));