网上很多示例代码都是直接通过元件名称去进行手动link,但是当元件超过10个时会很麻烦。也不利于各元件的封装以及工程化,所以我们只需要在注意管道元件的添加顺序,最后从管道中获取所有添加的元件,并倒序将所有元件进行link。
注意:我们需要知道那些元件需要跳过连接
例如:
1.末尾的元件 udp-sink、fakesink。
2.中间多分支的元件stream-demuxer、tee。
代码如下:
int AIStreamTask::ElementLink()
{
GstIterator* pGstIterator = gst_bin_iterate_elements(GST_BIN(pipeline));
GValue item = { 0, };
GstElement *element[pGstIterator->size];
int nsize = pGstIterator->size;
int i = 0;
//将元件从管道取出并存入数组
while (nsize--)
{
i++;
if (GST_ITERATOR_OK == gst_iterator_next(pGstIterator, &item))
{
element[i] = (GstElement *)g_value_get_object(&item);
char *pEnd = strstr(element[i]->object.name, "decode-bin");
if (NULL != pEnd)
{
dzlog_info("find the decode element names:%s", element[i]->object.name); //解码器管道元件跳过处理
i--;
continue;
}
dzlog_info("element names:%s", element[i]->object.name);
}
else
{
break;
}
}
dzlog_info("PipelineRun begin");
//当遇到streamdemux、Tee、udpsink、fakesink时跳过分段连接(由于mp4相关的操作时后面动态添加的开始不会加载进来跳过相关操作)
while (i > 2)
{
i--;
char *pEnd = strstr(element[i]->object.name, "stream-demuxer");
if (NULL != pEnd)
{
dzlog_info("find the element[%d] names:%s", i, element[i]->object.name);//分流器管道元件
continue;
}
pEnd = NULL;
pEnd = strstr(element[i]->object.name, "udp-sink");
if (NULL != pEnd)
{
dzlog_info("find the element[%d] names:%s", i, element[i]->object.name);
continue;
}
pEnd = NULL;
pEnd = strstr(element[i]->object.name, "fakesink");
if (NULL != pEnd)
{
dzlog_info("find the element[%d] names:%s", i, element[i]->object.name);
continue;
}
pEnd = NULL;
pEnd = strstr(element[i]->object.name, "tee");
if (NULL != pEnd)
{
dzlog_info("find the element[%d] names:%s", i, element[i]->object.name);
continue;
}
dzlog_info("element[%d] names:%s,element[%d] names:%s", i, element[i]->object.name, i - 1, element[i - 1]->object.name);
if (!gst_element_link_many(element[i],element[i - 1], NULL))
{
dzlog_error("elements link failure");
return FALSE;
}
}
return TRUE;
}