LiveKit C++ SDK
Real-time audio/video SDK for C++
Loading...
Searching...
No Matches
audio_stream.h
1/*
2 * Copyright 2025 LiveKit
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an “AS IS” BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <condition_variable>
20#include <cstdint>
21#include <deque>
22#include <memory>
23#include <mutex>
24#include <optional>
25#include <string>
26
27#include "audio_frame.h"
28#include "ffi_handle.h"
29#include "participant.h"
30#include "track.h"
31
32namespace livekit {
33
34namespace proto {
35class FfiEvent;
36}
37
47
65public:
67 struct Options {
74 std::size_t capacity{0};
75
79
83 };
84
86 static std::shared_ptr<AudioStream>
87 fromTrack(const std::shared_ptr<Track> &track, const Options &options);
88
90 static std::shared_ptr<AudioStream> fromParticipant(Participant &participant,
91 TrackSource track_source,
92 const Options &options);
93
94 virtual ~AudioStream();
95
97 AudioStream(const AudioStream &) = delete;
98 AudioStream &operator=(const AudioStream &) = delete;
99 AudioStream(AudioStream &&) noexcept;
100 AudioStream &operator=(AudioStream &&) noexcept;
101
108 bool read(AudioFrameEvent &out_event);
109
115 void close();
116
117private:
118 AudioStream() = default;
119
120 void initFromTrack(const std::shared_ptr<Track> &track,
121 const Options &options);
122 void initFromParticipant(Participant &participant, TrackSource track_source,
123 const Options &options);
124
125 // FFI event handler (registered with FfiClient)
126 void onFfiEvent(const proto::FfiEvent &event);
127
128 // Queue helpers
129 void pushFrame(AudioFrameEvent &&ev);
130 void pushEos();
131
132 mutable std::mutex mutex_;
133 std::condition_variable cv_;
134 std::deque<AudioFrameEvent> queue_;
135 std::size_t capacity_{0};
136 bool eof_{false};
137 bool closed_{false};
138
139 Options options_;
140
141 // Underlying FFI audio stream handle
142 FfiHandle stream_handle_;
143
144 // Listener id registered on FfiClient
145 std::int64_t listener_id_{0};
146};
147
148} // namespace livekit
Represents a raw PCM audio frame with interleaved int16 samples.
Definition audio_frame.h:37
Definition audio_stream.h:64
static std::shared_ptr< AudioStream > fromTrack(const std::shared_ptr< Track > &track, const Options &options)
Factory: create an AudioStream bound to a specific Track.
AudioStream(const AudioStream &)=delete
No copy, assignment constructors.
static std::shared_ptr< AudioStream > fromParticipant(Participant &participant, TrackSource track_source, const Options &options)
Factory: create an AudioStream from a Participant + TrackSource.
bool read(AudioFrameEvent &out_event)
Definition participant.h:31
Definition track.h:71
Event containing an audio frame received from an AudioStream.
Definition audio_stream.h:44
AudioFrame frame
The decoded PCM audio frame.
Definition audio_stream.h:45
Configuration options for AudioStream creation.
Definition audio_stream.h:67
std::size_t capacity
Definition audio_stream.h:74
std::string noise_cancellation_options_json
Definition audio_stream.h:82
std::string noise_cancellation_module
Definition audio_stream.h:78