LiveKit C++ SDK
Real-time audio/video SDK for C++
Loading...
Searching...
No Matches
audio_source.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 <cstdint>
20
21#include "livekit/audio_frame.h"
22#include "livekit/ffi_handle.h"
23
24namespace livekit {
25
26namespace proto {
27class FfiRequest;
28class FfiResponse;
29} // namespace proto
30
31class FfiClient;
32
37public:
71 AudioSource(int sample_rate, int num_channels, int queue_size_ms = 0);
72 virtual ~AudioSource() = default;
73
74 AudioSource(const AudioSource &) = delete;
75 AudioSource &operator=(const AudioSource &) = delete;
76 AudioSource(AudioSource &&) noexcept = default;
77 AudioSource &operator=(AudioSource &&) noexcept = default;
78
80 int sample_rate() const noexcept { return sample_rate_; }
81
83 int num_channels() const noexcept { return num_channels_; }
84
86 std::uint64_t ffi_handle_id() const noexcept {
87 return static_cast<std::uint64_t>(handle_.get());
88 }
89
91 double queuedDuration() const noexcept;
92
97 void clearQueue();
98
142 void captureFrame(const AudioFrame &frame, int timeout_ms = 20);
143
144private:
145 // Internal helper to reset the local queue tracking (like _release_waiter).
146 void resetQueueTracking() noexcept;
147
148 int sample_rate_;
149 int num_channels_;
150 int queue_size_ms_;
151
152 // RAII wrapper for this audio source's FFI handle
153 FfiHandle handle_;
154
155 // Queue tracking (all in seconds; based on steady_clock in the .cpp).
156 mutable double last_capture_{0.0};
157 mutable double q_size_{0.0};
158};
159
160} // namespace livekit
Represents a raw PCM audio frame with interleaved int16 samples.
Definition audio_frame.h:37
Definition audio_source.h:36
AudioSource(int sample_rate, int num_channels, int queue_size_ms=0)
void captureFrame(const AudioFrame &frame, int timeout_ms=20)
int num_channels() const noexcept
The number of audio channels.
Definition audio_source.h:83
double queuedDuration() const noexcept
Current duration of queued audio (in seconds).
std::uint64_t ffi_handle_id() const noexcept
Underlying FFI handle ID used in FFI requests.
Definition audio_source.h:86
int sample_rate() const noexcept
The sample rate of the audio source in Hz.
Definition audio_source.h:80
RAII wrapper for an FFI handle (uintptr_t) coming from Rust.
Definition ffi_handle.h:29