问题描述
我正在经历macOS上的官方GStreamer tutorials。在进行first basic tutorial时,我注意到视频没有播放,我只能听到音频。我在网上搜索了问题,并遇到了this的答案,该答案建议添加一个GMainLoop
类型的main event loop来轮询事件源并调用事件处理程序。添加循环并调用函数g_main_loop_run之后,问题已解决,我可以看到视频;但是,我仍然不清楚为什么在本教程在Ubuntu上按原样运行(没有事件循环)的情况下为什么在macOS上需要这样的事件循环。
无论如何,我继续教程,来到basic-tutorial-4,在其中添加了消息处理程序以查询管道中的流位置或持续时间,并寻找流中的其他位置(时间)。在这里,我注意到,如果我尝试执行相同的技巧并在将g_main_loop_run
的状态设置为playbin
之后调用GST_STATE_PLAYING
(请参见下面的代码),则视频和音频将按预期播放,但是其余代码似乎已被阻止;即我既无法从总线上获取任何消息,也无法在流中进行查询或查找。如果删除主事件循环,则该教程将按预期工作,并且可以进行查询/查找。但是,由于缺少主事件循环,因此无法播放视频。
如何配置主事件循环以获得所需的行为,即既可以观看视频又可以在流中查询和查找?
/* Basic Tutorial 4: Time Management */
#include <gst/gst.h>
typedef struct _CustomData {
GstElement *playbin;
gboolean playing;
gboolean terminate;
gboolean seek_enabled;
gboolean seek_done;
gint64 duration;
} CustomData;
static void handle_message(CustomData *data,GstMessage *msg);
int main( int argc,char* argv[] )
{
CustomData data;
GstBus *bus;
GstMessage *msg;
GstStateChangeReturn ret;
GMainLoop *main_loop;
data.playing = FALSE;
data.terminate = FALSE;
data.seek_enabled = FALSE;
data.seek_done = FALSE;
data.duration = GST_CLOCK_TIME_NONE;
/* Initialize GStreamer */
gst_init (&argc,&argv);
/* Create the elements */
data.playbin = gst_element_factory_make ("playbin","playbin");
if (!data.playbin) {
g_printerr ("Not all elements Could be created.\n");
return -1;
}
/* Set the URI to play */
// g_object_set (data.playbin,"uri","https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm",NULL);
g_object_set (data.playbin,"file:///Users/dev/media/sintel_trailer-480p.webm",NULL);
/* Start playing */
ret = gst_element_set_state (data.playbin,GST_STATE_PLAYING);
if (ret == GST_STATE_CHANGE_FAILURE) {
g_printerr ("Unable to set the pipeline to the playing state.\n");
gst_object_unref (data.playbin);
return -1;
}
/* For video to play on macOS */
main_loop = g_main_loop_new(NULL,FALSE);
g_main_loop_run(main_loop);
bus = gst_element_get_bus(data.playbin);
/* Listen to the bus */
do {
msg = gst_bus_timed_pop_filtered (bus,100 * GST_MSECOND,(GstMessageType) (GST_MESSAGE_STATE_CHANGED | GST_MESSAGE_ERROR | GST_MESSAGE_EOS | GST_MESSAGE_DURATION));
/* Parse the message */
if(msg != NULL) {
handle_message(&data,msg);
}
else
{
/* No message means that the timeout expired */
if(data.playing) {
gint64 current = -1;
/* Query the current position of the stream */
if(!gst_element_query_position(data.playbin,GST_FORMAT_TIME,¤t))
{
g_printerr("Could not query current position.\n");
}
/* 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.playbin,&data.duration)) {
g_printerr ("Could not query current duration.\n");
}
}
/* Print current position and total duration */
g_print ("Position %" GST_TIME_FORMAT " / %" GST_TIME_FORMAT "\r",GST_TIME_ARGS (current),GST_TIME_ARGS (data.duration));
/* If seeking is enabled,we have not done it yet,and the time is right,seek */
if (data.seek_enabled && !data.seek_done && current > 10 * GST_SECOND) {
g_print ("\nReached 10s,performing seek...\n");
gst_element_seek_simple (data.playbin,GstSeekFlags (GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT),30 * GST_SECOND);
data.seek_done = TRUE;
}
}
}
} while(!data.terminate);
/* Free resources */
gst_object_unref (bus);
gst_element_set_state (data.playbin,GST_STATE_NULL);
gst_object_unref (data.playbin);
return 0;
}
static void handle_message (CustomData *data,GstMessage *msg)
{
GError *err;
gchar *debug_info;
switch (GST_MESSAGE_TYPE (msg)) {
case GST_MESSAGE_ERROR:
gst_message_parse_error (msg,&err,&debug_info);
g_printerr ("Error received from element %s: %s\n",GST_OBJECT_NAME (msg->src),err->message);
g_printerr ("Debugging information: %s\n",debug_info ? debug_info : "none");
g_clear_error (&err);
g_free (debug_info);
data->terminate = TRUE;
break;
case GST_MESSAGE_EOS:
g_print ("End-Of-Stream reached.\n");
data->terminate = TRUE;
break;
case GST_MESSAGE_DURATION:
/* The duration has changed,mark the current one as invalid */
data->duration = GST_CLOCK_TIME_NONE;
break;
case GST_MESSAGE_STATE_CHANGED:
{
GstState old_state,new_state,pending_state;
gst_message_parse_state_changed(msg,&old_state,&new_state,&pending_state);
if(GST_MESSAGE_SRC (msg) == GST_OBJECT(data->playbin)) {
g_print("Pipeline state changed from %s to %s:\n",gst_element_state_get_name(old_state),gst_element_state_get_name(new_state));
/* Remember whether we are in the PLAYING state or not */
data->playing = (new_state == GST_STATE_PLAYING);
if (data->playing) {
/* We just moved to PLAYING. Check if seeking is possible */
GstQuery *query;
gint64 start,end;
query = gst_query_new_seeking (GST_FORMAT_TIME);
if (gst_element_query (data->playbin,query)) {
gst_query_parse_seeking (query,NULL,&data->seek_enabled,&start,&end);
if (data->seek_enabled) {
g_print ("Seeking is ENABLED from %" GST_TIME_FORMAT " to %" GST_TIME_FORMAT "\n",GST_TIME_ARGS (start),GST_TIME_ARGS (end));
} else {
g_print ("Seeking is disABLED for this stream.\n");
}
}
else {
g_printerr ("Seeking query Failed.");
}
gst_query_unref (query);
}
} break;
default:
/* We should not reach here */
g_printerr("Seeking query Failed.");
break;
}
gst_message_unref(msg);
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)