summaryrefslogtreecommitdiff
path: root/src/audio/stream_info.cpp
blob: 3927e5f8e96db4dd66d04d9bd853deb4a668a9f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * Copyright 2023 jacqueline <me@jacqueline.id.au>
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#include "stream_info.hpp"

#include <cstdint>
#include <optional>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#include <variant>

#include "result.hpp"
#include "span.hpp"
#include "types.hpp"

namespace audio {

void InputStream::consume(std::size_t bytes) const {
  assert(raw_->info->bytes_in_stream >= bytes);
  auto new_data =
      raw_->data.subspan(bytes, raw_->info->bytes_in_stream - bytes);
  std::move(new_data.begin(), new_data.end(), raw_->data.begin());
  raw_->info->bytes_in_stream = new_data.size_bytes();
}

void InputStream::mark_consumer_finished() const {
  raw_->info->is_consumer_finished = true;
  if (is_producer_finished()) {
    raw_->info->format = std::monostate();
  }
}

bool InputStream::is_producer_finished() const {
  return raw_->info->is_producer_finished;
}

const StreamInfo& InputStream::info() const {
  return *raw_->info;
}

cpp::span<const std::byte> InputStream::data() const {
  return raw_->data.first(raw_->info->bytes_in_stream);
}

void OutputStream::add(std::size_t bytes) const {
  assert(raw_->info->bytes_in_stream + bytes <= raw_->data.size_bytes());
  raw_->info->bytes_in_stream += bytes;
}

bool OutputStream::prepare(const StreamInfo::Format& new_format) {
  if (std::holds_alternative<std::monostate>(raw_->info->format) ||
      raw_->info->is_consumer_finished) {
    raw_->info->format = new_format;
    raw_->info->bytes_in_stream = 0;
    raw_->info->is_producer_finished = false;
    raw_->info->is_consumer_finished = false;
    return true;
  }
  return false;
}

void OutputStream::set_duration(std::size_t seconds) {
  raw_->info->duration_seconds = seconds;
}

const StreamInfo& OutputStream::info() const {
  return *raw_->info;
}

cpp::span<std::byte> OutputStream::data() const {
  return raw_->data.subspan(raw_->info->bytes_in_stream);
}

void OutputStream::mark_producer_finished() const {
  raw_->info->is_producer_finished = true;
  if (is_consumer_finished()) {
    raw_->info->format = std::monostate();
  }
}

bool OutputStream::is_consumer_finished() const {
  return raw_->info->is_consumer_finished;
}

}  // namespace audio