File: CorePlayer.cpp

package info (click to toggle)
alsaplayer 0.99.82-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,892 kB
  • sloc: ansic: 40,741; cpp: 14,400; makefile: 983; sh: 796; lex: 751; asm: 45; python: 29; sed: 16
file content (1334 lines) | stat: -rw-r--r-- 31,069 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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
/*  CorePlayer.cpp - Core player object, most of the hacking is done here!
 *	Copyright (C) 1998-2003 Andy Lo A Foe <andy@loafoe.com>
 *
 *  This file is part of AlsaPlayer.
 *
 *  AlsaPlayer 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 3 of the License, or
 *  (at your option) any later version.
 *
 *  AlsaPlayer 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/>.
 *
*/


/*
	Issues:
		- Things need to be cleaned up!

*/

#include "AlsaPlayer.h"
#include "CorePlayer.h"
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/resource.h>
#include <sys/errno.h>
#include <fcntl.h>
#include <dlfcn.h>
#include <sched.h>
#include <pthread.h>
#include <cmath>
#include "Effects.h"
#include "utilities.h"
#include "alsaplayer_error.h"
#include "ap_string.h"

//#include "MetadataReader.h" //TODO

// some arch don't define PATH_MAX
#ifndef PATH_MAX
#define PATH_MAX 1024
#endif

extern void exit_sighandler(int);
static char addon_dir[PATH_MAX];

int CorePlayer::plugin_count = 0;
int CorePlayer::plugins_loaded = 0;
pthread_mutex_t CorePlayer::plugins_mutex = PTHREAD_MUTEX_INITIALIZER;
input_plugin CorePlayer::plugins[MAX_INPUT_PLUGINS];

void CorePlayer::Lock()
{
	if (pthread_mutex_lock(&player_mutex))
		alsaplayer_error("FIRE!!!!!");
}


void CorePlayer::Unlock()
{
	pthread_mutex_unlock(&player_mutex);
}


void CorePlayer::LockNotifiers()
{
	pthread_mutex_lock(&notifier_mutex);
}


void CorePlayer::UnlockNotifiers()
{
	 pthread_mutex_unlock(&notifier_mutex);
}


void CorePlayer::RegisterNotifier(coreplayer_notifier *core_notif, void *data)
{
	if (!core_notif) {
		alsaplayer_error("Notifier is NULL");
		return;
	}
	if (data)
		core_notif->data = data;
	if (core_notif->speed_changed)
		core_notif->speed_changed(core_notif->data, GetSpeed());
	if (core_notif->pan_changed)
		core_notif->pan_changed(core_notif->data, GetPan());
	if (core_notif->volume_changed)
		core_notif->volume_changed(core_notif->data, GetVolume());
	if (core_notif->position_notify)
		core_notif->position_notify(core_notif->data, GetPosition());
	LockNotifiers();
	notifiers.insert(core_notif);
	UnlockNotifiers();
}


void CorePlayer::UnRegisterNotifier(coreplayer_notifier *core_notif)
{
	if (!core_notif)
		return;
	LockNotifiers();
	notifiers.erase(notifiers.find(core_notif));
	UnlockNotifiers();
}

void CorePlayer::load_input_addons()
{
	char path[PATH_MAX];

	DIR *dir;
	struct stat buf;
	dirent *entry;

	input_plugin_info_type input_plugin_info;

	snprintf(path, sizeof (path), "%s/input", addon_dir);

	memset(plugins, 0, sizeof(plugins));

	if ((dir = opendir(path)) == NULL)
		return;

	while ((entry = readdir(dir)) != NULL) {
		if (strcmp(entry->d_name, ".") == 0 ||
		    strcmp(entry->d_name, "..") == 0) {
				continue;
		}
		snprintf(path, sizeof (path), "%s/input/%s", addon_dir, entry->d_name);
		if (stat(path, &buf)) continue;
		if (!S_ISREG(buf.st_mode)) continue;

		void *handle;

		char *ext = strrchr(path, '.');
		if (!ext)
			continue;

		ext++;

		if (strcasecmp(ext, "so"))
			continue;

		if ((handle = dlopen(path, RTLD_NOW |RTLD_GLOBAL)) == NULL) {
			alsaplayer_error("%s", dlerror());
			continue;
		}

		input_plugin_info = (input_plugin_info_type)
				dlsym(handle, "input_plugin_info");

		if (!input_plugin_info) {
			alsaplayer_error("Could not find input_plugin_info symbol in shared object `%s'", path);
			dlclose(handle);
			continue;
		}

		//alsaplayer_error("Loading input plugin: %s", path);
		input_plugin *the_plugin = input_plugin_info();
		if (the_plugin) {
			the_plugin->handle = handle;
		}
		if (!RegisterPlugin(the_plugin)) {
			alsaplayer_error("Error loading %s", path);
			dlclose(handle);
			continue;
		}
	}
	closedir(dir);
}


CorePlayer::CorePlayer(AlsaNode *the_node)
{
	int i;

	/* Init mutexes */
	pthread_mutex_init(&counter_mutex, NULL);
	pthread_mutex_init(&player_mutex, NULL);
	pthread_mutex_init(&notifier_mutex, NULL);
	pthread_cond_init(&producer_ready, NULL);

	producer_thread = 0;
	total_blocks = 0;
	streaming = false;
	producing = false;
	jumped = false;
	//plugin_count = 0;
	plugin = NULL;
	jump_point = repitched = write_buf_changed = 0;
	new_block_number = 0;
	read_direction = DIR_FORWARD;
	node = the_node;
	pitch = 1.0;
	sub = NULL;
	last_read = -1;
	input_rate = output_rate = 44100;
	SetSpeedMulti(1.0);
	SetSpeed(1.0);
	SetVolume(1.0);
	SetPan(0.0);
	buffer = NULL;
	the_object = NULL;

	save_speed = 0.0;

	// allocate ringbuffer partition pointer table
	if ((buffer = new sample_buf[NR_BUF]) == NULL) {
		alsaplayer_error("Out of memory in CorePlayer::CorePlayer");
		exit(1);
	}

	for (i=0; i < NR_BUF; i++) {
		buffer[i].buf = new SampleBuffer(SAMPLE_STEREO, BUF_SIZE);
		if (buffer[i].buf == NULL) {
			alsaplayer_error("Out of memory in CorePlayer::CorePlayer");
			exit(1);
		}
		// Set up next/prev pointers
		if (i > 0) {
			buffer[i].prev = &buffer[i-1];
			buffer[i-1].next = &buffer[i];
		}
		buffer[i].start = -1;
	}
	// Connect head and tail
	buffer[0].prev = &buffer[NR_BUF-1];
	buffer[NR_BUF-1].next = &buffer[0];

	//memset(plugins, 0, sizeof(plugins));

	read_buf = write_buf = buffer;

	the_object = new input_object;
	the_object->ready = 0;

	pthread_mutex_init(&the_object->object_mutex, NULL);
	ap_strlcpy(addon_dir, ADDON_DIR, sizeof (addon_dir));

	// Load the input addons
	pthread_mutex_lock(&plugins_mutex);
	if (!plugins_loaded) {
		plugins_loaded = 1;
		load_input_addons();
	}
#if !defined(EMBEDDED)
	if ((input_buffer = new char[128 * 1024]) == NULL) {
		alsaplayer_error("cannot allocate mix buffer");
		exit(1);
	}
#endif
	pthread_mutex_unlock(&plugins_mutex);
}



void CorePlayer::ResetBuffer()
{
	for (int i=0; i < NR_BUF; i++) {
		buffer[i].start = -1;
		buffer[i].buf->Clear();
	}
}


CorePlayer::~CorePlayer()
{
	int i;

	UnregisterPlugins();

	for (i=0; i < NR_BUF; i++) {
		delete buffer[i].buf;
	}
	pthread_mutex_destroy(&counter_mutex);
	pthread_mutex_destroy(&player_mutex);
	pthread_mutex_destroy(&the_object->object_mutex);
	delete []buffer;
	delete the_object;
#if !defined(EMBEDDED)
	delete []input_buffer;
#endif
}


void CorePlayer::UnregisterPlugins()
{
	input_plugin *tmp;

	Stop();
	Close();

	Lock();
	for (int i = 0; i < plugin_count; i++) {
		tmp = &plugins[i];
		//alsaplayer_error("Unloading Input plugin: %s", tmp->name);
		if (tmp->handle) {
			//tmp->shutdown(); // Shutdown plugin
			dlclose(tmp->handle);
			tmp->handle = NULL;
			tmp = NULL;
		}
	}
	Unlock();
}

int CorePlayer::RegisterPlugin(input_plugin *the_plugin)
{
	int version;
	int error_count = 0;
	input_plugin *tmp;

	if (the_plugin->name == NULL) {
		alsaplayer_error("No name");
		error_count++;
	}
	if (the_plugin->author == NULL) {
		alsaplayer_error("No author");
		error_count++;
	}
	if (the_plugin->init == NULL) {
		alsaplayer_error("No init function");
		error_count++;
	}
	if (the_plugin->can_handle == NULL) {
		alsaplayer_error("No can_handle function");
		error_count++;
	}
	if (the_plugin->open == NULL) {
		alsaplayer_error("No open function");
		error_count++;
	}
	if (the_plugin->close == NULL) {
		alsaplayer_error("No close function");
		error_count++;
	}
	if (the_plugin->play_block == NULL) {
		alsaplayer_error("No play_block function");
		error_count++;
	}
	if (the_plugin->block_seek == NULL) {
		alsaplayer_error("No block_seek function");
		error_count++;
	}

	if (the_plugin->nr_blocks == NULL) {
		alsaplayer_error("No nr_blocks function");
		error_count++;
	}
	if (the_plugin->frame_count == NULL) {
		alsaplayer_error("No frame_count function");
		error_count++;
	}
	if (the_plugin->block_size == NULL) {
		alsaplayer_error("No block_size function");
		error_count++;
	}
	if (the_plugin->channels == NULL) {
		alsaplayer_error("No channels function");
		error_count++;
	}
	if (the_plugin->stream_info == NULL) {
		alsaplayer_error("No stream_info function");
//		error_count++; // we don't require this function since generic metadata parsing
	}
	if (the_plugin->shutdown == NULL) {
		alsaplayer_error("No shutdown function");
		error_count++;
	}
	if (error_count) {
	    	alsaplayer_error("At least %d error(s) were detected", error_count);
		return 0;
	}

	tmp = &plugins[plugin_count];
	tmp->version = the_plugin->version;
	if (tmp->version) {
		if ((version = tmp->version) != INPUT_PLUGIN_VERSION) {
			alsaplayer_error("Wrong version number on plugin (v%d, wanted v%d)",
					version - INPUT_PLUGIN_BASE_VERSION,
					INPUT_PLUGIN_VERSION - INPUT_PLUGIN_BASE_VERSION);
			// this is a minor error actually
			// no need to stop there
//			return 0;
		}
	}

	tmp->name = the_plugin->name;
	tmp->author = the_plugin->author;
	tmp->init = the_plugin->init;
	tmp->can_handle = the_plugin->can_handle;
	tmp->open = the_plugin->open;
	tmp->close = the_plugin->close;
	tmp->play_block = the_plugin->play_block;
	tmp->block_seek = the_plugin->block_seek;
	tmp->block_size = the_plugin->block_size;
	tmp->nr_blocks = the_plugin->nr_blocks;
	tmp->block_to_sec = the_plugin->block_to_sec;
	tmp->sample_rate = the_plugin->sample_rate;
	tmp->channels = the_plugin->channels;
	tmp->stream_info = the_plugin->stream_info;
	tmp->shutdown = the_plugin->shutdown;
	tmp->nr_tracks = the_plugin->nr_tracks;
	tmp->track_seek = the_plugin->track_seek;

	if (!tmp->init()) {
		alsaplayer_error("Plugin failed to initialize (\"%s\")", tmp->name);
		return 0;
	}
	plugin_count++;

	if (global_verbose)
		alsaplayer_error("Loading Input plugin: %s", tmp->name);

	return 1;
}


int CorePlayer::GetCurrentTime(int block)
{
	 long result = 0;

	Lock();
	if (plugin && the_object && the_object->ready) {
		result = plugin->block_to_sec(the_object, block < 0 ? GetPosition()
			: block);
	}
	Unlock();
	if (result < 0) {
		alsaplayer_error("Huh, negative????");
	}
	return (int)result;
}


int CorePlayer::GetPosition()
{
	int result;

	if (jumped)	// HACK!
		return jump_point;
	if (read_buf && plugin && the_object && the_object->ready) {
		if (read_buf->start < 0)
			return 0;

		int block_size = plugin->block_size(the_object);
		if (block_size)  {
			result = (read_buf->start + (read_buf->buf->read_index * 4) / block_size);
			if (result < 0) {
				alsaplayer_error("Found the error!");
			}
			return result;
		} else
			return 0;
	} else {
		return 0;
	}
}


int CorePlayer::GetBlocks()
{
	int result = 0;

	Lock();
	if (plugin && the_object && the_object->ready)
		result = plugin->nr_blocks(the_object);
	Unlock();

	return result;
}


int CorePlayer::GetTracks()
{
	int result = 0;
	Lock();
	if (plugin && the_object && the_object->ready) {
		if (plugin->nr_tracks) {
			result = plugin->nr_tracks(the_object);
		} else {
			result = 1;
		}
	}
	Unlock();

	return result;
}


int CorePlayer::GetChannels()
{
	return 2;	// Hardcoded for now
}

int CorePlayer::GetBlockSize()
{
	int result = 0;

	Lock();
	if (plugin && the_object && the_object->ready)
		result = plugin->block_size(the_object);
	Unlock();

	return result;
}


int CorePlayer::GetSampleRate()
{
	int result = 0;

	Lock();
	if (plugin && the_object && the_object->ready)
		result = plugin->sample_rate(the_object);
	Unlock();

	return result;
}


int CorePlayer::GetStreamInfo(stream_info *info)
{
	int result = 0;

	memset(info, 0, sizeof(stream_info));

	Lock();
	if (plugin && plugin->stream_info && info && the_object) {
		result = plugin->stream_info(the_object, info);
	}
	if (plugin && info && the_object) {
		if (plugin->stream_info) { // plugin provides its own metadata parser
			result = plugin->stream_info(the_object, info);
		}
		else{ // we'll use generic metadata parser
//TODO			result = get_stream_info (the_object->path, info);
		}
	}
	Unlock();

	return result;
}


// This is the first function to get documented. Believe it or not
// it was the most difficult to get right! We absolutely NEED to have
// a known state of all the threads of our object!

bool CorePlayer::Start()
{
	float result;
	int tries;

	if (IsPlaying())
		return false;

	if (!node) {
		alsaplayer_error("CorePlayer does not have a node");
		return false;
	}
	Lock();
	// First check if we have a filename to play
	if (!the_object->ready) {
		Unlock();
		return false;
	}
	producing = true; // So Read32() doesn't generate an error
	streaming = true;
	jumped = false;

	ResetBuffer();

	last_read = -1;
	output_rate = node->SamplingRate();
	input_rate = plugin->sample_rate(the_object);

	if (input_rate == output_rate)
		SetSpeedMulti(1.0);
	else
		SetSpeedMulti((float) input_rate / (float) output_rate);
	update_pitch();
	write_buf_changed = 0;
	read_buf = write_buf;
	result = GetSpeed();

	if (result < 0.0) {
		int start_block = plugin->nr_blocks(the_object) - 16;
		write_buf->start = start_block > 0 ? start_block : 0;
		SetDirection(DIR_BACK);
	} else {
		write_buf->start = 0;
		SetDirection(DIR_FORWARD);
	}
	pthread_create(&producer_thread, NULL,
		(void * (*)(void *))producer_func, this);

	// allow producer to actually start producing
	dosleep(20000);
	//alsaplayer_error("Prebuffering...");
	// wait up to 4 seconds to get all (really: half) the PCM buffers filled
	tries = 100;
	while (--tries && (FilledBuffers() < NR_CBUF / 2) && producing) {
		//alsaplayer_error("Waiting for buffers...");
		dosleep(40000);
	}
	sub = new AlsaSubscriber();
	if (!sub) {
		alsaplayer_error("Subscriber creation failed :-(\n");
		Unlock();
		return false;
	}
	sub->Subscribe(node);
	//alsaplayer_error("Starting streamer thread...");
	sub->EnterStream(streamer_func, this);
	Unlock();
	// Notify
	std::set<coreplayer_notifier *>::const_iterator i;
	LockNotifiers();
	for (i = notifiers.begin(); i != notifiers.end(); i++) {
		if ((*i)->start_notify) {
			(*i)->start_notify((*i)->data);
		}
	}
	UnlockNotifiers();
	return true;
}


void CorePlayer::Stop()
{
	Lock();

	if (sub) {
        	delete sub;
        	sub = NULL;
	}
	producing = false;
	streaming = false;
	//alsaplayer_error("Signalling 6...");
	pthread_cond_signal(&producer_ready);
#ifdef DEBUG
	alsaplayer_error("Waiting for producer_thread to finish...");
#endif
	if (producer_thread) {
	    if (pthread_join(producer_thread, NULL)) {
		    alsaplayer_error("producer_thread not running...");
	    }
	}
	pthread_mutex_destroy(&counter_mutex);

	pthread_mutex_init(&counter_mutex, NULL);
	producer_thread = 0;

	//alsaplayer_error("producer_func is gone now...");

	ResetBuffer();
	Unlock();

	// Notify
	std::set<coreplayer_notifier *>::const_iterator i;
	LockNotifiers();
	for (i = notifiers.begin(); i != notifiers.end(); i++) {
		if ((*i)->stop_notify) {
			(*i)->stop_notify((*i)->data);
		}
	}
	UnlockNotifiers();
}


void CorePlayer::SetVolume(float val)
{
	if (val > 2.0) {
		alsaplayer_error("ERROR: volume out of bound (%.2f)", val);
		return;
	}
	volume = val;

	std::set<coreplayer_notifier *>::const_iterator i;
	LockNotifiers();
	for (i = notifiers.begin(); i != notifiers.end(); i++) {
		if ((*i)->volume_changed)
			(*i)->volume_changed((*i)->data, volume);
	}
	UnlockNotifiers();
}

void CorePlayer::SetPan(float val)
{
	pan = val;

	std::set<coreplayer_notifier *>::const_iterator i;
	LockNotifiers();
	for (i = notifiers.begin(); i != notifiers.end(); i++) {
		if ((*i)->pan_changed)
			(*i)->pan_changed((*i)->data, pan);
	}
	UnlockNotifiers();
}


void CorePlayer::PositionUpdate()
{
	std::set<coreplayer_notifier *>::const_iterator i;
	LockNotifiers();
	for (i = notifiers.begin(); i != notifiers.end(); i++) {
		if ((*i)->position_notify)
			(*i)->position_notify((*i)->data, GetPosition());
	}
	UnlockNotifiers();
}


int CorePlayer::SetSpeed(float val)
{
	if (val < 0.0 && !CanSeek())
		val = 0.0; // Do not allow reverse play if we can't seek
	//alsaplayer_error("new speed = %.2f", val);
	pitch_point = val;
	repitched = 1;

	std::set<coreplayer_notifier *>::const_iterator i;

	LockNotifiers();
	for (i = notifiers.begin(); i != notifiers.end(); i++) {
		if ((*i)->speed_changed)
			(*i)->speed_changed((*i)->data, pitch_point);
	}
	UnlockNotifiers();

	return 0;
}

float CorePlayer::GetSpeed()
{
	if (repitched) { // Pitch was changed so return this instead
		return pitch_point;
	}
	float ret_pitch = pitch / pitch_multi;
	return (read_direction == DIR_FORWARD ? ret_pitch : -ret_pitch);
}


bool CorePlayer::CanSeek()
{
	if (plugin && the_object && the_object->ready) {
		if (the_object->flags & P_SEEK)
			return true;
	}
	return false;
}

int CorePlayer::Seek(int index)
{
	jump_point = index;
	jumped = true;
	return 0;
}


int CorePlayer::BlockSeek(int index)
{
	if (plugin && the_object && the_object->ready)
		index = plugin->block_seek(the_object, index);
	else
		index = -1;
	return index;
}


void CorePlayer::Close()
{
	Lock();
	if (plugin && the_object && the_object->ready) {
		free (the_object->path);
		the_object->ready = 0;
		plugin->close(the_object);
	}
	Unlock();
}


// Find best plugin to play a file
input_plugin *
CorePlayer::GetPlayer(const char *path)
{
	// Check we've got a path
	if (!*path) {
		return NULL;
	}

	float best_support = 0.0;
	input_plugin *best_plugin = NULL;

	// Go through plugins, asking them
	for (int i = 0; i < plugin_count; i++) {
		input_plugin *p = plugins + i;
		float sl = p->can_handle(path);
		if (sl > best_support) {
			best_support = sl;
			best_plugin = p;
			if (sl == 1.0) break;
		}
	}

	// Return best plugin, if there is one
	if(best_support <= 0.0) return NULL;
	return best_plugin;
}


bool CorePlayer::Open(const char *path)
{
	input_plugin *best_plugin;
	int block_size = 0;

	Close();

	if (path == NULL || strlen(path) == 0) {
		alsaplayer_error("No path supplied");
		return false;
	}
	if ((best_plugin = GetPlayer(path)) == NULL) {
		alsaplayer_error("No suitable plugin found for \"%s\"", path);
		return false;
	}
	Lock();

	the_object->local_data = NULL;

	if (best_plugin->open(the_object, path)) {
		if ((block_size = best_plugin->block_size(the_object)) > BUF_SIZE) {
			alsaplayer_error("CRITICAL ERROR: this plugin advertised a buffer size\n"
					 "larger than %d bytes. This is not supported by AlsaPlayer!\n"
					 "Contact the author to fix this problem.\n"
					 "TECHNICAL: A possible solution is to divide the block size\n"
					 "in 2 and double the number of effective blocks. This can be\n"
					 "handled relatively easily in the plugin (hopefully).\n\n"
					 "We will retreat, as chaos and despair await us on\n"
					 "this chosen path........................................", BUF_SIZE);
			best_plugin->close(the_object);
			Unlock();
			return false;
		} else {
			if (!block_size) {
					alsaplayer_error("No blocksize");
					best_plugin->close(the_object);
					Unlock();
					return false;
			}
			// result = true
		}
	} else {
		best_plugin->close(the_object);
		Unlock();
		return false;
	}

	// Copy path into input_object structure for future use
	the_object->path = strdup (path); // including trailing \0

	the_object->ready = 1;
	plugin = best_plugin;

	blocks_in_buffer = read_buf->buf->GetBufferSizeBytes(plugin->block_size(the_object)) / plugin->block_size(the_object);

	Unlock();

	return true;
}

#ifdef DEBUG
void print_buf(sample_buf *start)
{
	sample_buf *c = start;
	for (int i = 0; i < NR_BUF; i++, c = c->next) {
		alsaplayer_error("%d ", c->start);
	}
}
#endif

int CorePlayer::SetDirection(int direction)
{
	sample_buf *tmp_buf;
	int buffers = 0;

	if (read_direction != direction) {
		tmp_buf = read_buf;
		switch(direction) {
		 case DIR_FORWARD:
			while (buffers < NR_BUF-1 && tmp_buf->next->start ==
			       (tmp_buf->start + blocks_in_buffer)) {
				buffers++;
				tmp_buf = tmp_buf->next;
			}
			break;
	 	  case DIR_BACK:
			while (buffers < NR_BUF-1 && tmp_buf->prev->start ==
			       (tmp_buf->start - blocks_in_buffer)) {
				buffers++;
				tmp_buf = tmp_buf->prev;
			}
		}
		new_write_buf = tmp_buf;
		new_block_number = new_write_buf->start;
		write_buf_changed = 1;
		read_direction = direction;
		//alsaplayer_error("Signalling...1");
		pthread_cond_signal(&producer_ready);
	}
	return 0;

}


// return number of buffers already filled with (decoded) PCM data
//
int CorePlayer::FilledBuffers()
{
	int result = 0;
	sample_buf *tmp = read_buf;

	if (read_buf == write_buf) {
		return 0;
	}
	switch (read_direction) {
	 case DIR_FORWARD:
		while (tmp->next != write_buf) {
			tmp = tmp->next;
			result++;
		}
		break;
	 case DIR_BACK:
		while (tmp->prev != write_buf) {
			tmp = tmp->prev;
			result++;
		}
		break;
	}
	return result; // We can't use a buffer that is potentially being written to
			 // so a value of 1 should still return no buffers
}


void CorePlayer::update_pitch()
{
	if (pitch_point < 0) {
		SetDirection(DIR_BACK);
		pitch = -pitch_point;
	} else {
		SetDirection(DIR_FORWARD);
		pitch = pitch_point;
	}
	pitch *= pitch_multi;

	repitched = 0;
}


/*
	FIXME: some documentation needed

	returns the number of samples put into output buffer
	negative return values indicate errors
*/
int CorePlayer::Read32(void *data, int samples)
{
	if (repitched) {
		update_pitch();
	}

	if (pitch == 0.0 || (read_buf == write_buf)) {
		if (write_buf->next->start == -2 || !producing) {
			return -2;
		}
		//alsaplayer_error("Nothing to play (spitting out silence)");
		memset(data, 0, samples * 4);
		return samples;
	}
	int use_read_direction = read_direction;
	int *out_buf = (int *)data;
	int *in_buf = (int *)read_buf->buf->buffer_data;
	in_buf += read_buf->buf->read_index;
	int buf_index = 0;
	int tmp_index = 0;
	int check_index = read_buf->buf->read_index;
	int base_corr = 0;
	float use_pitch = pitch;

	if (jumped) {
		int i;
		jumped = false;
		new_write_buf = read_buf;
		// ---- Testing area ----
		sample_buf *sb;
		for (i=0, sb = buffer; i < NR_BUF; i++, sb = sb->next) {
			sb->start = -1;
			sb->buf->Clear();
		}
		// ---- Testing area ----
		new_write_buf->start = jump_point;
		new_block_number = jump_point;
		write_buf_changed = 1;
		//alsaplayer_error("Signalling 2...");
		pthread_cond_signal(&producer_ready);
	}
#ifdef EMBEDDED
	// provide a fast path implementation for the common case (no pitch)
	if ((use_read_direction == DIR_FORWARD) && (use_pitch == 1.0)) {

		// calculate amount of samples available in current read buffer
		int samplesInBuffer = read_buf->buf->write_index - check_index;

		if (samplesInBuffer >= samples)
		{
			// copy the whole block (usually 4KB) at once
			memcpy(out_buf, in_buf, samples * 4);
			tmp_index = samples - 1;
		}
		else
		{
			if (samplesInBuffer <= 0) {
				memset(data, 0, samples * 4);
				return samples;
			}
			// use rest of current block
			if (samplesInBuffer)
				memcpy(out_buf, in_buf, samplesInBuffer * 4);

			// get next block
			read_buf = read_buf->next;
			//alsaplayer_error("Signalling 3...");
			pthread_cond_signal(&producer_ready);
			read_buf->buf->SetReadDirection(DIR_FORWARD);
			read_buf->buf->ResetRead();
			in_buf = (int *)read_buf->buf->buffer_data;
			check_index = samples - samplesInBuffer;
			memcpy(out_buf + samplesInBuffer, in_buf, (samples - samplesInBuffer) * 4);
			tmp_index = -1;
		}
	}
	else
#endif
	if (use_read_direction == DIR_FORWARD) {
		while (buf_index < samples) {
			tmp_index = (int) ((float)use_pitch*(float)(buf_index-base_corr));
			if ((check_index + tmp_index) > (read_buf->buf->write_index)-1) {
				if (read_buf->next->start < 0 || read_buf->next == write_buf) {
					if (read_buf->next->start == -2) {
						//alsaplayer_error("Next in queue (2)");
						return -2;
					}
					memset(data, 0, samples * 4);
					if (!producing) {
						//alsaplayer_error("blah 1");
						return -1;
					}
					return samples;
				} else if (read_buf->next->start !=
					read_buf->start + blocks_in_buffer) {
					alsaplayer_error("------ WTF!!? %d - %d",
					read_buf->next->start,
					read_buf->start);
				}
				read_buf = read_buf->next;
				//alsaplayer_error("Signalling 4...");
				pthread_cond_signal(&producer_ready);
				read_buf->buf->SetReadDirection(DIR_FORWARD);
				read_buf->buf->ResetRead();
				in_buf = (int *)read_buf->buf->buffer_data;
				base_corr = buf_index;
				check_index = 0;
				// Recalc
				tmp_index = (int)((float)use_pitch*(float)(buf_index-base_corr));
			}
			out_buf[buf_index++] =  *(in_buf + tmp_index);
		}
	} else { // DIR_BACK
		while (buf_index < samples) {
			tmp_index = (int)((float)use_pitch*(float)(buf_index-base_corr));
			if ((check_index - tmp_index) < 0) {
				if (read_buf->prev->start < 0 ||
					read_buf->prev == write_buf) {
				//	printf("Back (%d %d) ", FilledBuffers(), read_buf->prev->start);
				//	print_buf(read_buf->prev);
					if (!producing) {
						//printf("blah 2\n");
						return -1;
					}
					memset(data, 0, samples * 4);
					return samples;
				}
				read_buf = read_buf->prev;
				//alsaplayer_error("Signalling 5...");
				pthread_cond_signal(&producer_ready);
				read_buf->buf->SetReadDirection(DIR_BACK);
				read_buf->buf->ResetRead();
				int buf_size = read_buf->buf->write_index;
				in_buf = (int *)read_buf->buf->buffer_data;
				in_buf += (buf_size + (check_index - tmp_index));
				base_corr = buf_index;
				check_index = buf_size-1;
				// Recalc
				tmp_index = (int)((float)use_pitch*(float)(buf_index-base_corr));
			}
			out_buf[buf_index++] = *(in_buf - tmp_index);
		}
	}

	read_buf->buf->Seek(check_index + ((use_read_direction == DIR_FORWARD) ?
		 tmp_index+1 : -((tmp_index+1) > check_index ? check_index : tmp_index+1)));
	if (!samples) alsaplayer_error("Humm, I copied nothing?");

	return samples;
}


int CorePlayer::pcm_worker(sample_buf *dest, int start)
{
	int result = 0;
	int blocks_read = 0;
	int samples_written = 0;
	short *sample_buffer;

	if (!dest || !the_object || !the_object->ready) {
		return -1;
	}

	int count = dest->buf->GetBufferSizeBytes(plugin->block_size(the_object));
	dest->buf->Clear();
	if (last_read != start) {
		BlockSeek(start);
	}
	if (start < 0) {
		return -1;
	}
	sample_buffer = dest->buf->buffer_data;
	while (count > 0 && producing) {
		if (!plugin->play_block(the_object, sample_buffer + samples_written)) {
			dest->start = start;
#ifdef DEBUG
			alsaplayer_error("blocks read = %d", blocks_read);
#endif
                        count = 0;
		} else {
			samples_written += plugin->block_size(the_object); // OPTIMIZE!
			blocks_read++;
		}
		count -= plugin->block_size(the_object);
	}
	dest->start = start;
	last_read = dest->start + blocks_read;
	dest->buf->SetSamples(samples_written >> 1);

	// Check if rates are still the same
	if ((result = plugin->sample_rate(the_object)) != input_rate) {
		alsaplayer_error("WARNING: Sample rate changed in midstream (new = %d)", result);
		input_rate = result;
		SetSpeedMulti((float) input_rate / (float) output_rate);
		update_pitch();
	}
	return blocks_read;
}


// fill PCM buffers with (decoded) data
//
void CorePlayer::producer_func(void *data)
{
	CorePlayer *obj = (CorePlayer *)data;
	int blocks_read;

#ifdef DEBUG
	alsaplayer_error("Starting new producer_func...");
#endif
	while (obj->producing) {
		if (obj->write_buf_changed) {
			obj->write_buf_changed = 0;
			obj->write_buf = obj->new_write_buf;
			obj->write_buf->start = obj->new_block_number;
		}
		// still at least one buffer left to fill?
		if (obj->FilledBuffers() < (NR_CBUF-1)) {
			//alsaplayer_error("producer: filling buffer");
			switch (obj->read_direction) {
			 case DIR_FORWARD:
				blocks_read = obj->pcm_worker(obj->write_buf, obj->write_buf->start);
				if (blocks_read != obj->blocks_in_buffer) {
					obj->producing = false;
					obj->write_buf->next->start = -2;
				}
				obj->write_buf = obj->write_buf->next;
				obj->write_buf->start = obj->write_buf->prev->start + obj->blocks_in_buffer;
				break;
			 case DIR_BACK:
				blocks_read = obj->pcm_worker(obj->write_buf, obj->write_buf->start);
				if (blocks_read != obj->blocks_in_buffer) {
					if (obj->write_buf->start >= 0)
						alsaplayer_error("an ERROR occured or EOF (%d)",
							obj->write_buf->start);
					obj->write_buf->prev->start = -2;
					obj->producing = false;
				}
				obj->write_buf = obj->write_buf->prev;
				obj->write_buf->start = obj->write_buf->next->start -
					obj->blocks_in_buffer;
				break;
			}
		} else {
			//alsaplayer_error("producer: waiting for free buffer");
			pthread_mutex_lock(&obj->counter_mutex);
			//alsaplayer_error("Waiting for a signal...");
			pthread_cond_wait(&obj->producer_ready, &obj->counter_mutex);
			//alsaplayer_error("Got signalled...");
			pthread_mutex_unlock(&obj->counter_mutex);
			//alsaplayer_error("producer: unblocked");
		}
	}
	//alsaplayer_error("Exitting producer_func (producing = %d)", obj->producing);
	pthread_exit(NULL);
}

extern int global_bal;
extern int global_vol_left;
extern int global_vol_right;
extern int global_vol;
extern int global_pitch;
extern int global_reverb_on;
extern int global_reverb_delay;
extern int global_reverb_feedback;

#ifdef EMBEDDED
// the simple streamer only handles a single stream with no software
// volume/pan calculation; intended for slow systems/embedded system use
bool CorePlayer::streamer_func(void *arg, void *data, int size)
{
	CorePlayer *obj = (CorePlayer *)arg;

	if (obj->Read32(data, size / sizeof(short)) >= 0)
		return true;

	obj->streaming = false;
	return false;
}

#else

bool CorePlayer::streamer_func(void *arg, void *data, int size)
{
	CorePlayer *obj = (CorePlayer *)arg;
	int count;

	if ((count = obj->Read32(obj->input_buffer, size / sizeof(short))) >= 0) {
		float v, p, left, right;
		p = obj->pan;
		v = obj->volume;
		if (v != 1.0 || p != 0.0) {
			if (p == 0.0) {
				left = right = 1.0;
			} else 	if (p > 0) {
				right = 1.0;
				left = 1.0 - p;
			} else {
				left = 1.0;
				right = 1.0 + p;
			}
			if (v != 1.0) {
				left *= v;
				right *= v;
			}
			//printf("v = %d, p = %d, left = %d, right = %d\n",
			//	p, v, left, right);
			volume_effect32(obj->input_buffer, count, left, right);
		}
		// Now add the data to the current stream
		int16_t *in_buffer;
		int16_t *out_buffer;
		int32_t level;
		in_buffer = (int16_t *)obj->input_buffer;
		out_buffer = (int16_t *)data;
		for (int i=0; i < size ; i++) {
				level = *(in_buffer++) + *out_buffer;
				if (level > 32767) level = 32767;
				else if (level < -32768) level = -32768;
				*(out_buffer++) = (int16_t) level;
		}
		return true;
	} else {
		//alsaplayer_error("Exiting from streamer_func...");
		obj->streaming = false;
		return false;
	}
}
#endif


bool CorePlayer::IsPaused()
{
	return ((GetSpeed() == 0.0) ? true : false);
}


void CorePlayer::Pause()
{
	save_speed = GetSpeed ();
	SetSpeed (0.0);
}


void CorePlayer::UnPause()
{
	if (save_speed)
	    SetSpeed (save_speed);
	else
	SetSpeed (1.0);
}