summaryrefslogtreecommitdiff
path: root/plugins/nullout/nullout.c
blob: febb1b07fb742f64b45c6d3741aac765fe08e43d (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
/*
    Null output plugin for DeaDBeeF Player
    Copyright (C) 2009-2014 Alexey Yakovenko

    This software is provided 'as-is', without any express or implied
    warranty.  In no event will the authors be held liable for any damages
    arising from the use of this software.

    Permission is granted to anyone to use this software for any purpose,
    including commercial applications, and to alter it and redistribute it
    freely, subject to the following restrictions:

    1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.

    2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.

    3. This notice may not be removed or altered from any source distribution.
*/

#include <stdint.h>
#include <unistd.h>
#ifdef __linux__
#include <sys/prctl.h>
#endif
#include <stdio.h>
#include <string.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 null_tid;
static int null_terminate;
static int state;

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

static void
pnull_thread (void *context);

static int
pnull_init (void);

static int
pnull_free (void);

int
pnull_setformat (ddb_waveformat_t *fmt);

static int
pnull_play (void);

static int
pnull_stop (void);

static int
pnull_pause (void);

static int
pnull_unpause (void);

int
pnull_init (void) {
    trace ("pnull_init\n");
    state = OUTPUT_STATE_STOPPED;
    null_terminate = 0;
    null_tid = deadbeef->thread_start (pnull_thread, NULL);
    return 0;
}

int
pnull_setformat (ddb_waveformat_t *fmt) {
    memcpy (&plugin.fmt, fmt, sizeof (ddb_waveformat_t));
    return 0;
}

int
pnull_free (void) {
    trace ("pnull_free\n");
    if (!null_terminate) {
        if (null_tid) {
            null_terminate = 1;
            deadbeef->thread_join (null_tid);
        }
        null_tid = 0;
        state = OUTPUT_STATE_STOPPED;
        null_terminate = 0;
    }
    return 0;
}

int
pnull_play (void) {
    if (!null_tid) {
        pnull_init ();
    }
    state = OUTPUT_STATE_PLAYING;
    return 0;
}

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

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

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

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

static void
pnull_thread (void *context) {
#ifdef __linux__
    prctl (PR_SET_NAME, "deadbeef-null", 0, 0, 0, 0);
#endif
    for (;;) {
        if (null_terminate) {
            break;
        }
        if (state != OUTPUT_STATE_PLAYING) {
            usleep (10000);
            continue;
        }
        
        char buf[4096];
        pnull_callback (buf, 1024);
    }
}

static void
pnull_callback (char *stream, int len) {
    if (!deadbeef->streamer_ok_to_read (len)) {
        memset (stream, 0, len);
        return;
    }
    int bytesread = deadbeef->streamer_read (stream, len);

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

int
pnull_get_state (void) {
    return state;
}

int
null_start (void) {
    return 0;
}

int
null_stop (void) {
    return 0;
}

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

// define plugin interface
static DB_output_t plugin = {
    .plugin.api_vmajor = 1,
    .plugin.api_vminor = 0,
    .plugin.version_major = 1,
    .plugin.version_minor = 0,
    .plugin.type = DB_PLUGIN_OUTPUT,
    .plugin.id = "nullout",
    .plugin.name = "Null output plugin",
    .plugin.descr = "This plugin takes the audio data, and discards it\n, so nothing will play.\nThis is useful for testing.",
    .plugin.copyright = 
    "Null output plugin for DeaDBeeF Player\n"
    "Copyright (C) 2009-2014 Alexey Yakovenko\n"
    "\n"
    "This software is provided 'as-is', without any express or implied\n"
    "warranty.  In no event will the authors be held liable for any damages\n"
    "arising from the use of this software.\n"
    "\n"
    "Permission is granted to anyone to use this software for any purpose,\n"
    "including commercial applications, and to alter it and redistribute it\n"
    "freely, subject to the following restrictions:\n"
    "\n"
    "1. The origin of this software must not be misrepresented; you must not\n"
    " claim that you wrote the original software. If you use this software\n"
    " in a product, an acknowledgment in the product documentation would be\n"
    " appreciated but is not required.\n"
    "\n"
    "2. Altered source versions must be plainly marked as such, and must not be\n"
    " misrepresented as being the original software.\n"
    "\n"
    "3. This notice may not be removed or altered from any source distribution.\n"
    ,
    .plugin.website = "http://deadbeef.sf.net",
    .plugin.start = null_start,
    .plugin.stop = null_stop,
    .init = pnull_init,
    .free = pnull_free,
    .setformat = pnull_setformat,
    .play = pnull_play,
    .stop = pnull_stop,
    .pause = pnull_pause,
    .unpause = pnull_unpause,
    .state = pnull_get_state,
    .fmt = {.samplerate = 44100, .channels = 2, .bps = 16, .channelmask = DDB_SPEAKER_FRONT_LEFT | DDB_SPEAKER_FRONT_RIGHT}
};