[M120 Migration][hbbtv] Audio tracks count notification
[platform/framework/web/chromium-efl.git] / media / filters / hls_data_source_provider.cc
1 // Copyright 2023 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/hls_data_source_provider.h"
6
7 namespace media {
8
9 HlsDataSourceProvider::~HlsDataSourceProvider() = default;
10
11 HlsDataSourceStream::HlsDataSourceStream(
12     StreamId stream_id,
13     base::OnceClosure on_destructed_cb,
14     absl::optional<hls::types::ByteRange> range)
15     : stream_id_(stream_id), on_destructed_cb_(std::move(on_destructed_cb)) {
16   if (range) {
17     read_position_ = range->GetOffset();
18     max_read_position_ = range->GetEnd();
19   }
20 }
21
22 HlsDataSourceStream::~HlsDataSourceStream() {
23   CHECK(!stream_locked_);
24   std::move(on_destructed_cb_).Run();
25 }
26
27 std::string_view HlsDataSourceStream::AsString() const {
28   return std::string_view(reinterpret_cast<const char*>(buffer_.data()),
29                           buffer_.size());
30 }
31
32 bool HlsDataSourceStream::CanReadMore() const {
33   if (reached_end_of_stream_) {
34     return false;
35   }
36   if (!max_read_position_.has_value()) {
37     return true;
38   }
39   return *max_read_position_ > read_position_;
40 }
41
42 void HlsDataSourceStream::Clear() {
43   CHECK(!stream_locked_);
44   buffer_.resize(0);
45   write_index_ = 0;
46 }
47
48 uint8_t* HlsDataSourceStream::LockStreamForWriting(int ensure_minimum_space) {
49   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
50   CHECK(!stream_locked_);
51   stream_locked_ = true;
52   CHECK_GE(buffer_.size(), write_index_);
53   int remaining_bytes = buffer_.size() - write_index_;
54   if (ensure_minimum_space > remaining_bytes) {
55     buffer_.resize(write_index_ + ensure_minimum_space);
56   }
57   return buffer_.data() + write_index_;
58 }
59
60 void HlsDataSourceStream::UnlockStreamPostWrite(int read_size,
61                                                 bool end_of_stream) {
62   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
63   CHECK(stream_locked_);
64   write_index_ += read_size;
65   read_position_ += read_size;
66   if (end_of_stream) {
67     reached_end_of_stream_ = true;
68     buffer_.resize(write_index_);
69   }
70   stream_locked_ = false;
71 }
72
73 }  // namespace media