工控网首页
>

应用设计

>

ARM平台基于嵌入式Linux Gstreamer 使用

ARM平台基于嵌入式Linux Gstreamer 使用

1). 简介

随着ARM平台性能的日益强大和嵌入式设备的发展,对于多媒体处理如音视频播放,摄像头,流媒体处理等需求也日益增多,本文就通过几个基于嵌入式Linux下多媒体应用的示例来简单展示下使用Gstreamer框架进行多媒体处理的方法.

Gstreamer是一个基于Pipeline的多媒体框架,基于GObject,以C语言写成,目前是嵌入式Linux最为常用的处理多媒体应用框架. Element是Gstreamer最重要和基本的对象类,通过Plugin的形式提供,多个Elements可以组合为bin,并进一步聚合形成一个Pipeline完成一个多媒体应用处理.

2). 硬件准备

./ 本文测试所使用平台为ToradexColibri i.MX6 ARM计算机核心板模块, 基于NXP i.MX6 SoC, 配合Colibri开发板.

./ 摄像头作为多媒体输入源使用Logitech720P USB摄像头

3). 软件准备

./ OS为Toradex官方Ycotoproject兼容嵌入式Linux发布 V2.5 beta3.

./ Gstreamer-0.10, 系统自带

./ 安装gstreamer相关插件包

----------------

root@colibri-imx6:~# opkg update

root@colibri-imx6:~# opkg installgst-plugins-base-meta gst-plugins-good-meta

root@colibri-imx6:~# wget http://feeds.angstrom-distribution.org/feeds/v2015.06/ipk/glibc/armv7ahf-vfp-neon/base/libavcodec54_9.16-r0.9_armv7ahf-vfp-neon.ipk

root@colibri-imx6:~# wgethttp://feeds.angstrom-distribution.org/feeds/v2015.06/ipk/glibc/armv7ahf-vfp-neon/base/libswscale2_9.16-r0.9_armv7ahf-vfp-neon.ipk

root@colibri-imx6:~# wgethttp://feeds.angstrom-distribution.org/feeds/v2015.06/ipk/glibc/armv7ahf-vfp-neon/base/libavutil52_9.16-r0.9_armv7ahf-vfp-neon.ipk

root@colibri-imx6:~# wgethttp://feeds.angstrom-distribution.org/feeds/v2015.06/ipk/glibc/armv7ahf-vfp-neon/base/libavformat54_9.16-r0.9_armv7ahf-vfp-neon.ipk

root@colibri-imx6:~# opkg installlibavutil52_9.16-r0.9_armv7ahf-vfp-neon.ipk

root@colibri-imx6:~# opkg installlibavcodec54_9.16-r0.9_armv7ahf-vfp-neon.ipk

root@colibri-imx6:~# opkg installlibswscale2_9.16-r0.9_armv7ahf-vfp-neon.ipk

root@colibri-imx6:~# opkg installlibavformat54_9.16-r0.9_armv7ahf-vfp-neon.ipk

root@colibri-imx6:~# opkg installgst-ffmpeg

----------------

4). 测试gstreamer

Gstreamer提供了两个非常方便的工具gst-launch和gst-inspect,在真正将Gstreamerpipeline集成到你的程序里面前,可以使用这两个工具在命令行下面进行pipeline的准备和测试,本文也主要基于这种模式进行演示,而详细的关于Gstreamer在程序中使用方法请参考这里.

gst-inspect可以用来查看当前系统已经安装的plugin或者具体查看某个element的属性来判定pipeline中elements互联兼容性,详细说明请见这里.

gst-launch 用于编译和运行一个pipeline,可以方便的对pipeline进行不同组合尝试并测试,如下面用来显示一个视频测试图案.

----------------

root@colibri-imx6:~# gst-launch videotestsrc! 'video/x-raw-rgb, width=(int)640,height=(int)480' ! ximagesink

----------------

5). 应用示例

a). 播放摄像头

使用v4l2src元件采集摄像头视频,视频格式为YUY2/帧率30/分辨率640x480; 使用imxv4l2sink元件利用GPU加速播放采集的视频; 如果使用ximagesink元件来播放则没有硬件加速.

----------------

root@colibri-imx6:~# gst-launch v4l2srcdevice=/dev/video0 ! 'video/x-raw-yuv, framerate=30/1, width=(int)640,height=(int)480, format=(fourcc)YUY2' ! imxv4l2sink disp-width=640disp-height=480

----------------

目标板CPU占用率: 37.5%

b). 采集摄像头保存为文件

同样使用v4l2src元件采集摄像头视频; ffmpegcolorspace元件转换颜色格式为I420; vpuenc元件调用硬件VPU进行H.264编码; matroskamux元件进行mkv mux;  filesink元件输出到文件

----------------

root@colibri-imx6:~# gst-launch -v v4l2srcdevice=/dev/video0 ! ffmpegcolorspace ! 'video/x-raw-yuv, framerate=30/1,width=(int)640, height=(int)480, format=(fourcc)I420' ! queue max-size-buffers=2! vpuenc codec=6 ! matroskamux ! filesink location=output.mkv

----------------

目标板CPU占用率: 40%

c). 回放视频文件

使用filesrc元件读取视频文件; aiurdemux元件进行demux; vpudec元件通过硬件VPU进行H.264解码; imxv4l2sink元件播放.

----------------

root@colibri-imx6:~# gst-launch filesrclocation=/home/root/output.mkv typefind=true ! aiurdemux ! queue ! vpudec !imxv4l2sink disp-width=640 disp-height=480

----------------

目标板CPU占用率: 3.5%

d).RTP摄像头流媒体到远程主机

使用v4l2src元件采集摄像头; ffmpegcolorspace转换颜色格式; vpuenc元件调用硬件VPU进行H.264编码;rtph264pay元件进行h264流rtp封装; udpsink元件进行udp传输.

----------------

//Ubuntu主机IP: 10.20.1.116

//Colibri i.MX6目标板IP: 10.20.1.122

/* Colibri i.MX6 */

gst-launch -v v4l2src device=/dev/video0 !ffmpegcolorspace! 'video/x-raw-yuv, framerate=30/1, width=(int)640,height=(int)480, format=(fourcc)I420' ! vpuenc codec=6 ! rtph264pay ! udpsinkhost=10.20.1.116 port=5000

/* Ubuntu 主机 */

//Ubuntu 14.04, 安装gstreamer-ffmpeg

wgethttp://ppa.launchpad.net/mc3man/gstffmpeg-keep/ubuntu/pool/main/g/gstreamer0.10-ffmpeg/gstreamer0.10-ffmpeg_0.10.13-5ubuntu1~trusty2.1_amd64.deb&& sudo dpkg -igstreamer0.10-ffmpeg_0.10.13-5ubuntu1~trusty2.1_amd64.deb

//播放流媒体, caps设置从目标板获得

gst-launch-0.10 udpsrc port=5000caps="application/x-rtp, media=(string)video, clock-rate=(int)90000,encoding-name=(string)H264, sprop-parameter-sets=(string)\"Z0JAHqaAoD2QAA\\=\\=\\,aM44gAA\\=\",payload=(int)96, ssrc=(uint)1172839120, clock-base=(uint)784846202,seqnum-base=(uint)58343" ! rtph264depay ! ffdec_h264 ! ximagesink

----------------

目标板CPU占用率: 35%

6). Gstreamer1.0应用示例

Colibri i.MX6 最新Linux发布版本V2.6beta1已经支持gstreamer1.0,升级方法请见这里

./ 软件准备

安装相关插件包

----------------

opkg update

opkg install gstreamer1.0-plugins-base-meta

opkg install gstreamer1.0-plugins-good-meta

opkg remove --force-dependslibgstimxcompositor0 //目前版本中需要移除否则无法使用gstreamer

----------------

./ 应用示例

a). 播放摄像头

----------------

root@colibri-imx6:~# gst-launch-1.0 imxv4l2srcdevice=/dev/video0 ! 'video/x-raw, framerate=30/1, width=(int)640,height=(int)480, format=(string)YUY2' ! imxv4l2sink overlay-width=640 overlay-height=480

----------------

目标板CPU占用率: 30%

b).  采集摄像头保存为文件

----------------

root@colibri-imx6:~# gst-launch-1.0 imxv4l2srcdevice=/dev/video0 ! videoconvert ! 'video/x-raw, framerate=30/1,width=(int)640, height=(int)480, format=(string)I420' ! queue max-size-buffers=2! vpuenc_h264 ! matroskamux ! filesink location=output.mkv

----------------

目标板CPU占用率: 16%

c). 回放视频文件

----------------

root@colibri-imx6:~# gst-launch-1.0 filesrclocation=/home/root/output.mkv typefind=true ! matroskademux ! queue ! vpudec !imxv4l2sink overlay-width=640 overlay-height=480

----------------

目标板CPU占用率: 4%

d).RTP摄像头流媒体到远程主机

----------------

//Ubuntu主机IP: 10.20.1.116

//Colibri i.MX6目标板IP: 10.20.1.122

/* Colibri i.MX6 */

gst-launch-1.0 imxv4l2srcdevice=/dev/video0 ! videoconvert ! 'video/x-raw, framerate=30/1,width=(int)640, height=(int)480, format=(string)I420' ! queue max-size-buffers=2! vpuenc_h264 ! rtph264pay ! udpsink host=10.20.1.116 port=5000

/* Ubuntu 主机 */

//Ubuntu 14.04, 安装libav (0.10 ffmpeg)

sudo apt-get install gstreamer1.0-libav

//播放流媒体

gst-launch-1.0 udpsrc port=5000caps="application/x-rtp, media=(string)video, clock-rate=(int)90000,encoding-name=(string)H264,payload=(int)96" ! rtph264depay ! avdec_h264 ! videoconvert ! ximagesink

----------------

目标板CPU占用率: 13%

e). 摄像头连接到远程主机tcp视频流到目标版再rtp回远程主机播放

----------------

//Ubuntu主机IP: 10.20.1.116

//Colibri i.MX6目标板IP: 10.20.1.122

/* Ubuntu 主机 */

//采集摄像头并建立tcp server

gst-launch-1.0 v4l2src device=/dev/video0 !videoconvert ! 'video/x-raw, framerate=30/1, width=(int)640, height=(int)480,format=(string)I420' ! jpegenc ! tcpserversink host=10.20.1.116 port=5000

/* Colibri i.MX6 */

//tcp server获取摄像头视频流,并进行H.264压缩后rtp回远程主机

gst-launch-1.0 tcpclientsrchost=10.20.1.116 port=5000 ! jpegdec ! queue max-size-buffers=2 ! vpuenc_h264 !rtph264pay ! udpsink host=10.20.1.116 port=5001

/* Ubuntu 主机 */

//播放rtp视频流

gst-launch-1.0 udpsrc port=5001caps="application/x-rtp, media=(string)video, clock-rate=(int)90000,encoding-name=(string)H264,payload=(int)96" ! rtph264depay ! avdec_h264 ! videoconvert ! ximagesink

----------------

目标板CPU占用率: 31%

7). 总结

由上面示例可见Gstreamer是一个非常强大且配置灵活的多媒体处理框架,并且配合相应的plugin,也可以充分调用硬件GPU/VPU加速,使其能够在嵌入式设备上面利用有限的资源高效的实现广泛的多媒体应用.

投诉建议

提交

查看更多评论
其他资讯

查看更多

在 Linux 系统上 Docker 容器的性能影响

上游优先 - Toradex 采用主线内核支持

NXP iMX8M Plus M7核心FreeRTOS开发

嵌入式Linux上使用Ramoops

使用容器编译Yocto镜像