-
Notifications
You must be signed in to change notification settings - Fork 40
pre-encoded ingest #260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
pre-encoded ingest #260
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule client-sdk-rust
updated
from 806641 to bae4df
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| /* | ||
| * Copyright 2026 LiveKit | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an “AS IS” BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cstdint> | ||
| #include <optional> | ||
| #include <vector> | ||
|
|
||
| #include "livekit/video_codec.h" | ||
| #include "livekit/video_source.h" | ||
| #include "livekit/visibility.h" | ||
|
|
||
| namespace livekit { | ||
|
|
||
| /// @brief Video source for publishing pre-encoded access units without re-encoding. | ||
| /// | ||
| /// Capture calls are synchronous and copy the payload before returning. This | ||
| /// type is not safe for concurrent capture calls. Feedback polling can run on | ||
| /// another application thread. | ||
| class LIVEKIT_API EncodedVideoSource final : public VideoSource { | ||
| public: | ||
| /// @brief One complete pre-encoded video access unit. | ||
| struct Frame { | ||
| /// True if this access unit is independently decodable. | ||
| bool is_keyframe = false; | ||
| /// Encoded frame width in pixels. | ||
| /// Set both width and height to zero to use the source resolution. | ||
| std::uint32_t width = 0; | ||
| /// Encoded frame height in pixels. | ||
| /// Set both width and height to zero to use the source resolution. | ||
| std::uint32_t height = 0; | ||
| /// Capture timestamp in microseconds. Set to zero to use the current time. | ||
| std::int64_t timestamp_us = 0; | ||
| /// Complete encoded access-unit payload. | ||
| std::vector<std::uint8_t> data; | ||
| /// Optional packet-trailer metadata. | ||
| std::optional<VideoFrameMetadata> metadata; | ||
| }; | ||
|
|
||
| /// @brief Latest encoder rate-control target requested by the publishing pipeline. | ||
| struct RateControl { | ||
| /// Requested target bitrate in bits per second. | ||
| std::uint64_t target_bitrate_bps = 0; | ||
| /// Requested frame rate in frames per second. | ||
| double framerate_fps = 0.0; | ||
| }; | ||
|
|
||
| /// @brief Pending feedback from the pre-encoded passthrough encoder. | ||
| struct Feedback { | ||
| /// True when the upstream encoder must produce a key frame. | ||
| bool keyframe_requested = false; | ||
| /// Latest rate-control target, if the publishing pipeline requested one. | ||
| std::optional<RateControl> rate_control; | ||
| }; | ||
|
|
||
| /// @brief Create a pre-encoded source for one codec and initial resolution. | ||
| /// @param codec Codec carried by every frame submitted to this source. This | ||
| /// must match TrackPublishOptions::video_codec. | ||
| /// @param width Initial source width in pixels. Must be in [1, 65535]. | ||
| /// @param height Initial source height in pixels. Must be in [1, 65535]. | ||
| /// @throws std::invalid_argument if either dimension is outside [1, 65535]. | ||
| /// @throws std::runtime_error if source creation fails. | ||
| EncodedVideoSource(VideoCodec codec, int width, int height); | ||
|
|
||
| /// @brief Codec carried by every frame submitted to this source. | ||
| VideoCodec codec() const noexcept { return codec_; } | ||
|
|
||
| /// @brief Submit one complete encoded access unit. | ||
| /// @param frame Encoded frame. Its payload is copied during this call. | ||
| /// @return True if the frame was accepted. | ||
| /// @throws std::invalid_argument if the frame is empty, too large, has only | ||
| /// one zero dimension, or has a dimension above 65535. | ||
| /// @throws std::runtime_error if the FFI request fails. | ||
| [[nodiscard]] bool captureFrame(const Frame& frame) const; | ||
|
|
||
| /// @brief Consume pending keyframe and rate-control feedback. | ||
| /// @return Feedback accumulated since the previous call. Rate-control | ||
| /// updates use latest-value-wins semantics. | ||
| /// @throws std::runtime_error if the FFI request fails. | ||
| [[nodiscard]] Feedback takeFeedback() const; | ||
|
|
||
| private: | ||
| using VideoSource::captureFrame; | ||
|
|
||
| VideoCodec codec_; | ||
| }; | ||
|
|
||
| } // namespace livekit | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /* | ||
| * Copyright 2026 LiveKit | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| namespace livekit { | ||
|
|
||
| /// @brief Codec used to publish a video track. | ||
| enum class VideoCodec { VP8 = 0, H264 = 1, AV1 = 2, VP9 = 3, H265 = 4 }; | ||
|
|
||
| } // namespace livekit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| /* | ||
| * Copyright 2026 LiveKit | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an “AS IS” BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #include "livekit/encoded_video_source.h" | ||
|
|
||
| #include <cstddef> | ||
| #include <stdexcept> | ||
|
|
||
| #include "ffi.pb.h" | ||
| #include "ffi_client.h" | ||
| #include "video_frame.pb.h" | ||
| #include "video_utils.h" | ||
|
|
||
| namespace livekit { | ||
| namespace { | ||
|
|
||
| constexpr std::size_t kMaxFrameSize = std::size_t{64} * 1024U * 1024U; | ||
| constexpr std::uint32_t kMaxFrameDimension = 65535U; | ||
|
|
||
| int validateDimension(int dimension) { | ||
| if (dimension <= 0 || static_cast<std::uint32_t>(dimension) > kMaxFrameDimension) { | ||
| throw std::invalid_argument("EncodedVideoSource: dimensions must be positive and at most 65535"); | ||
| } | ||
| return dimension; | ||
| } | ||
|
|
||
| proto::VideoCodec toProtoCodec(VideoCodec codec) { | ||
| switch (codec) { | ||
| case VideoCodec::H264: | ||
| return proto::VideoCodec::H264; | ||
| case VideoCodec::H265: | ||
| return proto::VideoCodec::H265; | ||
| case VideoCodec::VP8: | ||
| return proto::VideoCodec::VP8; | ||
| case VideoCodec::VP9: | ||
| return proto::VideoCodec::VP9; | ||
| case VideoCodec::AV1: | ||
| return proto::VideoCodec::AV1; | ||
| } | ||
| throw std::invalid_argument("EncodedVideoSource: unknown codec"); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| EncodedVideoSource::EncodedVideoSource(VideoCodec codec, int width, int height) | ||
| : VideoSource(validateDimension(width), validateDimension(height), VideoSource::SourceType::Encoded), | ||
|
stephen-derosa marked this conversation as resolved.
|
||
| codec_(codec) {} | ||
|
|
||
| bool EncodedVideoSource::captureFrame(const Frame& frame) const { | ||
| if ((frame.width == 0) != (frame.height == 0)) { | ||
| throw std::invalid_argument("EncodedVideoSource: frame dimensions must both be zero or both be non-zero"); | ||
| } | ||
| if (frame.width > kMaxFrameDimension || frame.height > kMaxFrameDimension) { | ||
| throw std::invalid_argument("EncodedVideoSource: frame dimensions must not exceed 65535"); | ||
| } | ||
| if (frame.data.empty() || frame.data.size() > kMaxFrameSize) { | ||
| throw std::invalid_argument("EncodedVideoSource: frame payload must be between 1 byte and 64 MiB"); | ||
| } | ||
| if (ffiHandleId() == 0) { | ||
| throw std::runtime_error("EncodedVideoSource: invalid FFI handle"); | ||
| } | ||
|
|
||
| proto::FfiRequest req; | ||
| auto* msg = req.mutable_capture_encoded_video_frame(); | ||
| msg->set_source_handle(ffiHandleId()); | ||
| auto* buffer = msg->mutable_buffer(); | ||
| buffer->set_data_ptr(reinterpret_cast<std::uintptr_t>(frame.data.data())); | ||
| buffer->set_data_len(frame.data.size()); | ||
| msg->set_codec(toProtoCodec(codec_)); | ||
| msg->set_frame_type(frame.is_keyframe ? proto::EncodedFrameType::ENCODED_FRAME_KEY | ||
| : proto::EncodedFrameType::ENCODED_FRAME_DELTA); | ||
| msg->set_width(frame.width == 0 ? static_cast<std::uint32_t>(width()) : frame.width); | ||
| msg->set_height(frame.height == 0 ? static_cast<std::uint32_t>(height()) : frame.height); | ||
| msg->set_timestamp_us(frame.timestamp_us); | ||
| if (auto metadata = toProto(frame.metadata)) { | ||
| msg->mutable_metadata()->CopyFrom(*metadata); | ||
| } | ||
|
|
||
| const proto::FfiResponse resp = FfiClient::instance().sendRequest(req); | ||
| if (!resp.has_capture_encoded_video_frame()) { | ||
| throw std::runtime_error("EncodedVideoSource: missing capture response"); | ||
| } | ||
| return resp.capture_encoded_video_frame().accepted(); | ||
| } | ||
|
|
||
| EncodedVideoSource::Feedback EncodedVideoSource::takeFeedback() const { | ||
| if (ffiHandleId() == 0) { | ||
| throw std::runtime_error("EncodedVideoSource: invalid FFI handle"); | ||
| } | ||
|
|
||
| proto::FfiRequest req; | ||
| req.mutable_take_encoded_video_source_feedback()->set_source_handle(ffiHandleId()); | ||
| const proto::FfiResponse resp = FfiClient::instance().sendRequest(req); | ||
| if (!resp.has_take_encoded_video_source_feedback()) { | ||
| throw std::runtime_error("EncodedVideoSource: missing feedback response"); | ||
| } | ||
|
|
||
| const auto& proto_feedback = resp.take_encoded_video_source_feedback(); | ||
| Feedback feedback; | ||
| feedback.keyframe_requested = proto_feedback.keyframe_requested(); | ||
| if (proto_feedback.has_rate_control()) { | ||
| feedback.rate_control = | ||
| RateControl{proto_feedback.rate_control().target_bitrate_bps(), proto_feedback.rate_control().framerate_fps()}; | ||
| } | ||
| return feedback; | ||
| } | ||
|
|
||
| } // namespace livekit | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.