fork/rust-sdks

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

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

main
6.6 KiB202 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 <stdint.h>
20
21#include <memory>
22#include <string>
23#include <vector>
24
25#include "api/crypto/frame_crypto_transformer.h"
26#include "api/scoped_refptr.h"
27#include "livekit/peer_connection.h"
28#include "livekit/peer_connection_factory.h"
29#include "livekit/rtp_receiver.h"
30#include "livekit/rtp_sender.h"
31#include "livekit/webrtc.h"
32#include "rtc_base/synchronization/mutex.h"
33#include "rust/cxx.h"
34
35namespace livekit {
36
37struct KeyProviderOptions;
38enum class Algorithm : ::std::int32_t;
39class RtcFrameCryptorObserverWrapper;
40class NativeFrameCryptorObserver;
41
42/// Shared secret key for frame encryption.
43class KeyProvider {
44 public:
45  KeyProvider(KeyProviderOptions options);
46  ~KeyProvider() {}
47
48  bool set_shared_key(int32_t index, rust::Vec<::std::uint8_t> key) const {
49    std::vector<uint8_t> key_vec;
50    std::copy(key.begin(), key.end(), std::back_inserter(key_vec));
51    return impl_->SetSharedKey(index, key_vec);
52  }
53
54  rust::Vec<::std::uint8_t> ratchet_shared_key(int32_t key_index) const {
55    rust::Vec<uint8_t> vec;
56    auto data = impl_->RatchetSharedKey(key_index);
57    if (data.empty()) {
58      throw std::runtime_error("ratchet_shared_key failed");
59    }
60
61    std::move(data.begin(), data.end(), std::back_inserter(vec));
62    return vec;
63  }
64
65  rust::Vec<::std::uint8_t> get_shared_key(int32_t key_index) const {
66    rust::Vec<uint8_t> vec;
67    auto data = impl_->ExportSharedKey(key_index);
68    if (data.empty()) {
69      throw std::runtime_error("get_shared_key failed");
70    }
71
72    std::move(data.begin(), data.end(), std::back_inserter(vec));
73    return vec;
74  }
75
76  /// Set the key at the given index.
77  bool set_key(const ::rust::String participant_id,
78               int32_t index,
79               rust::Vec<::std::uint8_t> key) const {
80    std::vector<uint8_t> key_vec;
81    std::copy(key.begin(), key.end(), std::back_inserter(key_vec));
82    return impl_->SetKey(
83        std::string(participant_id.data(), participant_id.size()), index,
84        key_vec);
85  }
86
87  rust::Vec<::std::uint8_t> ratchet_key(const ::rust::String participant_id,
88                                        int32_t key_index) const {
89    rust::Vec<uint8_t> vec;
90    auto data = impl_->RatchetKey(
91        std::string(participant_id.data(), participant_id.size()), key_index);
92    if (data.empty()) {
93      throw std::runtime_error("ratchet_key failed");
94    }
95
96    std::move(data.begin(), data.end(), std::back_inserter(vec));
97    return vec;
98  }
99
100  rust::Vec<::std::uint8_t> get_key(const ::rust::String participant_id,
101                                    int32_t key_index) const {
102    rust::Vec<uint8_t> vec;
103    auto data = impl_->ExportKey(
104        std::string(participant_id.data(), participant_id.size()), key_index);
105    if (data.empty()) {
106      throw std::runtime_error("get_key failed");
107    }
108
109    std::move(data.begin(), data.end(), std::back_inserter(vec));
110    return vec;
111  }
112
113  void set_sif_trailer(rust::Vec<::std::uint8_t> trailer) const {
114    std::vector<uint8_t> trailer_vec;
115    std::copy(trailer.begin(), trailer.end(), std::back_inserter(trailer_vec));
116    impl_->SetSifTrailer(trailer_vec);
117  }
118
119  rtc::scoped_refptr<webrtc::KeyProvider> rtc_key_provider() { return impl_; }
120
121 private:
122  rtc::scoped_refptr<webrtc::DefaultKeyProviderImpl> impl_;
123};
124
125class FrameCryptor {
126 public:
127  FrameCryptor(std::shared_ptr<RtcRuntime> rtc_runtime,
128               const std::string participant_id,
129               webrtc::FrameCryptorTransformer::Algorithm algorithm,
130               rtc::scoped_refptr<webrtc::KeyProvider> key_provider,
131               rtc::scoped_refptr<webrtc::RtpSenderInterface> sender);
132
133  FrameCryptor(std::shared_ptr<RtcRuntime> rtc_runtime,
134               const std::string participant_id,
135               webrtc::FrameCryptorTransformer::Algorithm algorithm,
136               rtc::scoped_refptr<webrtc::KeyProvider> key_provider,
137               rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver);
138  ~FrameCryptor();
139
140  /// Enable/Disable frame crypto for the sender or receiver.
141  void set_enabled(bool enabled) const;
142
143  /// Get the enabled state for the sender or receiver.
144  bool enabled() const;
145
146  /// Set the key index for the sender or receiver.
147  /// If the key index is not set, the key index will be set to 0.
148  void set_key_index(int32_t index) const;
149
150  /// Get the key index for the sender or receiver.
151  int32_t key_index() const;
152
153  rust::String participant_id() const { return participant_id_; }
154
155  void register_observer(
156      rust::Box<RtcFrameCryptorObserverWrapper> observer) const;
157
158  void unregister_observer() const;
159
160 private:
161  std::shared_ptr<RtcRuntime> rtc_runtime_;
162  const rust::String participant_id_;
163  mutable webrtc::Mutex mutex_;
164  rtc::scoped_refptr<webrtc::FrameCryptorTransformer> e2ee_transformer_;
165  rtc::scoped_refptr<webrtc::KeyProvider> key_provider_;
166  rtc::scoped_refptr<webrtc::RtpSenderInterface> sender_;
167  rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver_;
168  mutable rtc::scoped_refptr<NativeFrameCryptorObserver> observer_;
169};
170
171class NativeFrameCryptorObserver
172    : public webrtc::FrameCryptorTransformerObserver {
173 public:
174  NativeFrameCryptorObserver(rust::Box<RtcFrameCryptorObserverWrapper> observer,
175                             const FrameCryptor* fc);
176  ~NativeFrameCryptorObserver();
177
178  void OnFrameCryptionStateChanged(const std::string participant_id,
179                                   webrtc::FrameCryptionState error) override;
180
181 private:
182  rust::Box<RtcFrameCryptorObserverWrapper> observer_;
183  const FrameCryptor* fc_;
184};
185
186std::shared_ptr<FrameCryptor> new_frame_cryptor_for_rtp_sender(
187    std::shared_ptr<PeerConnectionFactory> peer_factory,
188    const ::rust::String participant_id,
189    Algorithm algorithm,
190    std::shared_ptr<KeyProvider> key_provider,
191    std::shared_ptr<RtpSender> sender);
192
193std::shared_ptr<FrameCryptor> new_frame_cryptor_for_rtp_receiver(
194    std::shared_ptr<PeerConnectionFactory> peer_factory,
195    const ::rust::String participant_id,
196    Algorithm algorithm,
197    std::shared_ptr<KeyProvider> key_provider,
198    std::shared_ptr<RtpReceiver> receiver);
199
200std::shared_ptr<KeyProvider> new_key_provider(KeyProviderOptions options);
201
202}  // namespace livekit