1 // Copyright 2012 The Chromium Authors
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"
10 #include "base/check_op.h"
11 #include "base/functional/callback.h"
15 FileDataSource::FileDataSource()
16 : force_read_errors_(false),
17 force_streaming_(false),
21 bool FileDataSource::Initialize(const base::FilePath& file_path) {
22 DCHECK(!file_.IsValid());
23 return file_.Initialize(file_path);
26 bool FileDataSource::Initialize(base::File file) {
27 DCHECK(!file_.IsValid());
28 return file_.Initialize(std::move(file));
31 void FileDataSource::Stop() {}
33 void FileDataSource::Abort() {}
35 void FileDataSource::Read(int64_t position,
38 DataSource::ReadCB read_cb) {
39 if (force_read_errors_ || !file_.IsValid()) {
40 std::move(read_cb).Run(kReadError);
44 int64_t file_size = file_.length();
46 CHECK_GE(file_size, 0);
47 CHECK_GE(position, 0);
50 // Cap position and size within bounds.
51 position = std::min(position, file_size);
52 int64_t clamped_size =
53 std::min(static_cast<int64_t>(size), file_size - position);
55 memcpy(data, file_.data() + position, clamped_size);
56 bytes_read_ += clamped_size;
57 std::move(read_cb).Run(clamped_size);
60 bool FileDataSource::GetSize(int64_t* size_out) {
61 *size_out = file_.length();
65 bool FileDataSource::IsStreaming() {
66 return force_streaming_;
69 void FileDataSource::SetBitrate(int bitrate) {}
71 FileDataSource::~FileDataSource() = default;
73 bool FileDataSource::PassedTimingAllowOriginCheck() {
74 // There are no HTTP responses, so this can safely return true.
78 bool FileDataSource::WouldTaintOrigin() {
79 // There are no HTTP responses, so this can safely return false.