im_sun.c 6.88 KB
Newer Older
Jack Moffitt's avatar
Jack Moffitt committed
1
2
3
/* im_sun.c
 * - Raw PCM input from Solaris audio devices
 *
4
 * $Id: im_sun.c,v 1.12 2003/12/24 15:52:09 karl Exp $
5
 *
Jack Moffitt's avatar
Jack Moffitt committed
6
7
8
9
10
11
12
13
14
15
 * by Ciaran Anscomb <ciarana@rd.bbc.co.uk>, based
 * on im_oss.c which is...
 * Copyright (c) 2001 Michael Smith <msmith@labyrinth.net.au>
 *
 * This program is distributed under the terms of the GNU General
 * Public License, version 2. You may use, modify, and redistribute
 * it under the terms of this license. A copy should be included
 * with this source.
 */

16
17
18
19
#ifdef HAVE_CONFIG_H
 #include <config.h>
#endif

Jack Moffitt's avatar
Jack Moffitt committed
20
21
22
23
24
25
26
27
28
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <ogg/ogg.h>
#include <sys/audioio.h>
#include <sys/ioctl.h>
#include <sys/types.h>
29
#ifdef HAVE_STROPTS_H
Jack Moffitt's avatar
Jack Moffitt committed
30
#include <stropts.h>
31
#endif
Jack Moffitt's avatar
Jack Moffitt committed
32
33
#include <fcntl.h>

34
#include "cfgparse.h"
Jack Moffitt's avatar
Jack Moffitt committed
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "stream.h"
#include "inputmodule.h"
#include "metadata.h"

#include "im_sun.h"

#define MODULE "input-sun/"
#include "logging.h"

#define BUFSIZE 8192

static void close_module(input_module_t *mod)
{
48
49
50
51
52
53
54
55
56
57
58
59
    if(mod)
    {
        if(mod->internal)
        {
            im_sun_state *s = mod->internal;
            if(s->fd >= 0)
                close(s->fd);
            thread_mutex_destroy(&s->metadatalock);
            free(s);
        }
        free(mod);
    }
Jack Moffitt's avatar
Jack Moffitt committed
60
61
62
}
static int event_handler(input_module_t *mod, enum event_type ev, void *param)
{
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
    im_sun_state *s = mod->internal;

    switch(ev)
    {
        case EVENT_SHUTDOWN:
            close_module(mod);
            break;
        case EVENT_NEXTTRACK:
            s->newtrack = 1;
            break;
        case EVENT_METADATAUPDATE:
            thread_mutex_lock(&s->metadatalock);
            if(s->metadata)
            {
                char **md = s->metadata;
                while(*md)
                    free(*md++);
                free(s->metadata);
            }
            s->metadata = (char **)param;
            s->newtrack = 1;
            thread_mutex_unlock(&s->metadatalock);
            break;
        default:
            LOG_WARN1("Unhandled event %d", ev);
            return -1;
    }

    return 0;
Jack Moffitt's avatar
Jack Moffitt committed
92
93
94
95
}

static void metadata_update(void *self, vorbis_comment *vc)
{
96
97
    im_sun_state *s = self;
    char **md;
Jack Moffitt's avatar
Jack Moffitt committed
98

99
    thread_mutex_lock(&s->metadatalock);
Jack Moffitt's avatar
Jack Moffitt committed
100

101
    md = s->metadata;
Jack Moffitt's avatar
Jack Moffitt committed
102

103
104
105
106
107
    if(md)
    {
        while(*md)
            vorbis_comment_add(vc, *md++);
    }
Jack Moffitt's avatar
Jack Moffitt committed
108

109
    thread_mutex_unlock(&s->metadatalock);
Jack Moffitt's avatar
Jack Moffitt committed
110
111
112
113
114
115
116
117
118
119
120
}

/* Core streaming function for this module
 * This is what actually produces the data which gets streamed.
 *
 * returns:  >0  Number of bytes read
 *            0  Non-fatal error.
 *           <0  Fatal error.
 */
static int sun_read(void *self, ref_buffer *rb)
{
121
122
123
    int result;
    im_sun_state *s = self;
    unsigned char *i, j;
Jack Moffitt's avatar
Jack Moffitt committed
124

125
    rb->buf = malloc(BUFSIZE*2*s->device_info.record.channels);
126
127
    if(!rb->buf)
        return -1;
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
    result = read(s->fd, rb->buf, BUFSIZE*2*s->device_info.record.channels);

    rb->len = result;
    rb->aux_data = s->device_info.record.sample_rate*s->device_info.record.channels*2;

    if(s->newtrack)
    {
        rb->critical = 1;
        s->newtrack = 0;
    }

    if(result == -1 && errno == EINTR)
    {
        return 0; /* Non-fatal error */
    }
    else if(result <= 0)
    {
        if(result == 0)
            LOG_INFO0("Reached EOF, no more data available");
        else
            LOG_ERROR1("Error reading from audio device: %s", strerror(errno));
        free(rb->buf);
        return -1;
    }

    return rb->len;
Jack Moffitt's avatar
Jack Moffitt committed
154
155
156
157
}

input_module_t *sun_open_module(module_param_t *params)
{
158
159
160
161
162
163
164
165
166
    input_module_t *mod = calloc(1, sizeof(input_module_t));
    im_sun_state *s;
    module_param_t *current;
    char *device = "/dev/audio"; /* default device */
    int sample_rate = 44100;
    int channels = 2;
    int use_metadata = 1; /* Default to on */

    mod->type = ICES_INPUT_PCM;
167
#ifdef WORDS_BIGENDIAN
168
    mod->subtype = INPUT_PCM_BE_16;
169
170
171
#else
    mod->subtype = INPUT_PCM_LE_16;
#endif
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
    mod->getdata = sun_read;
    mod->handle_event = event_handler;
    mod->metadata_update = metadata_update;

    mod->internal = calloc(1, sizeof(im_sun_state));
    s = mod->internal;

    s->fd = -1; /* Set it to something invalid, for now */

    thread_mutex_create(&s->metadatalock);

    current = params;

    while (current) {
        if (!strcmp(current->name, "rate"))
            sample_rate = s->device_info.record.sample_rate = atoi(current->value);
        else if (!strcmp(current->name, "channels"))
            channels = s->device_info.record.channels = atoi(current->value);
        else if (!strcmp(current->name, "device"))
            device = current->value;
        else if (!strcmp(current->name, "metadata"))
            use_metadata = atoi(current->value);
        else if(!strcmp(current->name, "metadatafilename"))
            ices_config->metadata_filename = current->value;
        else
            LOG_WARN1("Unknown parameter %s for sun module", current->name);
        current = current->next;
    }

    /* First up, lets open the audio device */
    if((s->fd = open(device, O_RDONLY, 0)) < 0) {
        LOG_ERROR2("Failed to open audio device %s: %s", 
                device, strerror(errno));
        goto fail;
    }

    /* Try and set up what we want */
    AUDIO_INITINFO(&s->device_info);
    s->device_info.record.sample_rate = sample_rate;
    s->device_info.record.channels = channels; 
    s->device_info.record.precision = 16;
    s->device_info.record.encoding = AUDIO_ENCODING_LINEAR;
    s->device_info.record.port = AUDIO_LINE_IN;
215
216
    s->device_info.record.pause = 0;

217
218
219
220
221
    if (ioctl(s->fd, AUDIO_SETINFO, &s->device_info) < 0) {
        LOG_ERROR2("Failed to configure audio device %s: %s",
                device, strerror(errno));
        goto fail;
    }
222
223
224
225
226
227
#ifdef __sun
    ioctl (s->fd, I_FLUSH, FLUSHR);
#endif
#ifdef __OpenBSD__
    ioctl (s->fd, AUDIO_FLUSH, NULL);
#endif
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252

    /* Check all went according to plan */
    if (s->device_info.record.sample_rate != sample_rate) {
        LOG_ERROR0("Couldn't set sampling rate");
        goto fail;
    }
    if (s->device_info.record.channels != channels) {
        LOG_ERROR0("Couldn't set number of channels");
        goto fail;
    }
    if (s->device_info.record.precision != 16) {
        LOG_ERROR0("Couldn't set 16 bit precision");
        goto fail;
    }
    if (s->device_info.record.encoding != AUDIO_ENCODING_LINEAR) {
        LOG_ERROR0("Couldn't set linear encoding");
        goto fail;
    }

    /* We're done, and we didn't fail! */
    LOG_INFO3("Opened audio device %s at %d channel(s), %d Hz", 
            device, channels, sample_rate);

    if(use_metadata)
    {
253
        if(ices_config->metadata_filename)
254
            thread_create("im_sun-metadata", metadata_thread_signal, mod, 1);
255
        else
256
257
258
            thread_create("im_sun-metadata", metadata_thread_stdin, mod, 1);
        LOG_INFO0("Started metadata update thread");
    }
Jack Moffitt's avatar
Jack Moffitt committed
259

260
    return mod;
Jack Moffitt's avatar
Jack Moffitt committed
261
262

fail:
263
264
    close_module(mod); /* safe, this checks for valid contents */
    return NULL;
Jack Moffitt's avatar
Jack Moffitt committed
265
266
}