WebCam snap应用实例

相信大家好多人都已经使用过webcam.对webcam的使用并不陌生.在今天的实例中,我们来介绍webcam的snap设计.如果大家先前看过我的ubuntu core的IoT设计,可能对这个项目并不陌生.大家可以参阅我的文章"snapcraft动手实践 --- Web Camera".只可惜,在那篇文章中的webcam在当时的情况下,没法在kvm中的环境中在桌面上测试,除非我们安装一个snappy的电脑系统.今天的snap实战中,我们的主要设计和以前的几乎是一样的,只是有一些细微的变化(这是由于我们的snap设计在不断演进中造成的).

webcam设计的源码我们可以在如下的地址找到:

https://github.com/snapcore/snapcraft/tree/master/demos/webcam-webui

我们可以它通过如下的方法把源代码下下来:

$ git clone https://github.com/ubuntu-core/snapcraft
$ cd snapcraft/demos

关于webcam应用的介绍,大家可以在如下的地址找到:

https://developer.ubuntu.com/en/snappy/build-apps/your-first-snap/

我们首先来看一下这个应用的snapcraft.yaml:

snapcraft.yaml

name: webcam-webui
version: 1
summary: Webcam web UI
description: Exposes your webcam over a web UI
confinement: strict

apps:
  webcam-webui:
    command: bin/webcam-webui
    daemon: simple
    plugs: [network-bind]

parts:
  cam:
    plugin: go
    go-packages:
      - github.com/mikix/golang-static-http
    stage-packages:
      - fswebcam
    filesets:
      fswebcam:
        - usr/bin/fswebcam
        - lib
        - usr/lib
      go-server:
        - bin/golang-*
    stage:
      - $fswebcam
      - $go-server
    snap:
      - $fswebcam
      - $go-server
      - -usr/share/doc
  glue:
    plugin: copy
    files:
      webcam-webui: bin/webcam-webui

fswebcam的用户手册可以在 链接找到.

注意:这是到目前为止的snapcraft.yaml文件,其中并没有camera plug.我们已经报告一个 bug了.如果大家直接打包这个应用:

$ snapcraft

并安装这个应用:

$ sudo snap install webcam-webui_1_amd64.snap --force-dangerous

由于这个应用是一个daemon类型的应用.我们视为一个service,也即我们并不需要手动来启动该应用.在安装的时候,它就自动被运行.我可以在我们的浏览器中打开如下的地址:



很显然,我们的webserver已经在正常运行,但是我们看不见任何的camera输出.这是为什么呢?
我们使用如下的命令来查看我们的service运行的日志:

$ journalctl -u snap.webcam-webui.webcam-webui

我们得到了如下的结果:


我们可以看出来,其中的一些信息(记住要翻到最后的信息).显然它显示了:

7月 19 10:33:13 liuxg ubuntu-core-launcher[13442]: Trying source module v4l2...
7月 19 10:33:13 liuxg ubuntu-core-launcher[13442]: Error opening device: /dev/video0
7月 19 10:33:13 liuxg ubuntu-core-launcher[13442]: open: Permission denied

这说明什么问题呢?它表明我们遇到了安全的问题.我们的应用不能打开设备/dev/video0.那么我们怎么来更正我们的问题呢?我们来在terminal中打入如下的命令:

liuxg@liuxg:~$ snap interfaces
Slot                 Plug
:camera              -
:cups-control        -
:firewall-control    -
:gsettings           -
:home                -
:locale-control      -
:log-observe         -
:modem-manager       -
:mount-observe       -
:network             -
:network-bind        webcam-webui
:network-control     -
:network-manager     -
:network-observe     -
:opengl              -
:optical-drive       -
:ppp                 -
:pulseaudio          -
:snapd-control       -
:system-observe      -
:timeserver-control  -
:timezone-control    -
:unity7              -
:x11                 -

请注意我们最前面的一个Slot: camera.我们在想我们的这个应用不就是一个camera应用吗?如果我们希望访问camera我们必须设置camera plug,这样我们才可以真正地访问我们的这个设备.有了这样的想法,我们在我们的snapcraft.yaml中加入camera.更后的snapcraft.yaml的文件如下:

snapcraft.yaml


name: webcam-webui
version: 1
summary: Webcam web UI
description: Exposes your webcam over a web UI
confinement: strict

apps:
  webcam-webui:
    command: bin/webcam-webui
    daemon: simple
    plugs: [camera,network-bind]

parts:
  cam:
    plugin: go
    go-packages:
      - github.com/mikix/golang-static-http
    stage-packages:
      - fswebcam
    filesets:
      fswebcam:
        - usr/bin/fswebcam
        - lib
        - usr/lib
      go-server:
        - bin/golang-*
    stage:
      - $fswebcam
      - $go-server
    snap:
      - $fswebcam
      - $go-server
      - -usr/share/doc
  glue:
    plugin: copy
    files:
      webcam-webui: bin/webcam-webui

请注意,我们在plug里加入了camera.重新打包并安装我们的应用.我们再次运行:

liuxg@liuxg:~$ snap interfaces
Slot                 Plug
:camera              -
:cups-control        -
:firewall-control    -
:gsettings           -
:home                -
:locale-control      -
:log-observe         -
:modem-manager       -
:mount-observe       -
:network             -
:network-bind        webcam-webui
:network-control     -
:network-manager     -
:network-observe     -
:opengl              -
:optical-drive       -
:ppp                 -
:pulseaudio          -
:snapd-control       -
:system-observe      -
:timeserver-control  -
:timezone-control    -
:unity7              -
:x11                 -
liuxg@liuxg:~$ snap interfaces
Slot                 Plug
:camera              -
:cups-control        -
:firewall-control    -
:gsettings           -
:home                -
:locale-control      -
:log-observe         -
:modem-manager       -
:mount-observe       -
:network             -
:network-bind        webcam-webui
:network-control     -
:network-manager     -
:network-observe     -
:opengl              -
:optical-drive       -
:ppp                 -
:pulseaudio          -
:snapd-control       -
:system-observe      -
:timeserver-control  -
:timezone-control    -
:unity7              -
:x11                 -
-                    webcam-webui:camera

显然,这次,我们看见了:

-                    webcam-webui:camera

这里虽然显示了camera的plug,但是它并没有真正地连接起来.我们查看文档 Interfaces.在这篇文章中:

camera

Can access the first video camera. Suitable for programs wanting to use the webcams.

Usage: common Auto-Connect: no

它显示了:Auto-Connect: no.我们需要手动连接.

$ sudo snap connect webcam-webui:camera ubuntu-core:camera

通过上面的命令,我们可以使得一个plug和一个slot进行手动连接.重新运行如下的命令:

liuxg@liuxg:~$ snap interfaces
Slot                 Plug
:camera              webcam-webui
:cups-control        -
:firewall-control    -
:gsettings           -
:home                -
:locale-control      -
:log-observe         -
:modem-manager       -
:mount-observe       -
:network             -
:network-bind        webcam-webui
:network-control     -
:network-manager     -
:network-observe     -
:opengl              -
:optical-drive       -
:ppp                 -
:pulseaudio          -
:snapd-control       -
:system-observe      -
:timeserver-control  -
:timezone-control    -
:unity7              -
:x11                 -

这次,我们看见了我们的应用webcam-webui已经和network-bin及camera建立起来连接了.我们再次打开我们的浏览器:






我们整个项目的源码在: https://github.com/liu-xiao-guo/webcam-snap

我们可以通过如下的方式来查询我们的service的运行情况:

$ systemctl status -l snap.webcam-webui.webcam-webui

liuxg@liuxg:~$ systemctl status snap.webcam-webui.webcam-webui
● snap.webcam-webui.webcam-webui.service - Service for snap application webcam-webui.webcam-webui
   Loaded: loaded (/etc/systemd/system/snap.webcam-webui.webcam-webui.service; enabled; vendor prese
   Active: active (running) since 二 2016-07-19 12:38:02 CST; 36min ago
 Main PID: 16325 (webcam-webui)
   CGroup: /system.slice/snap.webcam-webui.webcam-webui.service
           ├─16325 /bin/sh /snap/webcam-webui/x1/bin/webcam-webui
           ├─16327 golang-static-http
           └─17488 sleep 10

7月 19 13:14:31 liuxg ubuntu-core-launcher[16325]: Adjusting resolution from 384x288 to 960x540.
7月 19 13:14:31 liuxg ubuntu-core-launcher[16325]: --- Capturing frame...
7月 19 13:14:31 liuxg ubuntu-core-launcher[16325]: Captured frame in 0.00 seconds.
7月 19 13:14:32 liuxg ubuntu-core-launcher[16325]: --- Processing captured image...
7月 19 13:14:32 liuxg ubuntu-core-launcher[16325]: Fontconfig error: Cannot load default config file
7月 19 13:14:32 liuxg ubuntu-core-launcher[16325]: Fontconfig error: Cannot load default config file
7月 19 13:14:32 liuxg ubuntu-core-launcher[16325]: Fontconfig error: Cannot load default config file
7月 19 13:14:32 liuxg ubuntu-core-launcher[16325]: Unable to load font 'sans': fontconfig: Couldn't f
7月 19 13:14:32 liuxg ubuntu-core-launcher[16325]: Disabling the the banner.
7月 19 13:14:32 liuxg ubuntu-core-launcher[16325]: Writing JPEG image to 'shot.jpeg'.
 
  
我们可以通过如下的方式来停止一个service:
$ sudo systemctl stop snap.webcam-webui.webcam-webui

我们可以通过如下的方式来启动一个service:

$ sudo systemctl start snap.webcam-webui.webcam-webui





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值