summaryrefslogtreecommitdiffstats
path: root/nixos/tests/ustreamer.nix
blob: 8a1820d19d06d45dc41ec2e89e5eb4af9eef22a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
{ pkgs, ... }:
let
  test-output-filename = "snapshot.jpeg";
in
{
  name = "ustreamer-vmtest";
  nodes = {
    client =
      { ... }:
      {
        environment.systemPackages = [ pkgs.curl ];
      };
    camera =
      { config, ... }:
      let
        configFile = pkgs.writeText "akvcam-configFile" ''
          [Cameras]
          cameras/size = 2

          cameras/1/type = output
          cameras/1/mode = mmap, userptr, rw
          cameras/1/description = Virtual Camera (output device)
          cameras/1/formats = 2
          cameras/1/videonr = 7

          cameras/2/type = capture
          cameras/2/mode = mmap, rw
          cameras/2/description = Virtual Camera
          cameras/2/formats = 1, 2
          cameras/2/videonr = 9

          [Connections]
          connections/size = 1
          connections/1/connection = 1:2

          [Formats]
          formats/size = 2

          formats/1/format = YUY2
          formats/1/width = 640
          formats/1/height = 480
          formats/1/fps = 30

          formats/2/format = RGB24, YUY2
          formats/2/width = 640
          formats/2/height = 480
          formats/2/fps = 20/1, 15/2
        '';

        test-stream-name = "test_stream.jpeg";

        test-ustreamer-python =
          pkgs.writers.writePython3Bin "test-ustreamer-python"
            {
              libraries = [ pkgs.python3Packages.ustreamer ];
            }
            ''
              import ustreamer

              with ustreamer.Memsink("${test-stream-name}") as sink:
                  frame = sink.wait_frame()
              with open("${test-output-filename}", "wb") as file:
                  file.write(frame["data"])
            '';
      in
      {
        services.ustreamer = {
          enable = true;
          device = "/dev/video9";
          extraArgs = [
            "--device-timeout=8"
            "--sink=${test-stream-name}"
          ];
        };
        environment.systemPackages = [
          test-ustreamer-python
        ];
        networking.firewall.allowedTCPPorts = [ 8080 ];

        boot.extraModulePackages = [ config.boot.kernelPackages.akvcam ];
        boot.kernelModules = [ "akvcam" ];
        boot.extraModprobeConfig = ''
          options akvcam config_file=${configFile}
        '';
      };
  };

  testScript = ''
    start_all()

    camera.wait_for_unit("ustreamer.service")
    camera.wait_for_open_port(8080)

    client.wait_for_unit("multi-user.target")
    client.succeed("curl http://camera:8080")

    camera.succeed("test-ustreamer-python")
    camera.wait_for_file('${test-output-filename}')
  '';
}