首页 > 解决方案 > 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?

标签: chttp-headersgstreamer

解决方案


我似乎缺乏对使用 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...

推荐阅读