用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 用通配符查找指定目录中的文件
随机推荐
- UFT参数化
1.Resources-Record and Run Settings 2.打开程序进行录制操作 3.对Fly from和Fly to进行参数化 4.选中点击 5.输入名称,点击OK 6.在Data加 ...
- 手机遥控Office,变身演讲达人
编者按:在商业演讲中,需要在PPT/Word/Excel文件中切换以达到最佳演讲效果-Office Remote可帮助Windows Phone变身Office的智能遥控.以蓝牙控制电脑,触屏操作多种 ...
- 使用Hangfire MVC 做排程
Greg Yang Developer Taipei, Taiwan 108 POSTS 35 TAGS 所使用的是 Hangfire 強大排程器有 UI介面可以使用. 首先安裝PM> Inst ...
- jstl引入报错
jstl1.0的引入方式为: <taglib uri="http://java.sun.com/jstl/core" prefix="c" /> j ...
- Python实现线程交替打印字符串
import threading con = threading.Condition() word = u"12345上山打老虎" def work(): global word ...
- xstream的介绍及用法
使用xstream工具包导入xpp3_min-1.1.4c和xstream-1.4.9特点:代码简洁,超级方便,可以自己定义xml格式(适合做文件传输)属性特点:1. xStream.alias(&q ...
- Maximum Value(CodeForces - 484B)
Maximum Value Time limit 1000 ms Memory limit 262144 kB You are given a sequence a consisting of n i ...
- Python-rediscluster客户端
# -*- coding: UTF-8 -*- import redis import sys from rediscluster import StrictRedisCluster #host = ...
- C++走向远洋——34(友元函数,成员函数和一般函数的区别)
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:youyuan.cpp * 作者:常轩 * 微信公众号:Worl ...
- Rust入坑指南:朝生暮死
今天想和大家一起把我们之前挖的坑再刨深一些.在Java中,一个对象能存活多久全靠JVM来决定,程序员并不需要去关心对象的生命周期,但是在Rust中就大不相同,一个对象从生到死我们都需要掌握的很清楚. ...