拿掉無用的seek to功能
This commit is contained in:
parent
3e6d7e9e1c
commit
62503432b0
@ -16,5 +16,5 @@
|
||||
</option>
|
||||
</component>
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_16_PREVIEW" project-jdk-name="16" project-jdk-type="JavaSDK" />
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_16" project-jdk-name="16" project-jdk-type="JavaSDK" />
|
||||
</project>
|
||||
@ -24,13 +24,8 @@ GST_DEBUG_CATEGORY_STATIC (debug_category);
|
||||
# define SET_CUSTOM_DATA(env, thiz, fieldID, data) (*env)->SetLongField (env, thiz, fieldID, (jlong)(jint)data)
|
||||
#endif
|
||||
|
||||
/* Do not allow seeks to be performed closer than this distance. It is visually useless, and will probably
|
||||
* confuse some demuxers. */
|
||||
#define SEEK_MIN_DELAY (500 * GST_MSECOND)
|
||||
|
||||
/* Structure to contain all our information, so we can pass it to callbacks */
|
||||
typedef struct _CustomData
|
||||
{
|
||||
typedef struct _CustomData {
|
||||
jobject app; /* Application instance, used to call its methods. A global reference is kept. */
|
||||
GstElement *pipeline; /* The running pipeline */
|
||||
GMainContext *context; /* GLib context used to run the main loop */
|
||||
@ -39,15 +34,11 @@ typedef struct _CustomData
|
||||
ANativeWindow *native_window; /* The Android native window where video will be rendered */
|
||||
GstState state; /* Current pipeline state */
|
||||
GstState target_state; /* Desired pipeline state, to be set once buffering is complete */
|
||||
gint64 duration; /* Cached clip duration */
|
||||
gint64 desired_position; /* Position to seek to, once the pipeline is running */
|
||||
GstClockTime last_seek_time; /* For seeking overflow prevention (throttling) */
|
||||
gboolean is_live; /* Live streams do not use buffering */
|
||||
} CustomData;
|
||||
|
||||
/* playbin2 flags */
|
||||
typedef enum
|
||||
{
|
||||
typedef enum {
|
||||
GST_PLAY_FLAG_TEXT = (1 << 2) /* We want subtitle output */
|
||||
} GstPlayFlags;
|
||||
|
||||
@ -57,7 +48,6 @@ static pthread_key_t current_jni_env;
|
||||
static JavaVM *java_vm;
|
||||
static jfieldID custom_data_field_id;
|
||||
static jmethodID set_message_method_id;
|
||||
static jmethodID set_current_position_method_id;
|
||||
static jmethodID on_gstreamer_initialized_method_id;
|
||||
static jmethodID on_media_size_changed_method_id;
|
||||
|
||||
@ -114,100 +104,6 @@ static void set_ui_message (const gchar * message, CustomData * data) {
|
||||
(*env)->DeleteLocalRef(env, jmessage);
|
||||
}
|
||||
|
||||
/* Tell the application what is the current position and clip duration */
|
||||
static void set_current_ui_position (gint position, gint duration, CustomData * data) {
|
||||
JNIEnv *env = get_jni_env ();
|
||||
(*env)->CallVoidMethod (env, data->app, set_current_position_method_id,
|
||||
position, duration);
|
||||
if ((*env)->ExceptionCheck (env)) {
|
||||
GST_ERROR ("Failed to call Java method");
|
||||
(*env)->ExceptionClear (env);
|
||||
}
|
||||
}
|
||||
|
||||
/* If we have pipeline and it is running, query the current position and clip duration and inform
|
||||
* the application */
|
||||
static gboolean refresh_ui (CustomData * data) {
|
||||
gint64 current = -1;
|
||||
gint64 position;
|
||||
|
||||
/* We do not want to update anything unless we have a working pipeline in the PAUSED or PLAYING state */
|
||||
if (!data || !data->pipeline || data->state < GST_STATE_PAUSED)
|
||||
return TRUE;
|
||||
|
||||
/* If we didn't know it yet, query the stream duration */
|
||||
if (!GST_CLOCK_TIME_IS_VALID (data->duration)) {
|
||||
if (!gst_element_query_duration (data->pipeline, GST_FORMAT_TIME,
|
||||
&data->duration)) {
|
||||
/*GST_WARNING
|
||||
("Could not query current duration (normal for still pictures)");*/
|
||||
data->duration = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (!gst_element_query_position (data->pipeline, GST_FORMAT_TIME, &position)) {
|
||||
/*GST_WARNING
|
||||
("Could not query current position (normal for still pictures)");*/
|
||||
position = 0;
|
||||
}
|
||||
|
||||
/* Java expects these values in milliseconds, and GStreamer provides nanoseconds */
|
||||
set_current_ui_position (position / GST_MSECOND, data->duration / GST_MSECOND,
|
||||
data);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/* Forward declaration for the delayed seek callback */
|
||||
static gboolean delayed_seek_cb (CustomData * data);
|
||||
|
||||
/* Perform seek, if we are not too close to the previous seek. Otherwise, schedule the seek for
|
||||
* some time in the future. */
|
||||
static void execute_seek (gint64 desired_position, CustomData * data) {
|
||||
gint64 diff;
|
||||
|
||||
if (desired_position == GST_CLOCK_TIME_NONE)
|
||||
return;
|
||||
|
||||
diff = gst_util_get_timestamp () - data->last_seek_time;
|
||||
|
||||
if (GST_CLOCK_TIME_IS_VALID (data->last_seek_time) && diff < SEEK_MIN_DELAY) {
|
||||
/* The previous seek was too close, delay this one */
|
||||
GSource *timeout_source;
|
||||
|
||||
if (data->desired_position == GST_CLOCK_TIME_NONE) {
|
||||
/* There was no previous seek scheduled. Setup a timer for some time in the future */
|
||||
timeout_source =
|
||||
g_timeout_source_new ((SEEK_MIN_DELAY - diff) / GST_MSECOND);
|
||||
g_source_set_callback (timeout_source, (GSourceFunc) delayed_seek_cb,
|
||||
data, NULL);
|
||||
g_source_attach (timeout_source, data->context);
|
||||
g_source_unref (timeout_source);
|
||||
}
|
||||
/* Update the desired seek position. If multiple petitions are received before it is time
|
||||
* to perform a seek, only the last one is remembered. */
|
||||
data->desired_position = desired_position;
|
||||
GST_DEBUG ("Throttling seek to %" GST_TIME_FORMAT ", will be in %"
|
||||
GST_TIME_FORMAT, GST_TIME_ARGS (desired_position),
|
||||
GST_TIME_ARGS (SEEK_MIN_DELAY - diff));
|
||||
} else {
|
||||
/* Perform the seek now */
|
||||
GST_DEBUG ("Seeking to %" GST_TIME_FORMAT,
|
||||
GST_TIME_ARGS (desired_position));
|
||||
data->last_seek_time = gst_util_get_timestamp ();
|
||||
gst_element_seek_simple (data->pipeline, GST_FORMAT_TIME,
|
||||
GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT, desired_position);
|
||||
data->desired_position = GST_CLOCK_TIME_NONE;
|
||||
}
|
||||
}
|
||||
|
||||
/* Delayed seek callback. This gets called by the timer setup in the above function. */
|
||||
static gboolean delayed_seek_cb (CustomData * data) {
|
||||
GST_DEBUG ("Doing delayed seek to %" GST_TIME_FORMAT,
|
||||
GST_TIME_ARGS (data->desired_position));
|
||||
execute_seek (data->desired_position, data);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/* Retrieve errors from the bus and show them on the UI */
|
||||
static void error_cb(GstBus *bus, GstMessage *msg, CustomData *data) {
|
||||
GError *err;
|
||||
@ -229,15 +125,8 @@ static void error_cb (GstBus * bus, GstMessage * msg, CustomData * data) {
|
||||
/* Called when the End Of the Stream is reached. Just move to the beginning of the media and pause. */
|
||||
static void eos_cb(GstBus *bus, GstMessage *msg, CustomData *data) {
|
||||
data->target_state = GST_STATE_PAUSED;
|
||||
data->is_live |=
|
||||
(gst_element_set_state (data->pipeline,
|
||||
data->is_live |= (gst_element_set_state(data->pipeline,
|
||||
GST_STATE_PAUSED) == GST_STATE_CHANGE_NO_PREROLL);
|
||||
execute_seek (0, data);
|
||||
}
|
||||
|
||||
/* Called when the duration of the media changes. Just mark it as unknown, so we re-query it in the next UI refresh. */
|
||||
static void duration_cb (GstBus * bus, GstMessage * msg, CustomData * data) {
|
||||
data->duration = GST_CLOCK_TIME_NONE;
|
||||
}
|
||||
|
||||
/* Called when buffering messages are received. We inform the UI about the current buffering level and
|
||||
@ -319,10 +208,6 @@ static void state_changed_cb (GstBus * bus, GstMessage * msg, CustomData * data)
|
||||
if (old_state == GST_STATE_READY && new_state == GST_STATE_PAUSED) {
|
||||
/* By now the sink already knows the media size */
|
||||
check_media_size(data);
|
||||
|
||||
/* If there was a scheduled seek, perform it now that we have moved to the Paused state */
|
||||
if (GST_CLOCK_TIME_IS_VALID (data->desired_position))
|
||||
execute_seek (data->desired_position, data);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -397,20 +282,12 @@ static void *app_function (void *userdata) {
|
||||
g_signal_connect (G_OBJECT(bus), "message::eos", (GCallback) eos_cb, data);
|
||||
g_signal_connect (G_OBJECT(bus), "message::state-changed",
|
||||
(GCallback) state_changed_cb, data);
|
||||
g_signal_connect (G_OBJECT (bus), "message::duration",
|
||||
(GCallback) duration_cb, data);
|
||||
g_signal_connect (G_OBJECT(bus), "message::buffering",
|
||||
(GCallback) buffering_cb, data);
|
||||
g_signal_connect (G_OBJECT(bus), "message::clock-lost",
|
||||
(GCallback) clock_lost_cb, data);
|
||||
gst_object_unref(bus);
|
||||
|
||||
/* Register a function that GLib will call 4 times per second */
|
||||
timeout_source = g_timeout_source_new (250);
|
||||
g_source_set_callback (timeout_source, (GSourceFunc) refresh_ui, data, NULL);
|
||||
g_source_attach (timeout_source, data->context);
|
||||
g_source_unref (timeout_source);
|
||||
|
||||
/* Create a GLib Main Loop and set it to run */
|
||||
GST_DEBUG ("Entering main loop... (CustomData:%p)", data);
|
||||
data->main_loop = g_main_loop_new(data->context, FALSE);
|
||||
@ -437,8 +314,6 @@ static void *app_function (void *userdata) {
|
||||
/* Instruct the native code to create its internal data structure, pipeline and thread */
|
||||
static void gst_native_init(JNIEnv *env, jobject thiz) {
|
||||
CustomData *data = g_new0 (CustomData, 1);
|
||||
data->desired_position = GST_CLOCK_TIME_NONE;
|
||||
data->last_seek_time = GST_CLOCK_TIME_NONE;
|
||||
SET_CUSTOM_DATA (env, thiz, custom_data_field_id, data);
|
||||
GST_DEBUG_CATEGORY_INIT (debug_category, "player", 0,
|
||||
"Android tutorial 5");
|
||||
@ -477,10 +352,8 @@ void gst_native_set_uri (JNIEnv * env, jobject thiz, jstring uri) {
|
||||
gst_element_set_state(data->pipeline, GST_STATE_READY);
|
||||
g_object_set(data->pipeline, "uri", char_uri, NULL);
|
||||
(*env)->ReleaseStringUTFChars(env, uri, char_uri);
|
||||
data->duration = GST_CLOCK_TIME_NONE;
|
||||
data->is_live |=
|
||||
(gst_element_set_state (data->pipeline,
|
||||
data->target_state) == GST_STATE_CHANGE_NO_PREROLL);
|
||||
(gst_element_set_state(data->pipeline, data->target_state) == GST_STATE_CHANGE_NO_PREROLL);
|
||||
}
|
||||
|
||||
/* Set pipeline to PLAYING state */
|
||||
@ -490,9 +363,7 @@ static void gst_native_play (JNIEnv * env, jobject thiz) {
|
||||
return;
|
||||
GST_DEBUG ("Setting state to PLAYING");
|
||||
data->target_state = GST_STATE_PLAYING;
|
||||
data->is_live |=
|
||||
(gst_element_set_state (data->pipeline,
|
||||
GST_STATE_PLAYING) == GST_STATE_CHANGE_NO_PREROLL);
|
||||
data->is_live |= (gst_element_set_state(data->pipeline, GST_STATE_PLAYING) == GST_STATE_CHANGE_NO_PREROLL);
|
||||
}
|
||||
|
||||
/* Set pipeline to PAUSED state */
|
||||
@ -502,24 +373,7 @@ static void gst_native_pause (JNIEnv * env, jobject thiz) {
|
||||
return;
|
||||
GST_DEBUG ("Setting state to PAUSED");
|
||||
data->target_state = GST_STATE_PAUSED;
|
||||
data->is_live |=
|
||||
(gst_element_set_state (data->pipeline,
|
||||
GST_STATE_PAUSED) == GST_STATE_CHANGE_NO_PREROLL);
|
||||
}
|
||||
|
||||
/* Instruct the pipeline to seek to a different position */
|
||||
void gst_native_set_position (JNIEnv * env, jobject thiz, int milliseconds) {
|
||||
CustomData *data = GET_CUSTOM_DATA (env, thiz, custom_data_field_id);
|
||||
if (!data)
|
||||
return;
|
||||
gint64 desired_position = (gint64) (milliseconds * GST_MSECOND);
|
||||
if (data->state >= GST_STATE_PAUSED) {
|
||||
execute_seek (desired_position, data);
|
||||
} else {
|
||||
GST_DEBUG ("Scheduling seek to %" GST_TIME_FORMAT " for later",
|
||||
GST_TIME_ARGS (desired_position));
|
||||
data->desired_position = desired_position;
|
||||
}
|
||||
data->is_live |=(gst_element_set_state(data->pipeline, GST_STATE_PAUSED) == GST_STATE_CHANGE_NO_PREROLL);
|
||||
}
|
||||
|
||||
/* Static class initializer: retrieve method and field IDs */
|
||||
@ -528,16 +382,13 @@ static jboolean gst_native_class_init (JNIEnv * env, jclass klass) {
|
||||
(*env)->GetFieldID(env, klass, "native_custom_data", "J");
|
||||
set_message_method_id =
|
||||
(*env)->GetMethodID(env, klass, "setMessage", "(Ljava/lang/String;)V");
|
||||
set_current_position_method_id =
|
||||
(*env)->GetMethodID (env, klass, "setCurrentPosition", "(II)V");
|
||||
on_gstreamer_initialized_method_id =
|
||||
(*env)->GetMethodID(env, klass, "onGStreamerInitialized", "()V");
|
||||
on_media_size_changed_method_id =
|
||||
(*env)->GetMethodID(env, klass, "onMediaSizeChanged", "(II)V");
|
||||
|
||||
if (!custom_data_field_id || !set_message_method_id
|
||||
|| !on_gstreamer_initialized_method_id || !on_media_size_changed_method_id
|
||||
|| !set_current_position_method_id) {
|
||||
|| !on_gstreamer_initialized_method_id || !on_media_size_changed_method_id) {
|
||||
/* We emit this message through the Android log instead of the GStreamer log because the later
|
||||
* has not been initialized yet.
|
||||
*/
|
||||
@ -600,7 +451,6 @@ static JNINativeMethod native_methods[] = {
|
||||
{"nativeSetUri", "(Ljava/lang/String;)V", (void *) gst_native_set_uri},
|
||||
{"nativePlay", "()V", (void *) gst_native_play},
|
||||
{"nativePause", "()V", (void *) gst_native_pause},
|
||||
{"nativeSetPosition", "(I)V", (void *) gst_native_set_position},
|
||||
{"nativeSurfaceInit", "(Ljava/lang/Object;)V",
|
||||
(void *) gst_native_surface_init},
|
||||
{"nativeSurfaceFinalize", "()V", (void *) gst_native_surface_finalize},
|
||||
@ -618,8 +468,7 @@ jint JNI_OnLoad (JavaVM * vm, void *reserved) {
|
||||
"Could not retrieve JNIEnv");
|
||||
return 0;
|
||||
}
|
||||
jclass klass = (*env)->FindClass (env,
|
||||
"com/hisharp/gstreamer_player/GstLibrary");
|
||||
jclass klass = (*env)->FindClass(env, "com/hisharp/gstreamer_player/GstLibrary");
|
||||
(*env)->RegisterNatives(env, klass, native_methods,
|
||||
G_N_ELEMENTS (native_methods));
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user