资源简介
Ubuntu/Linux下V4L2摄像头视频采集经过x264编码后,再进行rtmp打包封装并推送到服务器上;源码有(rtmper.cpp sender.cpp vencoder.cpp...)视频流可以在VLC和Android app上播放;更多可关注:http://blog.csdn.net/yqw2007/article/details/43273259
代码片段和文件信息
extern “C“{
#ifdef __cplusplus
#define __STDC_CONSTANT_MACROS
#ifdef _STDINT_H
#undef _STDINT_H
#endif
# include
#endif
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
extern “C“ {
# include
# include
}
#include “capture.h“
static const int BUFFS_REQUEST = 4;
struct Buffer
{
void *start;
size_t length;
};
typedef struct Buffer Buffer;
struct Ctx
{
int vid;
int width height; // 输出图像大小
struct SwsContext *sws; // 用于转换
int rows; // 用于 sws_scale()
int bytesperrow; // 用于cp到 pic_src
AVPicture pic_src pic_target; // 用于 sws_scale
Buffer bufs[BUFFS_REQUEST]; // 用于 mmap
PixelFormat fmt;
};
typedef struct Ctx Ctx;
int capture_get_output_ptr (void *c unsigned char***ptr int **ls)
{
Ctx *ctx = (Ctx*)c;
*ptr = ctx->pic_target.data;
*ls = ctx->pic_target.linesize;
return 1;
}
/*
函数功能: 打开视频设备并完成设置和内存映射
参考资料: http://www.embedu.org/Column/Column320.htm
*/
void *capture_open (const char *dev_name int t_width int t_height PixelFormat tarfmt)
{
int id = open(dev_name O_RDWR);//打开视频设备 /dev/videoX
if (id < 0) return 0;
Ctx *ctx = new Ctx;
ctx->vid = id;
// to query caps
v4l2_capability caps;
ioctl(id VIDIOC_QUERYCAP &caps);
//测试是否支持捕获接口 The device supports the Video Capture interface.
if (caps.capabilities & V4L2_CAP_VIDEO_CAPTURE)
{
//测试是否支持读写 The device supports the read() and/or write() I/O methods.
if (caps.capabilities & V4L2_CAP_READWRITE)
{
// TODO: ...
}
//测试是否支持流接口 The device supports the streaming I/O method.
if (caps.capabilities & V4L2_CAP_STREAMING)
{
// 检查是否支持 MMAP 还是 USERPTR
v4l2_requestbuffers bufs;
memset(&bufs 0 sizeof(bufs));
bufs.count = BUFFS_REQUEST;//The number of buffers requested or granted. This field is only used when memory is set to V4L2_MEMORY_MMAP.
bufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;//Type of the stream or buffers this is the same as the struct v4l2_format type field. See Table 3-2 for valid values.
bufs.memory = V4L2_MEMORY_MMAP;
//向驱动申请帧缓存 开启内存映射或用户指针I/O
if (ioctl(id VIDIOC_REQBUFS &bufs) < 0)
{
fprintf(stderr “%s: don‘t support MEMORY_MMAP mode!\n“ __func__);
close(id);
delete ctx;
return 0;
}
fprintf(stderr “%s: using MEMORY_MMAP mode buf cnt=%d\n“ __func__ bufs.count);
// mmap
for (int i = 0; i < bufs.count; i++)
{
v4l2_buffer buf;
memset(&buf 0 sizeof(buf));
buf.index=i;
buf.type = bufs.type;
buf.memory = bufs.memory;
//获取到对应index的缓存信息,此处主要利用length信息及offset信息来完成后面的mmap操作。
if (ioctl(id VIDIOC_QUERYBUF &buf) < 0)
{
fprintf(stderr “%s: VIDIOC_QUERYBUF ERR\n“ __func__);
close(id);
delete ctx;
return 0;
}
//转换成相对地址
ctx->bufs[i].length = buf.length;
ctx->bufs[i
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
....... 84 2012-07-10 10:04 v4l2_x264_rtmp\run.txt
....... 13353 2015-02-04 15:36 v4l2_x264_rtmp\vencoder.cpp
....... 109 2011-07-14 11:54 v4l2_x264_rtmp\README
....... 1027 2011-07-14 11:54 v4l2_x264_rtmp\sender.cpp
....... 1106 2015-02-04 15:30 v4l2_x264_rtmp\Makefile
....... 234 2012-07-10 15:34 v4l2_x264_rtmp\types.h
....... 664 2015-02-04 15:33 v4l2_x264_rtmp\vencoder.h
....... 412 2011-07-14 11:54 v4l2_x264_rtmp\sender.h
....... 393 2012-07-10 11:26 v4l2_x264_rtmp\capture.h
文件 11713 2015-02-04 15:35 v4l2_x264_rtmp\rtmper.cpp
文件 50220 2015-02-04 15:38 v4l2_x264_rtmp\si\liveX264.PS
文件 408 2015-02-04 15:38 v4l2_x264_rtmp\si\liveX264.IMD
文件 2696 2015-02-04 15:38 v4l2_x264_rtmp\si\liveX264.PR
文件 8192 2015-02-04 15:38 v4l2_x264_rtmp\si\liveX264.IMB
文件 12241 2015-02-04 15:38 v4l2_x264_rtmp\si\liveX264.WK3
文件 14680 2015-02-04 15:38 v4l2_x264_rtmp\si\liveX264.PRI
文件 448 2015-02-04 15:38 v4l2_x264_rtmp\si\liveX264.IAD
文件 12288 2015-02-04 15:38 v4l2_x264_rtmp\si\liveX264.IAB
文件 0 2015-02-04 15:38 v4l2_x264_rtmp\si\liveX264.PFI
文件 776 2015-02-04 15:38 v4l2_x264_rtmp\si\liveX264.PO
....... 7949 2012-07-10 21:11 v4l2_x264_rtmp\capture.cpp
目录 0 2015-02-04 15:38 v4l2_x264_rtmp\si
目录 0 2015-02-04 15:37 v4l2_x264_rtmp
----------- --------- ---------- ----- ----
138993 23
- 上一篇:java获取鼠标坐标位置swing
- 下一篇:Java编码规范
评论
共有 条评论