通过网络将屏幕流式传输到多个设备


我需要在办公室的多个屏幕上显示带有监控功能的仪表板。 有几种旧的Raspberry Pi Model B +和具有几乎无限数量资源的管理程序。


显然,Raspberry Pi Model B +没有足够的随机性来保持浏览器持续运行并在其中绘制大量图形,因此,页面发生了部分错误,并经常崩溃。


有一个非常简单而优雅的解决方案,我想与您分享。


如您所知,所有Raspberry都具有相当强大的视频处理器,这对于硬件视频解码非常有用。 因此,提出了启动带有其他位置的仪表板的浏览器的想法,并将完成的流和呈现的图像传输到树莓派。


除此之外,还应该简化管理,因为在这种情况下,所有配置都将在一台虚拟机上执行,因此更易于更新和备份。


言归正传。


服务器端


我们将为Ubuntu使用现成的Cloud Image 。 它不需要安装,它包含快速部署虚拟机所需的一切, Cloud-Init支持可帮助您立即配置网络,添加ssh密钥并使其快速投入使用。


我们部署一个新的虚拟机 ,首先在其上安装Xorgnodmfluxbox


apt-get update apt-get install -y xserver-xorg nodm fluxbox sed -i 's/^NODM_USER=.*/NODM_USER=ubuntu/' /etc/default/nodm 

我们还使用Diego Ongaro友好地提供给我们的Xorg配置,仅添加了新的分辨率1920x1080 ,因为我们所有的监视器都将使用它:


 cat > /etc/X11/xorg.conf <<\EOT Section "Device" Identifier "device" Driver "vesa" EndSection Section "Screen" Identifier "screen" Device "device" Monitor "monitor" DefaultDepth 16 SubSection "Display" Modes "1920x1080" "1280x1024" "1024x768" "800x600" EndSubSection EndSection Section "Monitor" Identifier "monitor" HorizSync 20.0 - 50.0 VertRefresh 40.0 - 80.0 Option "DPMS" EndSection Section "ServerLayout" Identifier "layout" Screen "screen" EndSection EOT systemctl restart nodm 

现在安装Firefox,我们将其作为系统服务启动,因此同时我们将为其编写一个单元文件:


 apt-get install -y firefox xdotool cat > /etc/systemd/system/firefox.service <<\EOT [Unit] Description=Firefox After=network.target [Service] Restart=always User=ubuntu Environment="DISPLAY=:0" Environment="XAUTHORITY=/home/ubuntu/.Xauthority" ExecStart=/usr/bin/firefox -url 'http://example.org/mydashboard' ExecStartPost=/usr/bin/xdotool search --sync --onlyvisible --class "Firefox" windowactivate key F11 [Install] WantedBy=graphical.target EOT systemctl enable firefox systemctl start firefox 

我们需要Xdotool在全屏模式下直接运行firefox。
使用-url参数,可以指定任何页面,以便在浏览器启动时自动打开。


在这个阶段,我们的信息亭已经准备就绪,但是现在我们需要通过网络将图片导出到其他显示器和设备。 为此,我们将使用Motion JPEG的功能,该格式最常用于从大多数网络摄像机流式传输视频。


为此,我们需要做两件事:带有x11grab模块的FFmpeg ,用于从X和streamEye捕获图像,这会将其分发给我们的客户:


 apt-get install -y make gcc ffmpeg cd /tmp/ wget https://github.com/ccrisan/streameye/archive/master.tar.gz tar xvf master.tar.gz cd streameye-master/ make make install cat > /etc/systemd/system/streameye.service <<\EOT [Unit] Description=streamEye After=network.target [Service] Restart=always User=ubuntu Environment="DISPLAY=:0" Environment="XAUTHORITY=/home/ubuntu/.Xauthority" ExecStart=/bin/sh -c 'ffmpeg -f x11grab -s 1920x1080 -r 1 -i :0 -f mjpeg -q:v 5 - 2>/dev/null | streameye' [Install] WantedBy=graphical.target EOT systemctl enable streameye systemctl start streameye 

由于我们的图片不需要快速更新,因此我指出了刷新率:每秒1帧( -r 1参数)和压缩质量:5( -q:v 5参数)


现在,我们尝试转到http:// your-vm:8080 / ,作为响应,您将看到桌面的不断更新的屏幕截图。 太好了! -那是需要的。


客户部分


正如我所说的,这里仍然更简单,我们将使用Raspberry Pi Model B +。


首先,在其上安装Arch Linux ARM ,为此,我们按照官方网站上的说明进行操作


我们还需要为视频芯片分配更多的内存,为此,我们将在/boot/config.txt编辑


 gpu_mem=128 

启动我们的新系统,并记住初始化pacman密钥环,安装OMXPlayer


 pacman -Sy omxplayer 

值得注意的是,OMXPlayer可以在没有Xs的情况下工作,这就是为什么我们要做的就是为其编写一个单元文件并运行它:


 cat > /etc/systemd/system/omxplayer.service <<\EOT [Unit] Description=OMXPlayer Wants=network-online.target After=network-online.target [Service] Type=simple Restart=always ExecStart=/usr/bin/omxplayer -r --live -b http://your-vm:8080/ --aspect-mode full [Install] WantedBy=multi-user.target EOT systemctl enable omxplayer systemctl start omxplayer 

作为参数-b http://your-vm:8080/我们从服务器传递URL。


就是这样,来自我们服务器的图片应立即显示在连接的屏幕上。 如有任何问题,流将自动重新启动,客户端将重新连接到该流。


另外,您可以将生成的图片设置为办公室中所有计算机的屏幕保护程序。 为此,您将需要MPVXScreenSaver


 mode: one selected: 0 programs: \ "Monitoring Screen" mpv --really-quiet --no-audio --fs \ --loop=inf --no-stop-screensaver \ --wid=$XSCREENSAVER_WINDOW \ http://your-vm:8080/ \n\ maze -root \n\ electricsheep --root 1 \n\ 

现在您的同事会很高兴:)

Source: https://habr.com/ru/post/zh-CN450118/


All Articles