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. 4 5 #include "media/filters/file_data_source.h" 6 7 #include <algorithm> 8 9 #include "base/logging.h" 10 11 namespace media { 12 13 FileDataSource::FileDataSource() 14 : force_read_errors_(false), 15 force_streaming_(false) { 16 } 17 18 FileDataSource::FileDataSource(base::File file) 19 : force_read_errors_(false), 20 force_streaming_(false) { 21 file_.Initialize(file.Pass()); 22 } 23 24 bool FileDataSource::Initialize(const base::FilePath& file_path) { 25 DCHECK(!file_.IsValid()); 26 return file_.Initialize(file_path); 27 } 28 29 void FileDataSource::Stop() { 30 } 31 32 void FileDataSource::Read(int64 position, int size, uint8* data, 33 const DataSource::ReadCB& read_cb) { 34 if (force_read_errors_ || !file_.IsValid()) { 35 read_cb.Run(kReadError); 36 return; 37 } 38 39 int64 file_size = file_.length(); 40 41 CHECK_GE(file_size, 0); 42 CHECK_GE(position, 0); 43 CHECK_GE(size, 0); 44 45 // Cap position and size within bounds. 46 position = std::min(position, file_size); 47 int64 clamped_size = std::min(static_cast<int64>(size), file_size - position); 48 49 memcpy(data, file_.data() + position, clamped_size); 50 read_cb.Run(clamped_size); 51 } 52 53 bool FileDataSource::GetSize(int64* size_out) { 54 *size_out = file_.length(); 55 return true; 56 } 57 58 bool FileDataSource::IsStreaming() { 59 return force_streaming_; 60 } 61 62 void FileDataSource::SetBitrate(int bitrate) {} 63 64 FileDataSource::~FileDataSource() {} 65 66 } // namespace media 67