diff -r 5505e8908944 -r 71e347f905f2 gst_plugins_symbian/gst/devsound/gstdevsoundsink.c --- a/gst_plugins_symbian/gst/devsound/gstdevsoundsink.c Fri Jan 22 09:59:59 2010 +0200 +++ b/gst_plugins_symbian/gst/devsound/gstdevsoundsink.c Fri Mar 19 09:35:09 2010 +0200 @@ -32,6 +32,10 @@ #include "gstilbcdecoderinterface.h" #include "string.h" #include +#ifdef AV_SYNC +#include +#endif /*AV_SYNC*/ + GST_DEBUG_CATEGORY_EXTERN (devsound_debug); #define GST_CAT_DEFAULT devsound_debug @@ -59,12 +63,21 @@ static GstCaps *gst_devsound_sink_getcaps(GstBaseSink * bsink); static gboolean gst_devsound_sink_setcaps(GstBaseSink *bsink, GstCaps *caps); +static gboolean gst_devsound_sink_event(GstBaseSink * asink, GstEvent * event); +#ifdef AV_SYNC +static void gst_devsound_sink_get_times(GstBaseSink * bsink, GstBuffer * buffer, + GstClockTime * start, GstClockTime * end); +static GstClock *gst_devsound_sink_provide_clock (GstElement * element); +static GstClockTime gst_devsound_sink_get_time (GstClock * clock, + gpointer user_data); +#endif /*AV_SYNC*/ -static gboolean gst_devsound_sink_event(GstBaseSink * asink, GstEvent * event); +static GstStateChangeReturn gst_devsound_sink_change_state (GstElement * element, + GstStateChange transition); + static void *StartDevSoundThread(void *threadid); - //Error concealment interface impl static void gst_error_concealment_handler_init (gpointer g_iface, gpointer iface_data); @@ -97,6 +110,7 @@ static gint gst_SetIlbcDecoderMode(enum TIlbcDecodeMode aDecodeMode); static void gst_Apply_Ilbc_Decoder_Update(GstDevsoundSink* dssink ); +static void get_PopulateIntfcProperties(GstDevsoundSink* dssink); static gboolean gst_sink_start (GstBaseSink * sink); static gboolean gst_sink_stop (GstBaseSink * sink); @@ -160,23 +174,26 @@ VOLUME, MAXVOLUME, VOLUMERAMP, - CHANNELS, +/* CHANNELS,*/ LEFTBALANCE, RIGHTBALANCE, - RATE, +/* RATE,*/ PRIORITY, PREFERENCE, - SAMPLESPLAYED, - FOURCC, //FOURCC is not needed - MIMETYPE, +/* SAMPLESPLAYED,*/ +/* FOURCC, //FOURCC is not needed*/ +/* MIMETYPE,*/ OUTPUTDEVICE }; enum command_to_consumer_thread_enum { OPEN = 2, - WRITEDATA, + PLAYING, + PAUSE, + RESUME, /*UPDATE,*/ + WAIT, CLOSE }; enum command_to_consumer_thread_enum cmd; @@ -185,33 +202,14 @@ GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, - GST_STATIC_CAPS ("audio/x-raw-int, " - "endianness = (int) { " G_STRINGIFY (G_BYTE_ORDER) " }, " - "signed = (boolean) TRUE, " - "width = (int) 16, " - "depth = (int) 16, " - "rate = (int) [ 8000, 48000 ]," - "channels = (int) [ 1, 2 ]; " - "audio/amr, " - //"width = (int) 8, " - //"depth = (int) 8, " - "rate = (int) 8000, " - "channels = (int) 1 ; " - "audio/x-alaw, " - "rate = (int) [ 8000, 48000 ], " - "channels = (int) [ 1, 2 ]; " - "audio/g729, " - "rate = (int) [ 8000, 48000 ], " - "channels = (int) [ 1, 2 ]; " - "audio/mp3, " - "rate = (int) [ 8000, 48000 ], " - "channels = (int) [ 1, 2 ]; " - "audio/ilbc, " - "rate = (int) [ 8000, 48000 ], " - "channels = (int) [ 1, 2 ]; " - "audio/x-mulaw, " - "rate = (int) [ 8000, 48000 ], " - "channels = (int) [ 1, 2 ]") + GST_STATIC_CAPS ("audio/x-raw-int, " "endianness = (int) { " G_STRINGIFY (G_BYTE_ORDER) " }, " "signed = (boolean) TRUE, " "width = (int) 16, " "depth = (int) 16, " "rate = (int) [ 8000, 48000 ]," "channels = (int) [ 1, 2 ]; " + "audio/amr, " "rate = (int) 8000, " "channels = (int) 1 ; " + "audio/AMR, " "rate = (int) 8000, " "channels = (int) 1 ; " + "audio/x-alaw, " "rate = (int) [ 8000, 48000 ], " "channels = (int) [ 1, 2 ]; " + "audio/g729, " "rate = (int) [ 8000, 48000 ], " "channels = (int) [ 1, 2 ]; " + "audio/mp3, " "rate = (int) [ 8000, 48000 ], " "channels = (int) [ 1, 2 ]; " + "audio/ilbc, " "rate = (int) [ 8000, 48000 ], " "channels = (int) [ 1, 2 ]; " + "audio/x-mulaw, " "rate = (int) [ 8000, 48000 ], " "channels = (int) [ 1, 2 ]") ); static GstElementClass *parent_class= NULL; @@ -284,14 +282,20 @@ static void gst_devsound_sink_dispose(GObject * object) { - GstDevsoundSink *devsoundsink= GST_DEVSOUND_SINK (object); + GstDevsoundSink *devsoundsink = GST_DEVSOUND_SINK (object); if (devsoundsink->probed_caps) { gst_caps_unref(devsoundsink->probed_caps); devsoundsink->probed_caps = NULL; } - +#ifdef AV_SYNC + if (devsoundsink->clock) + { + gst_object_unref (devsoundsink->clock); + } + devsoundsink->clock = NULL; +#endif /*AV_SYNC*/ G_OBJECT_CLASS (parent_class)->dispose (object); } @@ -324,7 +328,10 @@ gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_devsound_sink_finalise); gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_devsound_sink_get_property); gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_devsound_sink_set_property); - + + + gstelement_class->change_state = GST_DEBUG_FUNCPTR(gst_devsound_sink_change_state); + g_object_class_install_property(gobject_class, PROP_DEVICE, g_param_spec_string("device", "Device", "Devsound device ", DEFAULT_DEVICE, G_PARAM_READWRITE)); @@ -348,11 +355,11 @@ g_object_class_install_property(gobject_class, RIGHTBALANCE, g_param_spec_int("rightbalance", "Right Balance", "Right Balance", -1, G_MAXINT, -1, G_PARAM_READWRITE)); - +/* g_object_class_install_property(gobject_class, SAMPLESPLAYED, g_param_spec_int("samplesplayed", "Samples Played", "Samples Played", -1, G_MAXINT, -1, G_PARAM_READABLE)); - +*/ g_object_class_install_property(gobject_class, PRIORITY, g_param_spec_int("priority", "Priority", "Priority ", -1, G_MAXINT, -1, @@ -362,7 +369,7 @@ g_param_spec_int("preference", "Preference", "Preference ", -1, G_MAXINT, -1, G_PARAM_READWRITE)); - +/* g_object_class_install_property(gobject_class, RATE, g_param_spec_int("rate", "Rate", "Rate ", -1, G_MAXINT, -1, @@ -372,12 +379,16 @@ g_param_spec_int("channels", "Channels", "Channels ", -1, G_MAXINT, -1, G_PARAM_READWRITE)); - +*/ g_object_class_install_property(gobject_class, OUTPUTDEVICE, g_param_spec_int("outputdevice", "Output Device", "Output Device ", -1, G_MAXINT, -1, G_PARAM_READWRITE)); +#ifdef AV_SYNC + gstelement_class->provide_clock = GST_DEBUG_FUNCPTR (gst_devsound_sink_provide_clock); +#endif /*AV_SYNC*/ + gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_sink_start); gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_sink_stop); gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_sink_render); @@ -385,35 +396,47 @@ gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_devsound_sink_getcaps); gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_devsound_sink_setcaps); gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_devsound_sink_event); +#ifdef AV_SYNC + gstbasesink_class->get_times = GST_DEBUG_FUNCPTR (gst_devsound_sink_get_times); +#endif /*AV_SYNC*/ } -static void gst_devsound_sink_init(GstDevsoundSink * devsoundsink) +static void gst_devsound_sink_init(GstDevsoundSink * dssink) { - GST_DEBUG_OBJECT(devsoundsink, "initializing devsoundsink"); - devsoundsink->device = g_strdup(DEFAULT_DEVICE); - devsoundsink->handle = NULL; - devsoundsink->preference = 0; //default=>EMdaPriorityPreferenceNone; - devsoundsink->priority = 0; //default=>EMdaPriorityNormal; + GST_DEBUG_OBJECT(dssink, "initializing devsoundsink"); + dssink->device = g_strdup(DEFAULT_DEVICE); + dssink->handle = NULL; + dssink->preference = 0; //default=>EMdaPriorityPreferenceNone; + dssink->priority = 0; //default=>EMdaPriorityNormal; +#ifdef AV_SYNC + dssink->time_or_samples_played = 0; + dssink->timeplayedavailable = FALSE; + /* Create the provided clock. */ + dssink->clock = gst_audio_clock_new ("clock", gst_devsound_sink_get_time, dssink); +#endif /*AV_SYNC*/ pthread_mutex_init(&ds_mutex, NULL); pthread_cond_init(&ds_condition, NULL); } static void *StartDevSoundThread(void *threadarg) { - - GstDevsoundSink *devsound; + GstDevsoundSink *dssink; gint remainingDataLen = 0; GstBuffer *buffer = NULL; gboolean lastBufferSet=FALSE; - devsound = (GstDevsoundSink*) threadarg; + dssink = (GstDevsoundSink*) threadarg; - open_devsound(&(devsound->handle)); + // TODO handle error here + open_devsound(&(dssink->handle)); +#ifdef AV_SYNC + dssink->timeplayedavailable = is_timeplayed_supported(dssink->handle); +#endif /*AV_SYNC*/ //get supported (in/out)put datatypes //from devsound to build caps - getsupporteddatatypes(devsound); + getsupporteddatatypes(dssink); // TODO obtain mutex to update variable here??? consumer_thread_state = CONSUMER_THREAD_INITIALIZED; @@ -438,82 +461,94 @@ { //TODO if there is preemption we have to somehow signal //the pipeline in the render - initialize_devsound(devsound); + initialize_devsound(dssink); - playinit(devsound->handle); - initproperties(devsound); + playinit(dssink->handle); + dssink->eosreceived = FALSE; + initproperties(dssink); } while (1) { switch (cmd) { - case WRITEDATA: + case PAUSE: + pause_devsound(dssink); + cmd = WAIT; + break; + + case RESUME: + resume_devsound(dssink); + cmd = PLAYING; + break; + + case WAIT: + pthread_mutex_lock(&ds_mutex); + pthread_cond_signal(&ds_condition); + pthread_mutex_unlock(&ds_mutex); + + pthread_mutex_lock(&ds_mutex); + pthread_cond_wait(&ds_condition, &ds_mutex); + pthread_mutex_unlock(&ds_mutex); + break; + + case PLAYING: { - pre_init_setconf(devsound); - gst_Apply_ErrorConcealment_Update(devsound); - gst_Apply_G711_Decoder_Update(devsound); - gst_Apply_G729_Decoder_Update(devsound); - gst_Apply_Ilbc_Decoder_Update(devsound); + pre_init_setconf(dssink); + gst_Apply_ErrorConcealment_Update(dssink); + gst_Apply_G711_Decoder_Update(dssink); + gst_Apply_G729_Decoder_Update(dssink); + gst_Apply_Ilbc_Decoder_Update(dssink); // TODO we could do this in BTBF callback - populateproperties(devsound); - - framemodereq = devsound->framemodereq; - g711cng = devsound->g711cng; - ilbccng = devsound->ilbccng; - output = devsound->output; - + populateproperties(dssink); + get_PopulateIntfcProperties(dssink); + if(buffer_queue->length > 0) { if (remainingDataLen == 0) { // TODO enable lock and unlock - GST_OBJECT_LOCK (devsound); + GST_OBJECT_LOCK (dssink); buffer = GST_BUFFER_CAST(g_queue_peek_head(buffer_queue)); - GST_OBJECT_UNLOCK(devsound); + GST_OBJECT_UNLOCK(dssink); remainingDataLen = GST_BUFFER_SIZE(buffer); } lastBufferSet = GST_BUFFER_FLAG_IS_SET(buffer,GST_BUFFER_FLAG_LAST); - remainingDataLen = write_data(devsound->handle, + remainingDataLen = write_data(dssink->handle, GST_BUFFER_DATA(buffer) + (GST_BUFFER_SIZE(buffer) - remainingDataLen), remainingDataLen, lastBufferSet); if (remainingDataLen == 0) { - GST_OBJECT_LOCK (devsound); + GST_OBJECT_LOCK (dssink); buffer = GST_BUFFER_CAST(g_queue_pop_head(buffer_queue)); - GST_OBJECT_UNLOCK(devsound); + GST_OBJECT_UNLOCK(dssink); gst_buffer_unref(buffer); buffer = NULL; } if (lastBufferSet && remainingDataLen == 0) { - // Last Buffer is already sent to DevSound - // and we have received PlayError so now we exit - // from the big loop next time -/* - pthread_mutex_lock(&ds_mutex); - pthread_cond_signal(&ds_condition); - pthread_mutex_unlock(&ds_mutex); -*/ - cmd = CLOSE; - } + lastBufferSet = FALSE; + dssink->eosreceived = FALSE; + playinit(dssink->handle); + initproperties(dssink); + get_PopulateIntfcProperties(dssink); + cmd = WAIT; + } } else { - pthread_mutex_lock(&ds_mutex); - pthread_cond_wait(&ds_condition, &ds_mutex); - pthread_mutex_unlock(&ds_mutex); + cmd = WAIT; } } break; case CLOSE: { - close_devsound(devsound); - devsound->handle= NULL; + close_devsound(dssink); + dssink->handle= NULL; pthread_mutex_lock(&ds_mutex); pthread_cond_signal(&ds_condition); pthread_mutex_unlock(&ds_mutex); @@ -537,7 +572,7 @@ static gboolean gst_sink_start (GstBaseSink * sink) { GstBuffer *tmp_gstbuffer=NULL; - GstDevsoundSink *devsound = GST_DEVSOUND_SINK(sink); + GstDevsoundSink *dssink = GST_DEVSOUND_SINK(sink); if(buffer_queue) { @@ -557,7 +592,7 @@ consumer_thread_state = CONSUMER_THREAD_INITIALIZING; cmd = OPEN; - pthread_create(&ds_thread, NULL, StartDevSoundThread, (void *)devsound); + pthread_create(&ds_thread, NULL, StartDevSoundThread, (void *)dssink); // Wait until consumer thread is created // TODO : obtain mutex to retreive thread state? @@ -574,7 +609,7 @@ static gboolean gst_sink_stop (GstBaseSink * sink) { GstBuffer *tmp_gstbuffer=NULL; - GstDevsoundSink *devsound = GST_DEVSOUND_SINK(sink); + GstDevsoundSink *dssink = GST_DEVSOUND_SINK(sink); cmd = CLOSE; @@ -582,7 +617,12 @@ pthread_cond_signal(&ds_condition); pthread_mutex_unlock(&ds_mutex); - GST_OBJECT_LOCK(devsound); + pthread_mutex_lock(&ds_mutex); + pthread_cond_wait(&ds_condition, &ds_mutex); + pthread_mutex_unlock(&ds_mutex); + + + GST_OBJECT_LOCK(dssink); while (buffer_queue->length) { tmp_gstbuffer = (GstBuffer*)g_queue_pop_tail(buffer_queue); @@ -590,7 +630,7 @@ } g_queue_free(buffer_queue); buffer_queue = NULL; - GST_OBJECT_UNLOCK(devsound); + GST_OBJECT_UNLOCK(dssink); return TRUE; } @@ -598,21 +638,21 @@ static GstFlowReturn gst_sink_render (GstBaseSink * sink, GstBuffer * buffer) { - GstDevsoundSink *devsound = GST_DEVSOUND_SINK(sink); + GstDevsoundSink *dssink = GST_DEVSOUND_SINK(sink); GstBuffer* tmp; - if (get_ds_cb_error(devsound->handle)) + if (get_ds_cb_error(dssink->handle)) { return GST_FLOW_CUSTOM_ERROR; } tmp = gst_buffer_copy(buffer); - GST_OBJECT_LOCK (devsound); + GST_OBJECT_LOCK (dssink); g_queue_push_tail (buffer_queue, tmp); - GST_OBJECT_UNLOCK (devsound); + GST_OBJECT_UNLOCK (dssink); - cmd = WRITEDATA; + cmd = PLAYING; pthread_mutex_lock(&ds_mutex); pthread_cond_signal(&ds_condition); pthread_mutex_unlock(&ds_mutex); @@ -622,7 +662,7 @@ static void gst_devsound_sink_finalise(GObject * object) { - GstDevsoundSink *devsoundsink= GST_DEVSOUND_SINK (object); + GstDevsoundSink *devsoundsink = GST_DEVSOUND_SINK (object); g_free(devsoundsink->device); } @@ -646,7 +686,7 @@ sink->probed_caps = NULL; } break; - case CHANNELS: +/* case CHANNELS: sink->channels = g_value_get_int(value); sink->pending.channelsupdate = TRUE; break; @@ -656,7 +696,7 @@ sink->rate = gst_devsound_sink_get_rate(sink->rate); sink->pending.rateupdate = TRUE; break; - case VOLUME: +*/ case VOLUME: sink->volume = g_value_get_int(value); sink->pending.volumeupdate = TRUE; break; @@ -680,14 +720,13 @@ sink->preference = g_value_get_int(value); sink->pending.preferenceupdate = TRUE; break; - case FOURCC: //FOURCC is not needed +/* case FOURCC: //FOURCC is not needed sink->fourcc = g_value_get_int(value); sink->pending.fourccupdate = TRUE; break; - case MIMETYPE: sink->mimetype = g_value_dup_string(value); - break; + break;*/ case OUTPUTDEVICE: sink->output = g_value_get_int(value); sink->pending.outputupdate = TRUE; @@ -710,21 +749,21 @@ case PROP_DEVICE: g_value_set_string(value, sink->device); break; - case CHANNELS: +/* case CHANNELS: g_value_set_int(value, sink->channels); break; case RATE: g_value_set_int(value, sink->rate); - break; + break;*/ case VOLUME: g_value_set_int(value, sink->volume); break; case MAXVOLUME: g_value_set_int(value, sink->maxvolume); break; - case SAMPLESPLAYED: +/* case SAMPLESPLAYED: g_value_set_int(value, sink->samplesplayed); - break; + break;*/ case OUTPUTDEVICE: g_value_set_int(value, sink->output); break; @@ -923,14 +962,14 @@ static gboolean gst_devsound_sink_event(GstBaseSink *asink, GstEvent *event) { - GstDevsoundSink *sink= GST_DEVSOUND_SINK (asink); + GstDevsoundSink *sink = GST_DEVSOUND_SINK (asink); GstBuffer* lastBuffer = NULL; switch (GST_EVENT_TYPE (event)) { case GST_EVENT_EOS: // end-of-stream, we should close down all stream leftovers here //reset_devsound(sink->handle); - + sink->eosreceived = TRUE; if(buffer_queue->length) { GST_OBJECT_LOCK(sink); @@ -945,7 +984,7 @@ GST_OBJECT_LOCK(sink); g_queue_push_tail(buffer_queue,lastBuffer); GST_OBJECT_UNLOCK(sink); - cmd = WRITEDATA; + cmd = PLAYING; pthread_mutex_lock(&ds_mutex); pthread_cond_signal(&ds_condition); pthread_mutex_unlock(&ds_mutex); @@ -962,6 +1001,103 @@ return TRUE; } +#ifdef AV_SYNC +static void gst_devsound_sink_get_times (GstBaseSink * bsink, GstBuffer * buffer, + GstClockTime * start, GstClockTime * end) + { + /* Like GstBaseAudioSink, we set these to NONE */ + *start = GST_CLOCK_TIME_NONE; + *end = GST_CLOCK_TIME_NONE; + } + +static GstClock *gst_devsound_sink_provide_clock (GstElement * element) + { + GstDevsoundSink *sink = GST_DEVSOUND_SINK (element); + return GST_CLOCK (gst_object_ref (sink->clock)); + } + +static GstClockTime gst_devsound_sink_get_time (GstClock * clock, gpointer user_data) + { + GstClockTime result = 0; + GstDevsoundSink *sink = GST_DEVSOUND_SINK (user_data); + + /* The value returned must be in nano seconds. 1 sec = 1000000000 nano seconds (9 zeros)*/ + /*If time played is available from DevSound (a3f devsound onwards) get it*/ + if (sink->timeplayedavailable) + { + result = sink->time_or_samples_played; + } + else if ((sink->time_or_samples_played > 0 ) && (sink->rate > 0 ))/*This is a pre-a3f devsound. So calculate times played based on samples played*/ + { /*GST_SECOND = 1000000000*/ + result = gst_util_uint64_scale_int (sink->time_or_samples_played, GST_SECOND, sink->rate); + } + GST_LOG_OBJECT (sink, "Time: %" GST_TIME_FORMAT, GST_TIME_ARGS (result)); + return result; + } +#endif /*AV_SYNC*/ + +static GstStateChangeReturn gst_devsound_sink_change_state (GstElement * element, GstStateChange transition) + { + GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS; + GstDevsoundSink *sink= GST_DEVSOUND_SINK (element); + + switch (transition) + { + case GST_STATE_CHANGE_NULL_TO_READY: + { +#ifdef AV_SYNC + sink->time_or_samples_played = 0; +#endif /*AV_SYNC*/ + } + break; + + case GST_STATE_CHANGE_PAUSED_TO_PLAYING: + if(cmd == WAIT) + { + cmd = RESUME; + pthread_mutex_lock(&ds_mutex); + pthread_cond_signal(&ds_condition); + pthread_mutex_unlock(&ds_mutex); + + pthread_mutex_lock(&ds_mutex); + pthread_cond_wait(&ds_condition, &ds_mutex); + pthread_mutex_unlock(&ds_mutex); + } + break; + default: + break; + } + + ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition); + if (G_UNLIKELY (ret == GST_STATE_CHANGE_FAILURE)) + goto activate_failed; + + switch (transition) { + + case GST_STATE_CHANGE_PLAYING_TO_PAUSED: + cmd = PAUSE; + pthread_mutex_lock(&ds_mutex); + pthread_cond_signal(&ds_condition); + pthread_mutex_unlock(&ds_mutex); + + pthread_mutex_lock(&ds_mutex); + pthread_cond_wait(&ds_condition, &ds_mutex); + pthread_mutex_unlock(&ds_mutex); + break; + default: + break; + } + + return ret; + + activate_failed: + { + GST_DEBUG_OBJECT (sink, + "element failed to change states -- activation problem?"); + return GST_STATE_CHANGE_FAILURE; + } + } + /************************************ * Error Concealment Interface begins @@ -1142,3 +1278,13 @@ customInfaceUpdate.ilbcdecodermodeupdate = FALSE; } } + +static void get_PopulateIntfcProperties(GstDevsoundSink* dssink) + { + framemode_rqrd_for_ec(dssink->handle,&framemodereq); + + get_cng(dssink->handle,&g711cng); + + get_ilbccng(dssink->handle,&ilbccng); + } +