File: devpsdl-common.c

package info (click to toggle)
ocp 1%3A3.2.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,660 kB
  • sloc: ansic: 361,942; cpp: 70,353; javascript: 12,429; makefile: 6,915; sh: 4,493; java: 1,331; tcl: 1,040; xml: 841; perl: 285; ruby: 126; python: 15
file content (597 lines) | stat: -rw-r--r-- 14,499 bytes parent folder | download
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/* OpenCP Module Player
 * copyright (c) 2011-'26 François Revol <revol@free.fr>
 *
 * Common code for SDL Player device 1.x and 2.x
 *
 * 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, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

static const struct plrDriverAPI_t *plrDriverAPI;

static const struct plrDriver_t plrSDL;

static void *devpSDLBuffer;
static struct ringbuffer_t *devpSDLRingBuffer;
static uint32_t devpSDLRate;
static int devpSDLPauseSamples;
static int devpSDLInPause;

#if SDL_VERSION_ATLEAST(2,0,18)
volatile static uint64_t lastCallbackTime;
#else
volatile static uint32_t lastCallbackTime;
#endif
static volatile unsigned int lastLength;

#if SDL_VERSION_ATLEAST(3,2,0)
static SDL_AudioStream *stream;
static SDL_Mutex *audiolock;
#elif SDL_VERSION_ATLEAST(2,0,0)
static SDL_AudioDeviceID status;
#else
static int status;
#endif

static void theRenderProc(void *userdata, Uint8 *stream, int len)
{
	int pos1, length1, pos2, length2;

	PRINT("%s(,,%d)\n", __FUNCTION__, len);

#if SDL_VERSION_ATLEAST(3,2,0)
	SDL_LockMutex (audiolock);
#elif SDL_VERSION_ATLEAST(2,0,0)
	SDL_LockAudioDevice (status);
#else
	SDL_LockAudio ();
#endif

#if SDL_VERSION_ATLEAST(3,2,0)
	lastCallbackTime = SDL_GetTicks ();
#elif SDL_VERSION_ATLEAST(2,0,18)
	lastCallbackTime = SDL_GetTicks64 ();
#else
	lastCallbackTime = SDL_GetTicks ();
#endif

	plrDriverAPI->ringbufferAPI->get_tail_samples (devpSDLRingBuffer, &pos1, &length1, &pos2, &length2);
	plrDriverAPI->ringbufferAPI->tail_consume_samples (devpSDLRingBuffer, length1 + length2);

	if (devpSDLPauseSamples)
	{
		if ((length1 + length2) > devpSDLPauseSamples)
		{
			devpSDLPauseSamples = 0;
		} else {
			devpSDLPauseSamples -= (length1 + length2);
		}
	}

	plrDriverAPI->ringbufferAPI->get_processing_bytes (devpSDLRingBuffer, &pos1, &length1, &pos2, &length2);

	if (length1 > len)
	{
		length1 = len;
	}
	memcpy (stream, (uint8_t *)devpSDLBuffer + pos1, length1);
	plrDriverAPI->ringbufferAPI->processing_consume_bytes (devpSDLRingBuffer, length1);
	len -= length1;
	stream += length1;
	lastLength = length1 >> 2 /* stereo + bit16 */;
	
	if (len && length2)
	{
		if (length2 > len)
		{
			length2 = len;
		}
		memcpy (stream, (uint8_t *)devpSDLBuffer + pos2, length2);
		plrDriverAPI->ringbufferAPI->processing_consume_bytes (devpSDLRingBuffer, length2);
		len -= length2;
		stream += length2;
		lastLength += length2 >> 2 /* stereo + bit16 */;
	}

#if SDL_VERSION_ATLEAST(3,2,0)
	SDL_UnlockMutex (audiolock);
#elif SDL_VERSION_ATLEAST(2,0,0)
	SDL_UnlockAudioDevice (status);
#else
	SDL_UnlockAudio ();
#endif

	if (len)
	{
		memset (stream, 0, len);
		PRINT("%s: buffer overrun - %d left\n", __FUNCTION__, len);
	}
}

#if SDL_VERSION_ATLEAST(3,2,0)
static void SDLCALL MyNewAudioCallback(void *userdata, SDL_AudioStream *stream, int additional_amount, int total_amount)
{
/* Calculate a little more audio here, maybe using `userdata`, write it to `stream`
 *
 * If you want to use the original callback, you could do something like this:
 */
	if (additional_amount > 0)
	{
		Uint8 *data = SDL_stack_alloc(Uint8, additional_amount);
		if (data)
		{
			theRenderProc(userdata, data, additional_amount);
			SDL_PutAudioStreamData(stream, data, additional_amount);
			SDL_stack_free(data);
		}
	}
}
#endif

static unsigned int devpSDLIdle (void)
{
	int pos1, length1, pos2, length2;
	unsigned int RetVal;

	PRINT("%s()\n", __FUNCTION__);

#if SDL_VERSION_ATLEAST(3,2,0)
	SDL_LockMutex (audiolock);
#elif SDL_VERSION_ATLEAST(2,0,0)
	SDL_LockAudioDevice (status);
#else
	SDL_LockAudio ();
#endif

/* START: this magic updates the tail by time, causing events in the ringbuffer to be fired if needed and audio-visuals to be more responsive */
	{
#if SDL_VERSION_ATLEAST(2,0,18)
		uint64_t curTime;
#else
		uint32_t curTime;
#endif
		signed int expect_consume;
		signed int expect_left;
		signed int consume;

		plrDriverAPI->ringbufferAPI->get_tail_samples (devpSDLRingBuffer, &pos1, &length1, &pos2, &length2);

#if SDL_VERSION_ATLEAST(3,2,0)
		curTime = SDL_GetTicks ();
#elif SDL_VERSION_ATLEAST(2,0,18)
		curTime = SDL_GetTicks64 ();
#else
		curTime = SDL_GetTicks ();
#endif
		expect_consume = devpSDLRate * (curTime - lastCallbackTime) / 1000;
		expect_left = (signed int)lastLength - expect_consume;
		if (expect_left < 0)
		{
			expect_left = 0;
		}
		consume = (signed int)(length1 + length2) - expect_left;
		if (consume > 0)
		{
			plrDriverAPI->ringbufferAPI->tail_consume_samples (devpSDLRingBuffer, consume);
		}
	}
/* STOP */

	plrDriverAPI->ringbufferAPI->get_tailandprocessing_samples (devpSDLRingBuffer, &pos1, &length1, &pos2, &length2);

	/* do we need to insert pause-samples? */
	if (devpSDLInPause)
	{
		int pos1, length1, pos2, length2;
		plrDriverAPI->ringbufferAPI->get_head_bytes (devpSDLRingBuffer, &pos1, &length1, &pos2, &length2);
		memset ((char *)devpSDLBuffer+pos1, 0, length1);
		if (length2)
		{
			memset ((char *)devpSDLBuffer+pos2, 0, length2);
		}
		plrDriverAPI->ringbufferAPI->head_add_pause_bytes (devpSDLRingBuffer, length1 + length2);
		devpSDLPauseSamples += (length1 + length2) >> 2; /* stereo + 16bit */
	}

#if SDL_VERSION_ATLEAST(3,2,0)
	SDL_UnlockMutex (audiolock);
#elif SDL_VERSION_ATLEAST(2,0,0)
	SDL_UnlockAudioDevice (status);
#else
	SDL_UnlockAudio ();
#endif

	RetVal = length1 + length2;

	if (devpSDLPauseSamples >= RetVal)
	{
		return 0;
	}
	return RetVal - devpSDLPauseSamples;
}

static void devpSDLPeekBuffer (void **buf1, unsigned int *buf1length, void **buf2, unsigned int *buf2length)
{
	int pos1, length1, pos2, length2;

	devpSDLIdle (); /* update the tail */

#if SDL_VERSION_ATLEAST(3,2,0)
	SDL_LockMutex (audiolock);
#elif SDL_VERSION_ATLEAST(2,0,0)
	SDL_LockAudioDevice (status);
#else
	SDL_LockAudio ();
#endif

	plrDriverAPI->ringbufferAPI->get_tailandprocessing_samples (devpSDLRingBuffer, &pos1, &length1, &pos2, &length2);


#if SDL_VERSION_ATLEAST(3,2,0)
	SDL_UnlockMutex (audiolock);
#elif SDL_VERSION_ATLEAST(2,0,0)
	SDL_UnlockAudioDevice (status);
#else
	SDL_UnlockAudio ();
#endif

	if (length1)
	{
		*buf1 = (char *)devpSDLBuffer + (pos1 << 2); /* stereo + 16bit */
		*buf1length = length1;
		if (length2)
		{
			*buf2 = (char *)devpSDLBuffer + (pos2 << 2); /* stereo + 16bit */
			*buf2length = length2;
		} else {
			*buf2 = 0;
			*buf2length = 0;
		}
	} else {
		*buf1 = 0;
		*buf1length = 0;
		*buf2 = 0;
		*buf2length = 0;
	}
}

static int devpSDLPlay (uint32_t *rate, enum plrRequestFormat *format, struct ocpfilehandle_t *source_file, struct cpifaceSessionAPI_t *cpifaceSession)
{
#if SDL_VERSION_ATLEAST(3,2,0)
#else
	SDL_AudioSpec desired, obtained;
#endif
	int plrbufsize; /* given in ms */
	int buflength;

	PRINT("%s(*%d,*%d)\n", __FUNCTION__, *rate, *format);

	devpSDLInPause = 0;
	devpSDLPauseSamples = 0;

	*format = PLR_STEREO_16BIT_SIGNED; /* fixed fixed fixed */

	if (!*rate)
	{
		*rate = 44100;
	}
	if (*rate < 22050)
	{
		*rate = 22050;
	}
	if (*rate > 96000)
	{
		*rate = 96000;
	}

#if SDL_VERSION_ATLEAST(3,2,0)
	const SDL_AudioSpec spec = { SDL_AUDIO_S16, 2, *rate };
	audiolock = SDL_CreateMutex();
	if (!audiolock)
	{
		fprintf (stderr, "[SDL] SDL_CreateMutex() failed\n");
		return 0;
	}
#else
	SDL_memset (&desired, 0, sizeof (desired));
	desired.freq = *rate;
	desired.format = AUDIO_S16SYS;
	desired.channels = 2;
	desired.samples = *rate / 8; /* 125 ms */
	desired.callback = theRenderProc;
	desired.userdata = NULL;
#endif

#if SDL_VERSION_ATLEAST(3,2,0)
	lastCallbackTime = SDL_GetTicks ();
#elif SDL_VERSION_ATLEAST(2,0,18)
	lastCallbackTime = SDL_GetTicks64 ();
#else
	lastCallbackTime = SDL_GetTicks ();
#endif
	lastLength = 0;

#if SDL_VERSION_ATLEAST(3,2,0)
	stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec, MyNewAudioCallback, NULL);
	if (!stream)
	{
		SDL_DestroyMutex (audiolock);
		audiolock = 0;
		fprintf (stderr, "[SDL] SDL_OpenAudioDeviceStream() failed\n");
		return 0;
	}
	devpSDLRate = *rate;
#else
# if SDL_VERSION_ATLEAST(2,0,0)
	status=SDL_OpenAudioDevice (NULL, 0, &desired, &obtained, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE | SDL_AUDIO_ALLOW_SAMPLES_CHANGE);
# else
	status=SDL_OpenAudio (&desired, &obtained);
# endif
	if (status < 0)
	{
		fprintf (stderr, "[SDL] SDL_OpenAudio returned %d (%s)\n", (int)status, SDL_GetError());
		free (devpSDLBuffer); devpSDLBuffer = 0;
		plrDriverAPI->ringbufferAPI->free (devpSDLRingBuffer); devpSDLRingBuffer = 0;
		return 0;
	}
	devpSDLRate = *rate = obtained.freq;
#endif

	plrbufsize = cpifaceSession->configAPI->GetProfileInt2 (cpifaceSession->configAPI->SoundSec, "sound", "plrbufsize", 200, 10);
	/* clamp the plrbufsize to be atleast 150ms and below 1000 ms */
	if (plrbufsize < 150)
	{
		plrbufsize = 150;
	}
	if (plrbufsize > 1000)
	{
		plrbufsize = 1000;
	}
	buflength = devpSDLRate * plrbufsize / 1000;

#if SDL_VERSION_ATLEAST(3,2,0)
#else
	if (buflength < obtained.samples * 2)
	{
		buflength = obtained.samples * 2;
	}
#endif

	if (!(devpSDLBuffer=calloc (buflength, 4)))
	{
#if SDL_VERSION_ATLEAST(3,2,0)
		SDL_DestroyAudioStream (stream);
		stream = 0;
		SDL_DestroyMutex (audiolock);
		audiolock = 0;
#elif SDL_VERSION_ATLEAST(2,0,0)
		SDL_CloseAudioDevice (status);
		status=-1;
#else
		SDL_CloseAudio ();
#endif
		return 0;
	}

	if (!(devpSDLRingBuffer = plrDriverAPI->ringbufferAPI->new_samples (RINGBUFFER_FLAGS_STEREO | RINGBUFFER_FLAGS_16BIT | RINGBUFFER_FLAGS_SIGNED | RINGBUFFER_FLAGS_PROCESS, buflength)))
	{
#if SDL_VERSION_ATLEAST(3,2,0)
		SDL_DestroyAudioStream (stream);
		stream = 0;
		SDL_DestroyMutex (audiolock);
		audiolock = 0;
#elif SDL_VERSION_ATLEAST(2,0,0)
		SDL_CloseAudioDevice (status);
		status=-1;
#else
		SDL_CloseAudio ();
#endif
		free (devpSDLBuffer); devpSDLBuffer = 0;
		return 0;
	}

	cpifaceSession->GetMasterSample = plrDriverAPI->GetMasterSample;
	cpifaceSession->GetRealMasterVolume = plrDriverAPI->GetRealMasterVolume;
	cpifaceSession->plrActive = 1;

#if SDL_VERSION_ATLEAST(3,2,0)
	SDL_ResumeAudioStreamDevice (stream);
#else
# warning This needs to delay until we have received the first commit
# if SDL_VERSION_ATLEAST(2,0,0)
	SDL_PauseAudioDevice (status, 0);
# else
	SDL_PauseAudio (0);
# endif
#endif
	return 1;
}

static void devpSDLGetBuffer (void **buf, unsigned int *samples)
{
	int pos1, length1;
	assert (devpSDLRingBuffer);

	PRINT("%s()\n", __FUNCTION__);

#if SDL_VERSION_ATLEAST(3,2,0)
	SDL_LockMutex (audiolock);
#elif SDL_VERSION_ATLEAST(2,0,0)
	SDL_LockAudioDevice (status);
#else
	SDL_LockAudio ();
#endif

	plrDriverAPI->ringbufferAPI->get_head_samples (devpSDLRingBuffer, &pos1, &length1, 0, 0);

#if SDL_VERSION_ATLEAST(3,2,0)
	SDL_UnlockMutex (audiolock);
#elif SDL_VERSION_ATLEAST(2,0,0)
	SDL_UnlockAudioDevice (status);
#else
	SDL_UnlockAudio ();
#endif

	*samples = length1;
	*buf = devpSDLBuffer + (pos1<<2); /* stereo + bit16 */
}

static uint32_t devpSDLGetRate (void)
{
	return devpSDLRate;
}

static void devpSDLOnBufferCallback (int samplesuntil, void (*callback)(void *arg, int samples_ago), void *arg)
{
	assert (devpSDLRingBuffer);

#if SDL_VERSION_ATLEAST(3,2,0)
	SDL_LockMutex (audiolock);
#elif SDL_VERSION_ATLEAST(2,0,0)
	SDL_LockAudioDevice (status);
#else
	SDL_LockAudio ();
#endif

	plrDriverAPI->ringbufferAPI->add_tail_callback_samples (devpSDLRingBuffer, samplesuntil, callback, arg);

#if SDL_VERSION_ATLEAST(3,2,0)
	SDL_UnlockMutex (audiolock);
#elif SDL_VERSION_ATLEAST(2,0,0)
	SDL_UnlockAudioDevice (status);
#else
	SDL_UnlockAudio ();
#endif
}

static void devpSDLCommitBuffer (unsigned int samples)
{
	PRINT("%s(%u)\n", __FUNCTION__, samples);

#if SDL_VERSION_ATLEAST(3,2,0)
	SDL_LockMutex (audiolock);
#elif SDL_VERSION_ATLEAST(2,0,0)
	SDL_LockAudioDevice (status);
#else
	SDL_LockAudio ();
#endif

	plrDriverAPI->ringbufferAPI->head_add_samples (devpSDLRingBuffer, samples);

#if SDL_VERSION_ATLEAST(3,2,0)
	SDL_UnlockMutex (audiolock);
#elif SDL_VERSION_ATLEAST(2,0,0)
	SDL_UnlockAudioDevice (status);
#else
	SDL_UnlockAudio ();
#endif
}

static void devpSDLPause (int pause)
{
	assert (devpSDLBuffer);
	devpSDLInPause = pause;
}

static void devpSDLStop (struct cpifaceSessionAPI_t *cpifaceSession)
{
	PRINT("%s()\n", __FUNCTION__);
	/* TODO, forceflush */

#if SDL_VERSION_ATLEAST(3,2,0)
	SDL_PauseAudioStreamDevice (stream);
	SDL_DestroyAudioStream (stream);
	stream = 0;
	SDL_DestroyMutex (audiolock);
	audiolock = 0;
#elif SDL_VERSION_ATLEAST(2,0,0)
	SDL_PauseAudioDevice (status, 1);
	SDL_CloseAudioDevice (status);
	status=-1;
#else
	SDL_PauseAudio (1);
	SDL_CloseAudio ();
#endif

	free(devpSDLBuffer); devpSDLBuffer=0;
	if (devpSDLRingBuffer)
	{
		plrDriverAPI->ringbufferAPI->reset (devpSDLRingBuffer);
		plrDriverAPI->ringbufferAPI->free (devpSDLRingBuffer);
		devpSDLRingBuffer = 0;
	}

	cpifaceSession->plrActive = 0;
}

static void devpSDLGetStats (uint64_t *committed, uint64_t *processed)
{
#if SDL_VERSION_ATLEAST(3,2,0)
	SDL_LockMutex (audiolock);
#elif SDL_VERSION_ATLEAST(2,0,0)
	SDL_LockAudioDevice (status);
#else
	SDL_LockAudio ();
#endif

	plrDriverAPI->ringbufferAPI->get_stats (devpSDLRingBuffer, committed, processed);

#if SDL_VERSION_ATLEAST(3,2,0)
	SDL_UnlockMutex (audiolock);
#elif SDL_VERSION_ATLEAST(2,0,0)
	SDL_UnlockAudioDevice (status);
#else
	SDL_UnlockAudio ();
#endif
}

static const struct plrDevAPI_t devpSDL = {
	devpSDLIdle,
	devpSDLPeekBuffer,
	devpSDLPlay,
	devpSDLGetBuffer,
	devpSDLGetRate,
	devpSDLOnBufferCallback,
	devpSDLCommitBuffer,
	devpSDLPause,
	devpSDLStop,
	0, /* VolRegs */
	0, /* ProcessKey */
	devpSDLGetStats
};

static void sdlClose (const struct plrDriver_t *driver)
{
	PRINT("%s()\n", __FUNCTION__);
	SDL_QuitSubSystem (SDL_INIT_AUDIO);
}

static int sdlDetect (const struct plrDriver_t *driver)
{
	PRINT("%s()\n", __FUNCTION__);

	return 1;
}

static int sdlPluginInit (struct PluginInitAPI_t *API)
{
	API->plrRegisterDriver (&plrSDL);

	return errOk;
}

static void sdlPluginClose (struct PluginCloseAPI_t *API)
{
	API->plrUnregisterDriver (&plrSDL);
}