- add sources.
[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 bool FileDataSource::Initialize(const base::FilePath& file_path) {
19   DCHECK(!file_.IsValid());
20
21   if (!file_.Initialize(file_path))
22     return false;
23
24   UpdateHostBytes();
25   return true;
26 }
27
28 bool FileDataSource::InitializeFromPlatformFile(
29     const base::PlatformFile& file) {
30   DCHECK(!file_.IsValid());
31
32   if (!file_.Initialize(file))
33     return false;
34
35   UpdateHostBytes();
36   return true;
37 }
38
39 void FileDataSource::set_host(DataSourceHost* host) {
40   DataSource::set_host(host);
41   UpdateHostBytes();
42 }
43
44 void FileDataSource::Stop(const base::Closure& callback) {
45   callback.Run();
46 }
47
48 void FileDataSource::Read(int64 position, int size, uint8* data,
49                           const DataSource::ReadCB& read_cb) {
50   if (force_read_errors_ || !file_.IsValid()) {
51     read_cb.Run(kReadError);
52     return;
53   }
54
55   int64 file_size = file_.length();
56
57   CHECK_GE(file_size, 0);
58   CHECK_GE(position, 0);
59   CHECK_GE(size, 0);
60
61   // Cap position and size within bounds.
62   position = std::min(position, file_size);
63   int64 clamped_size = std::min(static_cast<int64>(size), file_size - position);
64
65   memcpy(data, file_.data() + position, clamped_size);
66   read_cb.Run(clamped_size);
67 }
68
69 bool FileDataSource::GetSize(int64* size_out) {
70   *size_out = file_.length();
71   return true;
72 }
73
74 bool FileDataSource::IsStreaming() {
75   return force_streaming_;
76 }
77
78 void FileDataSource::SetBitrate(int bitrate) {}
79
80 FileDataSource::~FileDataSource() {}
81
82 void FileDataSource::UpdateHostBytes() {
83   if (host() && file_.IsValid()) {
84     host()->SetTotalBytes(file_.length());
85     host()->AddBufferedByteRange(0, file_.length());
86   }
87 }
88
89 }  // namespace media