forked from bkacjios/lua-mumble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio.c
132 lines (114 loc) · 3.4 KB
/
audio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "mumble.h"
int util_set_varint(uint8_t buffer[], const uint64_t value)
{
if (value < 0x80) {
buffer[0] = value;
return 1;
} else if (value < 0x4000) {
buffer[0] = (value >> 8) | 0x80;
buffer[1] = value & 0xFF;
return 2;
}
return -1;
}
#define VOICEPACKET_NORMAL 0
#define VOICEPACKET_OPUS 4
VoicePacket * voicepacket_init(VoicePacket *packet, uint8_t *buffer)
{
if (packet == NULL || buffer == NULL) {
return NULL;
}
packet->buffer = buffer;
packet->length = 0;
packet->header_length = 0;
return packet;
}
int voicepacket_setheader(VoicePacket *packet, const uint8_t type, const uint8_t target, const uint32_t sequence)
{
int offset;
if (packet == NULL) {
return -1;
}
if (packet->length > 0) {
return -2;
}
packet->buffer[0] = ((type & 0x7) << 5) | (target & 0x1F);
offset = util_set_varint(packet->buffer + 1, sequence);
packet->length = packet->header_length = 1 + offset;
return 1;
}
int voicepacket_setframe(VoicePacket *packet, const uint16_t length, uint8_t *buffer)
{
int offset;
if (packet == NULL || buffer == NULL || length <= 0 || length >= 0x2000) {
return -1;
}
if (packet->header_length <= 0) {
return -2;
}
offset = util_set_varint(packet->buffer + packet->header_length, length);
if (offset <= 0) {
return -3;
}
memmove(packet->buffer + packet->header_length + offset, buffer, length);
packet->length = packet->header_length + length + offset;
return 1;
}
int voicepacket_getlength(const VoicePacket *packet)
{
if (packet == NULL) {
return -1;
}
return packet->length;
}
static void audio_transmission_event_filter(float **pcm, long channels, long samples, void *param) {
AudioTransmission *sound = (AudioTransmission *)param;
int channel, sample;
for (channel = 0; channel < channels; channel++) {
for (sample = 0; sample < samples; sample++) {
pcm[channel][sample] *= sound->volume * sound->client->volume;
}
}
}
void audio_transmission_stop(MumbleClient *client)
{
if (client->audio_job != NULL) {
client->audio_job = NULL;
client->audio_finished = true;
}
AudioTransmission *sound = client->audio_job;
if (sound == NULL)
return;
ov_clear(&sound->ogg);
fclose(sound->file);
free(sound);
}
void audio_transmission_event(MumbleClient *client)
{
AudioTransmission *sound = client->audio_job;
VoicePacket packet;
uint8_t packet_buffer[PCM_BUFFER];
uint8_t output[PCM_BUFFER];
opus_int32 byte_count;
long long_ret;
voicepacket_init(&packet, packet_buffer);
voicepacket_setheader(&packet, VOICEPACKET_OPUS, client->audio_target, sound->sequence);
while (sound->buffer.size < OPUS_FRAME_SIZE * sizeof(opus_int16)) {
long_ret = ov_read_filter(&sound->ogg, sound->buffer.pcm + sound->buffer.size, PCM_BUFFER - sound->buffer.size, 0, 2, 1, NULL, audio_transmission_event_filter, sound);
if (long_ret <= 0) {
audio_transmission_stop(client);
return;
}
sound->buffer.size += long_ret;
}
byte_count = opus_encode(client->encoder, (opus_int16 *)sound->buffer.pcm, OPUS_FRAME_SIZE, output, sizeof(output));
if (byte_count < 0) {
audio_transmission_stop(client);
return;
}
sound->buffer.size -= OPUS_FRAME_SIZE * sizeof(opus_int16);
memmove(sound->buffer.pcm, sound->buffer.pcm + OPUS_FRAME_SIZE * sizeof(opus_int16), sound->buffer.size);
voicepacket_setframe(&packet, byte_count, output);
packet_sendex(client, PACKET_UDPTUNNEL, packet_buffer, voicepacket_getlength(&packet));
sound->sequence = (sound->sequence + 1) % 100000;
}