fork/rust-sdks

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

Théo Monnomfeat: stats request (#229)e33c5a9

main
4.5 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#pragma once
18
19#include <memory>
20
21#include "api/jsep.h"
22#include "api/ref_counted_base.h"
23#include "api/set_local_description_observer_interface.h"
24#include "api/set_remote_description_observer_interface.h"
25#include "api/stats/rtc_stats_collector_callback.h"
26#include "livekit/rtc_error.h"
27#include "rtc_base/ref_count.h"
28#include "rust/cxx.h"
29
30namespace livekit {
31class IceCandidate;
32class SessionDescription;
33};  // namespace livekit
34#include "webrtc-sys/src/jsep.rs.h"
35
36namespace livekit {
37
38class PeerContext;
39
40class IceCandidate {
41 public:
42  explicit IceCandidate(
43      std::unique_ptr<webrtc::IceCandidateInterface> ice_candidate);
44
45  rust::String sdp_mid() const;
46  int sdp_mline_index() const;
47  rust::String candidate() const;  // TODO(theomonnom) Return livekit::Candidate
48                                   // instead of rust::String
49
50  rust::String stringify() const;
51  std::unique_ptr<webrtc::IceCandidateInterface> release();
52
53 private:
54  std::unique_ptr<webrtc::IceCandidateInterface> ice_candidate_;
55};
56
57std::shared_ptr<IceCandidate> create_ice_candidate(rust::String sdp_mid,
58                                                   int sdp_mline_index,
59                                                   rust::String sdp);
60
61static std::shared_ptr<IceCandidate> _shared_ice_candidate() {
62  return nullptr;  // Ignore
63}
64
65class SessionDescription {
66 public:
67  explicit SessionDescription(
68      std::unique_ptr<webrtc::SessionDescriptionInterface> session_description);
69
70  SdpType sdp_type() const;
71  rust::String stringify() const;
72  std::unique_ptr<SessionDescription> clone() const;
73  std::unique_ptr<webrtc::SessionDescriptionInterface> release();
74
75 private:
76  std::unique_ptr<webrtc::SessionDescriptionInterface> session_description_;
77};
78
79std::unique_ptr<SessionDescription> create_session_description(
80    SdpType type,
81    rust::String sdp);
82
83static std::unique_ptr<SessionDescription> _unique_session_description() {
84  return nullptr;  // Ignore
85}
86
87class NativeCreateSdpObserver
88    : public webrtc::CreateSessionDescriptionObserver {
89 public:
90  NativeCreateSdpObserver(
91      rust::Box<PeerContext> ctx,
92      rust::Fn<void(rust::Box<PeerContext> ctx,
93                    std::unique_ptr<SessionDescription>)> on_success,
94      rust::Fn<void(rust::Box<PeerContext> ctx, RtcError)> on_error);
95
96  void OnSuccess(webrtc::SessionDescriptionInterface* desc) override;
97  void OnFailure(webrtc::RTCError error) override;
98
99 private:
100  rust::Box<PeerContext> ctx_;
101  rust::Fn<void(rust::Box<PeerContext>, std::unique_ptr<SessionDescription>)>
102      on_success_;
103  rust::Fn<void(rust::Box<PeerContext>, RtcError)> on_error_;
104};
105
106class NativeSetLocalSdpObserver
107    : public webrtc::SetLocalDescriptionObserverInterface {
108 public:
109  NativeSetLocalSdpObserver(
110      rust::Box<PeerContext> ctx,
111      rust::Fn<void(rust::Box<PeerContext>, RtcError)> on_complete);
112
113  void OnSetLocalDescriptionComplete(webrtc::RTCError error) override;
114
115 private:
116  rust::Box<PeerContext> ctx_;
117  rust::Fn<void(rust::Box<PeerContext>, RtcError)> on_complete_;
118};
119
120class NativeSetRemoteSdpObserver
121    : public webrtc::SetRemoteDescriptionObserverInterface {
122 public:
123  NativeSetRemoteSdpObserver(
124      rust::Box<PeerContext> ctx,
125      rust::Fn<void(rust::Box<PeerContext>, RtcError)> on_complete);
126
127  void OnSetRemoteDescriptionComplete(webrtc::RTCError error) override;
128
129 private:
130  rust::Box<PeerContext> ctx_;
131  rust::Fn<void(rust::Box<PeerContext>, RtcError)> on_complete_;
132};
133
134template <class T>  // Context type
135class NativeRtcStatsCollector : public webrtc::RTCStatsCollectorCallback {
136 public:
137  NativeRtcStatsCollector(rust::Box<T> ctx,
138                          rust::Fn<void(rust::Box<T>, rust::String)> on_stats)
139      : ctx_(std::move(ctx)), on_stats_(on_stats) {}
140
141  void OnStatsDelivered(
142      const rtc::scoped_refptr<const webrtc::RTCStatsReport>& report) override {
143    on_stats_(std::move(ctx_), report->ToJson());
144  }
145
146 private:
147  rust::Box<T> ctx_;
148  rust::Fn<void(rust::Box<T>, rust::String)> on_stats_;
149};
150
151}  // namespace livekit