fork/rust-sdks

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

Michael SloanUse rust::String::lossy for stringification to avoid crashing due to non-utf8 content (#485)ee5076b

livekit/livekit-ffi@0.13.0
4.8 KiB151 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/jsep.h"
18
19#include <iomanip>
20#include <memory>
21
22#include "livekit/rtc_error.h"
23#include "rtc_base/ref_counted_object.h"
24#include "rust/cxx.h"
25
26namespace livekit {
27
28std::string serialize_sdp_error(webrtc::SdpParseError error) {
29  std::stringstream ss;
30  ss << std::hex << std::setfill('0');
31  ss << std::setw(8) << (uint32_t)error.line.length();
32  ss << std::dec << std::setw(1) << error.line;
33  ss << std::dec << std::setw(1) << error.description;
34  return ss.str();
35}
36
37IceCandidate::IceCandidate(
38    std::unique_ptr<webrtc::IceCandidateInterface> ice_candidate)
39    : ice_candidate_(std::move(ice_candidate)) {}
40
41rust::String IceCandidate::sdp_mid() const {
42  return ice_candidate_->sdp_mid();
43}
44
45int IceCandidate::sdp_mline_index() const {
46  return ice_candidate_->sdp_mline_index();
47}
48
49rust::String IceCandidate::candidate() const {
50  return stringify();
51}
52
53rust::String IceCandidate::stringify() const {
54  std::string str;
55  ice_candidate_->ToString(&str);
56  return rust::String::lossy(str);
57}
58
59std::unique_ptr<webrtc::IceCandidateInterface> IceCandidate::release() {
60  return std::move(ice_candidate_);
61}
62
63std::shared_ptr<IceCandidate> create_ice_candidate(rust::String sdp_mid,
64                                                   int sdp_mline_index,
65                                                   rust::String sdp) {
66  webrtc::SdpParseError error;
67  auto ice_rtc = webrtc::CreateIceCandidate(sdp_mid.c_str(), sdp_mline_index,
68                                            sdp.c_str(), &error);
69  if (!ice_rtc) {
70    throw std::runtime_error(serialize_sdp_error(error));
71  }
72
73  return std::make_shared<IceCandidate>(
74      std::unique_ptr<webrtc::IceCandidateInterface>(ice_rtc));
75}
76
77SessionDescription::SessionDescription(
78    std::unique_ptr<webrtc::SessionDescriptionInterface> session_description)
79    : session_description_(std::move(session_description)) {}
80
81SdpType SessionDescription::sdp_type() const {
82  return static_cast<SdpType>(session_description_->GetType());
83}
84
85rust::String SessionDescription::stringify() const {
86  std::string str;
87  session_description_->ToString(&str);
88  return rust::String::lossy(str);
89}
90
91std::unique_ptr<SessionDescription> SessionDescription::clone() const {
92  return std::make_unique<SessionDescription>(session_description_->Clone());
93}
94
95std::unique_ptr<webrtc::SessionDescriptionInterface>
96SessionDescription::release() {
97  return std::move(session_description_);
98}
99
100std::unique_ptr<SessionDescription> create_session_description(
101    SdpType type,
102    rust::String sdp) {
103  webrtc::SdpParseError error;
104  auto rtc_sdp = webrtc::CreateSessionDescription(
105      static_cast<webrtc::SdpType>(type), sdp.c_str(), &error);
106  if (!rtc_sdp) {
107    throw std::runtime_error(serialize_sdp_error(error));
108  }
109
110  return std::make_unique<SessionDescription>(std::move(rtc_sdp));
111}
112
113NativeCreateSdpObserver::NativeCreateSdpObserver(
114    rust::Box<PeerContext> ctx,
115    rust::Fn<void(rust::Box<PeerContext>, std::unique_ptr<SessionDescription>)>
116        on_success,
117    rust::Fn<void(rust::Box<PeerContext>, RtcError)> on_error)
118    : ctx_(std::move(ctx)), on_success_(on_success), on_error_(on_error) {}
119
120void NativeCreateSdpObserver::OnSuccess(
121    webrtc::SessionDescriptionInterface* desc) {
122  // We have ownership of desc
123  on_success_(std::move(ctx_),
124              std::make_unique<SessionDescription>(
125                  std::unique_ptr<webrtc::SessionDescriptionInterface>(desc)));
126}
127
128void NativeCreateSdpObserver::OnFailure(webrtc::RTCError error) {
129  on_error_(std::move(ctx_), to_error(error));
130}
131
132NativeSetLocalSdpObserver::NativeSetLocalSdpObserver(
133    rust::Box<PeerContext> ctx,
134    rust::Fn<void(rust::Box<PeerContext>, RtcError)> on_complete)
135    : ctx_(std::move(ctx)), on_complete_(on_complete) {}
136
137void NativeSetLocalSdpObserver::OnSetLocalDescriptionComplete(
138    webrtc::RTCError error) {
139  on_complete_(std::move(ctx_), to_error(error));
140}
141
142NativeSetRemoteSdpObserver::NativeSetRemoteSdpObserver(
143    rust::Box<PeerContext> ctx,
144    rust::Fn<void(rust::Box<PeerContext>, RtcError)> on_complete)
145    : ctx_(std::move(ctx)), on_complete_(on_complete) {}
146
147void NativeSetRemoteSdpObserver::OnSetRemoteDescriptionComplete(
148    webrtc::RTCError error) {
149  on_complete_(std::move(ctx_), to_error(error));
150}
151}  // namespace livekit