summaryrefslogtreecommitdiffstats
path: root/plugins/alsa/alsa-softmixer.c
blob: bd9ee5e9cac05b5685b2912656d4a97959ff478a (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * Copyright (C) 2017 "IoT.bzh"
 * Author Fulup Ar Foll <fulup@iot.bzh>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#define _GNU_SOURCE  // needed for vasprintf

#include "alsa-softmixer.h"

// Force Lua2cWrapper inclusion within already existing plugin

CTLP_LUA_REGISTER("alsa-mixer")

SoftMixerHandleT *Softmixer;

// Call at initialisation time
CTLP_ONLOAD(plugin, callbacks) {
    AFB_ApiDebug(plugin->api, "SoftMixer plugin: uid='%s' 'info='%s'", plugin->uid, plugin->info);
    Softmixer = calloc(1, sizeof(SoftMixerHandleT));
    return NULL;
}

CTLP_LUA2C(AlsaDmix, source, argsJ, responseJ) {
    json_object* subscribeArgsJ = NULL;

    int error = 0;
    wrap_json_pack(&subscribeArgsJ, "{ss}", "value", "location");
    AFB_ApiNotice(source->api, "lua2c router with %s", json_object_to_json_string_ext(subscribeArgsJ, JSON_C_TO_STRING_PRETTY));

    return error;
}



CTLP_LUA2C(AlsaRouter, source, argsJ, responseJ) {
    json_object *sndInJ, *sndOutJ, *paramsJ = NULL;
    AlsaPcmInfoT *sndIn, *sndOut;
    int error;

    // make sndIn/Out a pointer to get cleaner code
    sndIn = calloc(1, sizeof (AlsaPcmInfoT));
    sndOut = calloc(1, sizeof (AlsaPcmInfoT));

    // set pcm options to defaults
    AlsaPcmHwInfoT *pcmOpts;
    pcmOpts = calloc(1, sizeof (AlsaPcmHwInfoT));
    pcmOpts->format = SND_PCM_FORMAT_UNKNOWN;
    pcmOpts->access = SND_PCM_ACCESS_RW_INTERLEAVED;

    error = wrap_json_unpack(argsJ, "{s:o,s:o,s?o}", "devin", &sndInJ, "devout", &sndOutJ, "params", &paramsJ);
    if (error) {
        AFB_ApiNotice(source->api, "--lua2c-- AlsaRouter ARGS missing devIn|devOut args=%s", json_object_get_string(argsJ));
        goto OnErrorExit;
    }

    error = wrap_json_unpack(sndInJ, "{s?s,s?s,s?i,s?i,s?i}", "path", &sndIn->devpath, "id", &sndIn->cardid, "numid", &sndIn->numid, "dev", &sndIn->device, "sub", &sndIn->subdev);
    if (error || (!sndIn->devpath && !sndIn->cardid)) {
        AFB_ApiNotice(source->api, "--lua2c-- AlsaRouter DEV-IN missing 'path|id|dev|sub|numid' devin=%s", json_object_get_string(sndInJ));
        goto OnErrorExit;
    }

    error = wrap_json_unpack(sndOutJ, "{s?s,s?s,s?i,s?i, s?i}", "path", &sndOut->devpath, "id", &sndOut->cardid, "numid", &sndOut->numid, "dev", &sndOut->device, "sub", &sndOut->subdev);
    if (error || (!sndOut->devpath && !sndOut->cardid)) {
        AFB_ApiNotice(source->api, "--lua2c-- AlsaRouter DEV-OUT missing 'path|id|dev|sub' devout=%s", json_object_get_string(sndOutJ));
        goto OnErrorExit;
    }

    if (paramsJ) if ((error = wrap_json_unpack(paramsJ, "{s?i, s?i, s?i, s?i}", "format", &pcmOpts->format, "access", &pcmOpts->access, "rate", &pcmOpts->rate, "channels", &pcmOpts->channels)) != 0) {
            AFB_ApiNotice(source->api, "--lua2c-- AlsaRouter PARAMS missing 'format|access|rate|channels' params=%s", json_object_get_string(paramsJ));
            goto OnErrorExit;
        }

    AFB_ApiNotice(source->api, "--lua2c-- AlsaRouter devin=%s devout=%s rate=%d channel=%d", sndIn->devpath, sndOut->devpath, pcmOpts->rate, pcmOpts->channels);

    // Check sndOut Exist and build a valid cardid config
    error = AlsaByPathDevid(source, sndOut);
    if (error) goto OnErrorExit;

    // open capture PCM
    AlsaPcmInfoT *pcmIn = AlsaByPathOpenPcm(source, sndIn, SND_PCM_STREAM_CAPTURE);
    if (!pcmIn) goto OnErrorExit;

    AlsaPcmInfoT *pcmDmix = AlsaCreateDmix(source, "DmixPlugPcm", sndOut);
    if (!pcmDmix) goto OnErrorExit;

    //AlsaPcmInfoT *pcmVol = AlsaCreateVol(source, "SoftVol", sndIn, pcmDmix);
    //if (!pcmVol) goto OnErrorExit;

    //error = AlsaPcmCopy(source, pcmIn, pcmVol, pcmOpts);
    //if (error) goto OnErrorExit;

    // Registration to event should be done after pcm_start
    if (sndIn->numid) {
        error = AlsaCtlRegister(source, pcmIn, sndIn->numid);
        if (error) goto OnErrorExit;
    }

    return 0;

OnErrorExit:
    AFB_ApiNotice(source->api, "--lua2c-- ERROR AlsaRouter sndIn=%s sndOut=%s rate=%d channel=%d", sndIn->devpath, sndOut->devpath, pcmOpts->rate, pcmOpts->channels);
    return -1;
}