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
3 // found in the LICENSE file.
5 #include "media/filters/file_data_source.h"
9 #include "base/logging.h"
13 FileDataSource::FileDataSource()
14 : force_read_errors_(false),
15 force_streaming_(false) {
18 FileDataSource::FileDataSource(base::File file
)
19 : force_read_errors_(false),
20 force_streaming_(false) {
21 if (!file_
.Initialize(file
.Pass()))
27 bool FileDataSource::Initialize(const base::FilePath
& file_path
) {
28 DCHECK(!file_
.IsValid());
30 if (!file_
.Initialize(file_path
))
37 void FileDataSource::set_host(DataSourceHost
* host
) {
38 DataSource::set_host(host
);
42 void FileDataSource::Stop(const base::Closure
& callback
) {
46 void FileDataSource::Read(int64 position
, int size
, uint8
* data
,
47 const DataSource::ReadCB
& read_cb
) {
48 if (force_read_errors_
|| !file_
.IsValid()) {
49 read_cb
.Run(kReadError
);
53 int64 file_size
= file_
.length();
55 CHECK_GE(file_size
, 0);
56 CHECK_GE(position
, 0);
59 // Cap position and size within bounds.
60 position
= std::min(position
, file_size
);
61 int64 clamped_size
= std::min(static_cast<int64
>(size
), file_size
- position
);
63 memcpy(data
, file_
.data() + position
, clamped_size
);
64 read_cb
.Run(clamped_size
);
67 bool FileDataSource::GetSize(int64
* size_out
) {
68 *size_out
= file_
.length();
72 bool FileDataSource::IsStreaming() {
73 return force_streaming_
;
76 void FileDataSource::SetBitrate(int bitrate
) {}
78 FileDataSource::~FileDataSource() {}
80 void FileDataSource::UpdateHostBytes() {
81 if (host() && file_
.IsValid()) {
82 host()->SetTotalBytes(file_
.length());
83 host()->AddBufferedByteRange(0, file_
.length());