-
Notifications
You must be signed in to change notification settings - Fork 7
/
audio_packet.c
134 lines (119 loc) · 4.21 KB
/
audio_packet.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
133
134
/*
* soliloque-server, an open source implementation of the TeamSpeak protocol.
* Copyright (C) 2009 Hugo Camboulive <hugo.camboulive AT gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "audio_packet.h"
#include "player.h"
#include "server.h"
#include "channel.h"
#include "array.h"
#include "server_stat.h"
#include "log.h"
#include <inttypes.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <errno.h>
#include <assert.h>
/** The size of the raw audio block (in bytes) */
size_t codec_audio_size[13] = {153, 51, 165, 132, 0, 27, 50, 75, 100, 138, 188, 228, 308};
/** The number of frames contained in one block */
size_t codec_nb_frames[13] = { 9, 3, 5, 4, 0, 5, 5, 5, 5, 5, 5, 5, 5};
/** The offset of the audio block after the 16 bytes of header */
size_t codec_offset[13] = {6, 6, 6, 6, 0, 1, 1, 1, 1, 1, 1, 1, 1};
/**
* Handle a received audio packet by sending its audio
* block to all the players in the same channel.
*
* @param in the received packet
* @param len size of the received packet
*
* @return 0 on success, -1 on failure.
*/
int audio_received(char *in, size_t len, struct server *s)
{
uint32_t pub_id, priv_id;
uint8_t data_codec;
struct player *sender;
struct channel *ch_in;
struct player *tmp_pl;
size_t data_size, audio_block_size, expected_size;
ssize_t err;
size_t iter;
char *data, *ptr, *ptrin;
ptrin = in;
ptrin += 3;
data_codec = ru8(&ptrin);
priv_id = ru32(&ptrin);
pub_id = ru32(&ptrin);
sender = get_player_by_ids(s, pub_id, priv_id);
if (sender != NULL) {
sender->stats->activ_time = time(NULL); /* update */
ch_in = sender->in_chan;
/* Security checks */
if (data_codec != ch_in->codec) {
logger(LOG_ERR, "Player sent a wrong codec ID : %" PRIu8 ", expected : %" PRIu8 ".", data_codec, ch_in->codec);
return -1;
}
audio_block_size = codec_offset[(int)data_codec] + codec_audio_size[(int)data_codec];
expected_size = 16 + audio_block_size;
if (len != expected_size) {
logger(LOG_ERR, "Audio packet's size is incorrect : %zu bytes, expected : %zu.", len,
expected_size);
return -1;
}
/* Initialize the packet we want to send */
data_size = len + 6; /* we will add the id of player sending */
data = (char *)calloc(data_size, sizeof(char));
if (data == NULL) {
logger(LOG_WARN, "audio_received, could not allocate packet : %s.", strerror(errno));
return -1;
}
ptr = data;
wu16(0xbef3, &ptr); /* function code */
/* 1 byte empty */ ptr += 1; /* NULL */
wu8(ch_in->codec, &ptr); /* codec */
/* private ID */ ptr += 4; /* empty yet */
/* public ID */ ptr += 4; /* empty yet */
wu16(0, &ptr); /* unknown, maybe server conversation ID? */
wu16(*(uint16_t *)(in + 14), &ptr); /* counter */
wu32(sender->public_id, &ptr); /* ID of sender */
wu16(*(uint16_t *)(in + 12), &ptr); /* conversation counter */
/* audio data */
memcpy(ptr, in + 16, audio_block_size);
ptr += audio_block_size;
/* assert we filled the whole packet */
assert((ptr - data) == data_size);
ar_each(struct player *, tmp_pl, iter, ch_in->players)
if (tmp_pl != sender && !ar_has(tmp_pl->muted, sender)) {
ptr = data + 4;
wu32(tmp_pl->private_id, &ptr);
wu32(tmp_pl->public_id, &ptr);
err = sendto(sender->in_chan->in_server->socket_desc, data, data_size, 0,
(struct sockaddr *)tmp_pl->cli_addr, tmp_pl->cli_len);
if (err == -1) {
logger(LOG_WARN, "audio_received, could not send packet : %s.", strerror(errno));
}
}
ar_end_each;
free(data);
return 0;
} else {
logger(LOG_ERR, "Wrong public/private ID pair : %x/%x.", pub_id, priv_id);
return -1;
}
}