用libvlc 播放指定缓冲区中的视频流
有时候,我们需要播放别的模块传输过来的视频流,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 播放指定缓冲区中的视频流的更多相关文章
- iOS开发系列--扩展--播放音乐库中的音乐
众所周知音乐是iOS的重要组成播放,无论是iPod.iTouch.iPhone还是iPad都可以在iTunes购买音乐或添加本地音乐到音乐 库中同步到你的iOS设备.在MediaPlayer.fram ...
- python 读取二进制数据到可变缓冲区中
想直接读取二进制数据到一个可变缓冲区中,而不需要做任何的中间复制操作.或者你想原地修改数据并将它写回到一个文件中去. 为了读取数据到一个可变数组中,使用文件对象的readinto() 方法.比如 im ...
- js实现未知宽高的元素在指定元素中垂直水平居中
js实现未知宽高的元素在指定元素中垂直水平居中:本章节介绍一下如何实现未知宽高的元素在指定元素下实现垂直水平居中效果,下面就以span元素为例子,介绍一下如何实现span元素在div中实现水平垂直居中 ...
- Mysql 导出数据库和指定表中的数据
参考地址:http://jingyan.baidu.com/article/b7001fe14240ab0e7282dde9.html [root@youo zw]# mysqldump -u roo ...
- Java读取excel指定sheet中的各行数据,存入二维数组,包括首行,并打印
1. 读取 //读取excel指定sheet中的各行数据,存入二维数组,包括首行 public static String[][] getSheetData(XSSFSheet sheet) thro ...
- JDBC批处理读取指定Excel中数据到Mysql关系型数据库
这个demo是有一个Excel中的数据,我需要读取其中的数据然后导入到关系型数据库中,但是为了向数据库中插入更多的数据,循环N次Excel中的结果. 关于JDBC的批处理还可以参考我总结的如下博文: ...
- 我用C#调用C编译的dll中有这样一个函数,函数大概的功能就是把数据保存到buf缓冲区中:
我用C#调用C编译的dll中有这样一个函数,函数大概的功能就是把数据保存到buf缓冲区中: C/C++ code ? 1 int retrieve(int scanno,void* buf); 在 ...
- oracle中从指定日期中获取月份或者部分数据
从指定日期中获取部分数据: 如月份: select to_CHAR(sysdate,'MM') FROM DUAL; 或者: select extract(month from sysdate) fr ...
- python glob 用通配符查找指定目录中的文件 - 开源中国社区
python glob 用通配符查找指定目录中的文件 - 开源中国社区 python glob 用通配符查找指定目录中的文件
随机推荐
- 吴裕雄--天生自然python学习笔记:python处理word文档
Office 文件是我们日常工作生活中都经常用到的文件格 式,其中以 Word 格式的文件最为常用 . Python 可通过 Win32com 纽件对 Micro so位 Office 文件 进行存取 ...
- ML modeling process
一.数据读取Load Data 二.数据分析EDA 三.数据预处理 四.特征工程Feature engineering 五.modeling & Tuning 六.Result 七.other ...
- 实例理解scala 隐式转换(隐式值,隐式方法,隐式类)
作用 简单说,隐式转换就是:当Scala编译器进行类型匹配时,如果找不到合适的候选,那么隐式转化提供了另外一种途径来告诉编译器如何将当前的类型转换成预期类型.话不多说,直接测试 ImplicitHel ...
- 转载【docker】CMD ENTRYPOINT 的使用方法
原文:https://blog.csdn.net/u010900754/article/details/78526443
- 【Linux_Shell 脚本编程学习笔记五、Oracle JDK1.8 安装shell 脚本】
脚本使用说明: 首先在脚本的同级目录下有个 jdk的安装包 脚本和安装包必须在同级目录下才能够安装(脚本没有优化,还可以使用 wget 从网上下载指定版本的 jdk 安装包) #!/bin/sh ...
- 创建框架链接--frameset的连接方法
首先看下小编的目录架构 1.html将作为主页面 2.html将作为目录页面,里面有2个目录,分别是目录一和目录二 3.html为目录一将要链接的页面 4.html为目录二将要链接的页面 然后,看下1 ...
- FPGA的存储方式大全
好的时序是通过该严密的逻辑来实现的.http://blog.csdn.net/i13919135998/article/details/52117053介绍的非常好 有RAM(随机存储器可读可写)RO ...
- UFT安装
1.下载解压双击setup.exe 2.点击安装 3.点击下一步 4.检测是否需要安装插件之后一路向下 5.安装之后图标 下载: 链接:https://pan.baidu.com/s/1sa0h037 ...
- 【React.js小书】动手实现 React-redux(五):Provider - 方志
我们要把 context 相关的代码从所有业务组件中清除出去,现在的代码里面还有一个地方是被污染的.那就是 src/index.js 里面的 Index: 1234567891011121314151 ...
- 微信小游戏排行榜页滚动查看排行榜(canvas指定区域溢出滚动,懒渲染)
在微信小游戏中,好友排名数据是能在关系数据域操作,整个关系数据域只会返回一个最终的sharedCanvas,并且这个canvas不能调用toDataURL()方法,所以要展示好友排行榜的话只能在关系数 ...