Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / media / filters / file_data_source.cc
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(const base::Closure& callback) {
30   callback.Run();
31 }
32
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;
38   }
39
40   int64 file_size = file_.length();
41
42   CHECK_GE(file_size, 0);
43   CHECK_GE(position, 0);
44   CHECK_GE(size, 0);
45
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);
49
50   memcpy(data, file_.data() + position, clamped_size);
51   read_cb.Run(clamped_size);
52 }
53
54 bool FileDataSource::GetSize(int64* size_out) {
55   *size_out = file_.length();
56   return true;
57 }
58
59 bool FileDataSource::IsStreaming() {
60   return force_streaming_;
61 }
62
63 void FileDataSource::SetBitrate(int bitrate) {}
64
65 FileDataSource::~FileDataSource() {}
66
67 }  // namespace media