LiveKit C++ SDK
Real-time audio/video SDK for C++
Loading...
Searching...
No Matches
video_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
26#include "ffi_handle.h"
27#include "participant.h"
28#include "track.h"
29#include "video_frame.h"
30#include "video_source.h"
31
32namespace livekit {
33
34// A single video frame event delivered by VideoStream::read().
36 VideoFrame frame;
37 std::int64_t timestamp_us;
38 VideoRotation rotation;
39};
40
41namespace proto {
42class FfiEvent;
43}
44
45// Represents a pull-based stream of decoded PCM audio frames coming from
46// a remote (or local) LiveKit track. Similar to VideoStream, but for audio.
47//
48// Typical usage:
49//
50// AudioStream::Options opts;
51// auto stream = AudioStream::fromTrack(remoteAudioTrack, opts);
52//
53// AudioFrameEvent ev;
54// while (stream->read(ev)) {
55// // ev.frame contains interleaved int16 PCM samples
56// }
57//
58// stream->close(); // optional, called automatically in destructor
59//
61public:
62 struct Options {
63 // Maximum number of VideoFrameEvent items buffered in the internal queue.
64 // 0 means "unbounded" (the queue can grow without limit).
65 //
66 // With a non-zero capacity, the queue behaves like a ring-buffer: if it
67 // is full, the oldest frame is dropped when a new one arrives.
68 std::size_t capacity{0};
69
70 // Preferred pixel format for frames delivered by read(). The FFI layer
71 // converts into this format if supported (e.g., RGBA, BGRA, I420, ...).
72 VideoBufferType format{VideoBufferType::RGBA};
73 };
74
75 // Factory: create a VideoStream bound to a specific Track
76 static std::shared_ptr<VideoStream>
77 fromTrack(const std::shared_ptr<Track> &track, const Options &options);
78
79 // Factory: create a VideoStream from a Participant + TrackSource
80 static std::shared_ptr<VideoStream> fromParticipant(Participant &participant,
81 TrackSource track_source,
82 const Options &options);
83
84 virtual ~VideoStream();
85
86 VideoStream(const VideoStream &) = delete;
87 VideoStream &operator=(const VideoStream &) = delete;
88 VideoStream(VideoStream &&) noexcept;
89 VideoStream &operator=(VideoStream &&) noexcept;
90
98
104 void close();
105
106private:
107 VideoStream() = default;
108
109 // Internal init helpers, used by the factories
110 void initFromTrack(const std::shared_ptr<Track> &track,
111 const Options &options);
112 void initFromParticipant(Participant &participant, TrackSource source,
113 const Options &options);
114
115 // FFI event handler (registered with FfiClient)
116 void onFfiEvent(const proto::FfiEvent &event);
117
118 // Queue helpers
119 void pushFrame(VideoFrameEvent &&ev);
120 void pushEos();
121
122 mutable std::mutex mutex_;
123 std::condition_variable cv_;
124 std::deque<VideoFrameEvent> queue_;
125 std::size_t capacity_{0};
126 bool eof_{false};
127 bool closed_{false};
128
129 // Underlying FFI handle for the video stream
130 FfiHandle stream_handle_;
131
132 // Listener id registered on FfiClient
133 std::int64_t listener_id_{0};
134};
135
136} // namespace livekit
Definition participant.h:31
Definition track.h:71
Definition video_frame.h:59
Definition video_stream.h:60
bool read(VideoFrameEvent &out)
Definition video_stream.h:35
Definition video_stream.h:62