Debian Codegraph @

src/dpp/voice/enabled/read_ready.cpp

raw symbols SCIP
1 /************************************************************************************
2 *
3 * D++, A Lightweight C++ library for Discord
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 * Copyright 2021 Craig Edwards and D++ contributors
7 * (https://github.com/brainboxdotcc/DPP/graphs/contributors)
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 ************************************************************************************/
22
23 #include <chrono>
24 #include <string_view>
25 #include <dpp/exception.h>
26 #include <dpp/isa_detection.h>
27 #include <dpp/discordvoiceclient.h>
28
29 #include <opus/opus.h>
30 #include "../../dave/decryptor.h"
31
32 #include "enabled.h"
33
34 namespace dpp {
35
36 void discord_voice_client::read_ready()
37 {
38 uint8_t buffer[65535];
39 int packet_size = this->udp_recv(reinterpret_cast<char*>(buffer), sizeof(buffer));
40
41 bool receive_handler_is_empty = creator->on_voice_receive.empty() && creator->on_voice_receive_combined.empty();
42 if (packet_size <= 0 || receive_handler_is_empty) {
43 /* Nothing to do */
44 return;
45 }
46
47 constexpr size_t header_size = 12;
48 if (static_cast<size_t>(packet_size) < header_size) {
49 /* Invalid RTP payload */
50 return;
51 }
52
53 /* It's a "silence packet" - throw it away. */
54 if (packet_size < 44) {
55 return;
56 }
57
58 if (uint8_t payload_type = buffer[1] & 0b0111'1111;
59 72 <= payload_type && payload_type <= 76) {
60 /*
61 * This is an RTCP payload. Discord is known to send
62 * RTCP Receiver Reports.
63 *
64 * See https://datatracker.ietf.org/doc/html/rfc3551#section-6
65 */
66 return;
67 }
68
69 voice_payload vp{0, // seq, populate later
70 0, // timestamp, populate later
71 std::make_unique<voice_receive_t>(owner, 0, std::string(reinterpret_cast<char*>(buffer), packet_size))};
72
73 vp.vr->voice_client = this;
74
75 uint32_t speaker_ssrc;
76 { /* Get the User ID of the speaker */
77 std::memcpy(&speaker_ssrc, &buffer[8], sizeof(uint32_t));
78 speaker_ssrc = ntohl(speaker_ssrc);
79 vp.vr->user_id = ssrc_map[speaker_ssrc];
80 }
81
82 /* Get the sequence number of the voice UDP packet */
83 std::memcpy(&vp.seq, &buffer[2], sizeof(rtp_seq_t));
84 vp.seq = ntohs(vp.seq);
85
86 /* Get the timestamp of the voice UDP packet */
87 std::memcpy(&vp.timestamp, &buffer[4], sizeof(rtp_timestamp_t));
88 vp.timestamp = ntohl(vp.timestamp);
89
90 vp.vr->audio_data.assign(buffer, buffer + packet_size);
91
92 {
93 std::lock_guard lk(voice_courier_shared_state.mtx);
94 auto& [range, payload_queue, pending_decoder_ctls, decoder] = voice_courier_shared_state.parked_voice_payloads[vp.vr->user_id];
95
96 if (!decoder) {
97 /*
98 * Most likely this is the first time we encounter this speaker.
99 * Do some initialization for not only the decoder but also the range.
100 */
101 range.min_seq = vp.seq;
102 range.min_timestamp = vp.timestamp;
103
104 int opus_error = 0;
105 decoder.reset(opus_decoder_create(opus_sample_rate_hz, opus_channel_count, &opus_error),
106 &opus_decoder_destroy);
107 if (opus_error) {
108 /**
109 * NOTE: The -10 here makes the opus_error match up with values of exception_error_code,
110 * which would otherwise conflict as every C library loves to use values from -1 downwards.
111 */
112 throw dpp::voice_exception((exception_error_code)(opus_error - 10), "discord_voice_client::discord_voice_client; opus_decoder_create() failed");
113 }
114 }
115
116 if (vp.seq < range.min_seq && vp.timestamp < range.min_timestamp) {
117 /* This packet arrived too late. We can only discard it. */
118 return;
119 }
120 range.max_seq = vp.seq;
121 range.max_timestamp = vp.timestamp;
122 payload_queue.push(std::move(vp));
123 }
124
125 voice_courier_shared_state.signal_iteration.notify_one();
126
127 if (!voice_courier.joinable()) {
128 /* Courier thread is not running, start it */
129 voice_courier = std::thread(&voice_courier_loop, std::ref(*this), std::ref(voice_courier_shared_state));
130 }
131 }
132
133 }