int64 requestFrame(int64 frame) {
int64 currentFrame;
GstFormat format = GST_FORMAT_DEFAULT;
// Seek to the specified frame's most recent keyframe.
bool out = (TRUE == gst_element_seek(pipeline, 1.0, format,
(GstSeekFlags)(GST_SEEK_FLAG_KEY_UNIT | GST_SEEK_FLAG_FLUSH),
GST_SEEK_TYPE_SET, frame, GST_SEEK_TYPE_SET, -1));
// Get the GStreamer bus to tell us when the seek is done.
GstBus* bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline));
GstMessage* msg = gst_bus_poll(bus, GST_MESSAGE_ASYNC_DONE, -1);
gst_message_unref(msg);
gst_object_unref(bus);
// Return the correct value for success/failure.
if (TRUE == gst_element_query_position(pipeline, &format, ¤tFrame)) {
cout << "Frame: " << currentFrame << endl;
return currentFrame;
}
return -1;
}
Use case:
// try to seek some places
for (int64 i = 0; i < 100; ++i) {
int64 sought = q->requestFrame(i);
if (sought == i) {
cout << "k";
} else {
cout << ".";
}
cout.flush();
}
cout << endl;