首页 > 代码库 > windows使用libvlc进行网络串流遇到的一些问题及解决方法

windows使用libvlc进行网络串流遇到的一些问题及解决方法

先贴代码,

#define WIFI_W 640
#define WIFI_H 480


struct
ctx{ uchar* frame;};class CConnectWIFI{public: CConnectWIFI(void); void Init(HWND hWnd); libvlc_instance_t* m_vlcInst; libvlc_media_player_t* m_vlcMplay; libvlc_media_t* m_vlcMedia; struct ctx context; ~CConnectWIFI(void);};CConnectWIFI::CConnectWIFI(void){ m_vlcInst = NULL; m_vlcMplay = NULL; m_vlcMedia = NULL; context.frame = new uchar[WIFI_W * WIFI_H * 3]; memset(context.frame, 0, WIFI_W*WIFI_H*3); m_vlcInst = libvlc_new(0, NULL); }CConnectWIFI::~CConnectWIFI(void){ if(m_vlcMplay) { libvlc_media_player_release(m_vlcMplay); m_vlcMplay = NULL; } if(m_vlcInst) { libvlc_release(m_vlcInst); m_vlcInst = NULL; } if (context.frame) { delete []context.frame; context.frame = NULL; }}void* lock(void* data, void** p_pixels){ struct ctx* ctx = (struct ctx*)data; *p_pixels = ctx->frame; return NULL;}void unlock(void* data, void* id, void* const* p_pixels){ uchar* pixels = (uchar*)*p_pixels; ImageProcessCallBack(pixels, WIFI_W, WIFI_H);}void display(void *data, void *id){ //(void) data; //ASSERT(id == NULL);}void CConnectWIFI::Init(HWND hWnd){ m_vlcMedia = libvlc_media_new_location(m_vlcInst, "http://10.10.1.1:8196"); libvlc_media_add_option(m_vlcMedia, ":network-caching=10"); m_vlcMplay = libvlc_media_player_new_from_media(m_vlcMedia); libvlc_media_player_set_hwnd(m_vlcMplay, hWnd); libvlc_media_release(m_vlcMedia); libvlc_video_set_callbacks(m_vlcMplay, lock, unlock, display, &context); libvlc_video_set_format(m_vlcMplay, "RV24", WIFI_W, WIFI_H, WIFI_W * 3); int res = libvlc_media_player_play(m_vlcMplay);}

以上代码是访问wifi图像设备的一段代码,也是第一次接触vlc,很多地方不是很清楚。

其中libvlc_media_add_option(m_vlcMedia, ":network-caching=10");是为了解决缓冲时间过长导致延迟的问题,如果太大了则延迟严重,太小了则很卡,根据情况设定。另外还有个问题不理解,连接正常之后,突然中断网络,竟然还能调用回调函数ImageProcessCallBack,且一只调用,只不过都是同一帧数据。难道是所谓的“拉”(pull)模式?这个问题怎么解决,有做过的麻烦请在下方留言,感谢!