+ * Handle seek event in push mode.
+ */
+static gboolean
+avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad, GstEvent * event)
+{
+ gdouble rate;
+ GstFormat format;
+ GstSeekFlags flags;
+ GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
+ gint64 cur, stop;
+ gboolean keyframe;
+ GstAviStream *stream;
+ guint index;
+ guint n, str_num;
+ guint64 min_offset;
+ GstSegment seeksegment;
+ gboolean update;
+
+ /* check we have the index */
+ if (!avi->have_index) {
+ GST_DEBUG_OBJECT (avi, "no seek index built, seek aborted.");
+ return FALSE;
+ } else {
+ GST_DEBUG_OBJECT (avi, "doing push-based seek with event");
+ }
+
+ gst_event_parse_seek (event, &rate, &format, &flags,
+ &cur_type, &cur, &stop_type, &stop);
+
+ if (format != GST_FORMAT_TIME) {
+ GstFormat fmt = GST_FORMAT_TIME;
+ gboolean res = TRUE;
+
+ if (cur_type != GST_SEEK_TYPE_NONE)
+ res = gst_pad_query_convert (pad, format, cur, &fmt, &cur);
+ if (res && stop_type != GST_SEEK_TYPE_NONE)
+ res = gst_pad_query_convert (pad, format, stop, &fmt, &stop);
+ if (!res) {
+ GST_DEBUG_OBJECT (avi, "unsupported format given, seek aborted.");
+ return FALSE;
+ }
+
+ format = fmt;
+ }
+
+ /* let gst_segment handle any tricky stuff */
+ GST_DEBUG_OBJECT (avi, "configuring seek");
+ memcpy (&seeksegment, &avi->segment, sizeof (GstSegment));
+ gst_segment_set_seek (&seeksegment, rate, format, flags,
+ cur_type, cur, stop_type, stop, &update);
+
+ keyframe = ! !(flags & GST_SEEK_FLAG_KEY_UNIT);
+ cur = seeksegment.last_stop;
+
+ GST_DEBUG_OBJECT (avi,
+ "Seek requested: ts %" GST_TIME_FORMAT " stop %" GST_TIME_FORMAT
+ ", kf %u, rate %lf", GST_TIME_ARGS (cur), GST_TIME_ARGS (stop), keyframe,
+ rate);
+
+ if (rate < 0) {
+ GST_DEBUG_OBJECT (avi, "negative rate seek not supported in push mode");
+ return FALSE;
+ }
+
+ /* FIXME, this code assumes the main stream with keyframes is stream 0,
+ * which is mostly correct... */
+ str_num = avi->main_stream;
+ stream = &avi->stream[str_num];
+
+ /* get the entry index for the requested position */
+ index = gst_avi_demux_index_for_time (avi, stream, cur);
+ GST_DEBUG_OBJECT (avi, "str %u: Found entry %u for %" GST_TIME_FORMAT,
+ str_num, index, GST_TIME_ARGS (cur));
+
+ /* check if we are already on a keyframe */
+ if (!ENTRY_IS_KEYFRAME (&stream->index[index])) {
+ GST_DEBUG_OBJECT (avi, "Entry is not a keyframe - searching back");
+ /* now go to the previous keyframe, this is where we should start
+ * decoding from. */
+ index = gst_avi_demux_index_prev (avi, stream, index, TRUE);
+ GST_DEBUG_OBJECT (avi, "Found previous keyframe at %u", index);
+ }
+
+ gst_avi_demux_get_buffer_info (avi, stream, index,
+ &stream->current_timestamp, &stream->current_ts_end,
+ &stream->current_offset, &stream->current_offset_end);
+
+ /* re-use cur to be the timestamp of the seek as it _will_ be */
+ cur = stream->current_timestamp;
+
+ min_offset = stream->index[index].offset;
+ avi->seek_kf_offset = min_offset - 8;
+
+ GST_DEBUG_OBJECT (avi,
+ "Seek to: ts %" GST_TIME_FORMAT " (on str %u, idx %u, offset %"
+ G_GUINT64_FORMAT ")", GST_TIME_ARGS (stream->current_timestamp), str_num,
+ index, min_offset);
+
+ for (n = 0; n < avi->num_streams; n++) {
+ GstAviStream *str = &avi->stream[n];
+ guint idx;
+
+ if (n == avi->main_stream)
+ continue;
+
+ /* get the entry index for the requested position */
+ idx = gst_avi_demux_index_for_time (avi, str, cur);
+ GST_DEBUG_OBJECT (avi, "str %u: Found entry %u for %" GST_TIME_FORMAT, n,
+ idx, GST_TIME_ARGS (cur));
+
+ /* check if we are already on a keyframe */
+ if (!ENTRY_IS_KEYFRAME (&str->index[idx])) {
+ GST_DEBUG_OBJECT (avi, "Entry is not a keyframe - searching back");
+ /* now go to the previous keyframe, this is where we should start
+ * decoding from. */
+ idx = gst_avi_demux_index_prev (avi, str, idx, TRUE);
+ GST_DEBUG_OBJECT (avi, "Found previous keyframe at %u", idx);
+ }
+
+ gst_avi_demux_get_buffer_info (avi, str, idx,
+ &str->current_timestamp, &str->current_ts_end,
+ &str->current_offset, &str->current_offset_end);
+
+ if (str->index[idx].offset < min_offset) {
+ min_offset = str->index[idx].offset;
+ GST_DEBUG_OBJECT (avi,
+ "Found an earlier offset at %" G_GUINT64_FORMAT ", str %u",
+ min_offset, n);
+ str_num = n;
+ stream = str;
+ index = idx;
+ }
+ }
+
+ GST_DEBUG_OBJECT (avi,
+ "Seek performed: str %u, offset %" G_GUINT64_FORMAT ", idx %u, ts %"
+ GST_TIME_FORMAT ", ts_end %" GST_TIME_FORMAT ", off %" G_GUINT64_FORMAT
+ ", off_end %" G_GUINT64_FORMAT, str_num, min_offset, index,
+ GST_TIME_ARGS (stream->current_timestamp),
+ GST_TIME_ARGS (stream->current_ts_end), stream->current_offset,
+ stream->current_offset_end);
+
+ /* index data refers to data, not chunk header (for pull mode convenience) */
+ min_offset -= 8;
+ GST_DEBUG_OBJECT (avi, "seeking to chunk at offset %" G_GUINT64_FORMAT,
+ min_offset);
+
+ if (!perform_seek_to_offset (avi, min_offset)) {
+ GST_DEBUG_OBJECT (avi, "seek event failed!");
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+/*
+ * Handle whether we can perform the seek event or if we have to let the chain
+ * function handle seeks to build the seek indexes first.
+ */
+static gboolean
+gst_avi_demux_handle_seek_push (GstAviDemux * avi, GstPad * pad,
+ GstEvent * event)
+{
+ /* check for having parsed index already */
+ if (!avi->have_index) {
+ guint64 offset = 0;
+ gboolean building_index;
+
+ GST_OBJECT_LOCK (avi);
+ /* handle the seek event in the chain function */
+ avi->state = GST_AVI_DEMUX_SEEK;
+
+ /* copy the event */
+ if (avi->seek_event)
+ gst_event_unref (avi->seek_event);
+ avi->seek_event = gst_event_ref (event);
+
+ /* set the building_index flag so that only one thread can setup the
+ * structures for index seeking. */
+ building_index = avi->building_index;
+ if (!building_index) {
+ avi->building_index = TRUE;
+ if (avi->stream[0].indexes) {
+ avi->odml_stream = 0;
+ avi->odml_subidxs = avi->stream[avi->odml_stream].indexes;
+ offset = avi->odml_subidxs[0];
+ } else {
+ offset = avi->idx1_offset;
+ }
+ }
+ GST_OBJECT_UNLOCK (avi);
+
+ if (!building_index) {
+ /* seek to the first subindex or legacy index */
+ GST_INFO_OBJECT (avi,
+ "Seeking to legacy index/first subindex at %" G_GUINT64_FORMAT,
+ offset);
+ return perform_seek_to_offset (avi, offset);
+ }
+
+ /* FIXME: we have to always return true so that we don't block the seek
+ * thread.
+ * Note: maybe it is OK to return true if we're still building the index */
+ return TRUE;
+ }
+
+ return avi_demux_handle_seek_push (avi, pad, event);
+}
+
+/*