self->running_time_to_wait_for = GST_CLOCK_TIME_NONE;
self->last_seen_video_running_time = GST_CLOCK_TIME_NONE;
+ self->first_audio_running_time = GST_CLOCK_TIME_NONE;
self->last_seen_tc = NULL;
self->video_eos_flag = FALSE;
+ self->audio_eos_flag = FALSE;
+ self->video_flush_flag = FALSE;
self->audio_flush_flag = FALSE;
self->shutdown_flag = FALSE;
self->dropping = TRUE;
gst_video_info_init (&self->vinfo);
g_mutex_init (&self->mutex);
g_cond_init (&self->cond);
+ g_cond_init (&self->audio_cond);
}
static void
g_mutex_lock (&self->mutex);
self->shutdown_flag = TRUE;
g_cond_signal (&self->cond);
+ g_cond_signal (&self->audio_cond);
g_mutex_unlock (&self->mutex);
break;
case GST_STATE_CHANGE_READY_TO_PAUSED:
g_mutex_lock (&self->mutex);
self->shutdown_flag = FALSE;
self->video_eos_flag = FALSE;
+ self->audio_eos_flag = FALSE;
+ self->video_flush_flag = FALSE;
self->audio_flush_flag = FALSE;
g_mutex_unlock (&self->mutex);
default:
self->vsegment.position = GST_CLOCK_TIME_NONE;
gst_video_info_init (&self->vinfo);
self->last_seen_video_running_time = GST_CLOCK_TIME_NONE;
+ self->first_audio_running_time = GST_CLOCK_TIME_NONE;
if (self->last_seen_tc)
gst_video_time_code_free (self->last_seen_tc);
self->last_seen_tc = NULL;
g_mutex_clear (&self->mutex);
g_cond_clear (&self->cond);
+ g_cond_clear (&self->audio_cond);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
g_cond_signal (&self->cond);
g_mutex_unlock (&self->mutex);
break;
+ case GST_EVENT_FLUSH_START:
+ g_mutex_lock (&self->mutex);
+ self->video_flush_flag = TRUE;
+ g_cond_signal (&self->audio_cond);
+ g_mutex_unlock (&self->mutex);
+ break;
case GST_EVENT_FLUSH_STOP:
g_mutex_lock (&self->mutex);
+ self->video_flush_flag = FALSE;
if (self->mode != MODE_RUNNING_TIME) {
GST_DEBUG_OBJECT (self, "First time reset in video flush");
self->running_time_to_wait_for = GST_CLOCK_TIME_NONE;
g_cond_signal (&self->cond);
g_mutex_unlock (&self->mutex);
break;
+ case GST_EVENT_EOS:
+ g_mutex_lock (&self->mutex);
+ self->audio_eos_flag = TRUE;
+ g_cond_signal (&self->audio_cond);
+ g_mutex_unlock (&self->mutex);
+ break;
case GST_EVENT_FLUSH_STOP:
g_mutex_lock (&self->mutex);
self->audio_flush_flag = FALSE;
GstClockTime running_time;
GstVideoTimeCode *tc = NULL;
GstVideoTimeCodeMeta *tc_meta;
+ gboolean retry = FALSE;
timestamp = GST_BUFFER_TIMESTAMP (inbuf);
if (timestamp == GST_CLOCK_TIME_NONE) {
}
self->last_seen_tc = tc;
}
+ while (self->mode == MODE_VIDEO_FIRST
+ && self->first_audio_running_time == GST_CLOCK_TIME_NONE
+ && !self->audio_eos_flag
+ && !self->shutdown_flag && !self->video_flush_flag) {
+ g_cond_wait (&self->audio_cond, &self->mutex);
+ }
+ if (self->video_flush_flag || self->shutdown_flag) {
+ GST_DEBUG_OBJECT (self, "Shutting down, ignoring buffer");
+ gst_buffer_unref (inbuf);
+ g_mutex_unlock (&self->mutex);
+ return GST_FLOW_FLUSHING;
+ }
switch (self->mode) {
case MODE_TIMECODE:{
if (self->tc != NULL && tc != NULL) {
"Recording started at %" GST_TIME_FORMAT " waiting for %"
GST_TIME_FORMAT " inbuf %p", GST_TIME_ARGS (running_time),
GST_TIME_ARGS (self->running_time_to_wait_for), inbuf);
- if (running_time < self->running_time_to_end_at ||
- self->running_time_to_end_at == GST_CLOCK_TIME_NONE) {
- /* We are before the end of the recording. Check if we just actually
- * started */
- if (running_time > self->running_time_to_wait_for) {
- /* We just started recording: synchronise the audio */
- self->audio_running_time_to_wait_for = running_time;
- gst_avwait_send_element_message (self, FALSE, running_time);
- } else {
- /* We will start in the future when running_time_to_wait_for is
- * reached */
- self->audio_running_time_to_wait_for = self->running_time_to_wait_for;
+ if (self->mode != MODE_VIDEO_FIRST ||
+ self->first_audio_running_time <= running_time ||
+ self->audio_eos_flag) {
+ if (running_time < self->running_time_to_end_at ||
+ self->running_time_to_end_at == GST_CLOCK_TIME_NONE) {
+ /* We are before the end of the recording. Check if we just actually
+ * started */
+ if (running_time > self->running_time_to_wait_for) {
+ /* We just started recording: synchronise the audio */
+ self->audio_running_time_to_wait_for = running_time;
+ gst_avwait_send_element_message (self, FALSE, running_time);
+ } else {
+ /* We will start in the future when running_time_to_wait_for is
+ * reached */
+ self->audio_running_time_to_wait_for =
+ self->running_time_to_wait_for;
+ }
+ self->audio_running_time_to_end_at = self->running_time_to_end_at;
}
- self->audio_running_time_to_end_at = self->running_time_to_end_at;
+ } else {
+ /* We are in video-first mode and behind the first audio timestamp. We
+ * should drop all video buffers until the first audio timestamp, so
+ * we can catch up with it. (In timecode mode and running-time mode, we
+ * don't care about when the audio starts, we start as soon as the
+ * target timecode or running time has been reached) */
+ gst_buffer_unref (inbuf);
+ inbuf = NULL;
+ retry = TRUE;
}
}
}
- self->was_recording = self->recording;
+ if (!retry)
+ self->was_recording = self->recording;
g_cond_signal (&self->cond);
g_mutex_unlock (&self->mutex);
if (inbuf)
GST_ERROR_OBJECT (self, "Could not get current running time");
return GST_FLOW_ERROR;
}
+ if (self->first_audio_running_time == GST_CLOCK_TIME_NONE) {
+ self->first_audio_running_time = current_running_time;
+ }
+ g_cond_signal (&self->audio_cond);
if (self->vsegment.format == GST_FORMAT_TIME) {
vsign =
gst_segment_to_running_time_full (&self->vsegment, GST_FORMAT_TIME,
static GstClockTime target_running_time;
static gboolean recording;
static gint mode;
+static gboolean audio_late;
static GstAudioInfo ainfo;
SwitchType switch_after_2s;
} ElementPadAndSwitchType;
+typedef struct _PadAndBoolean
+{
+ GstPad *pad;
+ gboolean b;
+} PadAndBoolean;
+
static void
set_default_params (void)
{
target_running_time = GST_CLOCK_TIME_NONE;
recording = TRUE;
mode = 2;
+ audio_late = FALSE;
first_audio_timestamp = GST_CLOCK_TIME_NONE;
last_audio_timestamp = GST_CLOCK_TIME_NONE;
push_abuffers (gpointer data)
{
GstSegment segment;
- GstPad *pad = data;
gint i;
- GstClockTime timestamp = 0;
GstCaps *caps;
guint buf_size = 1000;
guint channels = 2;
+ PadAndBoolean *e = data;
+ GstPad *pad = e->pad;
+ gboolean audio_late = e->b;
+ GstClockTime timestamp;
+
+ if (audio_late) {
+ timestamp = 50 * GST_MSECOND;
+ } else {
+ timestamp = 0;
+ }
gst_pad_send_event (pad, gst_event_new_stream_start ("test"));
GThread *athread, *vthread;
GstBus *bus;
ElementPadAndSwitchType *e;
+ PadAndBoolean *pb;
audio_buffer_count = 0;
video_buffer_count = 0;
e->element = avwait;
e->pad = vsink;
e->switch_after_2s = switch_after_2s;
+ pb = g_new0 (PadAndBoolean, 1);
+ pb->pad = asink;
+ pb->b = audio_late;
- athread = g_thread_new ("athread", (GThreadFunc) push_abuffers, asink);
+ athread = g_thread_new ("athread", (GThreadFunc) push_abuffers, pb);
vthread = g_thread_new ("vthread", (GThreadFunc) push_vbuffers, e);
g_thread_join (vthread);
gst_bus_set_flushing (bus, TRUE);
gst_object_unref (bus);
g_free (e);
+ g_free (pb);
gst_pad_unlink (asrc, aoutput_sink);
gst_object_unref (asrc);
gst_pad_unlink (vsrc, voutput_sink);
recording = TRUE;
switch_after_2s = SWITCH_FALSE;
test_avwait_generic ();
- fail_unless_equals_uint64 (first_audio_timestamp, 0);
+ fail_unless_equals_uint64 (first_audio_timestamp, first_video_timestamp);
fail_unless_equals_uint64 (first_video_timestamp, 0);
fail_unless_equals_uint64 (last_video_timestamp, 2 * GST_SECOND);
fail_unless_equals_uint64 (last_audio_timestamp, 2 * GST_SECOND);
GST_END_TEST;
+GST_START_TEST (test_avwait_audio_late)
+{
+ set_default_params ();
+ recording = TRUE;
+ audio_late = TRUE;
+ test_avwait_generic ();
+ fail_unless_equals_uint64 (first_audio_timestamp, 50 * GST_MSECOND);
+ fail_unless_equals_uint64 (first_video_timestamp, 50 * GST_MSECOND);
+}
+
+GST_END_TEST;
static Suite *
avwait_suite (void)
tcase_add_test (tc_chain, test_avwait_1stc_switch_to_false);
tcase_add_test (tc_chain, test_avwait_3stc_switch_to_true);
tcase_add_test (tc_chain, test_avwait_3stc_switch_to_false);
+ tcase_add_test (tc_chain, test_avwait_audio_late);
suite_add_tcase (s, tc_chain);
return s;