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
5.1 KiB154 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_transceiver.h"
18
19#include "api/peer_connection_interface.h"
20#include "api/scoped_refptr.h"
21
22namespace livekit {
23
24webrtc::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
39RtpTransceiver::RtpTransceiver(
40    std::shared_ptr<RtcRuntime> rtc_runtime,
41    rtc::scoped_refptr<webrtc::RtpTransceiverInterface> transceiver,
42    rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection)
43    : rtc_runtime_(rtc_runtime),
44      transceiver_(std::move(transceiver)),
45      peer_connection_(std::move(peer_connection)) {}
46
47MediaType RtpTransceiver::media_type() const {
48  return static_cast<MediaType>(transceiver_->media_type());
49}
50
51rust::String RtpTransceiver::mid() const {
52  // The error/Result is converted into an Option in Rust (Wait for Option
53  // suport in cxx.rs) (value throws an error if there's no value)
54  return transceiver_->mid().value();
55}
56
57std::shared_ptr<RtpSender> RtpTransceiver::sender() const {
58  return std::make_shared<RtpSender>(rtc_runtime_, transceiver_->sender(),
59                                     peer_connection_);
60}
61
62std::shared_ptr<RtpReceiver> RtpTransceiver::receiver() const {
63  return std::make_shared<RtpReceiver>(rtc_runtime_, transceiver_->receiver(),
64                                       peer_connection_);
65}
66
67bool RtpTransceiver::stopped() const {
68  return transceiver_->stopped();
69}
70
71bool RtpTransceiver::stopping() const {
72  return transceiver_->stopping();
73}
74
75RtpTransceiverDirection RtpTransceiver::direction() const {
76  return static_cast<RtpTransceiverDirection>(transceiver_->direction());
77}
78
79void RtpTransceiver::set_direction(RtpTransceiverDirection direction) const {
80  auto error = transceiver_->SetDirectionWithError(
81      static_cast<webrtc::RtpTransceiverDirection>(direction));
82
83  if (!error.ok()) {
84    throw std::runtime_error(serialize_error(to_error(error)));
85  }
86}
87
88RtpTransceiverDirection RtpTransceiver::current_direction() const {
89  return static_cast<RtpTransceiverDirection>(
90      transceiver_->current_direction().value());
91}
92
93RtpTransceiverDirection RtpTransceiver::fired_direction() const {
94  return static_cast<RtpTransceiverDirection>(
95      transceiver_->fired_direction().value());
96}
97
98void RtpTransceiver::stop_standard() const {
99  auto error = transceiver_->StopStandard();
100  if (!error.ok())
101    throw std::runtime_error(serialize_error(to_error(error)));
102}
103
104void RtpTransceiver::set_codec_preferences(
105    rust::Vec<RtpCodecCapability> codecs) const {
106  std::vector<webrtc::RtpCodecCapability> std_codecs;
107
108  for (auto codec : codecs)
109    std_codecs.push_back(to_native_rtp_codec_capability(codec));
110
111  auto error = transceiver_->SetCodecPreferences(std_codecs);
112  if (!error.ok())
113    throw std::runtime_error(serialize_error(to_error(error)));
114}
115
116rust::Vec<RtpCodecCapability> RtpTransceiver::codec_preferences() const {
117  rust::Vec<RtpCodecCapability> rust;
118  for (auto codec : transceiver_->codec_preferences())
119    rust.push_back(to_rust_rtp_codec_capability(codec));
120
121  return rust;
122}
123
124rust::Vec<RtpHeaderExtensionCapability>
125RtpTransceiver::header_extensions_to_negotiate() const {
126  rust::Vec<RtpHeaderExtensionCapability> rust;
127  for (auto header : transceiver_->GetHeaderExtensionsToNegotiate())
128    rust.push_back(to_rust_rtp_header_extension_capability(header));
129
130  return rust;
131}
132
133rust::Vec<RtpHeaderExtensionCapability>
134RtpTransceiver::negotiated_header_extensions() const {
135  rust::Vec<RtpHeaderExtensionCapability> rust;
136  for (auto header : transceiver_->GetNegotiatedHeaderExtensions())
137    rust.push_back(to_rust_rtp_header_extension_capability(header));
138
139  return rust;
140}
141
142void RtpTransceiver::set_header_extensions_to_negotiate(
143    rust::Vec<RtpHeaderExtensionCapability> header_extensions_to_offer) const {
144  std::vector<webrtc::RtpHeaderExtensionCapability> headers;
145
146  for (auto header : header_extensions_to_offer)
147    headers.push_back(to_native_rtp_header_extension_capability(header));
148
149  auto error = transceiver_->SetHeaderExtensionsToNegotiate(headers);
150  if (!error.ok())
151    throw std::runtime_error(serialize_error(to_error(error)));
152}
153
154}  // namespace livekit