diff options
Diffstat (limited to 'src/xine-engine/xine.c')
-rw-r--r-- | src/xine-engine/xine.c | 521 |
1 files changed, 350 insertions, 171 deletions
diff --git a/src/xine-engine/xine.c b/src/xine-engine/xine.c index eae13bec9..9f63d793c 100644 --- a/src/xine-engine/xine.c +++ b/src/xine-engine/xine.c @@ -1,18 +1,18 @@ /* * Copyright (C) 2000-2006 the xine project - * + * * This file is part of xine, a free video player. - * + * * xine 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 2 of the License, or * (at your option) any later version. - * + * * xine 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, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA @@ -36,6 +36,7 @@ #include <stdarg.h> #include <stdio.h> #include <ctype.h> +#include <unistd.h> #if defined (__linux__) || defined (__GLIBC__) #include <endian.h> #elif defined (__FreeBSD__) @@ -92,7 +93,7 @@ void _x_handle_stream_end (xine_stream_t *stream, int non_user) { * if they have called xine_stop explicitly, so only send * it if stream playback finished because of stream end reached */ - + xine_event_t event; event.data_length = 0; @@ -132,7 +133,7 @@ static int acquire_allowed_to_block(xine_ticket_t *this) { unsigned new_size; for(entry = 0; entry < this->holder_thread_count; ++entry) { - if(this->holder_threads[entry].holder == own_id) { + if(pthread_equal(this->holder_threads[entry].holder, own_id)) { /* This thread may already hold this ticket */ this->holder_threads[entry].count++; return (this->holder_threads[entry].count == 1); @@ -150,7 +151,7 @@ static int acquire_allowed_to_block(xine_ticket_t *this) { /* List too small. Realloc to larger size */ new_size = this->holder_thread_count * 2; lprintf("Reallocing from %d to %d entries\n", this->holder_thread_count, new_size); - + this->holder_threads = realloc(this->holder_threads, sizeof(*this->holder_threads) * new_size); memset(this->holder_threads + this->holder_thread_count, 0, this->holder_thread_count); @@ -159,15 +160,15 @@ static int acquire_allowed_to_block(xine_ticket_t *this) { this->holder_threads[this->holder_thread_count].holder = own_id; this->holder_thread_count = new_size; - return 1; + return 1; } - + static int ticket_acquire_internal(xine_ticket_t *this, int irrevocable, int nonblocking) { int must_wait = 0; pthread_mutex_lock(&this->lock); int allowed_to_block = acquire_allowed_to_block(this); - + if (this->ticket_revoked && !this->irrevocable_tickets) must_wait = !nonblocking; else if (this->atomic_revoke && !pthread_equal(this->atomic_revoker_thread, pthread_self())) @@ -181,12 +182,12 @@ static int ticket_acquire_internal(xine_ticket_t *this, int irrevocable, int non pthread_cond_wait(&this->issued, &this->lock); } - + this->tickets_granted++; if (irrevocable) this->irrevocable_tickets++; - - pthread_mutex_unlock(&this->lock); + + pthread_mutex_unlock(&this->lock); return 1; } @@ -201,9 +202,9 @@ static void ticket_acquire(xine_ticket_t *this, int irrevocable) { static int release_allowed_to_block(xine_ticket_t *this) { pthread_t own_id = pthread_self(); unsigned entry; - + for(entry = 0; entry < this->holder_thread_count; ++entry) { - if(this->holder_threads[entry].holder == own_id) { + if(pthread_equal(this->holder_threads[entry].holder, own_id)) { this->holder_threads[entry].count--; return this->holder_threads[entry].count == 0; } @@ -217,18 +218,18 @@ static void ticket_release_internal(xine_ticket_t *this, int irrevocable, int no pthread_mutex_lock(&this->lock); int allowed_to_block = release_allowed_to_block(this); - + this->tickets_granted--; if (irrevocable) this->irrevocable_tickets--; - + if (this->ticket_revoked && !this->tickets_granted) pthread_cond_broadcast(&this->revoked); if (allowed_to_block) { if (this->ticket_revoked && !this->irrevocable_tickets && !nonblocking) pthread_cond_wait(&this->issued, &this->lock); } - + pthread_mutex_unlock(&this->lock); } @@ -243,17 +244,17 @@ static void ticket_release(xine_ticket_t *this, int irrevocable) { static void ticket_renew(xine_ticket_t *this, int irrevocable) { pthread_mutex_lock(&this->lock); - + this->tickets_granted--; - + _x_assert(this->ticket_revoked); if (!this->tickets_granted) pthread_cond_broadcast(&this->revoked); if (!this->irrevocable_tickets || !irrevocable) pthread_cond_wait(&this->issued, &this->lock); - + this->tickets_granted++; - + pthread_mutex_unlock(&this->lock); } @@ -262,12 +263,12 @@ static void ticket_issue(xine_ticket_t *this, int atomic) { if (!atomic) pthread_mutex_lock(&this->revoke_lock); pthread_mutex_lock(&this->lock); - + this->pending_revocations--; if (!this->pending_revocations) pthread_cond_broadcast(&this->issued); this->atomic_revoke = 0; - + pthread_mutex_unlock(&this->lock); pthread_mutex_unlock(&this->revoke_lock); } @@ -276,7 +277,7 @@ static void ticket_revoke(xine_ticket_t *this, int atomic) { pthread_mutex_lock(&this->revoke_lock); pthread_mutex_lock(&this->lock); - + this->pending_revocations++; this->ticket_revoked = 1; if (this->tickets_granted) @@ -287,7 +288,7 @@ static void ticket_revoke(xine_ticket_t *this, int atomic) { this->atomic_revoke = 1; this->atomic_revoker_thread = pthread_self(); } - + pthread_mutex_unlock(&this->lock); if (!atomic) pthread_mutex_unlock(&this->revoke_lock); @@ -299,15 +300,15 @@ static void ticket_dispose(xine_ticket_t *this) { pthread_mutex_destroy(&this->revoke_lock); pthread_cond_destroy(&this->issued); pthread_cond_destroy(&this->revoked); - + free(this); } -static xine_ticket_t *ticket_init(void) { +static xine_ticket_t *XINE_MALLOC ticket_init(void) { xine_ticket_t *port_ticket; - - port_ticket = (xine_ticket_t *) xine_xmalloc(sizeof(xine_ticket_t)); - + + port_ticket = calloc(1, sizeof(xine_ticket_t)); + port_ticket->acquire_nonblocking = ticket_acquire_nonblocking; port_ticket->acquire = ticket_acquire; port_ticket->release_nonblocking = ticket_release_nonblocking; @@ -318,37 +319,44 @@ static xine_ticket_t *ticket_init(void) { port_ticket->dispose = ticket_dispose; port_ticket->holder_thread_count = XINE_MAX_TICKET_HOLDER_THREADS; port_ticket->holder_threads = calloc(XINE_MAX_TICKET_HOLDER_THREADS,sizeof(*port_ticket->holder_threads)); - + pthread_mutex_init(&port_ticket->lock, NULL); pthread_mutex_init(&port_ticket->revoke_lock, NULL); pthread_cond_init(&port_ticket->issued, NULL); pthread_cond_init(&port_ticket->revoked, NULL); - + return port_ticket; } static void set_speed_internal (xine_stream_t *stream, int speed) { xine_t *xine = stream->xine; + int old_speed = xine->clock->speed; - if (xine->clock->speed != XINE_SPEED_PAUSE && speed == XINE_SPEED_PAUSE) + if (old_speed != XINE_SPEED_PAUSE && speed == XINE_SPEED_PAUSE) /* get all decoder and post threads in a state where they agree to be blocked */ xine->port_ticket->revoke(xine->port_ticket, 0); - - if (xine->clock->speed == XINE_SPEED_PAUSE && speed != XINE_SPEED_PAUSE) + + if (old_speed == XINE_SPEED_PAUSE && speed != XINE_SPEED_PAUSE) /* all decoder and post threads may continue now */ xine->port_ticket->issue(xine->port_ticket, 0); - - stream->xine->clock->set_fine_speed (stream->xine->clock, speed); + + if (old_speed != XINE_SPEED_PAUSE && speed == XINE_SPEED_PAUSE) + /* set master clock so audio_out loop can pause in a safe place */ + stream->xine->clock->set_fine_speed (stream->xine->clock, speed); /* see coment on audio_out loop about audio_paused */ if( stream->audio_out ) { xine->port_ticket->acquire(xine->port_ticket, 1); - + /* inform audio_out that speed has changed - he knows what to do */ stream->audio_out->set_property (stream->audio_out, AO_PROP_CLOCK_SPEED, speed); - + xine->port_ticket->release(xine->port_ticket, 1); } + + if (old_speed == XINE_SPEED_PAUSE || speed != XINE_SPEED_PAUSE) + /* master clock is set after resuming the audio device (audio_out loop may continue) */ + stream->xine->clock->set_fine_speed (stream->xine->clock, speed); } @@ -398,18 +406,18 @@ void xine_stop (xine_stream_t *stream) { stream->video_out->set_property(stream->video_out, VO_PROP_DISCARD_FRAMES, 1); stop_internal (stream); - + if (stream->slave && (stream->slave_affection & XINE_MASTER_SLAVE_STOP)) xine_stop(stream->slave); if (stream->video_out) - stream->video_out->set_property(stream->video_out, VO_PROP_DISCARD_FRAMES, 0); + stream->video_out->set_property(stream->video_out, VO_PROP_DISCARD_FRAMES, 0); if (stream->audio_out) stream->audio_out->set_property(stream->audio_out, AO_PROP_DISCARD_BUFFERS, 0); - + stream->xine->port_ticket->release(stream->xine->port_ticket, 1); stream->ignore_speed_change = 0; - + pthread_cleanup_pop (0); pthread_mutex_unlock (&stream->frontend_lock); } @@ -418,6 +426,7 @@ void xine_stop (xine_stream_t *stream) { static void close_internal (xine_stream_t *stream) { int i ; + int gapless_switch = stream->gapless_switch; if( stream->slave ) { xine_close( stream->slave ); @@ -428,32 +437,32 @@ static void close_internal (xine_stream_t *stream) { } } - if( !stream->gapless_switch ) { + if( !gapless_switch ) { /* make sure that other threads cannot change the speed, especially pauseing the stream */ pthread_mutex_lock(&stream->speed_change_lock); stream->ignore_speed_change = 1; pthread_mutex_unlock(&stream->speed_change_lock); stream->xine->port_ticket->acquire(stream->xine->port_ticket, 1); - + if (stream->audio_out) stream->audio_out->set_property(stream->audio_out, AO_PROP_DISCARD_BUFFERS, 1); if (stream->video_out) stream->video_out->set_property(stream->video_out, VO_PROP_DISCARD_FRAMES, 1); } - + stop_internal( stream ); - - if( !stream->gapless_switch ) { + + if( !gapless_switch ) { if (stream->video_out) - stream->video_out->set_property(stream->video_out, VO_PROP_DISCARD_FRAMES, 0); + stream->video_out->set_property(stream->video_out, VO_PROP_DISCARD_FRAMES, 0); if (stream->audio_out) stream->audio_out->set_property(stream->audio_out, AO_PROP_DISCARD_BUFFERS, 0); - + stream->xine->port_ticket->release(stream->xine->port_ticket, 1); stream->ignore_speed_change = 0; } - + if (stream->demux_plugin) { _x_free_demux_plugin(stream, stream->demux_plugin); stream->demux_plugin = NULL; @@ -515,14 +524,14 @@ static int stream_rewire_audio(xine_post_out_t *output, void *data) return 0; stream->xine->port_ticket->revoke(stream->xine->port_ticket, 1); - + if (stream->audio_out->status(stream->audio_out, stream, &bits, &rate, &mode)) { /* register our stream at the new output port */ (new_port->open) (new_port, stream, bits, rate, mode); stream->audio_out->close(stream->audio_out, stream); } stream->audio_out = new_port; - + stream->xine->port_ticket->issue(stream->xine->port_ticket, 1); return 1; @@ -534,25 +543,25 @@ static int stream_rewire_video(xine_post_out_t *output, void *data) xine_video_port_t *new_port = (xine_video_port_t *)data; int64_t img_duration; int width, height; - + if (!data) return 0; stream->xine->port_ticket->revoke(stream->xine->port_ticket, 1); - + if (stream->video_out->status(stream->video_out, stream, &width, &height, &img_duration)) { /* register our stream at the new output port */ (new_port->open) (new_port, stream); stream->video_out->close(stream->video_out, stream); } stream->video_out = new_port; - + stream->xine->port_ticket->issue(stream->xine->port_ticket, 1); return 1; } -void xine_dispose_internal (xine_stream_t *stream); +static void xine_dispose_internal (xine_stream_t *stream); xine_stream_t *xine_stream_new (xine_t *this, xine_audio_port_t *ao, xine_video_port_t *vo) { @@ -569,7 +578,7 @@ xine_stream_t *xine_stream_new (xine_t *this, pthread_mutex_lock (&this->streams_lock); - stream = (xine_stream_t *) xine_xmalloc (sizeof (xine_stream_t)) ; + stream = (xine_stream_t *) calloc (1, sizeof (xine_stream_t)) ; stream->current_extra_info = malloc( sizeof( extra_info_t ) ); stream->audio_decoder_extra_info = malloc( sizeof( extra_info_t ) ); stream->video_decoder_extra_info = malloc( sizeof( extra_info_t ) ); @@ -595,30 +604,31 @@ xine_stream_t *xine_stream_new (xine_t *this, stream->early_finish_event = 0; stream->delay_finish_event = 0; stream->gapless_switch = 0; + stream->keep_ao_driver_open = 0; stream->video_out = vo; if (vo) stream->video_driver = vo->driver; else stream->video_driver = NULL; - + stream->video_channel = 0; stream->video_decoder_plugin = NULL; stream->video_decoder_streamtype = -1; - stream->header_count_audio = 0; - stream->header_count_video = 0; - stream->finished_count_audio = 0; + stream->header_count_audio = 0; + stream->header_count_video = 0; + stream->finished_count_audio = 0; stream->finished_count_video = 0; stream->err = 0; stream->broadcaster = NULL; - + /* * initial master/slave */ stream->master = stream; stream->slave = NULL; stream->slave_is_subtitle = 0; - + /* * init mutexes and conditions */ @@ -628,6 +638,7 @@ xine_stream_t *xine_stream_new (xine_t *this, pthread_mutex_init (&stream->meta_mutex, NULL); pthread_mutex_init (&stream->demux_lock, NULL); pthread_mutex_init (&stream->demux_mutex, NULL); + pthread_cond_init (&stream->demux_resume, NULL); pthread_mutex_init (&stream->event_queues_lock, NULL); pthread_mutex_init (&stream->counter_lock, NULL); pthread_cond_init (&stream->counter_changed, NULL); @@ -652,7 +663,7 @@ xine_stream_t *xine_stream_new (xine_t *this, _x_meta_info_reset(stream, i); _x_meta_info_public_reset(stream, i); } - + /* * event queues */ @@ -723,22 +734,22 @@ xine_stream_t *xine_stream_new (xine_t *this, stream->video_source.type = XINE_POST_DATA_VIDEO; stream->video_source.data = stream; stream->video_source.rewire = stream_rewire_video; - + stream->audio_source.name = "audio source"; stream->audio_source.type = XINE_POST_DATA_AUDIO; stream->audio_source.data = stream; stream->audio_source.rewire = stream_rewire_audio; - + return stream; } void _x_mrl_unescape(char *mrl) { - int i, len = strlen(mrl); + size_t i, len = strlen(mrl); for (i = 0; i < len; i++) { if ((mrl[i]=='%') && (i<(len-2))) { unsigned int c; - + if (sscanf(&mrl[i + 1], "%02x", &c) == 1) { mrl[i]= (char)c; memmove(mrl + i + 1, mrl + i + 3, len - i - 3); @@ -780,11 +791,25 @@ void _x_flush_events_queues (xine_stream_t *stream) { pthread_mutex_unlock (&stream->event_queues_lock); } -/*static*/ int open_internal (xine_stream_t *stream, const char *mrl) { +static inline int _x_path_looks_like_mrl (const char *path) +{ + if ((*path & 0xDF) < 'A' || (*path & 0xDF) > 'Z') + return 0; + + for (++path; *path; ++path) + if ((*path != '-' && *path < '0') || (*path > '9' && *path < 'A') || + (*path > 'Z' && *path < 'a') || *path > 'z') + break; + + return path[0] == ':' && path[1] == '/'; +} + +static int open_internal (xine_stream_t *stream, const char *mrl) { const char *stream_setup = NULL; + const char *mrl_proto = NULL; int no_cache = 0; - + if (!mrl) { xprintf (stream->xine, XINE_VERBOSITY_LOG, _("xine: error while parsing mrl\n")); stream->err = XINE_ERROR_MALFORMED_MRL; @@ -806,33 +831,50 @@ void _x_flush_events_queues (xine_stream_t *stream) { /* * look for a stream_setup in MRL and try finding an input plugin */ + stream_setup = strchr (mrl, '#'); if (isalpha (*mrl)) { - stream_setup = mrl + 1; - while (isalnum (*stream_setup) || *stream_setup == '+' || *stream_setup == '-' || *stream_setup == '.') - ++stream_setup; - if (stream_setup[0] == ':' && stream_setup[1] == '/') - stream_setup = strchr (mrl, '#'); - else - stream_setup = NULL; + mrl_proto = mrl + 1; + while (isalnum (*mrl_proto) || *mrl_proto == '+' || *mrl_proto == '-' || *mrl_proto == '.') + ++mrl_proto; + if (!mrl_proto[0] || mrl_proto[0] != ':' || mrl_proto[1] != '/') + mrl_proto = NULL; + } + + /* for raw filenames we must try every '#' checking if it is part of the filename */ + if( !mrl_proto && stream_setup) { + struct stat stat_buf; + int res; + + while( stream_setup ) { + char *raw_filename = strndup (mrl, stream_setup - mrl); + + res = stat(raw_filename, &stat_buf); + free(raw_filename); + if( !res ) + break; + stream_setup = strchr(stream_setup + 1, '#'); + } } - + { char *input_source = strndup (mrl, stream_setup ? stream_setup - mrl : strlen (mrl)); /* * find an input plugin */ + stream->input_plugin = _x_find_input_plugin (stream, input_source); + free(input_source); - if ((stream->input_plugin = _x_find_input_plugin (stream, input_source))) { + if ( stream->input_plugin ) { int res; xine_log (stream->xine, XINE_LOG_MSG, _("xine: found input plugin : %s\n"), stream->input_plugin->input_class->get_description(stream->input_plugin->input_class)); if (stream->input_plugin->input_class->eject_media) stream->eject_class = stream->input_plugin->input_class; - _x_meta_info_set_utf8(stream, XINE_META_INFO_INPUT_PLUGIN, + _x_meta_info_set_utf8(stream, XINE_META_INFO_INPUT_PLUGIN, (stream->input_plugin->input_class->get_identifier (stream->input_plugin->input_class))); res = (stream->input_plugin->open) (stream->input_plugin); @@ -840,7 +882,6 @@ void _x_flush_events_queues (xine_stream_t *stream) { case 1: /* Open successfull */ break; case -1: /* Open unsuccessfull, but correct plugin */ - free(input_source); stream->err = XINE_ERROR_INPUT_FAILED; _x_flush_events_queues (stream); return 0; @@ -851,10 +892,8 @@ void _x_flush_events_queues (xine_stream_t *stream) { stream->err = XINE_ERROR_INPUT_FAILED; } } - - free(input_source); } - + if (!stream->input_plugin) { xine_log (stream->xine, XINE_LOG_MSG, _("xine: cannot find input plugin for MRL [%s]\n"),mrl); stream->err = XINE_ERROR_NO_INPUT_PLUGIN; @@ -917,11 +956,12 @@ void _x_flush_events_queues (xine_stream_t *stream) { memcpy(filename, tmp, strlen(tmp)); filename[strlen(tmp)] = '\0'; } + _x_mrl_unescape(filename); xine_log(stream->xine, XINE_LOG_MSG, _("xine: join rip input plugin\n")); input_saver = _x_rip_plugin_get_instance (stream, filename); free(filename); - + if( input_saver ) { stream->input_plugin = input_saver; } else { @@ -1091,13 +1131,15 @@ void _x_flush_events_queues (xine_stream_t *stream) { memcpy(subtitle_mrl, tmp, strlen(tmp)); subtitle_mrl[strlen(tmp)] = '\0'; } - _x_mrl_unescape(subtitle_mrl); + /* unescape for xine_open() if the MRL looks like a raw pathname */ + if (!_x_path_looks_like_mrl(subtitle_mrl)) + _x_mrl_unescape(subtitle_mrl); stream->slave = xine_stream_new (stream->xine, NULL, stream->video_out ); stream->slave_affection = XINE_MASTER_SLAVE_PLAY | XINE_MASTER_SLAVE_STOP; if( xine_open( stream->slave, subtitle_mrl ) ) { xprintf (stream->xine, XINE_VERBOSITY_LOG, _("subtitle mrl opened '%s'\n"), subtitle_mrl); stream->slave->master = stream; - stream->slave_is_subtitle = 1; + stream->slave_is_subtitle = 1; } else { xprintf(stream->xine, XINE_VERBOSITY_LOG, _("xine: error opening subtitle mrl\n")); xine_dispose( stream->slave ); @@ -1152,6 +1194,27 @@ void _x_flush_events_queues (xine_stream_t *stream) { /* enable buffered input plugin (request optimizer) */ stream->input_plugin = _x_cache_plugin_get_instance(stream, 0); + /* Let the plugin request a specific demuxer (if the user hasn't). + * This overrides find-by-content & find-by-extension. + */ + if (!stream->demux_plugin) + { + char *default_demux = NULL; + stream->input_plugin->get_optional_data (stream->input_plugin, &default_demux, INPUT_OPTIONAL_DATA_DEMUXER); + if (default_demux) + { + stream->demux_plugin = _x_find_demux_plugin_by_name (stream, default_demux, stream->input_plugin); + if (stream->demux_plugin) + { + lprintf ("demux and input plugin found\n"); + _x_meta_info_set_utf8 (stream, XINE_META_INFO_SYSTEMLAYER, + stream->demux_plugin->demux_class->get_identifier (stream->demux_plugin->demux_class)); + } + else + xine_log (stream->xine, XINE_LOG_MSG, _("xine: couldn't load plugin-specified demux %s for >%s<\n"), default_demux, mrl); + } + } + if (!stream->demux_plugin) { /* @@ -1275,9 +1338,9 @@ static int play_internal (xine_stream_t *stream, int start_pos, int start_time) /* set normal speed */ if (_x_get_speed(stream) != XINE_SPEED_NORMAL) set_speed_internal (stream, XINE_FINE_SPEED_NORMAL); - + stream->xine->port_ticket->acquire(stream->xine->port_ticket, 1); - + /* only flush/discard output ports on master streams */ if( stream->master == stream && !stream->gapless_switch) { /* discard audio/video buffers to get engine going and take the lock faster */ @@ -1286,25 +1349,26 @@ static int play_internal (xine_stream_t *stream, int start_pos, int start_time) if (stream->video_out) stream->video_out->set_property(stream->video_out, VO_PROP_DISCARD_FRAMES, 1); } - + pthread_mutex_lock( &stream->demux_lock ); /* demux_lock taken. now demuxer is suspended */ stream->demux_action_pending = 0; + pthread_cond_signal(&stream->demux_resume); - /* set normal speed again (now that demuxer/input pair is suspended) + /* set normal speed again (now that demuxer/input pair is suspended) * some input plugin may have changed speed by itself, we must ensure * the engine is not paused. */ if (_x_get_speed(stream) != XINE_SPEED_NORMAL) set_speed_internal (stream, XINE_FINE_SPEED_NORMAL); - + /* * start/seek demux */ /* seek to new position (no data is sent to decoders yet) */ demux_status = stream->demux_plugin->seek (stream->demux_plugin, - start_pos, start_time, + start_pos, start_time, stream->demux_thread_running); /* only flush/discard output ports on master streams */ @@ -1314,9 +1378,9 @@ static int play_internal (xine_stream_t *stream, int start_pos, int start_time) if (stream->video_out) stream->video_out->set_property(stream->video_out, VO_PROP_DISCARD_FRAMES, 0); } - + stream->xine->port_ticket->release(stream->xine->port_ticket, 1); - + /* before resuming the demuxer, set first_frame_flag */ pthread_mutex_lock (&stream->first_frame_lock); stream->first_frame_flag = 2; @@ -1328,7 +1392,7 @@ static int play_internal (xine_stream_t *stream, int start_pos, int start_time) pthread_mutex_unlock( &stream->current_extra_info_lock ); demux_thread_running = stream->demux_thread_running; - + /* now resume demuxer thread if it is running already */ pthread_mutex_unlock( &stream->demux_lock ); @@ -1351,7 +1415,7 @@ static int play_internal (xine_stream_t *stream, int start_pos, int start_time) * see video_out.c */ wait_first_frame (stream); - + xprintf (stream->xine, XINE_VERBOSITY_DEBUG, "play_internal ...done\n"); return 1; @@ -1365,26 +1429,26 @@ int xine_play (xine_stream_t *stream, int start_pos, int start_time) { pthread_cleanup_push (mutex_cleanup, (void *) &stream->frontend_lock); stream->delay_finish_event = 0; - + ret = play_internal (stream, start_pos, start_time); if( stream->slave && (stream->slave_affection & XINE_MASTER_SLAVE_PLAY) ) xine_play (stream->slave, start_pos, start_time); - + stream->gapless_switch = 0; pthread_cleanup_pop (0); pthread_mutex_unlock (&stream->frontend_lock); - + return ret; } int xine_eject (xine_stream_t *stream) { - + int status; - if (!stream->eject_class) + if (!stream->eject_class) return 0; - + pthread_mutex_lock (&stream->frontend_lock); pthread_cleanup_push (mutex_cleanup, (void *) &stream->frontend_lock); @@ -1399,11 +1463,11 @@ int xine_eject (xine_stream_t *stream) { pthread_cleanup_pop (0); pthread_mutex_unlock (&stream->frontend_lock); - + return status; } -void xine_dispose_internal (xine_stream_t *stream) { +static void xine_dispose_internal (xine_stream_t *stream) { xine_list_iterator_t *ite; @@ -1416,6 +1480,7 @@ void xine_dispose_internal (xine_stream_t *stream) { pthread_mutex_destroy (&stream->current_extra_info_lock); pthread_cond_destroy (&stream->counter_changed); pthread_mutex_destroy (&stream->demux_mutex); + pthread_cond_destroy (&stream->demux_resume); pthread_mutex_destroy (&stream->demux_lock); pthread_mutex_destroy (&stream->first_frame_lock); pthread_cond_destroy (&stream->first_frame_reached); @@ -1430,7 +1495,7 @@ void xine_dispose_internal (xine_stream_t *stream) { pthread_mutex_unlock(&stream->xine->streams_lock); _x_refcounter_dispose(stream->refcounter); - + free (stream->current_extra_info); free (stream->video_decoder_extra_info); free (stream->audio_decoder_extra_info); @@ -1448,7 +1513,7 @@ void xine_dispose (xine_stream_t *stream) { xine_close(stream); if( stream->master != stream ) { - stream->master->slave = NULL; + stream->master->slave = NULL; } if( stream->slave && stream->slave->master == stream ) { stream->slave->master = NULL; @@ -1456,7 +1521,7 @@ void xine_dispose (xine_stream_t *stream) { if(stream->broadcaster) _x_close_broadcaster(stream->broadcaster); - + xprintf (stream->xine, XINE_VERBOSITY_DEBUG, "shutdown audio\n"); _x_audio_decoder_shutdown (stream); @@ -1488,13 +1553,13 @@ void xine_exit (xine_t *this) { if(this->clock) this->clock->exit (this->clock); - + if(this->config) this->config->dispose(this->config); if(this->port_ticket) this->port_ticket->dispose(this->port_ticket); - + #if defined(WIN32) WSACleanup(); #endif @@ -1510,7 +1575,7 @@ xine_t *xine_new (void) { int i_err; #endif - this = xine_xmalloc (sizeof (xine_t)); + this = calloc(1, sizeof (xine_t)); if (!this) _x_abort(); @@ -1552,7 +1617,7 @@ xine_t *xine_new (void) { #endif /* WIN32 */ this->verbosity = XINE_VERBOSITY_NONE; - + return this; } @@ -1599,18 +1664,17 @@ static void config_save_cb (void *this_gen, xine_cfg_entry_t *entry) { xine_t *this = (xine_t *)this_gen; char *homedir_trail_slash; - homedir_trail_slash = (char *)malloc(strlen(xine_get_homedir()) + 2); - sprintf(homedir_trail_slash, "%s/", xine_get_homedir()); + asprintf(&homedir_trail_slash, "%s/", xine_get_homedir()); if (entry->str_value[0] && (entry->str_value[0] != '/' || strstr(entry->str_value, "/.") || strcmp(entry->str_value, xine_get_homedir()) == 0 || strcmp(entry->str_value, homedir_trail_slash) == 0)) { xine_stream_t *stream; xine_list_iterator_t ite; - + xine_log(this, XINE_LOG_MSG, _("xine: The specified save_dir \"%s\" might be a security risk.\n"), entry->str_value); - + pthread_mutex_lock(&this->streams_lock); if ( (ite = xine_list_front(this->streams)) ) { stream = xine_list_get_value(this->streams, ite); @@ -1618,7 +1682,7 @@ static void config_save_cb (void *this_gen, xine_cfg_entry_t *entry) { } pthread_mutex_unlock(&this->streams_lock); } - + free(homedir_trail_slash); this->save_path = entry->str_value; } @@ -1627,6 +1691,12 @@ void xine_init (xine_t *this) { static const char *demux_strategies[] = {"default", "reverse", "content", "extension", NULL}; + /* + * locks + */ + pthread_mutex_init (&this->streams_lock, NULL); + pthread_mutex_init (&this->log_lock, NULL); + /* initialize color conversion tables and functions */ init_yuv_conversion(); @@ -1666,7 +1736,7 @@ void xine_init (xine_t *this) { * save directory */ this->save_path = this->config->register_filename ( - this->config, + this->config, "media.capture.save_dir", "", XINE_CONFIG_STRING_IS_DIRECTORY_NAME, _("directory for saving streams"), _("When using the stream save feature, files will be written only into this directory.\n" @@ -1674,7 +1744,7 @@ void xine_init (xine_t *this) { "can be used to fill files in it with arbitrary content. So you should be careful that " "the directory you specify is robust against any content in any file."), XINE_CONFIG_SECURITY, config_save_cb, this); - + /* * implicit configuration changes */ @@ -1708,19 +1778,13 @@ void xine_init (xine_t *this) { this->streams = xine_list_new(); /* - * locks - */ - pthread_mutex_init (&this->streams_lock, NULL); - pthread_mutex_init (&this->log_lock, NULL); - - /* * start metronom clock */ this->clock = _x_metronom_clock_init(this); this->clock->start_clock (this->clock, 0); - + /* * tickets */ @@ -1733,7 +1797,7 @@ void _x_select_spu_channel (xine_stream_t *stream, int channel) { stream->spu_channel_user = (channel >= -2 ? channel : -2); stream->xine->port_ticket->acquire(stream->xine->port_ticket, 0); - + switch (stream->spu_channel_user) { case -2: stream->spu_channel = -1; @@ -1753,7 +1817,7 @@ void _x_select_spu_channel (xine_stream_t *stream, int channel) { lprintf("set to %d\n",stream->spu_channel); stream->xine->port_ticket->release(stream->xine->port_ticket, 0); - + pthread_mutex_unlock (&stream->frontend_lock); } @@ -1813,10 +1877,10 @@ void _x_set_fine_speed (xine_stream_t *stream, int speed) { { if (speed <= XINE_SPEED_PAUSE) speed = XINE_SPEED_PAUSE; - + xprintf (stream->xine, XINE_VERBOSITY_DEBUG, "set_speed %d\n", speed); set_speed_internal (stream, speed); - + if (stream->slave && (stream->slave_affection & XINE_MASTER_SLAVE_SPEED)) set_speed_internal (stream->slave, speed); } @@ -1828,7 +1892,7 @@ int _x_get_fine_speed (xine_stream_t *stream) { } void _x_set_speed (xine_stream_t *stream, int speed) { - + if (speed > XINE_SPEED_FAST_4) speed = XINE_SPEED_FAST_4; @@ -1837,13 +1901,13 @@ void _x_set_speed (xine_stream_t *stream, int speed) { int _x_get_speed (xine_stream_t *stream) { int speed = _x_get_fine_speed (stream); - + /* - * ensure compatibility with old API, only valid XINE_SPEED_xxx + * ensure compatibility with old API, only valid XINE_SPEED_xxx * constants are allowed. XINE_SPEED_NORMAL may only be returned * if speed is exactly XINE_FINE_SPEED_NORMAL. */ - + if( speed <= XINE_SPEED_PAUSE ) return XINE_SPEED_PAUSE; if( speed <= XINE_SPEED_SLOW_4 * XINE_FINE_SPEED_NORMAL / XINE_SPEED_NORMAL ) @@ -1899,50 +1963,98 @@ int xine_get_pos_length (xine_stream_t *stream, int *pos_stream, return 1; } -int xine_get_current_frame (xine_stream_t *stream, int *width, int *height, - int *ratio_code, int *format, - uint8_t *img) { +static int _x_get_current_frame_data (xine_stream_t *stream, + xine_current_frame_data_t *data, + int flags, int img_size_unknown) { vo_frame_t *frame; + size_t required_size; stream->xine->port_ticket->acquire(stream->xine->port_ticket, 0); frame = stream->video_out->get_last_frame (stream->video_out); stream->xine->port_ticket->release(stream->xine->port_ticket, 0); - - if (!frame) + + if (!frame) { + data->img_size = 0; return 0; + } - *width = frame->width; - *height = frame->height; + data->width = frame->width; + data->height = frame->height; + data->crop_left = frame->crop_left; + data->crop_right = frame->crop_right; + data->crop_top = frame->crop_top; + data->crop_bottom = frame->crop_bottom; - *ratio_code = 10000.0 * frame->ratio; + data->ratio_code = 10000.0 * frame->ratio; /* make ratio_code backward compatible */ #define RATIO_LIKE(a, b) ((b) - 1 <= (a) && (a) <= 1 + (b)) - if (RATIO_LIKE(*ratio_code, 10000)) - *ratio_code = XINE_VO_ASPECT_SQUARE; - else if (RATIO_LIKE(*ratio_code, 13333)) - *ratio_code = XINE_VO_ASPECT_4_3; - else if (RATIO_LIKE(*ratio_code, 17778)) - *ratio_code = XINE_VO_ASPECT_ANAMORPHIC; - else if (RATIO_LIKE(*ratio_code, 21100)) - *ratio_code = XINE_VO_ASPECT_DVB; - - *format = frame->format; - - if (img){ + if (RATIO_LIKE(data->ratio_code, 10000)) + data->ratio_code = XINE_VO_ASPECT_SQUARE; + else if (RATIO_LIKE(data->ratio_code, 13333)) + data->ratio_code = XINE_VO_ASPECT_4_3; + else if (RATIO_LIKE(data->ratio_code, 17778)) + data->ratio_code = XINE_VO_ASPECT_ANAMORPHIC; + else if (RATIO_LIKE(data->ratio_code, 21100)) + data->ratio_code = XINE_VO_ASPECT_DVB; + + data->format = frame->format; + data->interlaced = frame->progressive_frame ? 0 : (2 - frame->top_field_first); + + switch (frame->format) { + + case XINE_IMGFMT_YV12: + required_size = frame->width * frame->height + + ((frame->width + 1) / 2) * ((frame->height + 1) / 2) + + ((frame->width + 1) / 2) * ((frame->height + 1) / 2); + break; + + case XINE_IMGFMT_YUY2: + required_size = frame->width * frame->height + + ((frame->width + 1) / 2) * frame->height + + ((frame->width + 1) / 2) * frame->height; + break; + + default: + if (data->img || (flags & XINE_FRAME_DATA_ALLOCATE_IMG)) { + xprintf (stream->xine, XINE_VERBOSITY_DEBUG, + "xine: error, snapshot function not implemented for format 0x%x\n", frame->format); + _x_abort (); + } + + required_size = 0; + } + + if (flags & XINE_FRAME_DATA_ALLOCATE_IMG) { + /* return allocated buffer size */ + data->img_size = required_size; + /* allocate img or fail */ + if (!(data->img = calloc(1, required_size))) + return 0; + } else { + /* fail if supplied buffer is to small */ + if (data->img && !img_size_unknown && data->img_size < required_size) { + data->img_size = required_size; + return 0; + } + /* return used buffer size */ + data->img_size = required_size; + } + + if (data->img) { switch (frame->format) { case XINE_IMGFMT_YV12: yv12_to_yv12( /* Y */ frame->base[0], frame->pitches[0], - img, frame->width, + data->img, frame->width, /* U */ frame->base[1], frame->pitches[1], - img+frame->width*frame->height, frame->width/2, + data->img+frame->width*frame->height, frame->width/2, /* V */ frame->base[2], frame->pitches[2], - img+frame->width*frame->height+frame->width*frame->height/4, frame->width/2, + data->img+frame->width*frame->height+frame->width*frame->height/4, frame->width/2, /* width x height */ frame->width, frame->height); break; @@ -1952,13 +2064,13 @@ int xine_get_current_frame (xine_stream_t *stream, int *width, int *height, /* src */ frame->base[0], frame->pitches[0], /* dst */ - img, frame->width*2, + data->img, frame->width*2, /* width x height */ frame->width, frame->height); break; default: - xprintf (stream->xine, XINE_VERBOSITY_DEBUG, + xprintf (stream->xine, XINE_VERBOSITY_DEBUG, "xine: error, snapshot function not implemented for format 0x%x\n", frame->format); _x_abort (); } @@ -1966,6 +2078,69 @@ int xine_get_current_frame (xine_stream_t *stream, int *width, int *height, return 1; } +int xine_get_current_frame_data (xine_stream_t *stream, + xine_current_frame_data_t *data, + int flags) { + + return _x_get_current_frame_data(stream, data, flags, 0); +} + +int xine_get_current_frame_alloc (xine_stream_t *stream, int *width, int *height, + int *ratio_code, int *format, + uint8_t **img, int *img_size) { + + int result; + xine_current_frame_data_t data; + + memset(&data, 0, sizeof (data)); + + result = _x_get_current_frame_data(stream, &data, img ? XINE_FRAME_DATA_ALLOCATE_IMG : 0, 0); + if (width) *width = data.width; + if (height) *height = data.height; + if (ratio_code) *ratio_code = data.ratio_code; + if (format) *format = data.format; + if (img_size) *img_size = data.img_size; + if (img) *img = data.img; + return result; +} + +int xine_get_current_frame_s (xine_stream_t *stream, int *width, int *height, + int *ratio_code, int *format, + uint8_t *img, int *img_size) { + int result; + xine_current_frame_data_t data; + + memset(&data, 0, sizeof (data)); + data.img = img; + if (img_size) + data.img_size = *img_size; + + result = _x_get_current_frame_data(stream, &data, 0, 0); + if (width) *width = data.width; + if (height) *height = data.height; + if (ratio_code) *ratio_code = data.ratio_code; + if (format) *format = data.format; + if (img_size) *img_size = data.img_size; + return result; +} + +int xine_get_current_frame (xine_stream_t *stream, int *width, int *height, + int *ratio_code, int *format, + uint8_t *img) { + int result; + xine_current_frame_data_t data; + + memset(&data, 0, sizeof (data)); + data.img = img; + + result = _x_get_current_frame_data(stream, &data, 0, 1); + if (width) *width = data.width; + if (height) *height = data.height; + if (ratio_code) *ratio_code = data.ratio_code; + if (format) *format = data.format; + return result; +} + int xine_get_video_frame (xine_stream_t *stream, int timestamp, /* msec */ int *width, int *height, @@ -2072,7 +2247,7 @@ static inline void check_log_alloc (xine_t *this, int buf) void xine_log (xine_t *this, int buf, const char *format, ...) { va_list argp; char buffer[SCRATCH_LINE_LEN_MAX]; - + check_log_alloc (this, buf); va_start (argp, format); @@ -2084,15 +2259,21 @@ void xine_log (xine_t *this, int buf, const char *format, ...) { vsnprintf(buffer, SCRATCH_LINE_LEN_MAX, format, argp); printf("%s", buffer); va_end (argp); - } + } + + if (this->log_cb) + this->log_cb (this->log_cb_user_data, buf); } -void xine_vlog(xine_t *this, int buf, const char *format, +void xine_vlog(xine_t *this, int buf, const char *format, va_list args) { check_log_alloc (this, buf); this->log_buffers[buf]->scratch_printf(this->log_buffers[buf], format, args); + + if (this->log_cb) + this->log_cb (this->log_cb_user_data, buf); } char *const *xine_get_log (xine_t *this, int buf) { @@ -2107,12 +2288,10 @@ char *const *xine_get_log (xine_t *this, int buf) { } void xine_register_log_cb (xine_t *this, xine_log_cb_t cb, void *user_data) { - - printf ("xine: xine_register_log_cb: not implemented yet.\n"); - _x_abort(); + this->log_cb = cb; + this->log_cb_user_data = user_data; } - int xine_get_error (xine_stream_t *stream) { return stream->err; } @@ -2139,13 +2318,13 @@ int _x_query_buffer_usage(xine_stream_t *stream, int *num_video_buffers, int *nu if (num_video_buffers) *num_video_buffers = (stream->video_fifo ? stream->video_fifo->size(stream->video_fifo) : 0); - + if (num_audio_buffers) *num_audio_buffers = (stream->audio_fifo ? stream->audio_fifo->size(stream->audio_fifo) : 0); - + if ((num_video_frames && stream->video_out) || (num_audio_frames && stream->audio_out)) { - + ticket_acquired = stream->xine->port_ticket->acquire_nonblocking(stream->xine->port_ticket, 1); } |