有时候,我们需要播放别的模块传输过来的视频流,VLC提供了这样的机制,但一般很少这样用,下面的例子实现了这样的功能。

其中用到一个关键的模块 imem.  vlc提供多种创建媒体的方式,如要从指定缓存中数据,可以如下方式指定

// Create a media item from file
m = libvlc_media_new_location (inst, "imem://"); /*##use memory as input*/

下面是完整的示例

// vlcTest.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <Windows.h>
#include "vlc/vlc.h"
#include <vector>
#include <qmutex>
#include <sstream>
#include <qimage> QMutex g_mutex;
bool g_isInit = false;
int IMG_WIDTH = ;
int IMG_HEIGHT = ;
char in_buffer[**];
char out_buffer[**];
FILE *local;
int frameNum = ; const char* TestFile = "b040_20170106.dat"; ////////////////////////////////////////////////////////////////////////// /**
\brief Callback method triggered by VLC to get image data from
a custom memory source. This is used to tell VLC where the
data is and to allocate buffers as needed. To set this callback, use the "--imem-get=<memory_address>"
option, with memory_address the address of this function in memory. When using IMEM, be sure to indicate the format for your data
using "--imem-cat=2" where 2 is video. Other options for categories are
0 = Unknown,
1 = Audio,
2 = Video,
3 = Subtitle,
4 = Data When creating your media instance, use libvlc_media_new_location and
set the location to "imem:/" and then play. \param[in] data Pointer to user-defined data, this is your data that
you set by passing the "--imem-data=<memory_address>" option when
initializing VLC instance.
\param[in] cookie A user defined string. This works the same way as
data, but for string. You set it by adding the "--imem-cookie=<your_string>"
option when you initialize VLC. Use this when multiple VLC instances are
running.
\param[out] dts The decode timestamp, value is in microseconds. This value
is the time when the frame was decoded/generated. For example, 30 fps
video would be every 33 ms, so values would be 0, 33333, 66666, 99999, etc.
\param[out] pts The presentation timestamp, value is in microseconds. This
value tells the receiver when to present the frame. For example, 30 fps
video would be every 33 ms, so values would be 0, 33333, 66666, 99999, etc.
\param[out] flags Unused,ignore.
\param[out] bufferSize Use this to set the size of the buffer in bytes.
\param[out] buffer Change to point to your encoded frame/audio/video data.
The codec format of the frame is user defined and set using the
"--imem-codec=<four_letter>," where 4 letter is the code for your
codec of your source data.
*/
int MyImemGetCallback (void *data,
const char *cookie,
int64_t *dts,
int64_t *pts,
unsigned *flags,
size_t * bufferSize,
void ** buffer)
{
static int64_t _dts = , _pts = ;
if (!g_isInit){
/*load local file*/
local = fopen(TestFile,"rb");
if (!local){
return true;
}
size_t count = fread(in_buffer,,IMG_WIDTH*IMG_HEIGHT*,local);
*bufferSize = count;
*buffer = in_buffer;
g_isInit = true;
*dts = _dts; *pts = _pts;
_dts+=; _pts+=;
return ;
}
size_t count = fread(in_buffer,,IMG_WIDTH*IMG_HEIGHT*,local);
*bufferSize = count;
*buffer = in_buffer;
*dts = _dts; *pts = _pts;
_dts+=; _pts+=;
if(count>) {
printf("read %d bytes\n",count);
return ;
}else{
return true; /*eof*/
}
} /**
\brief Callback method triggered by VLC to release memory allocated
during the GET callback. To set this callback, use the "--imem-release=<memory_address>"
option, with memory_address the address of this function in memory. \param[in] data Pointer to user-defined data, this is your data that
you set by passing the "--imem-data=<memory_address>" option when
initializing VLC instance.
\param[in] cookie A user defined string. This works the same way as
data, but for string. You set it by adding the "--imem-cookie=<your_string>"
option when you initialize VLC. Use this when multiple VLC instances are
running.
\param[int] bufferSize The size of the buffer in bytes.
\param[out] buffer Pointer to data you allocated or set during the GET
callback to handle or delete as needed.
*/
int MyImemReleaseCallback (void *data,
const char *cookie,
size_t bufferSize,
void * buffer)
{
// Since I did not allocate any new memory, I don't need
// to delete it here. However, if you did in your get method, you
// should delete/free it here.
return ;
} //////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
libvlc_instance_t * inst;
libvlc_media_player_t *mp;
libvlc_media_t *m; libvlc_time_t length;
int wait_time=; std::vector<const char*> options;
std::vector<const char*>::iterator option;
options.push_back("--no-video-title-show"); char imemDataArg[];
sprintf(imemDataArg, "--imem-data=%#p", in_buffer);
options.push_back(imemDataArg); char imemGetArg[];
sprintf(imemGetArg, "--imem-get=%#p", MyImemGetCallback);
options.push_back(imemGetArg); char imemReleaseArg[];
sprintf(imemReleaseArg, "--imem-release=%#p", MyImemReleaseCallback); options.push_back(imemReleaseArg);
options.push_back("--imem-cookie=\"IMEM\""); options.push_back("--imem-codec=H264");
// Video data.
options.push_back("--imem-cat=2"); /* Load the VLC engine */
inst = libvlc_new (int(options.size()), options.data()); // Configure any transcoding or streaming
// options for the media source.
options.clear(); // Create a media item from file
m = libvlc_media_new_location (inst, "imem://"); /*##use memory as input*/ // Set media options
for(option = options.begin(); option != options.end(); option++){
libvlc_media_add_option(m, *option);
} /* Create a media player playing environment */
mp = libvlc_media_player_new_from_media (m); /* No need to keep the media now */
libvlc_media_release (m); // play the media_player
libvlc_media_player_play (mp); //wait until the tracks are created
_sleep (wait_time);
length = libvlc_media_player_get_length(mp);
IMG_WIDTH = libvlc_video_get_width(mp);
IMG_HEIGHT = libvlc_video_get_height(mp);
printf("Stream Duration: %ds\n",length/);
printf("Resolution: %d x %d\n",IMG_WIDTH,IMG_HEIGHT); //Let it play
_sleep (length-wait_time); // Stop playing
libvlc_media_player_stop (mp); // Free the media_player
libvlc_media_player_release (mp); libvlc_release (inst); return ;
}

以上代码要注意的是: 用OPTIONS 告诉VLC , 要使用imem作为输入,还要告诉vlc 其中用到哪些回调函数, 这个与直接设置回调的方式不一样, 它用options中的字符串表示。

用libvlc 播放指定缓冲区中的视频流的更多相关文章

  1. iOS开发系列--扩展--播放音乐库中的音乐

    众所周知音乐是iOS的重要组成播放,无论是iPod.iTouch.iPhone还是iPad都可以在iTunes购买音乐或添加本地音乐到音乐 库中同步到你的iOS设备.在MediaPlayer.fram ...

  2. python 读取二进制数据到可变缓冲区中

    想直接读取二进制数据到一个可变缓冲区中,而不需要做任何的中间复制操作.或者你想原地修改数据并将它写回到一个文件中去. 为了读取数据到一个可变数组中,使用文件对象的readinto() 方法.比如 im ...

  3. js实现未知宽高的元素在指定元素中垂直水平居中

    js实现未知宽高的元素在指定元素中垂直水平居中:本章节介绍一下如何实现未知宽高的元素在指定元素下实现垂直水平居中效果,下面就以span元素为例子,介绍一下如何实现span元素在div中实现水平垂直居中 ...

  4. Mysql 导出数据库和指定表中的数据

    参考地址:http://jingyan.baidu.com/article/b7001fe14240ab0e7282dde9.html [root@youo zw]# mysqldump -u roo ...

  5. Java读取excel指定sheet中的各行数据,存入二维数组,包括首行,并打印

    1. 读取 //读取excel指定sheet中的各行数据,存入二维数组,包括首行 public static String[][] getSheetData(XSSFSheet sheet) thro ...

  6. JDBC批处理读取指定Excel中数据到Mysql关系型数据库

    这个demo是有一个Excel中的数据,我需要读取其中的数据然后导入到关系型数据库中,但是为了向数据库中插入更多的数据,循环N次Excel中的结果. 关于JDBC的批处理还可以参考我总结的如下博文: ...

  7. 我用C#调用C编译的dll中有这样一个函数,函数大概的功能就是把数据保存到buf缓冲区中:

    我用C#调用C编译的dll中有这样一个函数,函数大概的功能就是把数据保存到buf缓冲区中: C/C++ code   ? 1 int retrieve(int scanno,void* buf); 在 ...

  8. oracle中从指定日期中获取月份或者部分数据

    从指定日期中获取部分数据: 如月份: select to_CHAR(sysdate,'MM') FROM DUAL; 或者: select extract(month from sysdate) fr ...

  9. python glob 用通配符查找指定目录中的文件 - 开源中国社区

    python glob 用通配符查找指定目录中的文件 - 开源中国社区 python glob 用通配符查找指定目录中的文件

随机推荐

  1. 蓝桥杯-PREV3-带分数

    有人管蓝桥杯叫暴力杯,现在感觉还是挺贴切的.看到这题首先想到让i从1到n循环,首先判断i中无重复数字,再怎样判断能否用剩下的数构成n - i的假分数.之后看了题解.发现思路错了. 总结两点: 1.蓝桥 ...

  2. Invalid action class configuration that references an unknown class问题原因之s:select

    早先做个练习项目就出现了这个错误,各种查资料,然后各种尝试,依然没有解决,不过可以确定是前台页面导致的. 今天又碰到了这个问题,头疼啊!不能再略过了,使用最笨的方法,一个模块一个模块的排除.先看下我的 ...

  3. Ajax如何提交数据到springMVC后台

    现在好多web项目实现前段和后端分离,实现前端和后端技术人员,使他们加快开发,减少沟通上的问题,后台只需要提供访问接口,而前天只需要调用提供的接口即可.减少前后端的沟通上的成本 本项目是开发中发现aj ...

  4. 4K时代,你不能不知道的HEVC

    最近追的美剧更新啦!但手机没连wifi,看视频心疼流量:画面不清晰,老是卡机:真是令人苦恼不已.别着急,或许在HEVC大范围普及之后,这一切烦恼都将不复存在了. HEVC是什么?它是High Effi ...

  5. Thymeleaf模板笔记

    1.常用标签: 使用thymeleaf模板,首要在html中引入: <html xmlns:th="http://www.thymeleaf.org"> 引入css.j ...

  6. from PIL import image报错

    python中import PIL可以,但是from PIL import Image就报错? ’‘ 大家在安装pillow的时候,可能会安装成功,但是当运行from pIL import image ...

  7. js如何深度克隆

    var json = {a:6,b:4,c:[1,2,3]}; var json2 = clone(json); function clone(obj){ var oNew = new obj.con ...

  8. 接轨国际,碰撞更多科研火花——第八届ChinaSys大会专访微软亚洲研究院首席研究员张霖涛

    作者:微软亚洲研究院实习生 徐祎雪 卢思奇 2015年6月5日至6日,由中国科学院深圳先进技术研究院先进计算与数字工程研究所主办的第八届中国计算机系统(ChinaSys)学术研讨会在厦门大学召开.来自 ...

  9. VR、网剧如何成为民间骗子中的朝阳产业

    ​ 互联网的发达,让大众有了了解世界最好的传播工具.但与此同时,大量信息潮的涌来,让人们难以分辨其中的真假.于是,原本靠大力丸.保健药.假公章等行骗的骗子们开始转变方向,利用信息不对称性,大肆捏造与热 ...

  10. 如何在自己的CSDN博客中增添【高大上】的博客栏目?

    前几天看到过一位博主的博客界面,向下看 ☟ (博主对不起啊!把你的公众号给抹了~~~),感觉做这个东西挺好玩的,而且我竟然找不到在哪个地方可以设置!在百度上也没有搜到教程,最后问了一下贺老师知道了入口 ...