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