硬件配置:Raspberry Pi3 + 罗技C310摄像头
使用摄像头作为图像/音频输入,3.5mm耳机插口提供音频输出
RPi将音频与视频传至Windows
如果使用3.5mm音频插口,先将音频输出从默认的HDMI配置为audio jack
输出测试用:
$speaker-test -t sine -f 440 -c 2 -s 1
apt安装alsa相关包(略)
列出相关设备:
$arecord -l
**** List of CAPTURE Hardware Devices ****
card 1: U0x46d0x81b [USB Device 0x46d:0x81b], device 0: USB Audio [USB Audio]
Subdevices: 1/1
Subdevice #0: subdevice #0
录音进行测试:
$arecord -D plughw:1,0 test.wav -v -d 10
列出音频设备
$aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: ALSA [bcm2835 ALSA], device 0: bcm2835 ALSA [bcm2835 ALSA]
Subdevices: 8/8
Subdevice #0: subdevice #0
Subdevice #1: subdevice #1
Subdevice #2: subdevice #2
Subdevice #3: subdevice #3
Subdevice #4: subdevice #4
Subdevice #5: subdevice #5
Subdevice #6: subdevice #6
Subdevice #7: subdevice #7
card 0: ALSA [bcm2835 ALSA], device 1: bcm2835 ALSA [bcm2835 IEC958/HDMI]
Subdevices: 1/1
Subdevice #0: subdevice #0
传一个音频文件到RPi上测试输出:
$aplay test.wav
如果没有声音的话检查
1. 是不是连接了HDMI,修改强制声音从audio jack输出
2. alsamixer设置增益
$cat /proc/asound/cards
0 [ALSA ]: bcm2835 - bcm2835 ALSA
bcm2835 ALSA
1 [U0x46d0x81b ]: USB-Audio - USB Device 0x46d:0x81b
USB Device 0x46d:0x81b at usb-3f980000.usb-1.3, high speed$cat /proc/asound/pcm
00-00: bcm2835 ALSA : bcm2835 ALSA : playback 8
00-01: bcm2835 ALSA : bcm2835 IEC958/HDMI : playback 1
01-00: USB Audio : USB Audio : capture 1
安装gstreamer-1.0:
$sudo apt-get install gstreamer1.0
测试输出:
$gst-launch-1.0 audiotestsrc ! alsasink
或者”!”后的参数加上制定设备:
$gst-launch-1.0 audiotestsrc ! alsasink device=”hw:0”
通过插拔USB设备找出USB麦克风:
$pactl list short sources | cut -f2
alsa_input.usb-046d_081b_E63637A0-02-U0x46d0x81b.analog-mono
测试插入的设备(麦克风输入,耳机输出,播放似乎有问题):
$gst-launch-1.0 pulsesrc device=”alsa_input.usb-046d_081b_E63637A0-02-U0x46d0x81b.analog-mono” ! alsasink
测试保存为MP3,用omxplayer播放(-e参数意义不明):
$gst-launch-1.0 -e pulsesrc device=”alsa_input.usb-046d_081b_E63637A0-02-U0x46d0x81b.analog-mono” ! audioconvert ! lamemp3enc target=1 bitrate=128 cbr=true ! filesink location=audio.mp3
可以运行但是没有声音:
$gst-launch-1.0 alsasrc device=”plughw:1” ! alsasink
有声音保存下来:
$gst-launch-1.0 alsasrc device=”plughw:1” ! audioconvert ! lamemp3enc target=1 bitrate=128 cbr=true ! filesink location=audio.mp3
Raspbian发送声音脚本:
#!/bin/sh
gst-launch-1.0 alsasrc device="plughw:1" ! audio/x-raw ! mulawenc ! rtppcmupay ! udpsink host=192.168.1.158 port=5001
window端接受声音脚本:
@echo off
D:\gstreamer\1.0\x86_64\bin\gst-launch-1.0 -v udpsrc port=5001 caps="application/x-rtp" ! queue ! rtppcmudepay ! mulawdec ! audioconvert ! autoaudiosink sync=false
Raspbian脚本,1280*720@30fps,音频默认采样率,视频+声音,CPU占用率10%左右,网络上行占用带宽约1.2MB/s,下行300B/s:
#!/bin/sh
echo "start video stream"
gst-launch-1.0 -v -e v4l2src device=/dev/video0 ! 'image/jpeg,width=1280,height=720,framerate=30/1' ! multipartmux ! udpsink host=192.168.1.158 port=5000 > /dev/null &
echo "start audio streaming"
gst-launch-1.0 alsasrc device="plughw:1" ! audio/x-raw ! mulawenc ! rtppcmupay ! udpsink host=192.168.1.158 port=5001 > /dev/null
设置为1280*960会使得UDP包过大,程序卡住
Windows脚本(gstreamer装在了D盘)
d:
cd gstreamer\1.0\x86_64\bin\
start gst-launch-1.0.exe -v udpsrc port=5000 ! jpegdec ! autovideosink
start gst-launch-1.0.exe -v udpsrc port=5001 caps="application/x-rtp" ! queue ! rtppcmudepay ! mulawdec ! audioconvert ! autoaudiosink sync=false
降低音频采样率对于降低带宽的效果似乎比较明显。采样率设置到8000,CPU占用不到5%,上行网络在900KB/s内
帧率下调至20后上行占用在700KB/s内,声音几乎无延迟
gst-launch-1.0 -v -e v4l2src device=/dev/video0 ! 'image/jpeg,width=1280,height=720,framerate=20/1' ! multipartmux ! udpsink host=192.168.1.158 port=5000 > /dev/null &
gst-launch-1.0 alsasrc device="plughw:1" ! audioresample ! audio/x-raw, rate=8000 ! mulawenc ! rtppcmupay ! udpsink host=192.168.1.158 port=5001 > /dev/null
H.264压缩的脚本:
windows端server:
@echo off
D:\gstreamer\1.0\x86_64\bin\gst-launch-1.0 -v udpsrc port=5001 caps="application/x-rtp,
media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264" !
rtph264depay ! avdec_h264 ! videoconvert ! autovideosink sync=false
RPi端client:
gst-launch-1.0 -v -e v4l2src device=/dev/video0 ! 'video/x-
raw,format=I420,width=640,height=480,framerate=30/1' ! omxh264enc target -
bitrate=6500000 control-rate=variable ! rtph264pay pt=96 ! udpsink host=
192.168.10.2 port=5001