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.
5 #include "media/filters/hls_data_source_provider.h"
9 HlsDataSourceProvider::~HlsDataSourceProvider() = default;
11 HlsDataSourceStream::HlsDataSourceStream(
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)) {
17 read_position_ = range->GetOffset();
18 max_read_position_ = range->GetEnd();
22 HlsDataSourceStream::~HlsDataSourceStream() {
23 CHECK(!stream_locked_);
24 std::move(on_destructed_cb_).Run();
27 std::string_view HlsDataSourceStream::AsString() const {
28 return std::string_view(reinterpret_cast<const char*>(buffer_.data()),
32 bool HlsDataSourceStream::CanReadMore() const {
33 if (reached_end_of_stream_) {
36 if (!max_read_position_.has_value()) {
39 return *max_read_position_ > read_position_;
42 void HlsDataSourceStream::Clear() {
43 CHECK(!stream_locked_);
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);
57 return buffer_.data() + write_index_;
60 void HlsDataSourceStream::UnlockStreamPostWrite(int read_size,
62 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
63 CHECK(stream_locked_);
64 write_index_ += read_size;
65 read_position_ += read_size;
67 reached_end_of_stream_ = true;
68 buffer_.resize(write_index_);
70 stream_locked_ = false;