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),
29 flush_complete_event_(false, false),
30 internal_file_error_(0){
31 DCHECK(internal_file_);
34 ThreadedIoFile::~ThreadedIoFile() {}
37 DCHECK(internal_file_);
39 if (!internal_file_->Open())
42 size_ = internal_file_->Size();
45 base::Bind(mode_ == kInputMode ?
46 &ThreadedIoFile::RunInInputMode :
47 &ThreadedIoFile::RunInOutputMode,
48 base::Unretained(
this))));
54 DCHECK(internal_file_);
57 if (mode_ == kOutputMode)
63 bool result = internal_file_.release()->Close();
69 DCHECK(internal_file_);
71 DCHECK_EQ(kInputMode, mode_);
73 if (internal_file_error_)
74 return internal_file_error_;
79 return cache_.
Read(buffer, length);
83 DCHECK(internal_file_);
85 DCHECK_EQ(kOutputMode, mode_);
87 if (internal_file_error_)
88 return internal_file_error_;
91 return cache_.
Write(buffer, length);
95 DCHECK(internal_file_);
102 DCHECK(internal_file_);
104 DCHECK_EQ(kOutputMode, mode_);
108 flush_complete_event_.Wait();
109 return internal_file_->Flush();
112 void ThreadedIoFile::RunInInputMode() {
113 DCHECK(internal_file_);
115 DCHECK_EQ(kInputMode, mode_);
118 int64_t read_result = internal_file_->Read(&io_buffer_[0],
120 if (read_result <= 0) {
121 eof_ = read_result == 0;
122 internal_file_error_ = read_result;
126 cache_.
Write(&io_buffer_[0], read_result);
140 void ThreadedIoFile::RunInOutputMode() {
141 DCHECK(internal_file_);
143 DCHECK_EQ(kOutputMode, mode_);
146 uint64_t write_bytes = cache_.
Read(&io_buffer_[0], io_buffer_.size());
147 if (write_bytes == 0) {
151 flush_complete_event_.Signal();
156 int64_t write_result = internal_file_->Write(&io_buffer_[0], write_bytes);
157 if (write_result < 0) {
158 internal_file_error_ = write_result;
162 CHECK_EQ(write_result, static_cast<int64_t>(write_bytes));