| 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_receiver.h" |
| 18 | #include "livekit/jsep.h" |
| 19 | |
| 20 | #include <memory> |
| 21 | |
| 22 | #include "absl/types/optional.h" |
| 23 | #include "api/peer_connection_interface.h" |
| 24 | #include "api/scoped_refptr.h" |
| 25 | |
| 26 | namespace livekit_ffi { |
| 27 | |
| 28 | RtpReceiver::RtpReceiver( |
| 29 | std::shared_ptr<RtcRuntime> rtc_runtime, |
| 30 | webrtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver, |
| 31 | webrtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection) |
| 32 | : rtc_runtime_(rtc_runtime), |
| 33 | receiver_(std::move(receiver)), |
| 34 | peer_connection_(std::move(peer_connection)) {} |
| 35 | |
| 36 | std::shared_ptr<MediaStreamTrack> RtpReceiver::track() const { |
| 37 | return rtc_runtime_->get_or_create_media_stream_track(receiver_->track()); |
| 38 | } |
| 39 | |
| 40 | rust::Vec<rust::String> RtpReceiver::stream_ids() const { |
| 41 | rust::Vec<rust::String> rust; |
| 42 | for (auto id : receiver_->stream_ids()) |
| 43 | rust.push_back(id); |
| 44 | return rust; |
| 45 | } |
| 46 | |
| 47 | void RtpReceiver::get_stats( |
| 48 | rust::Box<ReceiverContext> ctx, |
| 49 | rust::Fn<void(rust::Box<ReceiverContext>, rust::String)> on_stats) const { |
| 50 | auto observer = |
| 51 | webrtc::make_ref_counted<NativeRtcStatsCollector<ReceiverContext>>(std::move(ctx), on_stats); |
| 52 | peer_connection_->GetStats(receiver_, observer); |
| 53 | } |
| 54 | |
| 55 | rust::Vec<MediaStreamPtr> RtpReceiver::streams() const { |
| 56 | rust::Vec<MediaStreamPtr> rust; |
| 57 | for (auto stream : receiver_->streams()) |
| 58 | rust.push_back( |
| 59 | MediaStreamPtr{std::make_shared<MediaStream>(rtc_runtime_, stream)}); |
| 60 | return rust; |
| 61 | } |
| 62 | |
| 63 | MediaType RtpReceiver::media_type() const { |
| 64 | return static_cast<MediaType>(receiver_->media_type()); |
| 65 | } |
| 66 | |
| 67 | rust::String RtpReceiver::id() const { |
| 68 | return receiver_->id(); |
| 69 | } |
| 70 | |
| 71 | RtpParameters RtpReceiver::get_parameters() const { |
| 72 | return to_rust_rtp_parameters(receiver_->GetParameters()); |
| 73 | } |
| 74 | |
| 75 | void RtpReceiver::set_jitter_buffer_minimum_delay(bool is_some, |
| 76 | double delay_seconds) const { |
| 77 | receiver_->SetJitterBufferMinimumDelay( |
| 78 | is_some ? absl::make_optional(delay_seconds) : absl::nullopt); |
| 79 | } |
| 80 | |
| 81 | rust::Vec<RtpSourceEntry> RtpReceiver::get_sources() const { |
| 82 | rust::Vec<RtpSourceEntry> entries; |
| 83 | for (const auto& source : receiver_->GetSources()) { |
| 84 | auto level = source.audio_level(); |
| 85 | entries.push_back(RtpSourceEntry{ |
| 86 | source.source_id(), |
| 87 | source.source_type() == webrtc::RtpSourceType::CSRC, |
| 88 | level ? static_cast<int16_t>(*level) : static_cast<int16_t>(-1), |
| 89 | source.timestamp().ms()}); |
| 90 | } |
| 91 | return entries; |
| 92 | } |
| 93 | |
| 94 | } // namespace livekit_ffi |