fork/rust-sdks

git clone https://git.julia.blue/fork/rust-sdks

Théo Monnomfeat: rtc stats (#218)a5ba08e

livekit/livekit-ffi@0.13.0
2.8 KiB93 linesraw
1/*
2 * Copyright 2023 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#include "livekit/rtp_sender.h"
18#include "livekit/jsep.h"
19
20#include "rust/cxx.h"
21#include "webrtc-sys/src/rtp_sender.rs.h"
22
23namespace livekit {
24
25
26
27RtpSender::RtpSender(
28    std::shared_ptr<RtcRuntime> rtc_runtime,
29    rtc::scoped_refptr<webrtc::RtpSenderInterface> sender,
30    rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection)
31    : rtc_runtime_(rtc_runtime),
32      sender_(std::move(sender)),
33      peer_connection_(std::move(peer_connection)) {}
34
35bool RtpSender::set_track(std::shared_ptr<MediaStreamTrack> track) const {
36  return sender_->SetTrack(track->rtc_track().get());
37}
38
39std::shared_ptr<MediaStreamTrack> RtpSender::track() const {
40  return rtc_runtime_->get_or_create_media_stream_track(sender_->track());
41}
42
43uint32_t RtpSender::ssrc() const {
44  return sender_->ssrc();
45}
46
47void RtpSender::get_stats(
48    rust::Box<SenderContext> ctx,
49    rust::Fn<void(rust::Box<SenderContext>, rust::String)> on_stats) const {
50  auto observer =
51      rtc::make_ref_counted<NativeRtcStatsCollector<SenderContext>>(std::move(ctx), on_stats);
52  peer_connection_->GetStats(sender_, observer);
53}
54
55MediaType RtpSender::media_type() const {
56  return static_cast<MediaType>(sender_->media_type());
57}
58
59rust::String RtpSender::id() const {
60  return sender_->id();
61}
62
63rust::Vec<rust::String> RtpSender::stream_ids() const {
64  rust::Vec<rust::String> vec;
65  for (auto str : sender_->stream_ids())
66    vec.push_back(str);
67
68  return vec;
69}
70
71void RtpSender::set_streams(const rust::Vec<rust::String>& stream_ids) const {
72  std::vector<std::string> std_stream_ids(stream_ids.begin(), stream_ids.end());
73  sender_->SetStreams(std_stream_ids);
74}
75
76rust::Vec<RtpEncodingParameters> RtpSender::init_send_encodings() const {
77  rust::Vec<RtpEncodingParameters> encodings;
78  for (auto encoding : sender_->init_send_encodings())
79    encodings.push_back(to_rust_rtp_encoding_parameters(encoding));
80  return encodings;
81}
82
83RtpParameters RtpSender::get_parameters() const {
84  return to_rust_rtp_parameters(sender_->GetParameters());
85}
86
87void RtpSender::set_parameters(RtpParameters params) const {
88  auto error = sender_->SetParameters(to_native_rtp_parameters(params));
89  if (!error.ok())
90    throw std::runtime_error(serialize_error(to_error(error)));
91}
92
93}  // namespace livekit