summaryrefslogtreecommitdiff
path: root/plugins/oss/oss.c
blob: 9bd59c2a6a072df5ebf4f7fd135002883dff9a65 (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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
    DeaDBeeF - ultimate music player for GNU/Linux systems with X11
    Copyright (C) 2009-2010 Alexey Yakovenko <waker@users.sourceforge.net>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#  include "../../config.h"
#endif
#include <stdint.h>
#include <unistd.h>
#ifdef __linux__
#include <sys/prctl.h>
#endif
#include <stdio.h>
#include <string.h>
#if HAVE_SYS_SOUNDCARD_H
#include <sys/soundcard.h>
#else
#include <soundcard.h>
#endif
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include "../../deadbeef.h"

#define trace(...) { fprintf(stderr, __VA_ARGS__); }
//#define trace(fmt,...)

static DB_output_t plugin;
DB_functions_t *deadbeef;

static intptr_t oss_tid;
static int oss_terminate;
static int oss_rate = 44100;
static int state;
static int fd;
static uintptr_t mutex;

#define BLOCKSIZE 8192

static void
oss_thread (void *context);

static void
oss_callback (char *stream, int len);

static int
oss_init (void) {
    trace ("oss_init\n");
    state = OUTPUT_STATE_STOPPED;
    oss_terminate = 0;
    mutex = 0;

    // prepare oss for playback
    const char *name = "/dev/dsp";
    fd = open (name, O_WRONLY);
    if (fd == -1) {
        trace ("oss: failed to open file\n");
        perror (name);
        plugin.free ();
        return -1;
    }

#if OSS_VERSION>=0x040000
/*
    int cooked = 1;
    ioctl (fd, SNDCTL_DSP_COOKEDMODE, &cooked);
    trace ("oss: cooked_mode=%d\n", cooked);

    int policy = 3;
    ioctl (fd, SNDCTL_DSP_POLICY, &policy);
    trace ("oss: policy=%d\n", policy);
*/
#endif

    int fmt = AFMT_S16_NE;
    if (ioctl (fd, SNDCTL_DSP_SETFMT, &fmt) == -1) {
        trace ("oss: failed to set format\n");
        perror ("SNDCTL_DSP_SETFMT");
        plugin.free ();
        return -1;
    }

    if (fmt != AFMT_S16_NE) {
        fprintf (stderr, "oss: device doesn't support 16 bit sample format\n");
        plugin.free ();
        return -1;
    }

    int channels = 2;
    if (ioctl (fd, SNDCTL_DSP_CHANNELS, &channels) == -1) {
        trace ("oss: failed to set channels\n");
        perror ("SNDCTL_DSP_CHANNELS");
        plugin.free ();
        return -1;
    }
    if (channels != 2) {
        trace ("oss: device doesn't support stereo output\n");
        plugin.free ();
        return -1;
    }

    if (ioctl (fd, SNDCTL_DSP_SPEED, &oss_rate) == -1) {
        trace ("oss: failed to set samplerate\n");
        perror ("SNDCTL_DSP_CHANNELS");
        plugin.free ();
        return -1;
    }

    trace ("oss: samplerate: %d\n", oss_rate);

    mutex = deadbeef->mutex_create ();

    oss_tid = deadbeef->thread_start (oss_thread, NULL);
    return 0;
}

static int
oss_change_rate (int rate) {
    if (!fd) {
        return oss_rate;
    }
    if (rate == oss_rate) {
        trace ("oss_change_rate: ignored\n", rate);
        return rate;
    }
    deadbeef->mutex_lock (mutex);
    if (ioctl (fd, SNDCTL_DSP_SPEED, &rate) == -1) {
        trace ("oss: can't switch to %d samplerate\n", rate);
        perror ("SNDCTL_DSP_CHANNELS");
        plugin.free ();
        return -1;
    }
    oss_rate = rate;
    deadbeef->mutex_unlock (mutex);
    return oss_rate;
}

static int
oss_free (void) {
    trace ("oss_free\n");
    if (!oss_terminate) {
        if (oss_tid) {
            oss_terminate = 1;
            deadbeef->thread_join (oss_tid);
        }
        oss_tid = 0;
        state = OUTPUT_STATE_STOPPED;
        oss_terminate = 0;
        if (fd) {
            close (fd);
        }
        if (mutex) {
            deadbeef->mutex_free (mutex);
            mutex = 0;
        }
    }
    return 0;
}

static int
oss_play (void) {
    if (!oss_tid) {
        if (oss_init () < 0) {
            return -1;
        }
    }
    state = OUTPUT_STATE_PLAYING;
    return 0;
}

static int
oss_stop (void) {
    state = OUTPUT_STATE_STOPPED;
    deadbeef->streamer_reset (1);
    return 0;
}

static int
oss_pause (void) {
    if (state == OUTPUT_STATE_STOPPED) {
        return -1;
    }
    // set pause state
    state = OUTPUT_STATE_PAUSED;
    return 0;
}

static int
oss_unpause (void) {
    // unset pause state
    if (state == OUTPUT_STATE_PAUSED) {
        state = OUTPUT_STATE_PLAYING;
    }
    return 0;
}

static int
oss_get_rate (void) {
    return oss_rate;
}

static int
oss_get_bps (void) {
    return 16;
}

static int
oss_get_channels (void) {
    return 2;
}

static int
oss_get_endianness (void) {
#if WORDS_BIGENDIAN
    return 1;
#else
    return 0;
#endif
}

static void
oss_thread (void *context) {
#ifdef __linux__
    prctl (PR_SET_NAME, "deadbeef-oss", 0, 0, 0, 0);
#endif
    for (;;) {
        if (oss_terminate) {
            break;
        }
        if (state != OUTPUT_STATE_PLAYING || !deadbeef->streamer_ok_to_read (-1)) {
            usleep (10000);
            continue;
        }
        
        char buf[BLOCKSIZE];
        oss_callback (buf, sizeof (buf));
        deadbeef->mutex_lock (mutex);
        int res = write (fd, buf, sizeof (buf));
        deadbeef->mutex_unlock (mutex);
        if (res != sizeof (buf)) {
            perror ("oss write");
            fprintf (stderr, "oss: failed to write buffer\n");
        }
        usleep (1000); // this must be here to prevent mutex deadlock
    }
}

static void
oss_callback (char *stream, int len) {
    int bytesread = deadbeef->streamer_read (stream, len);
    int16_t ivolume = deadbeef->volume_get_amp () * 1000;
    for (int i = 0; i < bytesread/2; i++) {
        ((int16_t*)stream)[i] = (int16_t)(((int32_t)(((int16_t*)stream)[i])) * ivolume / 1000);
    }

    if (bytesread < len) {
        memset (stream + bytesread, 0, len-bytesread);
    }
}

static int
oss_get_state (void) {
    return state;
}

static int
oss_plugin_start (void) {
    return 0;
}

static int
oss_plugin_stop (void) {
    return 0;
}

DB_plugin_t *
oss_load (DB_functions_t *api) {
    deadbeef = api;
    return DB_PLUGIN (&plugin);
}

// define plugin interface
static DB_output_t plugin = {
    DB_PLUGIN_SET_API_VERSION
    .plugin.version_major = 0,
    .plugin.version_minor = 1,
    .plugin.nostop = 0,
    .plugin.type = DB_PLUGIN_OUTPUT,
    .plugin.id = "oss",
    .plugin.name = "OSS output plugin",
    .plugin.descr = "plays sound via OSS API",
    .plugin.author = "Alexey Yakovenko",
    .plugin.email = "waker@users.sourceforge.net",
    .plugin.website = "http://deadbeef.sf.net",
    .plugin.start = oss_plugin_start,
    .plugin.stop = oss_plugin_stop,
    .init = oss_init,
    .free = oss_free,
    .change_rate = oss_change_rate,
    .play = oss_play,
    .stop = oss_stop,
    .pause = oss_pause,
    .unpause = oss_unpause,
    .state = oss_get_state,
    .samplerate = oss_get_rate,
    .bitspersample = oss_get_bps,
    .channels = oss_get_channels,
    .endianness = oss_get_endianness,
};