aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/alsa/alsa-api-zones.c
blob: 8f383dfb747b48cc1f877c864508369c197507e9 (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
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
/*
 * Copyright (C) 2018 "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"
#include <string.h>

// Fulup need to be cleanup with new controller version
extern Lua2cWrapperT Lua2cWrap;

STATIC int ProcessOneChannel(CtlSourceT *source, const char* uid, json_object *channelJ, AlsaPcmChannelT *channel) {
    const char*channelUid;

    int error = wrap_json_unpack(channelJ, "{ss,si,s?i !}", "target", &channelUid, "channel", &channel->port);
    if (error) goto OnErrorExit;

    channel->uid = strdup(channelUid);
    return 0;

OnErrorExit:
    AFB_ApiError(source->api, "ProcessOneChannel: zone=%s channel: missing (target||channel) json=%s", uid, json_object_get_string(channelJ));
    return -1;
}

STATIC int ProcessOneZone(CtlSourceT *source, json_object *zoneJ, AlsaSndZoneT *zone) {
    json_object *mappingJ;
    size_t count;
    const char* streamType;
    int error;

    error = wrap_json_unpack(zoneJ, "{ss,s?s,so !}"
            , "uid", &zone->uid
            , "type", &streamType
            , "mapping", &mappingJ
            );
    if (error) {
        AFB_ApiNotice(source->api, "ProcessOneZone missing 'uid|type|mapping' zone=%s", json_object_get_string(zoneJ));
        goto OnErrorExit;
    }

    if (!streamType) zone->type = SND_PCM_STREAM_PLAYBACK;
    else {
        if (!strcasecmp(streamType, "capture")) zone->type = SND_PCM_STREAM_CAPTURE;
        else if (!strcasecmp(streamType, "playback")) zone->type = SND_PCM_STREAM_PLAYBACK;
        else {
            AFB_ApiError(source->api, "ProcessOneZone:%s invalid stream type !(playback||capture) json=%s", zone->uid, json_object_get_string(zoneJ));
            goto OnErrorExit;
        }
    }

    // make sure remain valid even when json object is removed
    zone->uid = strdup(zone->uid);

    switch (json_object_get_type(mappingJ)) {
        case json_type_object:
            count = 1;
            zone->channels = calloc(count + 1, sizeof (AlsaPcmChannelT));
            error = ProcessOneChannel(source, zone->uid, mappingJ, &zone->channels[0]);
            if (error) goto OnErrorExit;
            break;
        case json_type_array:
            count = json_object_array_length(mappingJ);
            zone->channels = calloc(count + 1, sizeof (AlsaPcmChannelT));
            for (int idx = 0; idx < count; idx++) {
                json_object *channelJ = json_object_array_get_idx(mappingJ, idx);
                error = ProcessOneChannel(source, zone->uid, channelJ, &zone->channels[idx]);
                if (error) goto OnErrorExit;
            }
            break;
        default:
            AFB_ApiError(source->api, "ProcessOneZone:%s invalid mapping=%s", zone->uid, json_object_get_string(mappingJ));
            goto OnErrorExit;
    }

    return 0;

OnErrorExit:
    return -1;
}

PUBLIC int SndZones(CtlSourceT *source, json_object *argsJ) {
    SoftMixerHandleT *mixerHandle = (SoftMixerHandleT*) source->context;
    AlsaSndZoneT *zones=NULL;
    int error;
    size_t count;

    assert(mixerHandle);

    if (mixerHandle->routes) {
        AFB_ApiError(source->api, "SndZones: mixer=%s Zones already Registryed %s", mixerHandle->uid, json_object_get_string(argsJ));
        goto OnErrorExit;
    }

    switch (json_object_get_type(argsJ)) {
        case json_type_object:
            count = 1;
            zones = calloc(count + 1, sizeof (AlsaSndZoneT));
            error = ProcessOneZone(source, argsJ, &zones[0]);
            if (error) {
                AFB_ApiError(source->api, "SndZones: mixer=%s invalid zone= %s", mixerHandle->uid, json_object_get_string(argsJ));
                goto OnErrorExit;
            }
            break;

        case json_type_array:
            count = json_object_array_length(argsJ);
            zones = calloc(count + 1, sizeof (AlsaSndZoneT));
            for (int idx = 0; idx < count; idx++) {
                json_object *sndZoneJ = json_object_array_get_idx(argsJ, idx);
                error = ProcessOneZone(source, sndZoneJ, &zones[idx]);
                if (error) {
                    AFB_ApiError(source->api, "SndZones: mixer=%s invalid zone= %s", mixerHandle->uid, json_object_get_string(sndZoneJ));
                    goto OnErrorExit;
                }
            }
            break;
        default:
            AFB_ApiError(source->api, "SndZones: mixer=%s invalid argsJ=  %s", mixerHandle->uid, json_object_get_string(argsJ));
            goto OnErrorExit;
    }

    // Registry routed into global softmixer handle
    mixerHandle->routes= calloc(count + 1, sizeof (AlsaPcmInfoT*));

    // instantiate one route PCM per zone with multi plugin as slave
    for (int idx = 0; zones[idx].uid != NULL; idx++) {
        mixerHandle->routes[idx] = AlsaCreateRoute(source, &zones[idx], 0);
        if (!mixerHandle->routes[idx]) {
            AFB_ApiNotice(source->api, "SndZones: mixer=%s fail to create route zone=%s", mixerHandle->uid, zones[idx].uid);
            goto OnErrorExit;
        }
    }
    
    free (zones);
    return 0;

OnErrorExit:
    if (zones) free(zones);
    return -1;
}