How to get the duration of a raw audio file?

mdbtfu thibaud.furst at microdb.fr
Thu Aug 18 11:56:22 UTC 2016


Hello,


I have to play raw audio files with GStreamer and manage the time position
of the audio stream (setting and monitoring the time position). To manage
that time position, I also need the duration of the audio file, which
GStreamer apparently cannot get or calculate. I understand that raw data
doesn't have a header with meta-data (like wav files for example) but I
thought that maybe with the capabilities given (signed 16 bit,
little-endian, ...), GStreamer would succeed to get that duration.

So here is my question: how can I get that duration? Or how can I force
GStreamer to set a given duration to the file I'm playing?

Below is my code that doesn't manage to get a proper duration (it always
returns 0 as a duration)


I'll be really thankful for any help or advice!


-- 
Thibaud



[Code]


Here is the function that plays the raw audio file.

{
	// Create pipeline for raw audio data 
	QString str_audioPipeline = "filesrc location=myrawaudiofile.raw !
audio/x-raw,format=S16LE,rate=48000,channels=2 ! directsoundsink";
	
	// Gstreamer initialisation
	gst_init(NULL, NULL);

	// Parse pipeline
	GError * message = NULL;
	audioPlaybackPipeline =
gst_parse_launch(str_audioPipeline.toStdString().c_str(), &message);

	// Set callback to get position and length of the stream
	playbackTrackerID = g_timeout_add(200, (GSourceFunc)get_position,
audioPlaybackPipeline);

	// Set bus to get messages notifications
	GstBus *bus = gst_pipeline_get_bus(GST_PIPELINE(audioPlaybackPipeline));
	gst_bus_add_watch(bus, playback_bus_callback, NULL);
	gst_object_unref(bus);

	// Set the playback pipeline state to PLAYING
	gst_element_set_state(audioPlaybackPipeline, GST_STATE_PLAYING);

	// Check if the pipeline state changed with success
	if (gst_element_get_state(audioPlaybackPipeline, NULL, NULL, -1) ==
GST_STATE_CHANGE_FAILURE)
	{
		// AUDIO Playback::ERROR: Failed to go into PLAYING state
		return false;
	}
	else
	{
		// Create a GLib Main Loop and set it to run
		playbackLoop = g_main_loop_new(NULL, NULL);
		g_main_loop_run(playbackLoop);

		return true;
	}
}


Here is the callback used to get the position and duration of the stream

gboolean get_position(GstElement *pipeline)
{
        // Get the current position
	gst_element_query_position(pipeline, GST_FORMAT_TIME, &playbackPosition);

        // Get the duration
	gst_element_query_duration(pipeline, GST_FORMAT_TIME, &playbackDuration);
}



--
View this message in context: http://gstreamer-devel.966125.n4.nabble.com/How-to-get-the-duration-of-a-raw-audio-file-tp4679141.html
Sent from the GStreamer-devel mailing list archive at Nabble.com.


More information about the gstreamer-devel mailing list