GStreamer Tutorial 中文翻译:Basic tutorial 4: Time management

GStreamer Tutorial 4中文翻译



前言

由于工作原因,用的GStreamer的图像解码库,所以记录GStreamer Tutorial 的中文翻译和个人的理解以便学习。若有不足请多指教。侵删。


Basic tutorial 4: Time management

目的

本教程展示了如何使用GStreamer的时间相关示例。特别是:

  • 如何查询管道的信息,如流的位置或持续时间;
  • 如何寻求(跳转)到流中的不同位置(时间)。

简介

GstQuery是一种机制,允许向一个元素或pad询问一个信息。在这个例子中,我们问管道是否允许寻找(有些资源,如直播流,不允许寻找)。如果允许,那么,一旦电影运行了10秒钟,我们就用寻的方式跳到一个不同的位置。
在以前的教程中,一旦我们设置并运行了管道,我们的主函数就静止在那里,等待通过总线接收ERROR或EOS。在这里,我们修改了这个函数,以定期唤醒并查询管道的位置,这样我们就可以把它打印在屏幕上。这类似于一个媒体播放器所要做的,定期更新用户界面。
最后,只要有变化,就会查询并更新流的持续时间。

寻找的示例

** basic-tutorial-4.c**

#include <gst/gst.h>

/* Structure to contain all our information, so we can pass it around */
typedef struct _CustomData {
  GstElement *playbin;  /* Our one and only element */
  gboolean playing;      /* Are we in the PLAYING state? */
  gboolean terminate;    /* Should we terminate execution? */
  gboolean seek_enabled; /* Is seeking enabled for this media? */
  gboolean seek_done;    /* Have we performed the seek already? */
  gint64 duration;       /* How long does this media last, in nanoseconds */
} CustomData;

/* Forward definition of the message processing function */
static void handle_message (CustomData *data, GstMessage *msg);

int main(int argc, char *argv[]) {
  CustomData data;
  GstBus *bus;
  GstMessage *msg;
  GstStateChangeReturn ret;

  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);

  /* 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;
  }

  /* Listen to the bus */
  bus = gst_element_get_bus (data.playbin);
  do {
    msg = gst_bus_timed_pop_filtered (bus, 100 * GST_MSECOND,
        GST_MESSAGE_STATE_CHANGED | GST_MESSAGE_ERROR | GST_MESSAGE_EOS | GST_MESSAGE_DURATION);

    /* Parse message */
    if (msg != NULL) {
      handle_message (&data, msg);
    } else {
      /* We got no message, this means 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, &current)) {
          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, GST_FORMAT_TIME, &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, GST_FORMAT_TIME,
              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 ("\nEnd-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 ("Unexpected message received.\n");
      break;
  }
  gst_message_unref (msg);
}

代码走读

/* Structure to contain all our information, so we can pass it around */
typedef struct _CustomData {
  GstElement *playbin;  /* Our one and only element */
  gboolean playing;      /* Are we in the PLAYING state? */
  gboolean terminate;    /* Should we terminate execution? */
  gboolean seek_enabled; /* Is seeking enabled for this media? */
  gboolean seek_done;    /* Have we performed the seek already? */
  gint64 duration;       /* How long does this media last, in nanoseconds */
} CustomData;

/* Forward definition of the message processing function */
static void handle_message (CustomData *data, GstMessage *msg);

我们首先定义一个结构来包含我们所有的信息,这样我们就可以把它传递给其他函数。特别是,在这个例子中,我们把信息处理代码移到它自己的函数handle_message中,因为它有点太大。
然后我们建立一个由单一元素组成的管道,即playbin,我们已经在《基础教程1:Hello world!》中看到它了。然而,playbin本身就是一个管道,而且在这种情况下,它是管道中唯一的元素,所以我们直接使用playbin元素。我们将跳过细节:剪辑的URI通过URI属性给到playbin,管道被设置为播放状态。

msg = gst_bus_timed_pop_filtered (bus, 100 * GST_MSECOND,
    GST_MESSAGE_STATE_CHANGED | GST_MESSAGE_ERROR | GST_MESSAGE_EOS | GST_MESSAGE_DURATION);

以前我们没有给gst_bus_timed_pop_filtered()提供超时,这意味着它直到收到消息才会返回。现在我们使用100毫秒的超时,所以,如果在十分之一秒内没有收到消息,该函数将返回NULL。我们将使用这个逻辑来更新我们的 “用户界面”。
请注意,所需的超时必须被指定为GstClockTime,因此,以纳秒为单位。那么表示不同时间单位的数字应该乘以GST_SECOND或GST_MSECOND等宏。这也使你的代码更易读。
如果我们得到一个消息,我们在handle_message函数中处理它(下一小节),否则:

用户界面的刷新

/* We got no message, this means the timeout expired */
if (data.playing) {

如果管道处于PLAYING状态,就是刷新屏幕的时候了。如果不处于PLAYING状态,我们不想做任何事情,因为大多数查询都会失败。
我们在这里得到的是大约每秒10次的刷新率,这对我们的用户界面来说是足够好的。我们将在屏幕上打印出当前的媒体位置,我们可以通过查询管道来了解这个位置。这涉及到几个步骤,将在下一小节中展示,但是,由于位置和持续时间是足够常见的查询,GstElement提供了更容易的、现成的替代品。

/* Query the current position of the stream */
if (!gst_element_query_position (data.pipeline, GST_FORMAT_TIME, &current)) {
  g_printerr ("Could not query current position.\n");
}

gst_element_query_position()隐藏了查询对象的管理,直接为我们提供结果。

/* 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)) {
     g_printerr ("Could not query current duration.\n");
  }
}

现在是了解流的长度的好时机,用另一个GstElement辅助函数:gst_element_query_duration()

/* 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));

注意GST_TIME_FORMATGST_TIME_ARGS宏的使用,以提供GStreamer时间的用户友好表示。

/* 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.pipeline, GST_FORMAT_TIME,
      GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT, 30 * GST_SECOND);
  data.seek_done = TRUE;
}

现在我们通过在管道上调用gst_element_seek_simple()来 "简单 "地执行搜索。很多复杂的寻找方法都隐藏在这个方法中,这是件好事。
让我们回顾一下这些参数:
GST_FORMAT_TIME表示我们是以时间单位来指定目的地的。其他搜索格式使用不同的单位。
然后是GstSeekFlags,让我们回顾一下最常见的:
GST_SEEK_FLAG_FLUSH:在进行寻道之前,它会丢弃当前管道中的所有数据。当管道被重新填满和新数据开始显示时,可能会暂停一下,但会大大增加应用程序的 “响应速度”。如果不提供这个标志,"陈旧的 "数据可能会显示一段时间,直到新的位置出现在管道的末端。
GST_SEEK_FLAG_KEY_UNIT:在大多数编码的视频流中,寻找任意位置是不可能的,只能寻找某些称为关键帧的帧。当使用这个标志时,搜索将实际移动到最近的关键帧,并直接开始产生数据。如果不使用这个标志,管道将在内部移动到最近的关键帧(它没有其他选择),但数据将不会被显示,直到它到达要求的位置。这最后一种选择更准确,但可能需要更长的时间。
gst_seek_flag_accurate。有些媒体片段没有提供足够的索引信息,这意味着寻找到任意位置是很耗时的。在这些情况下,GStreamer通常会估计要寻找的位置,而且通常工作得很好。如果这个精度对你的情况来说不够好(你看到的寻道不是到你要求的确切时间),那么提供这个标志。请注意,它可能需要更长的时间来计算搜索位置(在某些文件上,时间很长)。
最后,我们提供要寻找的位置。由于我们要求的是GST_FORMAT_TIME,这个值必须是纳秒级的,所以为了简单起见,我们用秒表示时间,然后乘以GST_SECOND

信息提示

handle_message函数处理所有通过管道的总线收到的消息。ERROR和EOS的处理与以前的教程相同,所以我们跳到感兴趣的部分。

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->pipeline)) {
    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);

查询和时间查询一般只有在PAUSED或PLAYING状态下才能得到有效的回复,因为所有的元素都有机会接收信息并配置自己。这里,我们使用playing变量来跟踪管道是否处于playing状态。另外,如果我们刚刚进入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->pipeline, 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);
}

gst_query_new_seeking() 创建一个新的 "seeking "类型的查询对象,格式为GST_FORMAT_TIME。这表明我们对寻求感兴趣,指定了我们想要移动到的新时间。我们也可以要求GST_FORMAT_BYTES,然后在源文件中寻找一个特定的字节位置,但这通常不太有用。
这个查询对象然后通过gst_element_query()传递给管道。结果存储在同一个查询中,可以用gst_query_parse_seeking()轻松检索。它提取一个布尔值,表示是否允许寻找,以及可以寻找的范围。
当你用完查询对象时,别忘了取消对它的引用。
这就是了! 有了这些知识,就可以建立一个媒体播放器,它可以根据当前流的位置定期更新一个滑块,并允许通过移动滑块进行寻道

结论

这个例程展示了:

  • 如何使用GstQuery查询管道的信息;
  • 如何使用gst_element_query_position()gst_element_query_duration()获得位置和持续时间等常用信息道;
  • 如何使用gst_element_seek_simple()来寻找流中的任意位置;
  • 在哪些状态下可以进行所有这些操作。。
### 关于ArcGIS License Server无法启动的解决方案 当遇到ArcGIS License Server无法启动的情况,可以从以下几个方面排查并解决问题: #### 1. **检查网络配置** 确保License Server所在的计算机能够被其他客户端正常访问。如果是在局域网环境中部署了ArcGIS Server Local,则需要确认该环境下的网络设置是否允许远程连接AO组件[^1]。 #### 2. **验证服务状态** 检查ArcGIS Server Object Manager (SOM) 的运行情况。通常情况下,在Host SOM机器上需将此服务更改为由本地系统账户登录,并重启相关服务来恢复其正常工作流程[^2]。 #### 3. **审查日志文件** 查看ArcGIS License Manager的日志记录,寻找任何可能指示错误原因的信息。这些日志可以帮助识别具体是什么阻止了许可服务器的成功初始化。 #### 4. **权限问题** 确认用于启动ArcGIS License Server的服务账号具有足够的权限执行所需操作。这包括但不限于读取/写入特定目录的权利以及与其他必要进程通信的能力。 #### 5. **软件版本兼容性** 保证所使用的ArcGIS产品及其依赖项之间存在良好的版本匹配度。不一致可能会导致意外行为完全失败激活license server的功能。 #### 示例代码片段:修改服务登录身份 以下是更改Windows服务登录凭据的一个简单PowerShell脚本例子: ```powershell $serviceName = "ArcGISServerObjectManager" $newUsername = ".\LocalSystemUser" # 替换为实际用户名 $newPassword = ConvertTo-SecureString "" -AsPlainText -Force Set-Service -Name $serviceName -StartupType Automatic New-ServiceCredential -ServiceName $serviceName -Account $newUsername -Password $newPassword Restart-Service -Name $serviceName ``` 上述脚本仅作为示范用途,请依据实际情况调整参数值后再实施。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值