diff options
author | irungentoo <irungentoo@gmail.com> | 2014-11-09 20:21:57 -0500 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2014-11-09 20:21:57 -0500 |
commit | 5715a94061a2f585dbeb2dc18764eea41e3290aa (patch) | |
tree | c597e89610b90e02e4a53a6d303e9287d164ff4a /toxav | |
parent | bbbeecbaa0d0093ba140bb827a51dd4902721f27 (diff) |
Basic audio packet parsing code.
Diffstat (limited to 'toxav')
-rw-r--r-- | toxav/group.c | 55 |
1 files changed, 49 insertions, 6 deletions
diff --git a/toxav/group.c b/toxav/group.c index e165cb77..f5707aa6 100644 --- a/toxav/group.c +++ b/toxav/group.c | |||
@@ -80,7 +80,9 @@ static void terminate_queue(Group_JitterBuffer *q) | |||
80 | free(q); | 80 | free(q); |
81 | } | 81 | } |
82 | 82 | ||
83 | static void queue(Group_JitterBuffer *q, Group_Audio_Packet *pk) | 83 | /* Return 0 if packet was queued, -1 if it wasn't. |
84 | */ | ||
85 | static int queue(Group_JitterBuffer *q, Group_Audio_Packet *pk) | ||
84 | { | 86 | { |
85 | uint16_t sequnum = pk->sequnum; | 87 | uint16_t sequnum = pk->sequnum; |
86 | 88 | ||
@@ -91,16 +93,18 @@ static void queue(Group_JitterBuffer *q, Group_Audio_Packet *pk) | |||
91 | q->bottom = sequnum; | 93 | q->bottom = sequnum; |
92 | q->queue[num] = pk; | 94 | q->queue[num] = pk; |
93 | q->top = sequnum + 1; | 95 | q->top = sequnum + 1; |
94 | return; | 96 | return 0; |
95 | } | 97 | } |
96 | 98 | ||
97 | if (q->queue[num]) | 99 | if (q->queue[num]) |
98 | return; | 100 | return -1; |
99 | 101 | ||
100 | q->queue[num] = pk; | 102 | q->queue[num] = pk; |
101 | 103 | ||
102 | if ((sequnum - q->bottom) >= (q->top - q->bottom)) | 104 | if ((sequnum - q->bottom) >= (q->top - q->bottom)) |
103 | q->top = sequnum + 1; | 105 | q->top = sequnum + 1; |
106 | |||
107 | return 0; | ||
104 | } | 108 | } |
105 | 109 | ||
106 | /* success is 0 when there is nothing to dequeue, 1 when there's a good packet, 2 when there's a lost packet */ | 110 | /* success is 0 when there is nothing to dequeue, 1 when there's a good packet, 2 when there's a lost packet */ |
@@ -136,6 +140,8 @@ typedef struct { | |||
136 | OpusEncoder *audio_encoder; | 140 | OpusEncoder *audio_encoder; |
137 | 141 | ||
138 | unsigned int audio_channels, audio_sample_rate, audio_bitrate; | 142 | unsigned int audio_channels, audio_sample_rate, audio_bitrate; |
143 | |||
144 | uint16_t audio_sequnum; | ||
139 | } Group_AV; | 145 | } Group_AV; |
140 | 146 | ||
141 | typedef struct { | 147 | typedef struct { |
@@ -214,13 +220,27 @@ static void group_av_peer_delete(void *object, int groupnumber, int friendgroupn | |||
214 | static int handle_group_audio_packet(void *object, int groupnumber, int friendgroupnumber, void *peer_object, | 220 | static int handle_group_audio_packet(void *object, int groupnumber, int friendgroupnumber, void *peer_object, |
215 | const uint8_t *packet, uint16_t length) | 221 | const uint8_t *packet, uint16_t length) |
216 | { | 222 | { |
217 | if (!peer_object || !object) | 223 | if (!peer_object || !object || length <= sizeof(uint16_t)) |
218 | return -1; | 224 | return -1; |
219 | 225 | ||
220 | Group_Peer_AV *peer_av = peer_object; | 226 | Group_Peer_AV *peer_av = peer_object; |
221 | 227 | ||
222 | //TODO: parse packet into Group_Audio_Packet | 228 | Group_Audio_Packet *pk = calloc(1, sizeof(Group_Audio_Packet) + (length - sizeof(uint16_t))); |
223 | //queue(peer_av->buffer, Group_Audio_Packet *pk) | 229 | |
230 | if (!pk) | ||
231 | return -1; | ||
232 | |||
233 | uint16_t sequnum; | ||
234 | memcpy(&sequnum, packet, sizeof(sequnum)); | ||
235 | pk->sequnum = ntohs(sequnum); | ||
236 | pk->length = length - sizeof(uint16_t); | ||
237 | memcpy(pk->data, packet + sizeof(uint16_t), length - sizeof(uint16_t)); | ||
238 | |||
239 | if (queue(peer_av->buffer, pk) == -1) { | ||
240 | free(pk); | ||
241 | return -1; | ||
242 | } | ||
243 | |||
224 | return 0; | 244 | return 0; |
225 | } | 245 | } |
226 | 246 | ||
@@ -264,4 +284,27 @@ int add_av_groupchat(Group_Chats *g_c) | |||
264 | return groupnumber; | 284 | return groupnumber; |
265 | } | 285 | } |
266 | 286 | ||
287 | /* Send an encoded audio packet to the group chat. | ||
288 | * | ||
289 | * return 0 on success. | ||
290 | * return -1 on failure. | ||
291 | */ | ||
292 | static int send_audio_packet(Group_Chats *g_c, int groupnumber, uint8_t *packet, uint16_t length) | ||
293 | { | ||
294 | if (!length) | ||
295 | return -1; | ||
267 | 296 | ||
297 | Group_AV *group_av = group_get_object(g_c, groupnumber); | ||
298 | uint8_t data[1 + sizeof(uint16_t) + length]; | ||
299 | data[0] = GROUP_AUDIO_PACKET_ID; | ||
300 | |||
301 | uint16_t sequnum = htons(group_av->audio_sequnum); | ||
302 | memcpy(data + 1, &sequnum, sizeof(sequnum)); | ||
303 | memcpy(data + 1 + sizeof(sequnum), packet, length); | ||
304 | |||
305 | if (send_group_lossy_packet(g_c, groupnumber, data, sizeof(data)) == -1) | ||
306 | return -1; | ||
307 | |||
308 | ++group_av->audio_sequnum; | ||
309 | return 0; | ||
310 | } | ||