Skip to content

Commit

Permalink
split off more stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed Oct 3, 2024
1 parent f5a1396 commit 5c34c87
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 86 deletions.
86 changes: 0 additions & 86 deletions src/dpp/discordvoiceclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,6 @@ bool discord_voice_client::is_playing() {
return (!this->outbuf.empty());
}

int discord_voice_client::udp_send(const char* data, size_t length)
{
sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(this->port);
servaddr.sin_addr.s_addr = inet_addr(this->ip.c_str());
return (int) sendto(this->fd, data, (int)length, 0, (const sockaddr*)&servaddr, (int)sizeof(sockaddr_in));
}

int discord_voice_client::udp_recv(char* data, size_t max_length)
{
return (int) recv(this->fd, data, (int)max_length, 0);
}

uint16_t dave_binary_header_t::get_welcome_transition_id() const {
uint16_t transition{0};
std::memcpy(&transition, package, sizeof(uint16_t));
Expand Down Expand Up @@ -181,14 +166,6 @@ discord_voice_client& discord_voice_client::stop_audio() {
return *this;
}

void discord_voice_client::send(const char* packet, size_t len, uint64_t duration) {
std::lock_guard<std::mutex> lock(this->stream_mutex);
voice_out_packet frame;
frame.packet = std::string(packet, len);
frame.duration = duration;
outbuf.emplace_back(frame);
}

dpp::utility::uptime discord_voice_client::get_uptime()
{
return dpp::utility::uptime(time(nullptr) - connect_time);
Expand All @@ -199,19 +176,6 @@ bool discord_voice_client::is_connected()
return (this->get_state() == CONNECTED);
}

dpp::socket discord_voice_client::want_write() {
std::lock_guard<std::mutex> lock(this->stream_mutex);
if (!this->paused && !outbuf.empty()) {
return fd;
} else {
return INVALID_SOCKET;
}
}

dpp::socket discord_voice_client::want_read() {
return fd;
}

void discord_voice_client::error(uint32_t errorcode)
{
const static std::map<uint32_t, std::string> errortext = {
Expand Down Expand Up @@ -368,56 +332,6 @@ void discord_voice_client::one_second_timer()
}
}

size_t discord_voice_client::encode(uint8_t *input, size_t inDataSize, uint8_t *output, size_t &outDataSize)
{
#if HAVE_VOICE
outDataSize = 0;
int mEncFrameBytes = 11520;
int mEncFrameSize = 2880;
if (0 == (inDataSize % mEncFrameBytes)) {
bool isOk = true;
uint8_t *out = encode_buffer;

memset(out, 0, sizeof(encode_buffer));
repacketizer = opus_repacketizer_init(repacketizer);
if (!repacketizer) {
log(ll_warning, "opus_repacketizer_init(): failure");
return outDataSize;
}
for (size_t i = 0; i < (inDataSize / mEncFrameBytes); ++ i) {
const opus_int16* pcm = (opus_int16*)(input + i * mEncFrameBytes);
int ret = opus_encode(encoder, pcm, mEncFrameSize, out, 65536);
if (ret > 0) {
int retval = opus_repacketizer_cat(repacketizer, out, ret);
if (retval != OPUS_OK) {
isOk = false;
log(ll_warning, "opus_repacketizer_cat(): " + std::string(opus_strerror(retval)));
break;
}
out += ret;
} else {
isOk = false;
log(ll_warning, "opus_encode(): " + std::string(opus_strerror(ret)));
break;
}
}
if (isOk) {
int ret = opus_repacketizer_out(repacketizer, output, 65536);
if (ret > 0) {
outDataSize = ret;
} else {
log(ll_warning, "opus_repacketizer_out(): " + std::string(opus_strerror(ret)));
}
}
} else {
throw dpp::voice_exception(err_invalid_voice_packet_length, "Invalid input data length: " + std::to_string(inDataSize) + ", must be n times of " + std::to_string(mEncFrameBytes));
}
#else
throw dpp::voice_exception(err_no_voice_support, "Voice support not enabled in this build of D++");
#endif
return outDataSize;
}

discord_voice_client& discord_voice_client::insert_marker(const std::string& metadata) {
/* Insert a track marker. A track marker is a single 16 bit value of 0xFFFF.
* This is too small to be a valid RTP packet so the send function knows not
Expand Down
46 changes: 46 additions & 0 deletions src/dpp/voice/enabled/opus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,50 @@ discord_voice_client& discord_voice_client::send_audio_opus(uint8_t* opus_packet
return *this;
}

size_t discord_voice_client::encode(uint8_t *input, size_t inDataSize, uint8_t *output, size_t &outDataSize) {
outDataSize = 0;
int mEncFrameBytes = 11520;
int mEncFrameSize = 2880;
if (0 == (inDataSize % mEncFrameBytes)) {
bool isOk = true;
uint8_t *out = encode_buffer;

memset(out, 0, sizeof(encode_buffer));
repacketizer = opus_repacketizer_init(repacketizer);
if (!repacketizer) {
log(ll_warning, "opus_repacketizer_init(): failure");
return outDataSize;
}
for (size_t i = 0; i < (inDataSize / mEncFrameBytes); ++ i) {
const opus_int16* pcm = (opus_int16*)(input + i * mEncFrameBytes);
int ret = opus_encode(encoder, pcm, mEncFrameSize, out, 65536);
if (ret > 0) {
int retval = opus_repacketizer_cat(repacketizer, out, ret);
if (retval != OPUS_OK) {
isOk = false;
log(ll_warning, "opus_repacketizer_cat(): " + std::string(opus_strerror(retval)));
break;
}
out += ret;
} else {
isOk = false;
log(ll_warning, "opus_encode(): " + std::string(opus_strerror(ret)));
break;
}
}
if (isOk) {
int ret = opus_repacketizer_out(repacketizer, output, 65536);
if (ret > 0) {
outDataSize = ret;
} else {
log(ll_warning, "opus_repacketizer_out(): " + std::string(opus_strerror(ret)));
}
}
} else {
throw dpp::voice_exception(err_invalid_voice_packet_length, "Invalid input data length: " + std::to_string(inDataSize) + ", must be n times of " + std::to_string(mEncFrameBytes));
}
return outDataSize;
}


}
73 changes: 73 additions & 0 deletions src/dpp/voice/enabled/read_write.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/************************************************************************************
*
* D++, A Lightweight C++ library for Discord
*
* SPDX-License-Identifier: Apache-2.0
* Copyright 2021 Craig Edwards and D++ contributors
* (https://github.com/brainboxdotcc/DPP/graphs/contributors)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
************************************************************************************/

#include <string_view>
#include <fstream>
#include <dpp/exception.h>
#include <dpp/isa_detection.h>
#include <dpp/discordvoiceclient.h>

#include <opus/opus.h>
#include "../../dave/encryptor.h"

#include "enabled.h"

namespace dpp {

dpp::socket discord_voice_client::want_write() {
std::lock_guard<std::mutex> lock(this->stream_mutex);
if (!this->paused && !outbuf.empty()) {
return fd;
} else {
return INVALID_SOCKET;
}
}

dpp::socket discord_voice_client::want_read() {
return fd;
}


void discord_voice_client::send(const char* packet, size_t len, uint64_t duration) {
std::lock_guard<std::mutex> lock(this->stream_mutex);
voice_out_packet frame;
frame.packet = std::string(packet, len);
frame.duration = duration;
outbuf.emplace_back(frame);
}

int discord_voice_client::udp_send(const char* data, size_t length) {
sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(this->port);
servaddr.sin_addr.s_addr = inet_addr(this->ip.c_str());
return (int) sendto(this->fd, data, (int)length, 0, (const sockaddr*)&servaddr, (int)sizeof(sockaddr_in));
}

int discord_voice_client::udp_recv(char* data, size_t max_length)
{
return (int) recv(this->fd, data, (int)max_length, 0);
}


}
24 changes: 24 additions & 0 deletions src/dpp/voice/stub/stubs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,28 @@ namespace dpp {
return *this;
}

dpp::socket discord_voice_client::want_write() {
return INVALID_SOCKET;
}

dpp::socket discord_voice_client::want_read() {
return INVALID_SOCKET;
}


void discord_voice_client::send(const char* packet, size_t len, uint64_t duration) {
}

int discord_voice_client::udp_send(const char* data, size_t length) {
return -1;
}

int discord_voice_client::udp_recv(char* data, size_t max_length) {
return -1;
}

size_t discord_voice_client::encode(uint8_t *input, size_t inDataSize, uint8_t *output, size_t &outDataSize) {
return 0;
}

}

0 comments on commit 5c34c87

Please sign in to comment.