/* * Copyright 2023 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/rtp_transceiver.h" namespace livekit { webrtc::RtpTransceiverInit to_native_rtp_transceiver_init( RtpTransceiverInit init) { { webrtc::RtpTransceiverInit native{}; native.direction = static_cast(init.direction); native.stream_ids = std::vector(init.stream_ids.begin(), init.stream_ids.end()); for (auto encoding : init.send_encodings) native.send_encodings.push_back( to_native_rtp_encoding_paramters(encoding)); return native; } } RtpTransceiver::RtpTransceiver( rtc::scoped_refptr transceiver) : transceiver_(std::move(transceiver)) {} MediaType RtpTransceiver::media_type() const { return static_cast(transceiver_->media_type()); } rust::String RtpTransceiver::mid() const { // The error/Result is converted into an Option in Rust (Wait for Option // suport in cxx.rs) (value throws an error if there's no value) return transceiver_->mid().value(); } std::shared_ptr RtpTransceiver::sender() const { return std::make_shared(transceiver_->sender()); } std::shared_ptr RtpTransceiver::receiver() const { return std::make_shared(transceiver_->receiver()); } bool RtpTransceiver::stopped() const { return transceiver_->stopped(); } bool RtpTransceiver::stopping() const { return transceiver_->stopping(); } RtpTransceiverDirection RtpTransceiver::direction() const { return static_cast(transceiver_->direction()); } void RtpTransceiver::set_direction(RtpTransceiverDirection direction) const { auto error = transceiver_->SetDirectionWithError( static_cast(direction)); if (!error.ok()) { throw std::runtime_error(serialize_error(to_error(error))); } } RtpTransceiverDirection RtpTransceiver::current_direction() const { return static_cast( transceiver_->current_direction().value()); } RtpTransceiverDirection RtpTransceiver::fired_direction() const { return static_cast( transceiver_->fired_direction().value()); } void RtpTransceiver::stop_standard() const { auto error = transceiver_->StopStandard(); if (!error.ok()) throw std::runtime_error(serialize_error(to_error(error))); } void RtpTransceiver::set_codec_preferences( rust::Vec codecs) const { std::vector std_codecs; for (auto codec : codecs) std_codecs.push_back(to_native_rtp_codec_capability(codec)); auto error = transceiver_->SetCodecPreferences(std_codecs); if (!error.ok()) throw std::runtime_error(serialize_error(to_error(error))); } rust::Vec RtpTransceiver::codec_preferences() const { rust::Vec rust; for (auto codec : transceiver_->codec_preferences()) rust.push_back(to_rust_rtp_codec_capability(codec)); return rust; } rust::Vec RtpTransceiver::header_extensions_to_offer() const { rust::Vec rust; for (auto header : transceiver_->HeaderExtensionsToOffer()) rust.push_back(to_rust_rtp_header_extension_capability(header)); return rust; } rust::Vec RtpTransceiver::header_extensions_negotiated() const { rust::Vec rust; for (auto header : transceiver_->HeaderExtensionsNegotiated()) rust.push_back(to_rust_rtp_header_extension_capability(header)); return rust; } void RtpTransceiver::set_offered_rtp_header_extensions( rust::Vec header_extensions_to_offer) const { std::vector headers; for (auto header : header_extensions_to_offer) headers.push_back(to_native_rtp_header_extension_capability(header)); auto error = transceiver_->SetOfferedRtpHeaderExtensions(headers); if (!error.ok()) throw std::runtime_error(serialize_error(to_error(error))); } } // namespace livekit