Migrate away from the PrefMetricsService-based device ID in PrefHashCalculator.
[chromium-blink-merge.git] / media / filters / file_data_source.cc
blobe8b3292898f5d9d6c62feb260ed29695f44ed759
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"
7 #include <algorithm>
9 #include "base/logging.h"
11 namespace media {
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 file_.Initialize(file.Pass());
24 bool FileDataSource::Initialize(const base::FilePath& file_path) {
25 DCHECK(!file_.IsValid());
26 return file_.Initialize(file_path);
29 void FileDataSource::Stop(const base::Closure& callback) {
30 callback.Run();
33 void FileDataSource::Read(int64 position, int size, uint8* data,
34 const DataSource::ReadCB& read_cb) {
35 if (force_read_errors_ || !file_.IsValid()) {
36 read_cb.Run(kReadError);
37 return;
40 int64 file_size = file_.length();
42 CHECK_GE(file_size, 0);
43 CHECK_GE(position, 0);
44 CHECK_GE(size, 0);
46 // Cap position and size within bounds.
47 position = std::min(position, file_size);
48 int64 clamped_size = std::min(static_cast<int64>(size), file_size - position);
50 memcpy(data, file_.data() + position, clamped_size);
51 read_cb.Run(clamped_size);
54 bool FileDataSource::GetSize(int64* size_out) {
55 *size_out = file_.length();
56 return true;
59 bool FileDataSource::IsStreaming() {
60 return force_streaming_;
63 void FileDataSource::SetBitrate(int bitrate) {}
65 FileDataSource::~FileDataSource() {}
67 } // namespace media