BlueGit

ict rusty-teams

Files

rusty-teams / vendor / webrtc-sys / src / data_channel.cpp

data_channel.cpp

data_channel.cpp 3.4 KiB Download Raw
1/*
2 * Copyright 2025 LiveKit, Inc.
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/data_channel.h"
18
19#include <utility>
20
21#include "rtc_base/synchronization/mutex.h"
22#include "webrtc-sys/src/data_channel.rs.h"
23
24namespace livekit_ffi {
25
26webrtc::DataChannelInit to_native_data_channel_init(DataChannelInit init) {
27 webrtc::DataChannelInit rtc_init{};
28 rtc_init.id = init.id;
29 rtc_init.negotiated = init.negotiated;
30 rtc_init.ordered = init.ordered;
31 rtc_init.protocol = init.protocol.c_str();
32
33 if (init.has_max_retransmit_time)
34 rtc_init.maxRetransmitTime = init.max_retransmit_time;
35
36 if (init.has_max_retransmits)
37 rtc_init.maxRetransmits = init.max_retransmits;
38
39 if (init.has_priority)
40 rtc_init.priority = webrtc::PriorityValue(static_cast<webrtc::Priority>(init.priority));
41
42 return rtc_init;
43}
44
45DataChannel::DataChannel(
46 std::shared_ptr<RtcRuntime> rtc_runtime,
47 webrtc::scoped_refptr<webrtc::DataChannelInterface> data_channel)
48 : rtc_runtime_(rtc_runtime), data_channel_(std::move(data_channel)) {
49 RTC_LOG(LS_VERBOSE) << "DataChannel::DataChannel()";
50}
51
52DataChannel::~DataChannel() {
53 RTC_LOG(LS_VERBOSE) << "DataChannel::~DataChannel()";
54 unregister_observer();
55}
56
57void DataChannel::register_observer(
58 rust::Box<DataChannelObserverWrapper> observer) const {
59 webrtc::MutexLock lock(&mutex_);
60
61 data_channel_->UnregisterObserver();
62
63 observer_ =
64 std::make_unique<NativeDataChannelObserver>(std::move(observer), this);
65 data_channel_->RegisterObserver(observer_.get());
66}
67
68void DataChannel::unregister_observer() const {
69 webrtc::MutexLock lock(&mutex_);
70 data_channel_->UnregisterObserver();
71 observer_ = nullptr;
72}
73
74bool DataChannel::send(const DataBuffer& buffer) const {
75 return data_channel_->Send(webrtc::DataBuffer{
76 webrtc::CopyOnWriteBuffer(buffer.ptr, buffer.len), buffer.binary});
77}
78
79int DataChannel::id() const {
80 return data_channel_->id();
81}
82
83rust::String DataChannel::label() const {
84 return data_channel_->label();
85}
86
87DataState DataChannel::state() const {
88 return static_cast<DataState>(data_channel_->state());
89}
90
91void DataChannel::close() const {
92 return data_channel_->Close();
93}
94
95uint64_t DataChannel::buffered_amount() const {
96 return data_channel_->buffered_amount();
97}
98
99NativeDataChannelObserver::NativeDataChannelObserver(
100 rust::Box<DataChannelObserverWrapper> observer,
101 const DataChannel* dc)
102 : observer_(std::move(observer)), dc_(dc) {}
103
104void NativeDataChannelObserver::OnStateChange() {
105 observer_->on_state_change(dc_->state());
106}
107
108void NativeDataChannelObserver::OnMessage(const webrtc::DataBuffer& buffer) {
109 DataBuffer data{};
110 data.ptr = buffer.data.data();
111 data.len = buffer.data.size();
112 data.binary = buffer.binary;
113 observer_->on_message(data);
114}
115
116void NativeDataChannelObserver::OnBufferedAmountChange(
117 uint64_t sent_data_size) {
118 observer_->on_buffered_amount_change(sent_data_size);
119}
120
121} // namespace livekit_ffi