【问题标题】:Using libVLC to read RTSP video stream + convert data to OpenCV Mat使用 libVLC 读取 RTSP 视频流 + 将数据转换为 OpenCV Mat
【发布时间】:2017-04-27 09:37:08
【问题描述】:

我正在尝试使用 libVLC 库通过 IP 接口从 Sony FCB-EV7520 摄像机读取 RTSP 流,并将数据转换为 OpenCV 使用的格式,即 Mat 类型。几天来,我一直试图找到一个很好的例子,但到目前为止我发现的唯一结果是thisthis。我按照第一个示例中的代码进行了调整,使其适应 RTSP 用例,但我还没有检索到 Mat 中的任何数据。但是,从终端输出我似乎实现了与相机的连接。您在代码中看到任何明显的缺陷吗?还有其他(更简单的)方法可以实现我的目标吗?还有其他可以使用的库吗?任何帮助,将不胜感激!我正在运行的代码是:

#include <stdio.h>
#include <vlc/vlc.h>
#include <stdint.h>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

struct VideoDataStruct {
    int param;
};

int done = 0; 
libvlc_media_player_t *mp;
unsigned int videoBufferSize = 0;
uint8_t *videoBuffer = 0;

void cbVideoPrerender(void *p_video_data, uint8_t **pp_pixel_buffer, int size) {

    // Locking

    // Locking a mutex here once I get the code to work.    

    if (size > videoBufferSize || !videoBuffer){

        printf("Reallocate raw video buffer\n");
        free(videoBuffer);
        videoBuffer = (uint8_t *) malloc(size);
        videoBufferSize = size;

    }

    *pp_pixel_buffer = videoBuffer;
}  

void cbVideoPostrender(void *p_video_data, uint8_t *p_pixel_buffer, int width, int height, int pixel_pitch, int size, int64_t pts) {

    // Unlocking

    // Unlocking the mutex here once I get the code to work.

}

static void handleEvent(const libvlc_event_t* pEvt, void* pUserData) {
    libvlc_time_t time;
    switch(pEvt->type){

        case libvlc_MediaPlayerTimeChanged:

            time = libvlc_media_player_get_time(mp);
            printf("MediaPlayerTimeChanged %lld ms\n", (long long)time);
            break;

        case libvlc_MediaPlayerEndReached:

            printf ("MediaPlayerEndReached\n");
            done = 1;
            break;

        default:

            printf("%s\n", libvlc_event_type_name(pEvt->type));

    }
}

int main(int argc, char* argv[]) {

    // VLC pointers: 

    libvlc_instance_t *inst;
    libvlc_media_t *m;
    void *pUserData = 0;
    VideoDataStruct dataStruct;

    // VLC options:

    char smem_options[1000];

    // RV24:

    sprintf(smem_options
            , "#transcode{vcodec=h264}:smem{"
            "video-prerender-callback=%lld,"
            "video-postrender-callback=%lld,"
            "video-data=%lld,"
            "no-time-sync},"
            , (long long int)(intptr_t)(void*)&cbVideoPrerender
            , (long long int)(intptr_t)(void*)&cbVideoPostrender
            , (long long int)(intptr_t)(void*)&dataStruct
           );

    const char * const vlc_args[] = {
        "-I", "dummy",            // Don't use any interface
        "--ignore-config",        // Don't use VLC's config
        "--extraintf=logger",     // Log anything
        "--verbose=2",            // Be verbose
        "--sout", smem_options    // Stream to memory
    };

    // We launch VLC

    inst = libvlc_new(sizeof(vlc_args) / sizeof(vlc_args[0]), vlc_args);

    /* Create a new item */

    m = libvlc_media_new_location(inst, "rtsp://*****:*******@IP/videoinput_1/h264_1/media.stm");

    /* Create a media player playing environement */

    mp = libvlc_media_player_new_from_media (m);

    libvlc_event_manager_t* eventManager = libvlc_media_player_event_manager(mp);
    libvlc_event_attach(eventManager, libvlc_MediaPlayerTimeChanged, handleEvent, pUserData);
    libvlc_event_attach(eventManager, libvlc_MediaPlayerEndReached, handleEvent, pUserData);
    libvlc_event_attach(eventManager, libvlc_MediaPlayerPositionChanged, handleEvent, pUserData);

    libvlc_video_set_format(mp, "h264", 1920, 1080, 1920* 3 );

    /* play the media_player */
    libvlc_media_player_play (mp);

    while(1){

        if(videoBuffer){            // Check for invalid input

            Mat img = Mat(Size(1920, 1080), CV_8UC3, videoBuffer);  // CV_8UC3 = 8 bits, 3 chanels
            namedWindow("Display window", WINDOW_AUTOSIZE);     // Create a window for display.
            imshow("Display window", img);              // Show our image inside it.

        }
    }

    libvlc_release (inst);

}

我正在使用 OpenCV 3.2 和 libVLC 2.2.5.1 在 Ubuntu 16.04 上运行代码。如果您需要任何其他信息,请尽管询问。

PS:我知道流正在工作,因为我可以通过 VLC 播放器的流接口打开它。我也知道 libVLC 可以解码流,因为我已成功打开记录的流的 mp4。

【问题讨论】:

    标签: c++ opencv libvlc


    【解决方案1】:

    不是一个完整的答案,但评论太长了:

    cbVideoPrerender 中,videoBuffer 总是根据 vlc 的需要分配。
    但是,在main 中,您有循环while(1){ if (videoBuffer) { ... } },其中videoBuffer 从第一次调用cbVideoPrerender 开始始终为真。这意味着从那时起,循环是无限的和非阻塞的,所以视频的输入和处理之间没有同步,如果你只是想得到第一张图像,你就太早了。

    您的first link 建议使用cbVideoPostrender 作为同步点,这样您就知道可以读取帧并因此将其转换为所需的格式。它在函数本身中完成,但您可以使用condition variable(队列、事件...)的一些机制在另一个线程中处理帧并将图像传递给openCV。

    顺便说一句:通过在一个线程中设置并在另一个没有线程机制(互斥、原子)的情况下读取它来使用变量是一个坏主意。

    【讨论】:

      猜你喜欢
      • 2014-05-30
      • 1970-01-01
      • 2019-08-04
      • 1970-01-01
      • 2018-09-04
      • 2012-02-24
      • 2014-01-29
      • 2020-04-27
      • 1970-01-01
      相关资源
      最近更新 更多