I’m sure many of you have already used Webcam. You are no stranger to the use of Webcam. In today’s example, we introduce snap design for Webcam. If you’ve seen my IoT design for Ubuntu Core before, you’re probably familiar with this project. Please refer to my article “Snapcraft Hands-on – Web Camera”. Unfortunately, the WebCam in that article, in its current state, could not be tested on the desktop in a KVM environment unless we installed a Snappy computer system. In snap today, our main design is pretty much the same as before, with minor changes (due to the evolution of our SNAP design).

The source code of webcam design can be found at the following address:

Github.com/snapcore/sn…

We can get the source code down by using the following method:

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

Introduction to Webcam applications can be found at the following address:

Developer.ubuntu.com/en/snappy/b… \

Let’s first take a look at 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
Copy the code

\

The FSWebCAM user manual can be found at the link.

\

Note: This is the snapcraft. Yaml file so far without the camera plug. We have already reported a bug. If you just package the app:

\

$ snapcraft
Copy the code

And install the app:

\

$ sudo snap install webcam-webui_1_amd64.snap --force-dangerous
Copy the code

\

Since this application is a daemon application. We treat it as a service, meaning we don’t need to start the application manually. It runs automatically when installed. I can open the following address in our browser:

\

\

\

Obviously, our WebServer is already working, but we don’t see any camera output. Why is that?

We use the following command to view the log of our service run:

\

$ journalctl -u snap.webcam-webui.webcam-webui
Copy the code

\

We get the following results:

\

\

We can see that some of this information (remember to turn to the last information). Apparently it shows:

\

7月 19 10:33:13 liuxg Ubuntu-Core-Launcher [13442]: Trying Source Module V4L2... Liuxg Ubuntu-Core-Launcher [13442]: Error Opening Device: /dev/video0 7月 19 10:33:13 liuxg Ubuntu-core-Launcher [13442]: open: Permission deniedCopy the code

What does that mean? It shows that we have a security problem. Our application could not open the device /dev/video0. So how can we correct our problems? Let’s enter the following command in 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                 -
Copy the code

Note our first Slot: camera. We were thinking, isn’t our app just a camera app? If we want to access the camera we must set the Camera Plug so that we can actually access our device. With this in mind, we added camera to our snapcraft. Yaml. The later snapcraft. Yaml file is as follows:

\

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
Copy the code

\

Notice that we have added a camera to plug. Repackage and install our application. Let’s run again:

\

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
Copy the code

\

Apparently, this time, we saw:

\

-                    webcam-webui:camera
Copy the code

The camera plug is shown here, but it’s not really connected. We look at the document Interfaces. In the article:

\

camera

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

Usage: common Auto-Connect: no
Copy the code

It shows: auto-connect: no. We need to connect manually.

\

$ sudo snap connect webcam-webui:camera ubuntu-core:camera
Copy the code

\

With the command above, we can manually connect a plug to a slot. Re-run the following command:

\

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                 -
Copy the code

This time, we see that our application webcam-webui has been connected to network-bin and camera. Let’s open our browser again:

\


\

\

\

\

The source of our entire project is at: github.com/liu-xiao-gu…

\

We can query the running status of our service in the following way:

\

$ systemctl status -l snap.webcam-webui.webcam-webui
Copy the code

\

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 2 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 July 19 13:14:31 liuxg ubuntu - core - the launcher [16325] : Adjusting Resolution from 384x288 to 960x540. 7月 19 13:14:31 liuxg Ubuntu Core-launcher [16325]:... Liuxg Ubuntu-core-launcher [16325]: Captured frame in 0.00 seconds. 7月 19 13:14:32 LIUxG Ubuntu-Core-Launcher [16325]: Processing Captured image... 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 banner. 7月 19 13:14:32 liuxg Ubuntu Core-Launcher [16325]: Writing JPEG Image to 'shot.jpeg'Copy the code
Copy the code

We can stop a service by:

$ sudo systemctl stop snap.webcam-webui.webcam-webui
Copy the code

\

We can start a service as follows:

\

$ sudo systemctl start snap.webcam-webui.webcam-webui
Copy the code




\


\