c - Gstreamer C code for adding extra-header tokens in souphttpsrc
问题描述
I am accessing a file on an https server that requires an authorization token. From Windows cmd line, the following call discussed in the post Using basic auth with gstreamer souphttpsrc works without any issues:
gst-launch-1.0 souphttpsrc location="https://hls.rtst.co.za/video/france24/ovmaster.m3u8" extra-headers='test,Authorization=(string)\"Bearer\ eyJhbGci...token...psMshNg\"' ! filesink location="fetched/ovmaster.m3u8"
However, when I tried to implement it in C code it fails. My code fragment is:
// Set the source location parameter on the souphttpsrc element
GstElement* urisrc = gst_bin_get_by_name(GST_BIN(data->pipeline), "httphlssrc");
g_object_set(urisrc, "location", from.c_str(), NULL);
// Add to token to GET header
// extra-headers='test,Authorization=(string)"Bearer\ eyJh....token.....SKrbJJnHA"'
string src = data->uriSrcName;
if (!data->token.empty())
{
gchar* authChar = "test,Authorization=(string)\"Bearer\ eyJhbG...token...JpsMshNg\"";
GstStructure* extraHeaders = gst_structure_new("extra-headers", "authorization", G_TYPE_STRING, authChar, NULL);
g_object_set(urisrc, "extra-headers", extraHeaders, NULL);
gst_structure_free(extraHeaders);
}//end if token...
g_object_unref(urisrc);
The error is printed as:
Error received from element httphlssrc: Unauthorized../ext/soup/gstsouphttpsrc.c(1436): gst_soup_http_src_parse_status (): /GstPipeline:pipeline0/GstSoupHTTPSrc:httphlssrc:
Unauthorized (401), URL: https://hls.rtst.co.za/video/france24/ovmaster.m3u8, Redirect to: (NULL)
What am I missing?
解决方案
我似乎缺乏对使用 GstStructure 的理解,而 gst_structure_from_string() 是正确使用的函数。有效的代码段如下:
// Add to token to GET header
// extra-headers='test,Authorization=(string)"Bearer\ eyJh.........SKrbJJnHA"'
string src = data->uriSrcName;
if (!data->token.empty())
{
string authStr = "token, Authorization=(string)\"Bearer\\ " + data->token + "\"";
GstStructure* extraHeaders = gst_structure_from_string((const gchar*)(authStr.c_str()), NULL);
if (extraHeaders != NULL)
{
g_object_set(urisrc, "extra-headers", extraHeaders, NULL);
gst_structure_free(extraHeaders);
}//end if extraHeaders...
}//end if token...
推荐阅读
- yocto - 在 Yocto 中以非 root 身份安装守护进程
- excel - 来自 Excel 的电子邮件通知
- c# - 将Log4Net添加到依赖注入后如何访问
- mysql - 为了避免命名错误的列和大量额外的表,制作表示相似但不同模型数据的表的最佳方法是什么?
- mongodb - MongoDB查询以获取具有字符串数组的字段
- reactjs - 从 Express 后端在 Reactjs 中显示图像
- html - div 内的 div 有一个上边距,我不知道如何删除
- javascript - 如何在 MomentJS 上获取 Date 对象,但作为 UTC(防止 `toDate()` 成为 `locale`)?
- python - 在多个条件下选择带有过滤器正则表达式的列
- android - 单击按钮时如何设置文本值?