[M120 Migration][hbbtv] Audio tracks count notification
[platform/framework/web/chromium-efl.git] / media / filters / file_data_source.cc
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.
4
5 #include "media/filters/file_data_source.h"
6
7 #include <algorithm>
8 #include <utility>
9
10 #include "base/check_op.h"
11 #include "base/functional/callback.h"
12
13 namespace media {
14
15 FileDataSource::FileDataSource()
16     : force_read_errors_(false),
17       force_streaming_(false),
18       bytes_read_(0) {
19 }
20
21 bool FileDataSource::Initialize(const base::FilePath& file_path) {
22   DCHECK(!file_.IsValid());
23   return file_.Initialize(file_path);
24 }
25
26 bool FileDataSource::Initialize(base::File file) {
27   DCHECK(!file_.IsValid());
28   return file_.Initialize(std::move(file));
29 }
30
31 void FileDataSource::Stop() {}
32
33 void FileDataSource::Abort() {}
34
35 void FileDataSource::Read(int64_t position,
36                           int size,
37                           uint8_t* data,
38                           DataSource::ReadCB read_cb) {
39   if (force_read_errors_ || !file_.IsValid()) {
40     std::move(read_cb).Run(kReadError);
41     return;
42   }
43
44   int64_t file_size = file_.length();
45
46   CHECK_GE(file_size, 0);
47   CHECK_GE(position, 0);
48   CHECK_GE(size, 0);
49
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);
54
55   memcpy(data, file_.data() + position, clamped_size);
56   bytes_read_ += clamped_size;
57   std::move(read_cb).Run(clamped_size);
58 }
59
60 bool FileDataSource::GetSize(int64_t* size_out) {
61   *size_out = file_.length();
62   return true;
63 }
64
65 bool FileDataSource::IsStreaming() {
66   return force_streaming_;
67 }
68
69 void FileDataSource::SetBitrate(int bitrate) {}
70
71 FileDataSource::~FileDataSource() = default;
72
73 bool FileDataSource::PassedTimingAllowOriginCheck() {
74   // There are no HTTP responses, so this can safely return true.
75   return true;
76 }
77
78 bool FileDataSource::WouldTaintOrigin() {
79   // There are no HTTP responses, so this can safely return false.
80   return false;
81 }
82
83 }  // namespace media