about summary refs log tree commit diff
path: root/nixos/modules/services/video/v4l2-relayd.nix
blob: 2a9dbe00158f746081514b47af886b4879ef5609 (plain) (blame)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
{ config, lib, pkgs, utils, ... }:
let

  inherit (lib) attrValues concatStringsSep filterAttrs length listToAttrs literalExpression
    makeSearchPathOutput mkEnableOption mkIf mkOption nameValuePair optionals types;
  inherit (utils) escapeSystemdPath;

  cfg = config.services.v4l2-relayd;

  kernelPackages = config.boot.kernelPackages;

  gst = (with pkgs.gst_all_1; [
    gst-plugins-bad
    gst-plugins-base
    gst-plugins-good
    gstreamer.out
  ]);

  instanceOpts = { name, ... }: {
    options = {
      enable = mkEnableOption (lib.mdDoc "this v4l2-relayd instance");

      name = mkOption {
        type = types.str;
        default = name;
        description = lib.mdDoc ''
          The name of the instance.
        '';
      };

      cardLabel = mkOption {
        type = types.str;
        description = lib.mdDoc ''
          The name the camera will show up as.
        '';
      };

      extraPackages = mkOption {
        type = with types; listOf package;
        default = [ ];
        description = lib.mdDoc ''
          Extra packages to add to {env}`GST_PLUGIN_PATH` for the instance.
        '';
      };

      input = {
        pipeline = mkOption {
          type = types.str;
          description = lib.mdDoc ''
            The gstreamer-pipeline to use for the input-stream.
          '';
        };

        format = mkOption {
          type = types.str;
          default = "YUY2";
          description = lib.mdDoc ''
            The video-format to read from input-stream.
          '';
        };

        width = mkOption {
          type = types.ints.positive;
          default = 1280;
          description = lib.mdDoc ''
            The width to read from input-stream.
          '';
        };

        height = mkOption {
          type = types.ints.positive;
          default = 720;
          description = lib.mdDoc ''
            The height to read from input-stream.
          '';
        };

        framerate = mkOption {
          type = types.ints.positive;
          default = 30;
          description = lib.mdDoc ''
            The framerate to read from input-stream.
          '';
        };
      };

      output = {
        format = mkOption {
          type = types.str;
          default = "YUY2";
          description = lib.mdDoc ''
            The video-format to write to output-stream.
          '';
        };
      };

    };
  };

in
{

  options.services.v4l2-relayd = {

    instances = mkOption {
      type = with types; attrsOf (submodule instanceOpts);
      default = { };
      example = literalExpression ''
        {
          example = {
            cardLabel = "Example card";
            input.pipeline = "videotestsrc";
          };
        }
      '';
      description = lib.mdDoc ''
        v4l2-relayd instances to be created.
      '';
    };

  };

  config =
    let

      mkInstanceService = instance: {
        description = "Streaming relay for v4l2loopback using GStreamer";

        after = [ "modprobe@v4l2loopback.service" "systemd-logind.service" ];
        wantedBy = [ "multi-user.target" ];

        serviceConfig = {
          Type = "simple";
          Restart = "always";
          PrivateNetwork = true;
          PrivateTmp = true;
          LimitNPROC = 1;
        };

        environment = {
          GST_PLUGIN_PATH = makeSearchPathOutput "lib" "lib/gstreamer-1.0" (gst ++ instance.extraPackages);
          V4L2_DEVICE_FILE = "/run/v4l2-relayd-${instance.name}/device";
        };

        script =
          let
            appsrcOptions = concatStringsSep "," [
              "caps=video/x-raw"
              "format=${instance.input.format}"
              "width=${toString instance.input.width}"
              "height=${toString instance.input.height}"
              "framerate=${toString instance.input.framerate}/1"
            ];

            outputPipeline = [
              "appsrc name=appsrc ${appsrcOptions}"
              "videoconvert"
            ] ++ optionals (instance.input.format != instance.output.format) [
              "video/x-raw,format=${instance.output.format}"
              "queue"
            ] ++ [ "v4l2sink name=v4l2sink device=$(cat $V4L2_DEVICE_FILE)" ];
          in
          ''
            exec ${pkgs.v4l2-relayd}/bin/v4l2-relayd -i "${instance.input.pipeline}" -o "${concatStringsSep " ! " outputPipeline}"
          '';

        preStart = ''
          mkdir -p $(dirname $V4L2_DEVICE_FILE)
          ${kernelPackages.v4l2loopback.bin}/bin/v4l2loopback-ctl add -x 1 -n "${instance.cardLabel}" > $V4L2_DEVICE_FILE
        '';

        postStop = ''
          ${kernelPackages.v4l2loopback.bin}/bin/v4l2loopback-ctl delete $(cat $V4L2_DEVICE_FILE)
          rm -rf $(dirname $V4L2_DEVICE_FILE)
        '';
      };

      mkInstanceServices = instances: listToAttrs (map
        (instance:
          nameValuePair "v4l2-relayd-${escapeSystemdPath instance.name}" (mkInstanceService instance)
        )
        instances);

      enabledInstances = attrValues (filterAttrs (n: v: v.enable) cfg.instances);

    in
    {

      boot = mkIf ((length enabledInstances) > 0) {
        extraModulePackages = [ kernelPackages.v4l2loopback ];
        kernelModules = [ "v4l2loopback" ];
      };

      systemd.services = mkInstanceServices enabledInstances;

    };

  meta.maintainers = with lib.maintainers; [ betaboon ];
}