Linux平台x86_64|aarch64架构如何实现轻量级RTSP服务

本文涉及的产品
视觉智能开放平台,图像资源包5000点
视觉智能开放平台,分割抠图1万点
视觉智能开放平台,视频资源包5000点
简介: 为满足在Linux平台(x86_64与aarch64架构)上实现轻量级RTSP服务的需求,我们开发了一套解决方案。该方案通过调用`start_rtsp_server()`函数启动RTSP服务,并设置端口号及认证信息。支持AAC音频和H.264视频编码,可推送纯音频、纯视频或音视频流。此外,还支持X11屏幕采集、部分V4L2摄像头采集、帧率/GOP/码率调整、摄像头设备选择与预览等功能。对于音频采集,支持alsa-lib和libpulse接口。整体设计旨在提供150-400ms的低延迟体验,适用于多种应用场景。

技术背景

我们在做Linux平台x86_64架构或aarch64架构的推送模块的时候,有公司提出这样的技术需求,希望在Linux平台,实现轻量级RTSP服务,实现对摄像头或屏幕对外RTSP拉流,同步到大屏上去。

技术实现

废话不多说,直接上代码,先调用start_rtsp_server()指定端口号,启动RTSP服务。

image.gif

LogInit();
    NT_SmartPublisherSDKAPI push_api;
    if (!PushSDKInit(push_api))
    {
        XDestroyWindow(display, sub_wid);
        XDestroyWindow(display, main_wid);
        XCloseDisplay(display);
        return 0;
    }
    // auto rtsp_server_handle = start_rtsp_server(&push_api, 8554, "test", "12345");
    auto rtsp_server_handle = start_rtsp_server(&push_api, 8554, "", "");
    if (nullptr == rtsp_server_handle) {
        fprintf(stderr, "start_rtsp_server failed.\n");
        XDestroyWindow(display, sub_wid);
        XDestroyWindow(display, main_wid);
        XCloseDisplay(display);
        push_api.UnInit();
        return 0;
    }
    auto push_handle = open_config_instance(&push_api, 20);
    if (nullptr == push_handle) {
        fprintf(stderr, "open_config_instance failed.\n");
        XDestroyWindow(display, sub_wid);
        XDestroyWindow(display, main_wid);
        XCloseDisplay(display);
        stop_rtsp_server(&push_api, rtsp_server_handle);
        push_api.UnInit();
        return 0;
    }

image.gif

PushSDKInit()实现如下:

/*
     * publisherdemo.cpp
     * Author: daniusdk.com
     */
    bool PushSDKInit(NT_SmartPublisherSDKAPI& push_api)
    {
        memset(&push_api, 0, sizeof(push_api));
        NT_GetSmartPublisherSDKAPI(&push_api);
        auto ret = push_api.Init(0, nullptr);
        if (NT_ERC_OK != ret)
        {
            fprintf(stderr, "push_api.Init failed!\n");
            return false;
        }
        else
        {
            fprintf(stdout, "push_api.Init ok!\n");
        }
        return true;
    }

image.gif

启动RTSP服务对应的代码如下:

NT_HANDLE start_rtsp_server(NT_SmartPublisherSDKAPI* push_api, int port, std::string user_name, std::string password) {
        NT_HANDLE rtsp_server_handle = nullptr;
        if (NT_ERC_OK != push_api->OpenRtspServer(&rtsp_server_handle, 0)) {
            fprintf(stderr, "OpenRtspServer failed\n");
            return nullptr;
        }
        if (nullptr == rtsp_server_handle) {
            fprintf(stderr, "rtsp_server_handle is null\n");
            return nullptr;
        }
        if (NT_ERC_OK != push_api->SetRtspServerPort(rtsp_server_handle, port)) {
            push_api->CloseRtspServer(rtsp_server_handle);
            return nullptr;
        }
        if (!user_name.empty() && !password.empty())
            push_api->SetRtspServerUserNamePassword(rtsp_server_handle, user_name.c_str(), password.c_str());
        if (NT_ERC_OK == push_api->StartRtspServer(rtsp_server_handle, 0))
            return rtsp_server_handle;
        fprintf(stderr, "StartRtspServer failed\n");
        push_api->CloseRtspServer(rtsp_server_handle);
        
        return nullptr;
    }

image.gif

open_config_instance()实现如下,可以获取摄像头或屏幕数据,并做基础的编码等参数配置:

NT_HANDLE open_config_instance(NT_SmartPublisherSDKAPI* push_api, int dst_fps) {
        NT_INT32 pulse_device_number = 0;
        if (NT_ERC_OK == push_api->GetAuidoInputDeviceNumber(2, &pulse_device_number))
        {
            fprintf(stdout, "[daniusdk.com]Pulse device num:%d\n", pulse_device_number);
            char device_name[512];
            for (auto i = 0; i < pulse_device_number; ++i)
            {
                if (NT_ERC_OK == push_api->GetAuidoInputDeviceName(2, i, device_name, 512))
                {
                    fprintf(stdout, "[daniusdk.com]index:%d name:%s\n", i, device_name);
                }
            }
        }
        NT_INT32 alsa_device_number = 0;
        if (pulse_device_number < 1)
        {
            if (NT_ERC_OK == push_api->GetAuidoInputDeviceNumber(1, &alsa_device_number))
            {
                fprintf(stdout, "Alsa device num:%d\n", alsa_device_number);
                char device_name[512];
                for (auto i = 0; i < alsa_device_number; ++i)
                {
                    if (NT_ERC_OK == push_api->GetAuidoInputDeviceName(1, i, device_name, 512))
                    {
                        fprintf(stdout, "[daniusdk.com]index:%d name:%s\n", i, device_name);
                    }
                }
            }
        }
        NT_INT32 capture_speaker_flag = 0;
        if (NT_ERC_OK == push_api->IsCanCaptureSpeaker(2, &capture_speaker_flag))
        {
            if (capture_speaker_flag)
                fprintf(stdout, "[daniusdk.com]Support speaker capture\n");
            else
                fprintf(stdout, "[daniusdk.com]UnSupport speaker capture\n");
        }
        NT_INT32 is_support_window_capture = 0;
        if (NT_ERC_OK == push_api->IsCaptureXWindowSupported(NULL, &is_support_window_capture))
        {
            if (is_support_window_capture)
                fprintf(stdout, "[daniusdk.com]Support window capture\n");
            else
                fprintf(stdout, "[daniusdk.com]UnSupport window capture\n");
        }
        if (is_support_window_capture)
        {
            NT_INT32 win_count = 0;
            if (NT_ERC_OK == push_api->UpdateCaptureXWindowList(NULL, &win_count) && win_count > 0)
            {
                fprintf(stdout, "X Capture Winows list++\n");
                for (auto i = 0; i < win_count; ++i)
                {
                    NT_UINT64 wid;
                    char title[512];
                    if (NT_ERC_OK == push_api->GetCaptureXWindowInfo(i, &wid, title, sizeof(title) / sizeof(char)))
                    {
                        x_win_list.push_back(wid);
                        fprintf(stdout, "wid:%llu, title:%s\n", wid, title);
                    }
                }
                fprintf(stdout, "[daniusdk.com]X Capture Winows list--\n");
            }
        }
        std::vector<CameraInfo> cameras;
        GetCameraInfo(push_api, cameras);
        if (!cameras.empty())
        {
            fprintf(stdout, "cameras count:%d\n", (int)cameras.size());
            for (const auto& c : cameras)
            {
                fprintf(stdout, "camera name:%s, id:%s, cap_num:%d\n", c.name_.c_str(), c.id_.c_str(), (int)c.capabilities_.size());
                for (const auto& i : c.capabilities_)
                {
                    fprintf(stdout, "[daniusdk.com]cap w:%d, h:%d, fps:%d\n", i.width_, i.height_, i.max_frame_rate_);
                }
            }
        }
        NT_UINT32 auido_option = NT_PB_E_AUDIO_OPTION_NO_AUDIO;
        if (pulse_device_number > 0 || alsa_device_number > 0)
        {
            auido_option = NT_PB_E_AUDIO_OPTION_CAPTURE_MIC;
        }
        else if (capture_speaker_flag)
        {
            auido_option = NT_PB_E_AUDIO_OPTION_CAPTURE_SPEAKER;
        }
        //auido_option = NT_PB_E_AUDIO_OPTION_CAPTURE_MIC_SPEAKER_MIXER;
        NT_UINT32 video_option = NT_PB_E_VIDEO_OPTION_SCREEN;
        if (!cameras.empty())
        {
            video_option = NT_PB_E_VIDEO_OPTION_CAMERA;
        }
        else if (is_support_window_capture)
        {
            video_option = NT_PB_E_VIDEO_OPTION_WINDOW;
        }
        // video_option = NT_PB_E_VIDEO_OPTION_LAYER;
        //video_option = NT_PB_E_VIDEO_OPTION_NO_VIDEO;
        NT_HANDLE push_handle = nullptr;
        //if (NT_ERC_OK != push_api->Open(&push_handle, NT_PB_E_VIDEO_OPTION_LAYER, NT_PB_E_AUDIO_OPTION_CAPTURE_SPEAKER, 0, NULL))
        if (NT_ERC_OK != push_api->Open(&push_handle, video_option, auido_option, 0, NULL))
        {
            return nullptr;
        }
        push_api->SetEventCallBack(push_handle, nullptr, OnSDKEventHandle);
        //push_api->SetXDisplayName(push_handle, ":0");
        //push_api->SetXDisplayName(push_handle, NULL);
        // 视频层配置方式
        if (NT_PB_E_VIDEO_OPTION_LAYER == video_option)
        {
            std::vector<std::shared_ptr<nt_pb_sdk::layer_conf_wrapper_base> > layer_confs;
            auto index = 0;
            //// 第0层填充RGBA矩形, 目的是保证帧率, 颜色就填充全黑
            auto rgba_layer_c0 = std::make_shared<nt_pb_sdk::RGBARectangleLayerConfigWrapper>(index++, true, 0, 0, 1280, 720);
            rgba_layer_c0->conf_.red_ = 200;
            rgba_layer_c0->conf_.green_ = 200;
            rgba_layer_c0->conf_.blue_ = 200;
            rgba_layer_c0->conf_.alpha_ = 255;
            layer_confs.push_back(rgba_layer_c0);
            ////// 第一层为桌面层
            //auto screen_layer_c1 = std::make_shared<nt_pb_sdk::ScreenLayerConfigWrapper>(index++, true, 0, 0, 1280, 720);
            //screen_layer_c1->conf_.scale_filter_mode_ = 3;
            //layer_confs.push_back(screen_layer_c1);
            //// 第一层为窗口
            if (!x_win_list.empty())
            {
                auto window_layer_c1 = std::make_shared<nt_pb_sdk::WindowLayerConfigWrapper>(index++, true, 0, 0, 640, 360);
                window_layer_c1->conf_.xwindow_ = x_win_list.back();
                layer_confs.push_back(window_layer_c1);
            }
            //// 摄像头层
            if (!cameras.empty())
            {
                auto camera_layer_c1 = std::make_shared<nt_pb_sdk::CameraLayerConfigWrapper>(index++, true,
                    640, 0, 640, 360);
                strcpy(camera_layer_c1->conf_.device_unique_id_, cameras.front().id_.c_str());
                camera_layer_c1->conf_.is_flip_horizontal_ = 0;
                camera_layer_c1->conf_.is_flip_vertical_ = 0;
                camera_layer_c1->conf_.rotate_degress_ = 0;
                layer_confs.push_back(camera_layer_c1);
                if (cameras.size() > 1)
                {
                    auto camera_layer_c2 = std::make_shared<nt_pb_sdk::CameraLayerConfigWrapper>(index++, true,
                        640, 0, 320, 240);
                    strcpy(camera_layer_c2->conf_.device_unique_id_, cameras.back().id_.c_str());
                    camera_layer_c2->conf_.is_flip_horizontal_ = 0;
                    camera_layer_c2->conf_.is_flip_vertical_ = 0;
                    camera_layer_c2->conf_.rotate_degress_ = 0;
                    layer_confs.push_back(camera_layer_c2);
                }
            }
            auto image_layer1 = std::make_shared<nt_pb_sdk::ImageLayerConfigWrapper>(index++, true, 650, 120, 324, 300);
            strcpy(image_layer1->conf_.file_name_utf8_, "./testpng/tca.png");
            layer_confs.push_back(image_layer1);
            auto image_layer2 = std::make_shared<nt_pb_sdk::ImageLayerConfigWrapper>(index++, true, 120, 380, 182, 138);
            strcpy(image_layer2->conf_.file_name_utf8_, "./testpng/t4.png");
            layer_confs.push_back(image_layer2);
            std::vector<const NT_PB_LayerBaseConfig* > layer_base_confs;
            for (const auto& i : layer_confs)
            {
                layer_base_confs.push_back(i->getBase());
            }
            if (NT_ERC_OK != push_api->SetLayersConfig(push_handle, 0, layer_base_confs.data(),
                layer_base_confs.size(), 0, nullptr))
            {
                push_api->Close(push_handle);
                push_handle = nullptr;
                return nullptr;
            }
        }
        // push_api->SetScreenClip(push_handle, 0, 0, 1280, 720);
        if (video_option == NT_PB_E_VIDEO_OPTION_CAMERA)
        {
            if (!cameras.empty())
            {
                push_api->SetVideoCaptureDeviceBaseParameter(push_handle, cameras.front().id_.c_str(),
                    640, 480);
                //push_api->FlipVerticalCamera(push_handle, 1);
                //push_api->FlipHorizontalCamera(push_handle, 1);
                //push_api->RotateCamera(push_handle, 0);
            }
        }
        if (video_option == NT_PB_E_VIDEO_OPTION_WINDOW)
        {
            if (!x_win_list.empty())
            {
                //push_api->SetCaptureXWindow(push_handle, x_win_list[0]);
                push_api->SetCaptureXWindow(push_handle, x_win_list.back());
            }
        }
        push_api->SetFrameRate(push_handle, dst_fps); // 帧率设置
        push_api->SetVideoEncoder(push_handle, 0, 1, NT_MEDIA_CODEC_ID_H264, 0);
        push_api->SetVideoBitRate(push_handle, 2000);  // 平均码率2000kbps
        push_api->SetVideoQuality(push_handle, 26);
        push_api->SetVideoMaxBitRate(push_handle, 4000); // 最大码率4000kbps
        // openh264 配置特定参数
        push_api->SetVideoEncoderSpecialInt32Option(push_handle, "usage_type", 0); //0是摄像头编码, 1是屏幕编码
        push_api->SetVideoEncoderSpecialInt32Option(push_handle, "rc_mode", 1); // 0是质量模式, 1是码率模式
        push_api->SetVideoEncoderSpecialInt32Option(push_handle, "enable_frame_skip", 0); // 0是关闭跳帧, 1是打开跳帧
        push_api->SetVideoKeyFrameInterval(push_handle, dst_fps * 2); // 关键帧间隔
        push_api->SetVideoEncoderProfile(push_handle, 3); // H264 high
        push_api->SetVideoEncoderSpeed(push_handle, 3); // 编码速度设置到3
        if (pulse_device_number > 0)
        {
            push_api->SetAudioInputLayer(push_handle, 2);
            push_api->SetAuidoInputDeviceId(push_handle, 0);
        }
        else if (alsa_device_number > 0)
        {
            push_api->SetAudioInputLayer(push_handle, 1);
            push_api->SetAuidoInputDeviceId(push_handle, 0);
        }
        push_api->SetEchoCancellation(push_handle, 1, 0);
        push_api->SetNoiseSuppression(push_handle, 1);
        push_api->SetAGC(push_handle, 1);
        push_api->SetVAD(push_handle, 1);
        push_api->SetInputAudioVolume(push_handle, 0, 1.0);
        push_api->SetInputAudioVolume(push_handle, 1, 0.2);
        // 音频配置
        push_api->SetPublisherAudioCodecType(push_handle, 1);
        //push_api->SetMute(push_handle, 1);
        return push_handle;
    }

image.gif

发布RTSP流实现如下:

bool start_rtsp_stream(NT_SmartPublisherSDKAPI* push_api, NT_HANDLE rtsp_server_handle, NT_HANDLE handle, const std::string stream_name) {
        push_api->SetRtspStreamName(handle, stream_name.c_str());
        push_api->ClearRtspStreamServer(handle);
        push_api->AddRtspStreamServer(handle, rtsp_server_handle, 0);
        
        if (NT_ERC_OK != push_api->StartRtspStream(handle, 0))
            return false;
        return true;
    }

image.gif

如果需要本地摄像头或者屏幕预览数据,调研预览接口即可:

// 开启预览,也可以不开启, 根据需求来
    push_api.SetPreviewXWindow(push_handle, "", sub_wid);
    push_api.StartPreview(push_handle, 0, nullptr);

image.gif

如需停止:

fprintf(stdout, "StopRtspStream++\n");
    push_api.StopRtspStream(push_handle);
    
    fprintf(stdout, "StopRtspStream--\n");
    fprintf(stdout, "stop_rtsp_server++\n");
    stop_rtsp_server(&push_api, rtsp_server_handle);
    fprintf(stdout, "stop_rtsp_server--\n");
    push_api.StopPreview(push_handle);
    // push_api.StopPublisher(push_handle);
    push_api.Close(push_handle);
    push_handle = nullptr;
    XDestroyWindow(display, sub_wid);
    XDestroyWindow(display, main_wid);
    XCloseDisplay(display);
    push_api.UnInit();

image.gif

总结

Linux平台arm64实现轻量级RTSP服务,目前实现的功能如下:

  • 音频编码:AAC;
  • 视频编码:H.264;
  • 协议:RTSP;
  • [音视频]支持纯音频/纯视频/音视频推送;
  • 支持X11屏幕采集;
  • 支持部分V4L2摄像头设备采集;
  • [屏幕/V4L2摄像头]支持帧率、关键帧间隔(GOP)、码率(bit-rate)设置;
  • [V4L2摄像头]支持V4L2摄像头设备选择(设备文件名范围:[/dev/video0, /dev/video63])、分辨率设置、帧率设置;
  • [V4L2摄像头]支持水平反转、垂直反转、0° 90° 180° 270°旋转;
  • [音频]支持基于alsa-lib接口的音频采集;
  • [音频]支持基于libpulse接口采集本机PulseAudio服务音频;
  • [预览]支持实时预览; 支持RTSP端口设置;
  • 支持RTSP鉴权用户名、密码设置;
  • 支持获取当前RTSP服务会话连接数;
  • 支持x64_64架构、aarch64架构(需要glibc-2.21及以上版本的Linux系统, 需要libX11.so.6, 需要GLib–2.0, 需安装 libstdc++.so.6.0.21、GLIBCXX_3.4.21、 CXXABI_1.3.9)。

配合我们的RTSP播放器,可轻松实现150-400ms低延迟体验,感兴趣的开发者,可以单独跟我沟通。

相关文章
|
24天前
|
Linux Docker 容器
Linux 中停止 Docker 服务报 warning 导致无法彻底停止问题如何解决?
在 Linux 系统中,停止 Docker 服务时遇到警告无法彻底停止的问题,可以通过系统管理工具停止服务、强制终止相关进程、检查系统资源和依赖关系、以及重置 Docker 环境来解决。通过以上步骤,能够有效地排查和解决 Docker 服务停止不彻底的问题,确保系统的稳定运行。
110 19
|
23天前
|
监控 Linux
Linux systemd 服务启动失败Main process exited, code=exited, status=203/EXEC
通过以上步骤,可以有效解决 systemd 服务启动失败并报错 `Main process exited, code=exited, status=203/EXEC` 的问题。关键在于仔细检查单元文件配置、验证可执行文件的有效性,并通过日志分析具体错误原因。确保可执行文件路径正确、文件具有执行权限,并且可以独立运行,将有助于快速定位和解决问题。
268 7
|
3月前
|
Linux 应用服务中间件 Shell
linux系统服务二!
本文详细介绍了Linux系统的启动流程,包括CentOS 7的具体启动步骤,从BIOS自检到加载内核、启动systemd程序等。同时,文章还对比了CentOS 6和CentOS 7的启动流程,分析了启动过程中的耗时情况。接着,文章讲解了Linux的运行级别及其管理命令,systemd的基本概念、优势及常用命令,并提供了自定义systemd启动文件的示例。最后,文章介绍了单用户模式和救援模式的使用方法,包括如何找回忘记的密码和修复启动故障。
58 5
linux系统服务二!
|
3月前
|
缓存 运维 网络协议
深入Linux内核架构:操作系统的核心奥秘
深入Linux内核架构:操作系统的核心奥秘
135 2
|
3月前
|
监控 Oracle 关系型数据库
Linux平台Oracle开机自启动设置
【11月更文挑战第8天】在 Linux 平台设置 Oracle 开机自启动有多种方法,本文以 CentOS 为例,介绍了两种常见方法:使用 `rc.local` 文件(较简单但不推荐用于生产环境)和使用 `systemd` 服务(推荐)。具体步骤包括编写启动脚本、赋予执行权限、配置 `rc.local` 或创建 `systemd` 服务单元文件,并设置开机自启动。通过 `systemd` 方式可以更好地与系统启动过程集成,更规范和可靠。
284 2
|
3月前
|
Oracle Ubuntu 关系型数据库
Linux平台Oracle开机自启动设置
【11月更文挑战第7天】本文介绍了 Linux 系统中服务管理机制,并详细说明了如何在使用 systemd 和 System V 的系统上设置 Oracle 数据库的开机自启动。包括创建服务单元文件、编辑启动脚本、设置开机自启动和启动服务的具体步骤。最后建议重启系统验证设置是否成功。
|
2月前
|
弹性计算 API 持续交付
后端服务架构的微服务化转型
本文旨在探讨后端服务从单体架构向微服务架构转型的过程,分析微服务架构的优势和面临的挑战。文章首先介绍单体架构的局限性,然后详细阐述微服务架构的核心概念及其在现代软件开发中的应用。通过对比两种架构,指出微服务化转型的必要性和实施策略。最后,讨论了微服务架构实施过程中可能遇到的问题及解决方案。
|
3月前
|
Cloud Native Devops 云计算
云计算的未来:云原生架构与微服务的革命####
【10月更文挑战第21天】 随着企业数字化转型的加速,云原生技术正迅速成为IT行业的新宠。本文深入探讨了云原生架构的核心理念、关键技术如容器化和微服务的优势,以及如何通过这些技术实现高效、灵活且可扩展的现代应用开发。我们将揭示云原生如何重塑软件开发流程,提升业务敏捷性,并探索其对企业IT架构的深远影响。 ####
83 3
|
3月前
|
Cloud Native 安全 数据安全/隐私保护
云原生架构下的微服务治理与挑战####
随着云计算技术的飞速发展,云原生架构以其高效、灵活、可扩展的特性成为现代企业IT架构的首选。本文聚焦于云原生环境下的微服务治理问题,探讨其在促进业务敏捷性的同时所面临的挑战及应对策略。通过分析微服务拆分、服务间通信、故障隔离与恢复等关键环节,本文旨在为读者提供一个关于如何在云原生环境中有效实施微服务治理的全面视角,助力企业在数字化转型的道路上稳健前行。 ####
|
2月前
|
Java 开发者 微服务
从单体到微服务:如何借助 Spring Cloud 实现架构转型
**Spring Cloud** 是一套基于 Spring 框架的**微服务架构解决方案**,它提供了一系列的工具和组件,帮助开发者快速构建分布式系统,尤其是微服务架构。
303 69
从单体到微服务:如何借助 Spring Cloud 实现架构转型

热门文章

最新文章