关于vlc二次开发问题,视频花屏,延迟很长

目前公司用vlc连接网络摄像头,连海康的延迟大概两秒,视频正常,这里延迟几秒没关系,但是连接华为的摄像头延迟能有几十秒,而且还会有花屏现象,开始持续时间较短,后面持续时间越来越长,注:设置相同的宽和高,华为摄像头分辨率明显比海康的高

设置回调函数
void ConnectNetworkCamera::setCallBack()
{
//imshow("test", *m_context.image);
waitKey(1);
s_context.ctx = &m_context;
s_context.conn = this;
libvlc_video_set_callbacks(m_mp, ConnectNetworkCamera::lock, ConnectNetworkCamera::unlock, ConnectNetworkCamera::display, &s_context);
libvlc_video_set_format(m_mp, "RV24", VIDEO_WIDTH, VIDEO_HEIGHT, VIDEO_WIDTH * 24 / 8); // pitch = width * BitsPerPixel / 8
// int ret = libvlc_media_player_play(m_mp);
// showfps();
m_thread_handle = CreateThread(NULL, NULL, threadProc, this, NULL, NULL);

}

void* ConnectNetworkCamera::lock(void data, void*p_pixels)
{
PHL_VLC_IN_CONTEXT ctx = (PHL_VLC_IN_CONTEXT)data;

WaitForSingleObject(ctx->ctx->mutex, INFINITE);
// pixel will be stored on image pixel space

*p_pixels = ctx->ctx->image_data.pImageData;
return NULL;

}

void ConnectNetworkCamera::display(void *data, void *id){

PHL_VLC_IN_CONTEXT ctx = (PHL_VLC_IN_CONTEXT)data;
//imshow("test", *ctx->image);
int width = ctx->ctx->image->cols;
int height = ctx->ctx->image->rows;
ctx->ctx->image_data.nWid = libvlc_video_get_width(ctx->conn->m_mp);
ctx->ctx->image_data.nHei = libvlc_video_get_height(ctx->conn->m_mp);
waitKey(1);
assert(id == NULL);

}

void ConnectNetworkCamera::unlock(void *data, void *id, void *const *p_pixels){

// get back data structure 
PHL_VLC_IN_CONTEXT ctx = (PHL_VLC_IN_CONTEXT)data;

/* VLC just rendered the video, but we can also render stuff */
// show rendered image
ReleaseMutex(ctx->ctx->mutex);

}

播放
void ConnectNetworkCamera::play()
{
int ii = 0;
int key = 0;
while (key != 27)
{

    ii++;
    if (ii > 5)
    {
        libvlc_media_player_play(m_mp);
    }
    float fps = libvlc_media_player_get_fps(m_mp);
    printf("fps:%f\r\n", fps);
    key = waitKey(40); // wait 100ms for Esc key
}

libvlc_media_player_stop(m_mp);

}

初始化:
int ConnectNetworkCamera::initialize()
{

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 much more verbose then normal for debugging purpose
};

// m_vlcInstance = libvlc_new(sizeof(vlc_args) / sizeof(vlc_args[0]), vlc_args);
m_vlcInstance = libvlc_new(0, NULL);
if (m_vlcInstance == NULL)
{
printf("初始化VLC失败\n");
return 0;
}
m_close_event = CreateEvent(NULL, TRUE, TRUE, NULL);
return 1;
}

连接摄像头
int ConnectNetworkCamera::ConnectIPCamera(const std::string& connStr)
{
//string connStr = "rtsp://admin:HuaWei123@192.168.1.221:554/LiveMedia/ch1/Media1";
//m_media = libvlc_media_new_location(m_vlcInstance, "rtsp://admin:hl123456@192.168.1.65:554/h264/ch1/main/av_stream");
m_media = libvlc_media_new_location(m_vlcInstance, connStr.c_str());
libvlc_release(m_vlcInstance);
//m_media = libvlc_media_new_location(m_vlcInstance, str_connect.c_str());
if (m_media == NULL)
{
//printf("连接摄像头失败\n");
return 0;
}
//创建一个播放器对象
m_mp = libvlc_media_player_new_from_media(m_media);
if (m_mp == NULL)
{
//printf("创建播放器对象失败\n");
return 0;
}
libvlc_media_release(m_media);

//获取网络摄像机参数

vector<string> str_vec;
splite(connStr, "//", str_vec);
if (str_vec.size() == 0)
{
    return 0;//输入字符窜格式错误
}
string str = *str_vec.rbegin();
str_vec.clear();
splite(str, "/", str_vec);
if (str_vec.size() == 0)
{
    return 0;//输入字符窜格式错误
}
str = *str_vec.begin();
str_vec.clear();
splite(str, "@", str_vec);
if (str_vec.size() != 2)
{
    return 0;//输入字符窜格式错误
}
//前面是用户名密码,后面是ip地址端口号
vector<string> str_user_pass;
vector<string> str_ip_port;
splite(*str_vec.begin(), ":", str_user_pass);
splite(*str_vec.rbegin(), ":", str_ip_port);
if (str_user_pass.size() != 2 || str_ip_port.size() != 2)
{
    return 0;
}
m_CameraInfo.IPAddress = *str_ip_port.begin();
m_CameraInfo.Port = *str_ip_port.rbegin();
m_CameraInfo.UserID = *str_user_pass.begin();
m_CameraInfo.UserPWD = *str_user_pass.rbegin();
return 1;

}

http://blog.csdn.net/xiaoliouc/article/details/8531433