| 1 | /* |
| 2 | * Copyright 2025 LiveKit, Inc. |
| 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 | #include "livekit/rtp_transceiver.h" |
| 18 | |
| 19 | #include "api/peer_connection_interface.h" |
| 20 | #include "api/scoped_refptr.h" |
| 21 | |
| 22 | namespace livekit_ffi { |
| 23 | |
| 24 | webrtc::RtpTransceiverInit to_native_rtp_transceiver_init( |
| 25 | RtpTransceiverInit init) { |
| 26 | { |
| 27 | webrtc::RtpTransceiverInit native{}; |
| 28 | native.direction = |
| 29 | static_cast<webrtc::RtpTransceiverDirection>(init.direction); |
| 30 | native.stream_ids = std::vector<std::string>(init.stream_ids.begin(), |
| 31 | init.stream_ids.end()); |
| 32 | for (auto encoding : init.send_encodings) |
| 33 | native.send_encodings.push_back( |
| 34 | to_native_rtp_encoding_paramters(encoding)); |
| 35 | return native; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | RtpTransceiver::RtpTransceiver( |
| 40 | std::shared_ptr<RtcRuntime> rtc_runtime, |
| 41 | webrtc::scoped_refptr<webrtc::RtpTransceiverInterface> transceiver, |
| 42 | webrtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection) |
| 43 | : rtc_runtime_(rtc_runtime), |
| 44 | transceiver_(std::move(transceiver)), |
| 45 | peer_connection_(std::move(peer_connection)) {} |
| 46 | |
| 47 | MediaType RtpTransceiver::media_type() const { |
| 48 | return static_cast<MediaType>(transceiver_->media_type()); |
| 49 | } |
| 50 | |
| 51 | rust::String RtpTransceiver::mid() const { |
| 52 | // libwebrtc is typically built with -fno-exceptions, so calling |
| 53 | // std::optional::value() on an empty optional aborts the process instead of |
| 54 | // throwing. Use value_or("") and treat empty string as "no mid yet" on the |
| 55 | // Rust side (see libwebrtc/src/native/rtp_transceiver.rs::mid). |
| 56 | return transceiver_->mid().value_or(""); |
| 57 | } |
| 58 | |
| 59 | std::shared_ptr<RtpSender> RtpTransceiver::sender() const { |
| 60 | return std::make_shared<RtpSender>(rtc_runtime_, transceiver_->sender(), |
| 61 | peer_connection_); |
| 62 | } |
| 63 | |
| 64 | std::shared_ptr<RtpReceiver> RtpTransceiver::receiver() const { |
| 65 | return std::make_shared<RtpReceiver>(rtc_runtime_, transceiver_->receiver(), |
| 66 | peer_connection_); |
| 67 | } |
| 68 | |
| 69 | bool RtpTransceiver::stopped() const { |
| 70 | return transceiver_->stopped(); |
| 71 | } |
| 72 | |
| 73 | bool RtpTransceiver::stopping() const { |
| 74 | return transceiver_->stopping(); |
| 75 | } |
| 76 | |
| 77 | RtpTransceiverDirection RtpTransceiver::direction() const { |
| 78 | return static_cast<RtpTransceiverDirection>(transceiver_->direction()); |
| 79 | } |
| 80 | |
| 81 | void RtpTransceiver::set_direction(RtpTransceiverDirection direction) const { |
| 82 | auto error = transceiver_->SetDirectionWithError( |
| 83 | static_cast<webrtc::RtpTransceiverDirection>(direction)); |
| 84 | |
| 85 | if (!error.ok()) { |
| 86 | throw std::runtime_error(serialize_error(to_error(error))); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | RtpTransceiverDirection RtpTransceiver::current_direction() const { |
| 91 | return static_cast<RtpTransceiverDirection>( |
| 92 | transceiver_->current_direction().value()); |
| 93 | } |
| 94 | |
| 95 | RtpTransceiverDirection RtpTransceiver::fired_direction() const { |
| 96 | return static_cast<RtpTransceiverDirection>( |
| 97 | transceiver_->fired_direction().value()); |
| 98 | } |
| 99 | |
| 100 | void RtpTransceiver::stop_standard() const { |
| 101 | auto error = transceiver_->StopStandard(); |
| 102 | if (!error.ok()) |
| 103 | throw std::runtime_error(serialize_error(to_error(error))); |
| 104 | } |
| 105 | |
| 106 | void RtpTransceiver::set_codec_preferences( |
| 107 | rust::Vec<RtpCodecCapability> codecs) const { |
| 108 | std::vector<webrtc::RtpCodecCapability> std_codecs; |
| 109 | |
| 110 | for (auto codec : codecs) |
| 111 | std_codecs.push_back(to_native_rtp_codec_capability(codec)); |
| 112 | |
| 113 | auto error = transceiver_->SetCodecPreferences(std_codecs); |
| 114 | if (!error.ok()) |
| 115 | throw std::runtime_error(serialize_error(to_error(error))); |
| 116 | } |
| 117 | |
| 118 | rust::Vec<RtpCodecCapability> RtpTransceiver::codec_preferences() const { |
| 119 | rust::Vec<RtpCodecCapability> rust; |
| 120 | for (auto codec : transceiver_->codec_preferences()) |
| 121 | rust.push_back(to_rust_rtp_codec_capability(codec)); |
| 122 | |
| 123 | return rust; |
| 124 | } |
| 125 | |
| 126 | rust::Vec<RtpHeaderExtensionCapability> |
| 127 | RtpTransceiver::header_extensions_to_negotiate() const { |
| 128 | rust::Vec<RtpHeaderExtensionCapability> rust; |
| 129 | for (auto header : transceiver_->GetHeaderExtensionsToNegotiate()) |
| 130 | rust.push_back(to_rust_rtp_header_extension_capability(header)); |
| 131 | |
| 132 | return rust; |
| 133 | } |
| 134 | |
| 135 | rust::Vec<RtpHeaderExtensionCapability> |
| 136 | RtpTransceiver::negotiated_header_extensions() const { |
| 137 | rust::Vec<RtpHeaderExtensionCapability> rust; |
| 138 | for (auto header : transceiver_->GetNegotiatedHeaderExtensions()) |
| 139 | rust.push_back(to_rust_rtp_header_extension_capability(header)); |
| 140 | |
| 141 | return rust; |
| 142 | } |
| 143 | |
| 144 | void RtpTransceiver::set_header_extensions_to_negotiate( |
| 145 | rust::Vec<RtpHeaderExtensionCapability> header_extensions_to_offer) const { |
| 146 | std::vector<webrtc::RtpHeaderExtensionCapability> headers; |
| 147 | |
| 148 | for (auto header : header_extensions_to_offer) |
| 149 | headers.push_back(to_native_rtp_header_extension_capability(header)); |
| 150 | |
| 151 | auto error = transceiver_->SetHeaderExtensionsToNegotiate(headers); |
| 152 | if (!error.ok()) |
| 153 | throw std::runtime_error(serialize_error(to_error(error))); |
| 154 | } |
| 155 | |
| 156 | } // namespace livekit_ffi |