fork/rust-sdks

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

Théo Monnomfix: datachannel deadlocks & dispose crashes (#87)33dfcb2

main
3.7 KiB116 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/media_stream_interface.h"
22#include "api/rtp_receiver_interface.h"
23#include "api/rtp_sender_interface.h"
24#include "livekit/helper.h"
25#include "rtc_base/logging.h"
26#include "rtc_base/physical_socket_server.h"
27#include "rtc_base/ssl_adapter.h"
28#include "rust/cxx.h"
29
30#ifdef WEBRTC_WIN
31#include "rtc_base/win32_socket_init.h"
32#endif
33
34namespace livekit {
35class RtcRuntime;
36class LogSink;
37}  // namespace livekit
38#include "webrtc-sys/src/webrtc.rs.h"
39
40namespace livekit {
41
42class MediaStreamTrack;
43class RtpReceiver;
44class RtpSender;
45
46// Using a shared_ptr in RtcRuntime allows us to keep a strong reference to it
47// on resources that depend on it. (e.g: AudioTrack, VideoTrack).
48class RtcRuntime : public std::enable_shared_from_this<RtcRuntime> {
49 public:
50  [[nodiscard]] static std::shared_ptr<RtcRuntime> create() {
51    return std::shared_ptr<RtcRuntime>(new RtcRuntime());
52  }
53
54  RtcRuntime(const RtcRuntime&) = delete;
55  RtcRuntime& operator=(const RtcRuntime&) = delete;
56  ~RtcRuntime();
57
58  rtc::Thread* network_thread() const;
59  rtc::Thread* worker_thread() const;
60  rtc::Thread* signaling_thread() const;
61
62  std::shared_ptr<MediaStreamTrack> get_or_create_media_stream_track(
63      rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track);
64
65  std::shared_ptr<AudioTrack> get_or_create_audio_track(
66      rtc::scoped_refptr<webrtc::AudioTrackInterface> track);
67
68  std::shared_ptr<VideoTrack> get_or_create_video_track(
69      rtc::scoped_refptr<webrtc::VideoTrackInterface> track);
70
71 private:
72  RtcRuntime();
73
74  std::unique_ptr<rtc::Thread> network_thread_;
75  std::unique_ptr<rtc::Thread> worker_thread_;
76  std::unique_ptr<rtc::Thread> signaling_thread_;
77
78  // Lists used to make sure we don't create multiple wrappers for one
79  // underlying webrtc object. (e.g: webrtc::VideoTrackInterface should only
80  // have one livekit::VideoTrack associated with it).
81  // The only reason we to do that is to allow to add states inside our
82  // wrappers (e.g: the sinks_ member inside AudioTrack)
83  // DataChannel and the PeerConnectionFactory don't need to do this (There's no
84  // way to retrieve them after creation)
85  webrtc::Mutex mutex_;
86  std::vector<std::weak_ptr<MediaStreamTrack>> media_stream_tracks_;
87  // We don't have additonal state in RtpReceiver and RtpSender atm..
88  // std::vector<std::weak_ptr<RtpReceiver>> rtp_receivers_;
89  // std::vector<std::weak_ptr<RtpSender>> rtp_senders_;
90
91#ifdef WEBRTC_WIN
92  // rtc::WinsockInitializer winsock_;
93  // rtc::PhysicalSocketServer ss_;
94  // rtc::AutoSocketServerThread main_thread_{&ss_};
95#endif
96};
97
98class LogSink : public rtc::LogSink {
99 public:
100  LogSink(rust::Fn<void(rust::String message, LoggingSeverity severity)> fnc);
101  ~LogSink();
102
103  void OnLogMessage(const std::string& message,
104                    rtc::LoggingSeverity severity) override;
105  void OnLogMessage(const std::string& message) override {}
106
107 private:
108  rust::Fn<void(rust::String message, LoggingSeverity severity)> fnc_;
109};
110
111std::unique_ptr<LogSink> new_log_sink(
112    rust::Fn<void(rust::String, LoggingSeverity)> fnc);
113
114rust::String create_random_uuid();
115
116}  // namespace livekit