summaryrefslogtreecommitdiffstats
path: root/src/module-protocol-stub.c
blob: 7338abc94d6f590b5f5ad50ced3f2a7f993e7a54 (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
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <assert.h>
#include <arpa/inet.h>
#include <unistd.h>

#include "module.h"
#include "socket-server.h"
#include "socket-util.h"
#include "util.h"
#include "modargs.h"

#ifdef USE_PROTOCOL_SIMPLE
  #include "protocol-simple.h"
  #define protocol_new pa_protocol_simple_new
  #define protocol_free pa_protocol_simple_free
  #define IPV4_PORT 4711
  #define UNIX_SOCKET "/tmp/polypaudio/simple"
  #define MODULE_ARGUMENTS "rate", "format", "channels", "sink", "source", "playback", "record",
#else
  #ifdef USE_PROTOCOL_CLI
    #include "protocol-cli.h" 
    #define protocol_new pa_protocol_cli_new
    #define protocol_free pa_protocol_cli_free
    #define IPV4_PORT 4712
    #define UNIX_SOCKET "/tmp/polypaudio/cli"
    #define MODULE_ARGUMENTS 
  #else
    #ifdef USE_PROTOCOL_NATIVE
      #include "protocol-native.h"
      #define protocol_new pa_protocol_native_new
      #define protocol_free pa_protocol_native_free
      #define IPV4_PORT 4713
      #define UNIX_SOCKET "/tmp/polypaudio/native"
      #define MODULE_ARGUMENTS "public", "cookie",
    #else
      #ifdef USE_PROTOCOL_ESOUND
        #include "protocol-esound.h"
        #include "esound-spec.h"
        #define protocol_new pa_protocol_esound_new
        #define protocol_free pa_protocol_esound_free
        #define IPV4_PORT ESD_DEFAULT_PORT
        #define UNIX_SOCKET ESD_UNIX_SOCKET_NAME
        #define MODULE_ARGUMENTS "sink", "source", "public", "cookie",
      #else
        #error "Broken build system" 
      #endif
    #endif 
  #endif
#endif

static const char* const valid_modargs[] = {
    MODULE_ARGUMENTS
#ifdef USE_TCP_SOCKETS
    "port",
    "loopback",
#else
    "socket",
#endif
    NULL
};

struct pa_socket_server *create_socket_server(struct pa_core *c, struct pa_modargs *ma) {
    struct pa_socket_server *s;
#ifdef USE_TCP_SOCKETS
    uint32_t loopback = 0, port = IPV4_PORT;

    if (pa_modargs_get_value_u32(ma, "loopback", &loopback) < 0) {
        fprintf(stderr, "loopback= expects a numerical argument.\n");
        return NULL;
    }

    if (pa_modargs_get_value_u32(ma, "port", &port) < 0) {
        fprintf(stderr, "port= expects a numerical argument.\n");
        return NULL;
    }
    
    if (!(s = pa_socket_server_new_ipv4(c->mainloop, loopback ? INADDR_LOOPBACK : INADDR_ANY, port)))
        return NULL;
#else
    int r;
    const char *p;

    p = pa_modargs_get_value(ma, "socket", UNIX_SOCKET);
    assert(p);

    if (pa_unix_socket_make_secure_dir(p) < 0) {
        fprintf(stderr, "Failed to create secure socket directory.\n");
        return NULL;
    }

    if ((r = pa_unix_socket_remove_stale(p)) < 0) {
        fprintf(stderr, "Failed to remove stale UNIX socket '%s': %s\n", p, strerror(errno));
        return NULL;
    }
    
    if (r)
        fprintf(stderr, "Removed stale UNIX socket '%s'.", p);
    
    if (!(s = pa_socket_server_new_unix(c->mainloop, p)))
        return NULL;
    
#endif
    return s;
}

int pa_module_init(struct pa_core *c, struct pa_module*m) {
    struct pa_socket_server *s;
    struct pa_modargs *ma = NULL;
    int ret = -1;
    assert(c && m);

    if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
        fprintf(stderr, "Failed to parse module arguments\n");
        goto finish;
    }

    if (!(s = create_socket_server(c, ma)))
        goto finish;

    if (!(m->userdata = protocol_new(c, s, m, ma))) {
        pa_socket_server_free(s);
        goto finish;
    }

    ret = 0;

finish:
    if (ma)
        pa_modargs_free(ma);

    return ret;
}

void pa_module_done(struct pa_core *c, struct pa_module*m) {
    assert(c && m);

    protocol_free(m->userdata);
}