上篇传送门:OpenHarmony AVPlayer扩展支持rtsp协议:编译gstreamer插件库(一)

五、插件库

1、gstrtsp
rtsp插件库,库名必须为libgstrtsp.z.so,不可修改
third_party/gstreamer/gstplugins_good/BUILD.gn 增加

ohos_source_set("gstrtsp_source") {
  sources = [
    "gst/rtsp/xxx.c",
    ...
  ]
  configs = [ ":gst_plugins_config" ]
}

ohos_shared_library("gstrtsp") {
  deps = [
    ":gstrtsp_source",
    "//third_party/glib:glib",
    "//third_party/glib:gobject",
    "//third_party/glib:gio",
    "//third_party/gstreamer/gstreamer:gstbase",
    "//third_party/gstreamer/gstreamer:gstreamer",
    "//third_party/gstreamer/gstplugins_base:gstrtsp-1.0",
    "//third_party/gstreamer/gstplugins_base:gstrtp-1.0",
    "//third_party/gstreamer/gstplugins_base:gstsdp",
  ]
  relative_install_dir = "media/plugins"
  part_name = "gstreamer"
  subsystem_name = "thirdparty"
}

编译生成libgstrtsp.z.so,安装到/system/lib/media/plugins 目录

2、gstrtpmanager

rtpmanager插件库,库名必须为libgstrtpmanager.z.so,不可修改
third_party/gstreamer/gstplugins_good/BUILD.gn 增加

ohos_source_set("gstrtpmanager_source") {
  sources = [
    "gst/rtpmanager/xxx.c",
    ...
  ]
  configs = [ ":gst_plugins_config" ]
}

ohos_shared_library("gstrtpmanager") {
  deps = [
    ":gstrtpmanager_source",
    "//third_party/glib:glib",
    "//third_party/glib:gobject",
    "//third_party/glib:gio",
    "//third_party/gstreamer/gstreamer:gstbase",
    "//third_party/gstreamer/gstreamer:gstreamer",
    "//third_party/gstreamer/gstplugins_base:gstaudio",
    "//third_party/gstreamer/gstplugins_base:gstrtp-1.0",
  ]
  relative_install_dir = "media/plugins"
  part_name = "gstreamer"
  subsystem_name = "thirdparty"
}

编译生成libgstrtpmanager.z.so,安装到/system/lib/media/plugins 目录

3、gstrtp

rtp插件库,库名必须为libgstrtp.z.so,不可修改
third_party/gstreamer/gstplugins_good/BUILD.gn 增加

ohos_source_set("gstrtp_source") {
  sources = [
    "gst/rtp/xxx.c",
    ...
  ]

  configs = [ ":gst_plugins_config" ]
}

ohos_shared_library("gstrtp") {
  deps = [
    ":gstrtp_source",
    "//third_party/glib:glib",
    "//third_party/glib:gobject",
    "//third_party/gstreamer/gstreamer:gstbase",
    "//third_party/gstreamer/gstreamer:gstreamer",
    "//third_party/gstreamer/gstplugins_base:gstaudio",
    "//third_party/gstreamer/gstplugins_base:gstvideo",
    "//third_party/gstreamer/gstplugins_base:gsttag",
    "//third_party/gstreamer/gstplugins_base:gstrtp-1.0",
    "//third_party/gstreamer/gstplugins_base:gstpbutils",
  ]
  relative_install_dir = "media/plugins"
  part_name = "gstreamer"
  subsystem_name = "thirdparty"
}

编译生成libgstrtp.z.so,安装到/system/lib/media/plugins 目录

4、gstudp

如果rtp底层传输协议使用的是udp,需要编译该库
udp插件库,库名必须为libgstudp.z.so,不可修改
third_party/gstreamer/gstplugins_good/BUILD.gn 增加

ohos_source_set("gstudp_source") {
  sources = [
    "gst/udp/xxx.c",
    ...
  ]

  configs = [ ":gst_plugins_config" ]
}

ohos_shared_library("gstudp") {
  deps = [
    ":gstudp_source",
    "//third_party/glib:glib",
    "//third_party/glib:gobject",
    "//third_party/glib:gio",
    "//third_party/gstreamer/gstreamer:gstbase",
    "//third_party/gstreamer/gstreamer:gstreamer",
  ]
  relative_install_dir = "media/plugins"
  part_name = "gstreamer"
  subsystem_name = "thirdparty"
}

third_party/gstreamer/gstplugins_good/gst/udp/gstudpsrc.c 修改,_GNU_SOURCE重定义错误

+    #ifdef _GNU_SOURCE
+    #undef _GNU_SOURCE
+    #endif
     #define _GNU_SOURCE
     /* Needed for OSX/iOS to define the IPv6 variants */
     #define __APPLE_USE_RFC_3542
     #include <sys/types.h>
     #ifdef HAVE_SYS_SOCKET_H
     #include <sys/socket.h>
     #endif

编译生成libgstudp.z.so,安装到/system/lib/media/plugins 目录

5、gstplugins_good_packages

third_party/gstreamer/gstplugins_good/BUILD.gn 修改

group("gstplugins_good_packages") {
  deps = [
    ":gstaudiofx",
    ":gstaudioparsers",
    ":gstisomp4",
    ":gstmatroska",
    ":gstmultifile",
    ":gstwavparse",
+  ":gstrtsp",
+  ":gstrtpmanager",
+  ":gstrtp",
+  ":gstudp",
   ]
}

六、AVPlayer framework

1、在AVPlayer的NAPI代码中限制只能设置http前缀的网络url,需要解除限制
/foundation/multimedia/player_framework/frameworks/js/avplayer/avplayer_napi.cpp 修改

void AVPlayerNapi::SetSource(std::string url)
{
    MEDIA_LOGI("input url is %{public}s!", url.c_str());
    bool isFd = (url.find("fd://") != std::string::npos) ? true : false;
-   bool isNetwork = (url.find("http") != std::string::npos) ? true : false;
+   bool isNetwork = ((url.find("http") != std::string::npos) || (url.find("rtsp") != std::string::npos)) ? true : false;
    if (isNetwork) {
        auto task = std::make_shared<TaskHandler<void>>([this, url]() {
        MEDIA_LOGI("SetNetworkSource Task");
        std::unique_lock<std::mutex> lock(taskMutex_);
        auto state = GetCurrentState();
        if (state != AVPlayerState::STATE_IDLE) {
            OnErrorCb(MSERR_EXT_API9_OPERATE_NOT_PERMIT, "current state is not idle, unsupport set url");
            return;
        }
        if (player_ != nullptr) {
            if (player_->SetSource(url) != MSERR_OK) {
                OnErrorCb(MSERR_EXT_API9_INVALID_PARAMETER, "failed to SetSourceNetWork");
            }
            stopWait_ = false;
            LISTENER(stateChangeCond_.wait(lock, [this]() { return stopWait_.load(); }), "SetSourceNetWork", false)
        }
    });
    (void)taskQue_->EnqueueTask(task);
} else if (isFd) {
   ...
}

2、支持网络缓冲
/foundation/multimedia/services/engine/gstreamer/common/playbin_adapter/playbin_ctrler_base.cpp 修改

int32_t PlayBinCtrlerBase::SetSource(const std::string &url)
{
    std::unique_lock<std::mutex> lock(mutex_);
    uri_ = url;
-   if (url.find("http") == 0 || url.find("https") == 0 || EnableBufferingBySysParam()) {
+   if (url.find("http") == 0 || url.find("https") == 0 || url.find("rtsp") == 0 || EnableBufferingBySysParam()) {
        isNetWorkPlay_ = true;
    }

    MEDIA_LOGI("Set source: %{public}s", url.c_str());
    return MSERR_OK;
}

七、rtp over tcp

RTSP支持以单播或组播模式通过TCP或UDP传输rtp数据包。默认情况下,rtspsrc将按以下顺序协商连接:UDP单播/UDP组播/TCP,不能更改顺序,但可以使用“protocols”属性控制允许的协议。
注:1>、安装上面的库后tcp和udp传输协议都能支持;2>、以下代码仅用于测试rtp over tcp的能力,除非有明确需求只能通过tcp传输,否则不要同步

1、protocols 属性支持的参数如下:
third_party/gstreamer/gstplugins_base/gst-libs/gst/rtsp/gstrtsptransport.h

typedef enum {
  GST_RTSP_LOWER_TRANS_UNKNOWN   = 0,
  GST_RTSP_LOWER_TRANS_UDP       = (1 << 0),
  GST_RTSP_LOWER_TRANS_UDP_MCAST = (1 << 1),
  GST_RTSP_LOWER_TRANS_TCP       = (1 << 2),
  GST_RTSP_LOWER_TRANS_HTTP      = (1 << 4),
  GST_RTSP_LOWER_TRANS_TLS       = (1 << 5)
} GstRTSPLowerTrans;

2、/foundation/multimedia/services/engine/gstreamer/common/playbin_adapter/playbin_ctrler_base.cpp 添加

void PlayBinCtrlerBase::OnElementSetup(GstElement &elem)
{
    MEDIA_LOGD("element setup: %{public}s", ELEM_NAME(&elem));
    ...

    if (OnVideoDecoderSetup(elem) || strncmp(ELEM_NAME(&elem), "multiqueue", strlen("multiqueue")) == 0 ||
        strncmp(ELEM_NAME(&elem), "qtdemux", strlen("qtdemux")) == 0) {
        MEDIA_LOGI("add msgfilter element: %{public}s", ELEM_NAME(&elem));
        msgProcessor_->AddMsgFilter(ELEM_NAME(&elem));
    }

+   if (strncmp(ELEM_NAME(&elem), "source", strlen("source") == 0) { 
+       GstElementFactory *elementFac = gst_element_get_factory(&elem); 
+       const gchar *name = gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (elementFac)); 
+       if ((name != nullptr) && (strstr(name, "rtspsrc") != nullptr)) { 
+           g_object_set(&elem, "protocols", (1 << 2), nullptr);    // (1<<2) 代表GST_RTSP_LOWER_TRANS_TCP,设置rtspsrc只允许用tcp协议与服务器协商
+      }
+   } 
    ...
}

八、dot图

待补充
gstudpsrc->gstrtpsession->gstrtpstorage->gstrtpssrcdemux->gstrtpjitterbuffer->gstrtpptdemux->gsttypefindelement->gstrtpmp2tdepay->tsdemux...

九、注意事项

1、上述代码中BUILD.gn的sources 需要编译的C文件基本都没写,代码有点多,后续有时间补充;最简单的方法:gio库添加gio目录下所有的除window相关的源文件,gstreamer库添加相应目录下所有的源文件,编译过即可;其实部分源文件并不会被rtsp依赖,可以适当裁剪部分元素以减少动态库大小。
2、插件库的库名必须和文章中定义的相同,并设置relative_install_dir=“media/plugins”将库安装到/system/lib/media/plugins目录。

十、结语

下一篇将介绍gstreamer rtsp插件及板端播放rtsp节目异常的一些调试方法。

Logo

社区规范:仅讨论OpenHarmony相关问题。

更多推荐