有时候,我们需要播放别的模块传输过来的视频流,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. UVALive 3835:Highway(贪心 Grade D)

    VJ题目链接 题意:平面上有n个点,在x轴上放一些点,使得平面上所有点都能找到某个x轴上的点,使得他们的距离小于d.求最少放几个点. 思路:以点为中心作半径为d的圆,交x轴为一个线段.问题转换成用最少 ...

  2. SHELL用法六(Find语句)

    1.SHELL编程Find语句案例实战 1)SHELL编程四剑客工具:Find.Grep.Sed.Awk,通过四剑客可以完成常规Linux指令无法完成或者比较复杂的功能,学好SHELL编程四剑客有助于 ...

  3. 撰写introduction|引用

    科研论文写作-introduction Introduction主要是写研究的来龙去脉,即该研究的历史,包括以前存在问题及其评价,和现今研究创新点,这样引导读者便于理解,阐述的内容也是由背景.目的.方 ...

  4. java异常分析;剖析printStackTrace和fillInStackTrace

    Java异常的栈轨迹(Stack Trace) 捕获到异常时,往往需要进行一些处理.比较简单直接的方式就是打印异常栈轨迹Stack Trace.说起栈轨迹,可能很多人和我一样,第一反应就是printS ...

  5. 3DMAX卸载/完美解决安装失败/如何彻底卸载清除干净3DMAX各种残留注册表和文件的方法

    在卸载3dmax重装3dmax时发现安装失败,提示是已安装3dmax或安装失败.这是因为上一次卸载3dmax没有清理干净,系统会误认为已经安装3dmax了.有的同学是新装的系统也会出现3dmax安装失 ...

  6. <JZOJ5913>林下风气

    快乐dp 反正考场写挂 #include<cstdio> #include<cstring> #include<cctype> #include<iostre ...

  7. Apollo配置中心介绍与使用指南

    转载于https://github.com/ctripcorp/apollo,by Ctrip, Inc. Apollo配置中心介绍 Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中 ...

  8. iOS多线程开发之GCD(中级篇)

    前文回顾: 上篇博客讲到GCD的实现是由队列和任务两部分组成,其中获取队列的方式有两种,第一种是通过GCD的API的dispatch_queue_create函数生成Dispatch Queue:第二 ...

  9. (为容器分配独立IP方法二)通过虚拟IP实现docker宿主机增加对外IP接口

    虚拟IP.何为虚拟IP,就是一个未分配给真实主机的IP,也就是说对外提供数据库服务器的主机除了有一个真实IP外还有一个虚IP,使用这两个IP中的任意一个都可以连接到这台主机,所有项目中数据库链接一项配 ...

  10. 码海拾遗:strcpy()、strncpy()和strcpy_s()区别

    1.strcpy() 原型:char *strcpy(char *dst,const char *src) 功能:将以src为首地址的字符串复制到以dst为首地址的字符串,包括'\0'结束符,返回ds ...