LiveKit C++ Client SDK v1.1.0
Real-time audio/video/data 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 "livekit/audio_frame.h"
28#include "livekit/ffi_handle.h"
29#include "livekit/participant.h"
30#include "livekit/track.h"
31#include "livekit/visibility.h"
32
33namespace livekit {
34
35namespace proto {
36class FfiEvent;
37}
38
39// NOLINTBEGIN(bugprone-exception-escape)
40// AudioFrame can throw in various places monitored by bugprone-exception-escape
41// Suppressing for now, would require significant refactor to fix
42
47// NOLINTEND(bugprone-exception-escape)
48
63class LIVEKIT_API AudioStream {
64public:
66 struct Options {
73 std::size_t capacity{0};
74
78
82 };
83
85 static std::shared_ptr<AudioStream> fromTrack(const std::shared_ptr<Track>& track, const Options& options);
86
88 static std::shared_ptr<AudioStream> fromParticipant(Participant& participant, TrackSource track_source,
89 const Options& options);
90
91 virtual ~AudioStream();
92
94 AudioStream(const AudioStream&) = delete;
95 AudioStream& operator=(const AudioStream&) = delete;
96 AudioStream(AudioStream&&) noexcept;
97 AudioStream& operator=(AudioStream&&) noexcept;
98
105 bool read(AudioFrameEvent& out_event);
106
112 void close();
113
114private:
115 AudioStream() = default;
116
117 void initFromTrack(const std::shared_ptr<Track>& track, const Options& options);
118 void initFromParticipant(Participant& participant, TrackSource track_source, const Options& options);
119
120 // FFI event handler (registered with FfiClient)
121 void onFfiEvent(const proto::FfiEvent& event);
122
123 // Queue helpers
124 void pushFrame(AudioFrameEvent&& ev);
125 void pushEos();
126
127 mutable std::mutex mutex_;
128 std::condition_variable cv_;
129 std::deque<AudioFrameEvent> queue_;
130 std::size_t capacity_{0};
131 bool eof_{false};
132 bool closed_{false};
133
134 Options options_;
135
136 // Underlying FFI audio stream handle
137 FfiHandle stream_handle_;
138
139 // Listener id registered on FfiClient
140 std::int32_t listener_id_{0};
141};
142
143} // namespace livekit
Represents a raw PCM audio frame with interleaved int16 samples.
Definition audio_frame.h:37
Represents a pull-based stream of decoded PCM audio frames coming from a remote (or local) LiveKit tr...
Definition audio_stream.h:63
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.
Base class for local and remote room participants.
Definition participant.h:34
Base class for local and remote media tracks.
Definition track.h:77
Public API for the LiveKit C++ Client SDK.
Definition audio_frame.h:25
TrackSource
Source category for a published track.
Definition track.h:43
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:66
std::string noise_cancellation_options_json
Optional: JSON-encoded configuration for the noise cancellation module.
Definition audio_stream.h:81
std::string noise_cancellation_module
Optional: name of a noise cancellation module to enable for this stream.
Definition audio_stream.h:77