1 /*
2 * Copyright (C) 2000-2020 the xine project
3 *
4 * This file is part of xine, a free video player.
5 *
6 * xine is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * xine is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
19 *
20 * Demuxer helper functions
21 * hide some xine engine details from demuxers and reduce code duplication
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <pthread.h>
31 #include <sched.h>
32 #include <sys/time.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35 #include <errno.h>
36
37 #define LOG_MODULE "demux"
38 #define LOG_VERBOSE
39 /*
40 #define LOG
41 */
42
43 #include <xine/xine_internal.h>
44 #include <xine/demux.h>
45 #include <xine/buffer.h>
46 #include "xine_private.h"
47
48 #ifdef WIN32
49 #include <winsock.h>
50 #endif
51
52 /*
53 * Flush audio and video buffers. It is called from demuxers on
54 * seek/stop, and may be useful when user input changes a stream and
55 * xine-lib has cached buffers that have yet to be played.
56 *
57 * warning: after clearing decoders fifos an absolute discontinuity
58 * indication must be sent. relative discontinuities are likely
59 * to cause "jumps" on metronom.
60 */
_x_demux_flush_engine(xine_stream_t * s)61 void _x_demux_flush_engine (xine_stream_t *s) {
62
63 xine_stream_private_t *stream = (xine_stream_private_t *)s;
64 xine_private_t *xine = (xine_private_t *)stream->s.xine;
65 buf_element_t *buf;
66
67 stream = stream->side_streams[0];
68
69 if (stream->gapless_switch || stream->finished_naturally)
70 return;
71
72 xine->port_ticket->acquire (xine->port_ticket, 1);
73
74 /* only flush/discard output ports on master streams */
75 if (stream->s.master == &stream->s) {
76 if (stream->s.video_out) {
77 stream->s.video_out->set_property (stream->s.video_out, VO_PROP_DISCARD_FRAMES, 1);
78 }
79 if (stream->s.audio_out) {
80 stream->s.audio_out->set_property (stream->s.audio_out, AO_PROP_DISCARD_BUFFERS, 1);
81 }
82 }
83
84 stream->s.video_fifo->clear (stream->s.video_fifo);
85 stream->s.audio_fifo->clear (stream->s.audio_fifo);
86
87 pthread_mutex_lock (&stream->demux.pair);
88
89 buf = stream->s.video_fifo->buffer_pool_alloc (stream->s.video_fifo);
90 buf->type = BUF_CONTROL_RESET_DECODER;
91 stream->s.video_fifo->put (stream->s.video_fifo, buf);
92
93 buf = stream->s.audio_fifo->buffer_pool_alloc (stream->s.audio_fifo);
94 buf->type = BUF_CONTROL_RESET_DECODER;
95 stream->s.audio_fifo->put (stream->s.audio_fifo, buf);
96
97 pthread_mutex_unlock (&stream->demux.pair);
98
99 /* on seeking we must wait decoder fifos to process before doing flush.
100 * otherwise we flush too early (before the old data has left decoders)
101 */
102 _x_demux_control_headers_done (&stream->s);
103
104 if (stream->s.video_out) {
105 video_overlay_manager_t *ovl = stream->s.video_out->get_overlay_manager (stream->s.video_out);
106 ovl->flush_events(ovl);
107 }
108
109 /* only flush/discard output ports on master streams */
110 if (stream->s.master == &stream->s) {
111 if (stream->s.video_out) {
112 stream->s.video_out->flush (stream->s.video_out);
113 stream->s.video_out->set_property (stream->s.video_out, VO_PROP_DISCARD_FRAMES, 0);
114 }
115
116 if (stream->s.audio_out) {
117 stream->s.audio_out->flush (stream->s.audio_out);
118 stream->s.audio_out->set_property (stream->s.audio_out, AO_PROP_DISCARD_BUFFERS, 0);
119 }
120 }
121
122 xine->port_ticket->release (xine->port_ticket, 1);
123 }
124
125
_x_demux_control_newpts(xine_stream_t * s,int64_t pts,uint32_t flags)126 void _x_demux_control_newpts (xine_stream_t *s, int64_t pts, uint32_t flags) {
127 xine_stream_private_t *stream = (xine_stream_private_t *)s;
128 buf_element_t *bufa, *bufv;
129
130 stream = stream->side_streams[0];
131
132 if (flags & BUF_FLAG_SEEK) {
133 pthread_mutex_lock (&stream->demux.pair);
134 if (stream->demux.max_seek_bufs == 0) {
135 pthread_mutex_unlock (&stream->demux.pair);
136 return;
137 }
138 stream->demux.max_seek_bufs--;
139 pthread_mutex_unlock (&stream->demux.pair);
140 }
141
142 bufv = stream->s.video_fifo->buffer_pool_alloc (stream->s.video_fifo);
143 bufa = stream->s.audio_fifo->buffer_pool_alloc (stream->s.audio_fifo);
144
145 pthread_mutex_lock (&stream->demux.pair);
146
147 bufv->type = BUF_CONTROL_NEWPTS;
148 bufv->decoder_flags = flags;
149 bufv->disc_off = pts;
150 stream->s.video_fifo->put (stream->s.video_fifo, bufv);
151
152 bufa->type = BUF_CONTROL_NEWPTS;
153 bufa->decoder_flags = flags;
154 bufa->disc_off = pts;
155 stream->s.audio_fifo->put (stream->s.audio_fifo, bufa);
156
157 pthread_mutex_unlock (&stream->demux.pair);
158 }
159
160 /* avoid ao_loop being stuck in a pthread_cond_wait, waiting for data;
161 * return 1 if the stream is stopped
162 * (better fix wanted!)
163 */
demux_unstick_ao_loop(xine_stream_t * s)164 static int demux_unstick_ao_loop (xine_stream_t *s) {
165 xine_stream_private_t *stream = (xine_stream_private_t *)s;
166 /* if (!stream->audio_thread_created)
167 return 0;
168 */
169 int status = xine_get_status (&stream->s);
170 if (status != XINE_STATUS_QUIT && status != XINE_STATUS_STOP && stream->demux.plugin->get_status (stream->demux.plugin) != DEMUX_FINISHED)
171 return 0;
172 #if 0
173 /* right, stream is stopped... */
174 audio_buffer_t *buf = stream->s.audio_out->get_buffer (stream->s.audio_out);
175 buf->num_frames = 0;
176 buf->stream = NULL;
177 stream->s.audio_out->put_buffer (stream->s.audio_out, buf, stream);
178 #endif
179 lprintf("stuck\n");
180 return 1;
181 }
182
183 /* sync with decoder fifos, making sure everything gets processed */
_x_demux_control_headers_done(xine_stream_t * s)184 void _x_demux_control_headers_done (xine_stream_t *s) {
185
186 xine_stream_private_t *stream = (xine_stream_private_t *)s;
187 int headers_audio;
188 int headers_video;
189 unsigned int max_iterations;
190 buf_element_t *buf_video, *buf_audio;
191
192 stream = stream->side_streams[0];
193
194 /* we use demux.action_pending to wake up sleeping spu decoders */
195 _x_action_raise (&stream->s);
196
197 /* allocate the buffers before grabbing the lock to prevent cyclic wait situations */
198 buf_video = stream->s.video_fifo->buffer_pool_alloc (stream->s.video_fifo);
199 buf_audio = stream->s.audio_fifo->buffer_pool_alloc (stream->s.audio_fifo);
200
201 pthread_mutex_lock (&stream->counter.lock);
202
203 if (stream->video_thread_created) {
204 headers_video = stream->counter.headers_video + 1;
205 } else {
206 headers_video = 0;
207 }
208
209 if (stream->audio_thread_created) {
210 headers_audio = stream->counter.headers_audio + 1;
211 } else {
212 headers_audio = 0;
213 }
214
215 pthread_mutex_lock (&stream->demux.pair);
216
217 buf_video->type = BUF_CONTROL_HEADERS_DONE;
218 stream->s.video_fifo->put (stream->s.video_fifo, buf_video);
219
220 buf_audio->type = BUF_CONTROL_HEADERS_DONE;
221 stream->s.audio_fifo->put (stream->s.audio_fifo, buf_audio);
222
223 pthread_mutex_unlock (&stream->demux.pair);
224 max_iterations = 0;
225
226 while ((stream->counter.headers_audio < headers_audio) ||
227 (stream->counter.headers_video < headers_video)) {
228 struct timespec ts = {0, 0};
229 int ret_wait;
230
231 lprintf ("waiting for headers. v:%d %d a:%d %d\n",
232 stream->counter.headers_video, headers_video,
233 stream->counter.headers_audio, headers_audio);
234
235 xine_gettime (&ts);
236 ts.tv_sec += 1;
237
238 /* use timedwait to workaround buggy pthread broadcast implementations */
239 ret_wait = pthread_cond_timedwait (&stream->counter.changed, &stream->counter.lock, &ts);
240
241 if (ret_wait == ETIMEDOUT && demux_unstick_ao_loop (&stream->s) && ++max_iterations > 4) {
242 xine_log (stream->s.xine,
243 XINE_LOG_MSG,_("Stuck in _x_demux_control_headers_done(). Taking the emergency exit\n"));
244 stream->emergency_brake = 1;
245 break;
246 }
247 }
248
249 _x_action_lower (&stream->s);
250
251 lprintf ("headers processed.\n");
252
253 pthread_mutex_unlock (&stream->counter.lock);
254 }
255
_x_demux_control_start(xine_stream_t * s)256 void _x_demux_control_start (xine_stream_t *s) {
257 xine_stream_private_t *stream = (xine_stream_private_t *)s;
258 buf_element_t *bufa, *bufv;
259 uint32_t flags, id_flag;
260
261 id_flag = stream->id_flag;
262 stream = stream->side_streams[0];
263
264 /* if an _other_ side stream already sent this, skip our duplicate.
265 * the _same_ side is supposed to know what it is doing (input_vdr). */
266 pthread_mutex_lock (&stream->demux.pair);
267 if (stream->demux.start_buffers_sent & (~id_flag)) {
268 pthread_mutex_unlock (&stream->demux.pair);
269 xprintf (stream->s.xine, XINE_VERBOSITY_DEBUG,
270 "demux: stream %p: skipping duplicate start buffers.\n", (void *)stream);
271 return;
272 }
273 pthread_mutex_unlock (&stream->demux.pair);
274
275 flags = (stream->gapless_switch || stream->finished_naturally) ? BUF_FLAG_GAPLESS_SW : 0;
276
277 bufv = stream->s.video_fifo->buffer_pool_alloc (stream->s.video_fifo);
278 bufa = stream->s.audio_fifo->buffer_pool_alloc (stream->s.audio_fifo);
279
280 pthread_mutex_lock (&stream->demux.pair);
281
282 bufv->type = BUF_CONTROL_START;
283 bufv->decoder_flags = flags;
284 stream->s.video_fifo->put (stream->s.video_fifo, bufv);
285
286 bufa->type = BUF_CONTROL_START;
287 bufa->decoder_flags = flags;
288 stream->s.audio_fifo->put (stream->s.audio_fifo, bufa);
289
290 stream->demux.start_buffers_sent |= id_flag;
291
292 pthread_mutex_unlock (&stream->demux.pair);
293 }
294
_x_demux_control_end(xine_stream_t * s,uint32_t flags)295 void _x_demux_control_end (xine_stream_t *s, uint32_t flags) {
296
297 xine_stream_private_t *stream = (xine_stream_private_t *)s;
298 buf_element_t *bufa, *bufv;
299
300 stream = stream->side_streams[0];
301
302 bufv = stream->s.video_fifo->buffer_pool_alloc (stream->s.video_fifo);
303 bufa = stream->s.audio_fifo->buffer_pool_alloc (stream->s.audio_fifo);
304
305 pthread_mutex_lock (&stream->demux.pair);
306
307 bufv->type = BUF_CONTROL_END;
308 bufv->decoder_flags = flags;
309 stream->s.video_fifo->put (stream->s.video_fifo, bufv);
310
311 bufa->type = BUF_CONTROL_END;
312 bufa->decoder_flags = flags;
313 stream->s.audio_fifo->put (stream->s.audio_fifo, bufa);
314
315 pthread_mutex_unlock (&stream->demux.pair);
316 }
317
_x_demux_control_nop(xine_stream_t * s,uint32_t flags)318 void _x_demux_control_nop (xine_stream_t *s, uint32_t flags ) {
319
320 xine_stream_private_t *stream = (xine_stream_private_t *)s;
321 buf_element_t *bufa, *bufv;
322
323 stream = stream->side_streams[0];
324
325 bufv = stream->s.video_fifo->buffer_pool_alloc (stream->s.video_fifo);
326 bufa = stream->s.audio_fifo->buffer_pool_alloc (stream->s.audio_fifo);
327
328 pthread_mutex_lock (&stream->demux.pair);
329
330 bufv->type = BUF_CONTROL_NOP;
331 bufv->decoder_flags = flags;
332 stream->s.video_fifo->put (stream->s.video_fifo, bufv);
333
334 bufa->type = BUF_CONTROL_NOP;
335 bufa->decoder_flags = flags;
336 stream->s.audio_fifo->put (stream->s.audio_fifo, bufa);
337
338 pthread_mutex_unlock (&stream->demux.pair);
339 }
340
demux_loop(void * stream_gen)341 static void *demux_loop (void *stream_gen) {
342 xine_stream_private_t *stream = (xine_stream_private_t *)stream_gen;
343 xine_stream_private_t *m = stream->side_streams[0];
344 int status;
345 int non_user;
346 int iterations = 0, seeks = 0;
347
348 struct timespec seek_time = {0, 0};
349
350 lprintf ("loop starting...\n");
351
352 xprintf (stream->s.xine, XINE_VERBOSITY_DEBUG,
353 "demux: starting stream %p.\n", (void *)stream);
354 pthread_mutex_lock (&m->counter.lock);
355 m->counter.demuxers_running++;
356 pthread_mutex_unlock (&m->counter.lock);
357
358 pthread_mutex_lock (&stream->demux.lock);
359 m->emergency_brake = 0;
360
361 /* do-while needed to seek after demux finished */
362 do {
363 xine_gettime (&seek_time);
364
365 {
366 uint32_t input_caps = 0;
367 /* tell xine_play_internal () whether we can seek.
368 * so it may flush fifos early, and suspend us faster. */
369 if (stream->s.input_plugin)
370 input_caps = stream->s.input_plugin->get_capabilities (stream->s.input_plugin);
371 pthread_mutex_lock (&stream->demux.action_lock);
372 stream->demux.input_caps = input_caps;
373 pthread_mutex_unlock (&stream->demux.action_lock);
374 }
375
376 /* main demuxer loop */
377 status = stream->demux.plugin->get_status (stream->demux.plugin);
378 while (status == DEMUX_OK && stream->demux.thread_running && !m->emergency_brake) {
379
380 iterations++;
381 status = stream->demux.plugin->send_chunk (stream->demux.plugin);
382
383 /* someone may want to interrupt us */
384 if (stream->demux.action_pending > 0) {
385 pthread_mutex_lock (&stream->demux.action_lock);
386 if (stream->demux.action_pending > 0) {
387 pthread_mutex_unlock (&stream->demux.lock);
388 do {
389 pthread_cond_wait (&stream->demux.resume, &stream->demux.action_lock);
390 } while (stream->demux.action_pending > 0);
391 pthread_mutex_unlock (&stream->demux.action_lock);
392 pthread_mutex_lock (&stream->demux.lock);
393 xine_gettime (&seek_time);
394 seeks++;
395 } else {
396 pthread_mutex_unlock (&stream->demux.action_lock);
397 }
398 }
399 }
400
401 lprintf ("main demuxer loop finished (status: %d)\n", status);
402
403 /* let demux plugin do some needed cleanup */
404 if (stream->demux.plugin->get_capabilities (stream->demux.plugin) & DEMUX_CAP_STOP)
405 stream->demux.plugin->get_optional_data (stream->demux.plugin, NULL, DEMUX_OPTIONAL_DATA_STOP);
406
407 /* tell to the net_buf_ctrl that we are at the end of the stream
408 * then the net_buf_ctrl will not pause
409 */
410 _x_demux_control_nop (&stream->s, BUF_FLAG_END_STREAM);
411
412 /* wait before sending end buffers: user might want to do a new seek */
413 while(stream->demux.thread_running &&
414 ((stream->s.video_fifo->size (stream->s.video_fifo)) ||
415 (stream->s.audio_fifo->size (stream->s.audio_fifo))) &&
416 status == DEMUX_FINISHED && !m->emergency_brake){
417 struct timespec ts = {0, 0};
418 xine_gettime (&ts);
419 ts.tv_nsec += 100000000;
420 if (ts.tv_nsec >= 1000000000) {
421 ts.tv_nsec -= 1000000000;
422 ts.tv_sec += 1;
423 }
424 pthread_cond_timedwait (&stream->demux.resume, &stream->demux.lock, &ts);
425 status = stream->demux.plugin->get_status (stream->demux.plugin);
426 }
427
428 if (stream->demux.thread_running && (status == DEMUX_FINISHED)) do {
429 struct timespec ts = {0, 0};
430 if (stream->delay_finish_event > 0) {
431 /* delay sending finished event - used for image presentations */
432 xine_gettime (&ts);
433 ts.tv_sec += stream->delay_finish_event / 10;
434 ts.tv_nsec += (stream->delay_finish_event % 10) * 100000000;
435 if (ts.tv_nsec >= 1000000000) {
436 ts.tv_nsec -= 1000000000;
437 ts.tv_sec += 1;
438 }
439 stream->delay_finish_event = 0;
440 } else if (stream->delay_finish_event < 0) {
441 /* infinitely delay sending finished event - used for image presentations */
442 stream->delay_finish_event = 0;
443 xine_gettime (&ts);
444 do {
445 ts.tv_sec += 1;
446 pthread_cond_timedwait (&stream->demux.resume, &stream->demux.lock, &ts);
447 status = stream->demux.plugin->get_status (stream->demux.plugin);
448 } while (stream->demux.thread_running && (status == DEMUX_FINISHED));
449 break;
450 } else {
451 /* there may be no first frame at all here.
452 * make sure xine_play returns first. */
453 pthread_mutex_lock (&m->first_frame.lock);
454 if (m->first_frame.flag) {
455 m->first_frame.flag = 0;
456 pthread_cond_broadcast (&m->first_frame.reached);
457 pthread_mutex_unlock (&m->first_frame.lock);
458 xprintf (stream->s.xine, XINE_VERBOSITY_DEBUG,
459 "demux: unblocked xine_play_internal ().\n");
460 } else {
461 pthread_mutex_unlock (&m->first_frame.lock);
462 }
463 /* stream end may well happen during xine_play () (seek close to end).
464 * Lets not confuse frontend, and delay that message a bit. */
465 ts = seek_time;
466 ts.tv_nsec += 300000000;
467 if (ts.tv_nsec >= 1000000000) {
468 ts.tv_nsec -= 1000000000;
469 ts.tv_sec += 1;
470 }
471 xine_gettime (&seek_time);
472 if (seek_time.tv_sec > ts.tv_sec)
473 break;
474 if ((seek_time.tv_sec == ts.tv_sec) && (seek_time.tv_nsec >= ts.tv_nsec))
475 break;
476 xprintf (stream->s.xine, XINE_VERBOSITY_DEBUG,
477 "demux: very short seek segment, delaying finish message.\n");
478 }
479 do {
480 int e = pthread_cond_timedwait (&stream->demux.resume, &stream->demux.lock, &ts);
481 status = stream->demux.plugin->get_status (stream->demux.plugin);
482 if (e == ETIMEDOUT)
483 break;
484 } while (stream->demux.thread_running && (status == DEMUX_FINISHED));
485 } while (0);
486
487 } while (status == DEMUX_OK && stream->demux.thread_running && !m->emergency_brake);
488
489 lprintf ("loop finished (status: %d)\n", status);
490
491 /* demux.thread_running is zero if demux loop has been stopped by user */
492 non_user = stream->demux.thread_running;
493 stream->demux.thread_running = 0;
494
495
496 /* do stream end stuff only if this is the last side stream. */
497 {
498 unsigned int n;
499
500 pthread_mutex_lock (&m->counter.lock);
501 n = --(m->counter.demuxers_running);
502 if (n == 0) {
503
504 int finisheds_audio = 0;
505 int finisheds_video = 0;
506
507 if (m->audio_thread_created)
508 finisheds_audio = m->counter.finisheds_audio + 1;
509 if (m->video_thread_created)
510 finisheds_video = m->counter.finisheds_video + 1;
511 pthread_mutex_unlock (&m->counter.lock);
512
513 _x_demux_control_end (&m->s, non_user);
514 lprintf ("loop finished, end buffer sent\n");
515 pthread_mutex_unlock (&stream->demux.lock);
516
517 pthread_mutex_lock (&m->counter.lock);
518 n = 0;
519 while ((m->counter.finisheds_audio < finisheds_audio) ||
520 (m->counter.finisheds_video < finisheds_video)) {
521 int ret_wait;
522 struct timespec ts = {0, 0};
523 lprintf ("waiting for finisheds.\n");
524 xine_gettime (&ts);
525 ts.tv_sec += 1;
526 ret_wait = pthread_cond_timedwait (&m->counter.changed, &m->counter.lock, &ts);
527 if (ret_wait == ETIMEDOUT && demux_unstick_ao_loop (&m->s) && ++n > 4) {
528 xine_log (m->s.xine, XINE_LOG_MSG,_("Stuck in demux_loop(). Taking the emergency exit\n"));
529 m->emergency_brake = 1;
530 break;
531 }
532 }
533 pthread_mutex_unlock (&m->counter.lock);
534
535 _x_handle_stream_end (&m->s, non_user);
536 xprintf (stream->s.xine, XINE_VERBOSITY_DEBUG,
537 "demux: %s last stream %p after %d iterations and %d seeks.\n",
538 non_user ? "finished" : "stopped", (void *)stream, iterations, seeks);
539
540 } else {
541
542 pthread_mutex_unlock (&m->counter.lock);
543 pthread_mutex_unlock (&stream->demux.lock);
544 xprintf (stream->s.xine, XINE_VERBOSITY_DEBUG,
545 "demux: %s stream %p after %d iterations and %d seeks.\n",
546 non_user ? "finished" : "stopped", (void *)stream, iterations, seeks);
547
548 }
549 }
550
551 return NULL;
552 }
553
_x_demux_start_thread(xine_stream_t * s)554 int _x_demux_start_thread (xine_stream_t *s) {
555
556 xine_stream_private_t *stream = (xine_stream_private_t *)s;
557 int err;
558
559 lprintf ("start thread called\n");
560
561 _x_action_raise (&stream->s);
562 pthread_mutex_lock (&stream->demux.lock);
563 _x_action_lower (&stream->s);
564
565 if (!stream->demux.thread_running) {
566
567 if (stream->demux.thread_created) {
568 void *p;
569 pthread_join (stream->demux.thread, &p);
570 }
571
572 stream->demux.thread_running = 1;
573 stream->demux.thread_created = 1;
574 if ((err = pthread_create (&stream->demux.thread,
575 NULL, demux_loop, (void *)stream)) != 0) {
576 xprintf (stream->s.xine, XINE_VERBOSITY_LOG,
577 "demux: can't create new thread (%s)\n", strerror(err));
578 stream->demux.thread_running = 0;
579 stream->demux.thread_created = 0;
580 return -1;
581 }
582 }
583
584 pthread_mutex_unlock (&stream->demux.lock);
585 return 0;
586 }
587
_x_demux_stop_thread(xine_stream_t * s)588 int _x_demux_stop_thread (xine_stream_t *s) {
589
590 xine_stream_private_t *stream = (xine_stream_private_t *)s;
591 void *p;
592
593 lprintf ("stop thread called\n");
594
595 _x_action_raise (&stream->s);
596 pthread_mutex_lock (&stream->demux.lock);
597 stream->demux.thread_running = 0;
598 _x_action_lower (&stream->s);
599
600 /* At that point, the demuxer has sent the last audio/video buffer,
601 * so it's a safe place to flush the engine.
602 */
603 _x_demux_flush_engine (&stream->s);
604 pthread_mutex_unlock (&stream->demux.lock);
605
606 lprintf ("joining thread\n" );
607
608 if (stream->demux.thread_created) {
609 pthread_join (stream->demux.thread, &p);
610 stream->demux.thread_created = 0;
611 }
612
613 /*
614 * Wake up xine_play if it's waiting for a frame
615 */
616 {
617 xine_stream_private_t *m = stream->side_streams[0];
618 pthread_mutex_lock (&m->first_frame.lock);
619 if (m->first_frame.flag) {
620 m->first_frame.flag = 0;
621 pthread_cond_broadcast (&m->first_frame.reached);
622 }
623 pthread_mutex_unlock (&m->first_frame.lock);
624 }
625
626 return 0;
627 }
628
_x_demux_read_header(input_plugin_t * input,void * buffer,off_t size)629 int _x_demux_read_header (input_plugin_t *input, void *buffer, off_t size) {
630 int want_size = size;
631 uint32_t caps;
632
633 if (!input || !buffer || (want_size <= 0))
634 return 0;
635
636 caps = input->get_capabilities (input);
637
638 if (caps & INPUT_CAP_SEEKABLE) {
639 if (input->seek (input, 0, SEEK_SET) != 0)
640 return 0;
641 want_size = input->read (input, buffer, want_size);
642 if (input->seek (input, 0, SEEK_SET) != 0)
643 return 0; /* no point to continue any further */
644 return want_size;
645 }
646
647 if ((caps & INPUT_CAP_SIZED_PREVIEW) && (want_size >= (int)sizeof (want_size))) {
648 memcpy (buffer, &want_size, sizeof (want_size));
649 return input->get_optional_data (input, buffer, INPUT_OPTIONAL_DATA_SIZED_PREVIEW);
650 }
651
652 if (caps & INPUT_CAP_PREVIEW) {
653 if (want_size < MAX_PREVIEW_SIZE) {
654 int read_size;
655 uint8_t *temp = malloc (MAX_PREVIEW_SIZE);
656 if (!temp)
657 return 0;
658 read_size = input->get_optional_data (input, temp, INPUT_OPTIONAL_DATA_PREVIEW);
659 if (read_size <= 0) {
660 free (temp);
661 return 0;
662 }
663 if (read_size < want_size)
664 want_size = read_size;
665 memcpy (buffer, temp, want_size);
666 free (temp);
667 return want_size;
668 }
669 return input->get_optional_data (input, buffer, INPUT_OPTIONAL_DATA_PREVIEW);
670 }
671
672 return 0;
673 }
674
_x_demux_check_extension(const char * mrl,const char * extensions)675 int _x_demux_check_extension (const char *mrl, const char *extensions){
676 char *last_dot, *e, *ext_copy, *ext_work;
677 int found = 0;
678
679 /* An empty extensions string means that the by-extension method can't
680 be used, so consider those cases as always passing. */
681 if ( extensions == NULL ) return 1;
682
683 ext_copy = strdup(extensions);
684 ext_work = ext_copy;
685
686 last_dot = strrchr (mrl, '.');
687 if (last_dot) {
688 last_dot++;
689 }
690
691 while ( ( e = xine_strsep(&ext_work, " ")) != NULL ) {
692 if ( strstr(e, ":/") ) {
693 if ( strncasecmp (mrl, e, strlen (e)) == 0 ) {
694 found = 1;
695 break;
696 }
697 } else if (last_dot) {
698 if (strcasecmp (last_dot, e) == 0) {
699 found = 1;
700 break;
701 }
702 }
703 }
704 free(ext_copy);
705 return found;
706 }
707
708
709 /*
710 * read from socket/file descriptor checking demux.action_pending
711 *
712 * network input plugins should use this function in order to
713 * not freeze the engine.
714 *
715 * aborts with zero if no data is available and demux.action_pending is set
716 */
_x_read_abort(xine_stream_t * stream,int fd,char * buf,off_t todo)717 off_t _x_read_abort (xine_stream_t *stream, int fd, char *buf, off_t todo) {
718 size_t have, left;
719
720 if (todo <= 0)
721 return 0;
722 left = todo;
723 have = 0;
724
725 while (left) {
726
727 while (1) {
728 fd_set rset;
729 struct timeval timeout;
730
731 FD_ZERO (&rset);
732 FD_SET (fd, &rset);
733 timeout.tv_sec = 0;
734 timeout.tv_usec = 50000;
735
736 if (select (fd + 1, &rset, NULL, NULL, &timeout) > 0)
737 break;
738 /* aborts current read if action pending. otherwise xine
739 * cannot be stopped when no more data is available. */
740 if (_x_action_pending (stream))
741 return have;
742 }
743
744 {
745 ssize_t r;
746 #ifndef WIN32
747 r = read (fd, buf + have, left);
748 if (r <= 0) {
749 if (r == 0) /* EOF */
750 break;
751 if (errno == EAGAIN)
752 continue;
753 perror ("_x_read_abort");
754 return r;
755 }
756 #else
757 r = recv (fd, buf + have, left, 0);
758 if (r <= 0) {
759 perror ("_x_read_abort");
760 return r;
761 }
762 #endif
763 have += r;
764 left -= r;
765 }
766 }
767
768 return have;
769 }
770
_x_action_pending(xine_stream_t * s)771 int _x_action_pending (xine_stream_t *s) {
772 xine_stream_private_t *stream = (xine_stream_private_t *)s;
773 int a;
774 if (!stream)
775 return 0;
776 a = stream->demux.action_pending & 0xffff;
777 if (a) {
778 /* On seek, xine_play_internal () sets this, waits for demux to stop,
779 * grabs demux lock, resets this again, performs the seek, and finally
780 * unlocks demux. Due to per processor core L1 data caches, demux may
781 * still see this set for some time, and abort input for no real reason.
782 * Avoid that trap by checking again with lock here. */
783 pthread_mutex_lock (&stream->demux.action_lock);
784 a = stream->demux.action_pending & 0xffff;
785 pthread_mutex_unlock (&stream->demux.action_lock);
786 }
787 return a;
788 }
789
790 /* set demux.action_pending in a thread-safe way */
_x_action_raise(xine_stream_t * s)791 void _x_action_raise (xine_stream_t *s) {
792 xine_stream_private_t *stream = (xine_stream_private_t *)s;
793 pthread_mutex_lock (&stream->demux.action_lock);
794 stream->demux.action_pending += 0x10001;
795 pthread_mutex_unlock (&stream->demux.action_lock);
796 }
797
798 /* reset demux.action_pending in a thread-safe way */
_x_action_lower(xine_stream_t * s)799 void _x_action_lower (xine_stream_t *s) {
800 xine_stream_private_t *stream = (xine_stream_private_t *)s;
801 pthread_mutex_lock (&stream->demux.action_lock);
802 stream->demux.action_pending -= 0x10001;
803 if (stream->demux.action_pending <= 0)
804 pthread_cond_signal (&stream->demux.resume);
805 pthread_mutex_unlock (&stream->demux.action_lock);
806 }
807
808 /*
809 * demuxer helper function to send data to fifo, breaking into smaller
810 * pieces (bufs) as needed.
811 *
812 * it has quite some parameters, but only the first 6 are needed.
813 *
814 * the other ones help enforcing that demuxers provide the information
815 * they have about the stream, so things like the GUI slider bar can
816 * work as expected.
817 */
_x_demux_send_data(fifo_buffer_t * fifo,uint8_t * data,int size,int64_t pts,uint32_t type,uint32_t decoder_flags,int input_normpos,int input_time,int total_time,uint32_t frame_number)818 void _x_demux_send_data(fifo_buffer_t *fifo, uint8_t *data, int size,
819 int64_t pts, uint32_t type, uint32_t decoder_flags,
820 int input_normpos,
821 int input_time, int total_time,
822 uint32_t frame_number) {
823 buf_element_t *buf;
824
825 decoder_flags |= BUF_FLAG_FRAME_START;
826
827 _x_assert(size > 0);
828 while (fifo && size > 0) {
829
830 buf = fifo->buffer_pool_size_alloc (fifo, size);
831
832 if ( size > buf->max_size ) {
833 buf->size = buf->max_size;
834 buf->decoder_flags = decoder_flags;
835 } else {
836 buf->size = size;
837 buf->decoder_flags = BUF_FLAG_FRAME_END | decoder_flags;
838 }
839 decoder_flags &= ~BUF_FLAG_FRAME_START;
840
841 xine_fast_memcpy (buf->content, data, buf->size);
842 data += buf->size;
843 size -= buf->size;
844
845 buf->pts = pts;
846 pts = 0;
847
848 buf->extra_info->input_normpos = input_normpos;
849 buf->extra_info->input_time = input_time;
850 buf->extra_info->total_time = total_time;
851 buf->extra_info->frame_number = frame_number;
852
853 buf->type = type;
854
855 fifo->put (fifo, buf);
856 }
857 }
858
859 /*
860 * Analogous to above, but reads data from input plugin
861 *
862 * If reading fails, -1 is returned
863 */
_x_demux_read_send_data(fifo_buffer_t * fifo,input_plugin_t * input,int size,int64_t pts,uint32_t type,uint32_t decoder_flags,off_t input_normpos,int input_time,int total_time,uint32_t frame_number)864 int _x_demux_read_send_data(fifo_buffer_t *fifo, input_plugin_t *input,
865 int size, int64_t pts, uint32_t type,
866 uint32_t decoder_flags, off_t input_normpos,
867 int input_time, int total_time,
868 uint32_t frame_number) {
869 buf_element_t *buf;
870
871 decoder_flags |= BUF_FLAG_FRAME_START;
872
873 _x_assert(size > 0);
874 while (fifo && size > 0) {
875
876 buf = fifo->buffer_pool_size_alloc (fifo, size);
877
878 if ( size > buf->max_size ) {
879 buf->size = buf->max_size;
880 buf->decoder_flags = decoder_flags;
881 } else {
882 buf->size = size;
883 buf->decoder_flags = BUF_FLAG_FRAME_END | decoder_flags;
884 }
885 decoder_flags &= ~BUF_FLAG_FRAME_START;
886
887 if(input->read(input, buf->content, buf->size) < buf->size) {
888 buf->free_buffer(buf);
889 return -1;
890 }
891 size -= buf->size;
892
893 buf->pts = pts;
894 pts = 0;
895
896 buf->extra_info->input_normpos = input_normpos;
897 buf->extra_info->input_time = input_time;
898 buf->extra_info->total_time = total_time;
899 buf->extra_info->frame_number = frame_number;
900
901 buf->type = type;
902
903 fifo->put (fifo, buf);
904 }
905
906 return 0;
907 }
908
909 /*
910 * Helper function for sending MRL reference events
911 */
_x_demux_send_mrl_reference(xine_stream_t * stream,int alternative,const char * mrl,const char * title,int start_time,int duration)912 void _x_demux_send_mrl_reference (xine_stream_t *stream, int alternative,
913 const char *mrl, const char *title,
914 int start_time, int duration)
915 {
916 xine_event_t event;
917 union {
918 xine_mrl_reference_data_ext_t *e;
919 XINE_DISABLE_DEPRECATION_WARNINGS
920 xine_mrl_reference_data_t *b;
921 XINE_ENABLE_DEPRECATION_WARNINGS
922 } data;
923 const size_t mrl_len = strlen (mrl);
924
925 if (!title)
926 title = "";
927
928 /* extended MRL reference event */
929
930 event.stream = stream;
931 event.data_length = offsetof (xine_mrl_reference_data_ext_t, mrl) +
932 mrl_len + strlen (title) + 2;
933 data.e = event.data = malloc (event.data_length);
934
935 data.e->alternative = alternative;
936 data.e->start_time = start_time;
937 data.e->duration = duration;
938 strcpy((char *)data.e->mrl, mrl);
939 strcpy((char *)data.e->mrl + mrl_len + 1, title);
940
941 event.type = XINE_EVENT_MRL_REFERENCE_EXT;
942 xine_event_send (stream, &event);
943
944 /* plain MRL reference event */
945
946 XINE_DISABLE_DEPRECATION_WARNINGS
947 event.data_length = offsetof (xine_mrl_reference_data_t, mrl) + mrl_len + 1;
948 XINE_ENABLE_DEPRECATION_WARNINGS
949
950 /*data.b->alternative = alternative;*/
951 strcpy (data.b->mrl, mrl);
952
953 event.type = XINE_EVENT_MRL_REFERENCE;
954 xine_event_send (stream, &event);
955
956 free (data.e);
957 }
958
_x_demux_seek(xine_stream_t * s,off_t start_pos,int start_time,int playing)959 int _x_demux_seek (xine_stream_t *s, off_t start_pos, int start_time, int playing) {
960 xine_stream_private_t *stream = (xine_stream_private_t *)s;
961 int ret = -1;
962
963 pthread_mutex_lock (&stream->side_streams[0]->frontend_lock);
964 if (stream->demux.plugin)
965 ret = stream->demux.plugin->seek (stream->demux.plugin, start_pos, start_time, playing);
966 pthread_mutex_unlock (&stream->side_streams[0]->frontend_lock);
967
968 return ret;
969 }
970
971