diff options
Diffstat (limited to 'toxav/media.c')
-rw-r--r-- | toxav/media.c | 314 |
1 files changed, 314 insertions, 0 deletions
diff --git a/toxav/media.c b/toxav/media.c new file mode 100644 index 00000000..2594f99a --- /dev/null +++ b/toxav/media.c | |||
@@ -0,0 +1,314 @@ | |||
1 | /** media.c | ||
2 | * | ||
3 | * Audio and video codec intitialization, encoding/decoding and playback | ||
4 | * | ||
5 | * Copyright (C) 2013 Tox project All Rights Reserved. | ||
6 | * | ||
7 | * This file is part of Tox. | ||
8 | * | ||
9 | * Tox is free software: you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation, either version 3 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * Tox is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
17 | * GNU General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
21 | * | ||
22 | */ | ||
23 | |||
24 | |||
25 | #ifdef HAVE_CONFIG_H | ||
26 | #include "config.h" | ||
27 | #endif /* HAVE_CONFIG_H */ | ||
28 | |||
29 | #include <stdio.h> | ||
30 | #include <stdlib.h> | ||
31 | #include <math.h> | ||
32 | #include <assert.h> | ||
33 | |||
34 | #include "rtp.h" | ||
35 | #include "media.h" | ||
36 | |||
37 | struct jitter_buffer { | ||
38 | RTPMessage **queue; | ||
39 | uint16_t capacity; | ||
40 | uint16_t size; | ||
41 | uint16_t front; | ||
42 | uint16_t rear; | ||
43 | uint8_t queue_ready; | ||
44 | uint16_t current_id; | ||
45 | uint32_t current_ts; | ||
46 | uint8_t id_set; | ||
47 | }; | ||
48 | |||
49 | |||
50 | struct jitter_buffer *create_queue(int capacity) | ||
51 | { | ||
52 | struct jitter_buffer *q; | ||
53 | q = (struct jitter_buffer *)calloc(sizeof(struct jitter_buffer), 1); | ||
54 | q->queue = (RTPMessage **)calloc(sizeof(RTPMessage *), capacity); | ||
55 | int i = 0; | ||
56 | |||
57 | for (i = 0; i < capacity; ++i) { | ||
58 | q->queue[i] = NULL; | ||
59 | } | ||
60 | |||
61 | q->size = 0; | ||
62 | q->capacity = capacity; | ||
63 | q->front = 0; | ||
64 | q->rear = -1; | ||
65 | q->queue_ready = 0; | ||
66 | q->current_id = 0; | ||
67 | q->current_ts = 0; | ||
68 | q->id_set = 0; | ||
69 | return q; | ||
70 | } | ||
71 | |||
72 | /* returns 1 if 'a' has a higher sequence number than 'b' */ | ||
73 | uint8_t sequence_number_older(uint16_t sn_a, uint16_t sn_b, uint32_t ts_a, uint32_t ts_b) | ||
74 | { | ||
75 | /* TODO: There is already this kind of function in toxrtp.c. | ||
76 | * Maybe merge? | ||
77 | */ | ||
78 | return (sn_a > sn_b || ts_a > ts_b); | ||
79 | } | ||
80 | |||
81 | /* success is 0 when there is nothing to dequeue, 1 when there's a good packet, 2 when there's a lost packet */ | ||
82 | RTPMessage *dequeue(struct jitter_buffer *q, int *success) | ||
83 | { | ||
84 | if (q->size == 0 || q->queue_ready == 0) { | ||
85 | q->queue_ready = 0; | ||
86 | *success = 0; | ||
87 | return NULL; | ||
88 | } | ||
89 | |||
90 | int front = q->front; | ||
91 | |||
92 | if (q->id_set == 0) { | ||
93 | q->current_id = q->queue[front]->header->sequnum; | ||
94 | q->current_ts = q->queue[front]->header->timestamp; | ||
95 | q->id_set = 1; | ||
96 | } else { | ||
97 | int next_id = q->queue[front]->header->sequnum; | ||
98 | int next_ts = q->queue[front]->header->timestamp; | ||
99 | |||
100 | /* if this packet is indeed the expected packet */ | ||
101 | if (next_id == (q->current_id + 1) % MAX_SEQU_NUM) { | ||
102 | q->current_id = next_id; | ||
103 | q->current_ts = next_ts; | ||
104 | } else { | ||
105 | if (sequence_number_older(next_id, q->current_id, next_ts, q->current_ts)) { | ||
106 | printf("nextid: %d current: %d\n", next_id, q->current_id); | ||
107 | q->current_id = (q->current_id + 1) % MAX_SEQU_NUM; | ||
108 | *success = 2; /* tell the decoder the packet is lost */ | ||
109 | return NULL; | ||
110 | } else { | ||
111 | /* packet too old */ | ||
112 | printf("packet too old\n"); | ||
113 | *success = 0; | ||
114 | return NULL; | ||
115 | } | ||
116 | } | ||
117 | } | ||
118 | |||
119 | q->size--; | ||
120 | q->front++; | ||
121 | |||
122 | if (q->front == q->capacity) | ||
123 | q->front = 0; | ||
124 | |||
125 | *success = 1; | ||
126 | q->current_id = q->queue[front]->header->sequnum; | ||
127 | q->current_ts = q->queue[front]->header->timestamp; | ||
128 | return q->queue[front]; | ||
129 | } | ||
130 | |||
131 | int empty_queue(struct jitter_buffer *q) | ||
132 | { | ||
133 | while (q->size > 0) { | ||
134 | q->size--; | ||
135 | rtp_free_msg(NULL, q->queue[q->front]); | ||
136 | q->front++; | ||
137 | |||
138 | if (q->front == q->capacity) | ||
139 | q->front = 0; | ||
140 | } | ||
141 | |||
142 | q->id_set = 0; | ||
143 | q->queue_ready = 0; | ||
144 | return 0; | ||
145 | } | ||
146 | |||
147 | int queue(struct jitter_buffer *q, RTPMessage *pk) | ||
148 | { | ||
149 | if (q->size == q->capacity) { | ||
150 | printf("buffer full, emptying buffer...\n"); | ||
151 | empty_queue(q); | ||
152 | return 0; | ||
153 | } | ||
154 | |||
155 | if (q->size > 8) | ||
156 | q->queue_ready = 1; | ||
157 | |||
158 | ++q->size; | ||
159 | ++q->rear; | ||
160 | |||
161 | if (q->rear == q->capacity) | ||
162 | q->rear = 0; | ||
163 | |||
164 | q->queue[q->rear] = pk; | ||
165 | |||
166 | int a; | ||
167 | int b; | ||
168 | int j; | ||
169 | a = q->rear; | ||
170 | |||
171 | for (j = 0; j < q->size - 1; ++j) { | ||
172 | b = a - 1; | ||
173 | |||
174 | if (b < 0) | ||
175 | b += q->capacity; | ||
176 | |||
177 | if (sequence_number_older(q->queue[b]->header->sequnum, q->queue[a]->header->sequnum, | ||
178 | q->queue[b]->header->timestamp, q->queue[a]->header->timestamp)) { | ||
179 | RTPMessage *temp; | ||
180 | temp = q->queue[a]; | ||
181 | q->queue[a] = q->queue[b]; | ||
182 | q->queue[b] = temp; | ||
183 | printf("had to swap\n"); | ||
184 | } else { | ||
185 | break; | ||
186 | } | ||
187 | |||
188 | a -= 1; | ||
189 | |||
190 | if (a < 0) | ||
191 | a += q->capacity; | ||
192 | } | ||
193 | |||
194 | if (pk) | ||
195 | return 1; | ||
196 | |||
197 | return 0; | ||
198 | } | ||
199 | |||
200 | |||
201 | int init_video_decoder(CodecState *cs) | ||
202 | { | ||
203 | if (vpx_codec_dec_init_ver(&cs->v_decoder, VIDEO_CODEC_DECODER_INTERFACE, NULL, 0, | ||
204 | VPX_DECODER_ABI_VERSION) != VPX_CODEC_OK) { | ||
205 | fprintf(stderr, "Init video_decoder failed!\n"); | ||
206 | return -1; | ||
207 | } | ||
208 | |||
209 | return 0; | ||
210 | } | ||
211 | |||
212 | int init_audio_decoder(CodecState *cs, uint32_t audio_channels) | ||
213 | { | ||
214 | int rc; | ||
215 | cs->audio_decoder = opus_decoder_create(cs->audio_sample_rate, audio_channels, &rc ); | ||
216 | |||
217 | if ( rc != OPUS_OK ) { | ||
218 | fprintf(stderr, "Error while starting audio decoder!\n"); | ||
219 | return -1; | ||
220 | } | ||
221 | |||
222 | return 0; | ||
223 | } | ||
224 | |||
225 | |||
226 | int init_video_encoder(CodecState *cs, uint16_t width, uint16_t height, uint32_t video_bitrate) | ||
227 | { | ||
228 | vpx_codec_enc_cfg_t cfg; | ||
229 | int res = vpx_codec_enc_config_default(VIDEO_CODEC_ENCODER_INTERFACE, &cfg, 0); | ||
230 | |||
231 | if (res) { | ||
232 | printf("Failed to get config: %s\n", vpx_codec_err_to_string(res)); | ||
233 | return -1; | ||
234 | } | ||
235 | |||
236 | cfg.rc_target_bitrate = video_bitrate; | ||
237 | cfg.g_w = width; | ||
238 | cfg.g_h = height; | ||
239 | |||
240 | if (vpx_codec_enc_init_ver(&cs->v_encoder, VIDEO_CODEC_ENCODER_INTERFACE, &cfg, 0, | ||
241 | VPX_ENCODER_ABI_VERSION) != VPX_CODEC_OK) { | ||
242 | fprintf(stderr, "Failed to initialize encoder\n"); | ||
243 | return -1; | ||
244 | } | ||
245 | |||
246 | return 0; | ||
247 | } | ||
248 | |||
249 | int init_audio_encoder(CodecState *cs, uint32_t audio_channels) | ||
250 | { | ||
251 | int err = OPUS_OK; | ||
252 | cs->audio_encoder = opus_encoder_create(cs->audio_sample_rate, audio_channels, OPUS_APPLICATION_AUDIO, &err); | ||
253 | err = opus_encoder_ctl(cs->audio_encoder, OPUS_SET_BITRATE(cs->audio_bitrate)); | ||
254 | err = opus_encoder_ctl(cs->audio_encoder, OPUS_SET_COMPLEXITY(10)); | ||
255 | |||
256 | |||
257 | return err == OPUS_OK ? 0 : -1; | ||
258 | } | ||
259 | |||
260 | |||
261 | CodecState *codec_init_session ( uint32_t audio_bitrate, | ||
262 | uint16_t audio_frame_duration, | ||
263 | uint32_t audio_sample_rate, | ||
264 | uint32_t audio_channels, | ||
265 | uint16_t video_width, | ||
266 | uint16_t video_height, | ||
267 | uint32_t video_bitrate ) | ||
268 | { | ||
269 | CodecState *_retu = calloc(sizeof(CodecState), 1); | ||
270 | assert(_retu); | ||
271 | |||
272 | _retu->audio_bitrate = audio_bitrate; | ||
273 | _retu->audio_sample_rate = audio_sample_rate; | ||
274 | |||
275 | /* Encoders */ | ||
276 | if (!video_width || !video_height) { | ||
277 | video_width = 320; | ||
278 | video_height = 240; | ||
279 | } | ||
280 | |||
281 | if ( 0 == init_video_encoder(_retu, video_width, video_height, video_bitrate) ) | ||
282 | printf("Video encoder initialized!\n"); | ||
283 | |||
284 | if ( 0 == init_audio_encoder(_retu, audio_channels) ) | ||
285 | printf("Audio encoder initialized!\n"); | ||
286 | |||
287 | |||
288 | /* Decoders */ | ||
289 | if ( 0 == init_video_decoder(_retu) ) | ||
290 | printf("Video decoder initialized!\n"); | ||
291 | |||
292 | if ( 0 == init_audio_decoder(_retu, audio_channels) ) | ||
293 | printf("Audio decoder initialized!\n"); | ||
294 | |||
295 | |||
296 | return _retu; | ||
297 | } | ||
298 | |||
299 | void codec_terminate_session ( CodecState *cs ) | ||
300 | { | ||
301 | if ( cs->audio_encoder ) { | ||
302 | opus_encoder_destroy(cs->audio_encoder); | ||
303 | printf("Terminated encoder!\n"); | ||
304 | } | ||
305 | |||
306 | if ( cs->audio_decoder ) { | ||
307 | opus_decoder_destroy(cs->audio_decoder); | ||
308 | printf("Terminated decoder!\n"); | ||
309 | } | ||
310 | |||
311 | /* TODO: Terminate video */ | ||
312 | vpx_codec_destroy(&cs->v_decoder); | ||
313 | vpx_codec_destroy(&cs->v_encoder); | ||
314 | } | ||