LiveKit C++ Client SDK v1.1.0
Real-time audio/video/data SDK for C++
Loading...
Searching...
No Matches
data_track_stream.h
1/*
2 * Copyright 2026 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 <mutex>
22#include <optional>
23
24#include "livekit/data_track_error.h"
25#include "livekit/data_track_frame.h"
26#include "livekit/ffi_handle.h"
27#include "livekit/visibility.h"
28
29namespace livekit {
30
31namespace proto {
32class FfiEvent;
33class DataTrackStreamReadResponse;
34} // namespace proto
35
54class LIVEKIT_API DataTrackStream {
55public:
57 struct Options {
59 std::optional<std::uint32_t> buffer_size{std::nullopt};
60 };
61
62 virtual ~DataTrackStream();
63
64 DataTrackStream(const DataTrackStream&) = delete;
65 DataTrackStream& operator=(const DataTrackStream&) = delete;
66 // The FFI listener captures `this`, so moving the object would leave the
67 // registered callback pointing at the old address.
68 DataTrackStream(DataTrackStream&&) noexcept = delete;
69 // Instances are created and returned as std::shared_ptr, so value-move
70 // support is not required by the current API.
71 DataTrackStream& operator=(DataTrackStream&&) noexcept = delete;
72
78 bool read(DataTrackFrame& out);
79
85 std::optional<SubscribeDataTrackError> terminalError() const;
86
91 void close();
92
93private:
94 friend class RemoteDataTrack;
95#ifdef LIVEKIT_TEST_ACCESS
96 friend class DataTrackStreamTest;
97#endif
98
99 DataTrackStream() = default;
101 void init(FfiHandle subscription_handle);
102
104 void onFfiEvent(const proto::FfiEvent& event);
105
107 void handleReadResponse(const proto::DataTrackStreamReadResponse& response);
108
110 void failProtocolError(const char* message);
111
113 void pushFrame(DataTrackFrame&& frame);
114
116 void pushEos(std::optional<SubscribeDataTrackError> error = std::nullopt);
117
119 mutable std::mutex mutex_;
120
122 std::condition_variable cv_;
123
127 std::optional<DataTrackFrame> frame_;
128
130 bool eof_{false};
131
133 bool closed_{false};
134
136 std::optional<SubscribeDataTrackError> terminal_error_;
137
139 FfiHandle subscription_handle_;
140
142 std::int32_t listener_id_{-1};
143};
144
145} // namespace livekit
Represents a pull-based stream of frames from a remote data track.
Definition data_track_stream.h:54
RAII wrapper for an FFI handle (uintptr_t) coming from Rust.
Definition ffi_handle.h:29
Represents a data track published by a remote participant.
Definition remote_data_track.h:52
Public API for the LiveKit C++ Client SDK.
Definition audio_frame.h:25
A single frame of data published or received on a data track.
Definition data_track_frame.h:36
Options for subscribing to a remote data track stream.
Definition data_track_stream.h:57
Error details returned when subscribing to a remote data track fails.
Definition data_track_error.h:83