aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_audio.c
blob: f7bfaccf9060f87a66bfdc9d110e7637934e9727 (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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
 * test_audio.c - Test program for new API
 *
 * Copyright (C) 2000 Ralph  Metzler <ralph@convergence.de>
 *                  & Marcus Metzler <marcus@convergence.de>
                      for convergence integrated media GmbH
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1
 * 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 Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

#include <sys/ioctl.h>
#include <stdio.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>

#include <ost/dmx.h>
#include <ost/frontend_old.h>
#include <ost/sec.h>
#include <ost/audio.h>
#include <sys/poll.h>

int audioStop(int fd)
{
	int ans;

	if ((ans = ioctl(fd,AUDIO_STOP,0)) < 0) {
		perror("AUDIO STOP: ");
		return -1;
	}

	return 0;
}

int audioPlay(int fd)
{
	int ans;

	if ((ans = ioctl(fd,AUDIO_PLAY)) < 0) {
		perror("AUDIO PLAY: ");
		return -1;
	}

	return 0;
}


int audioPause(int fd)
{
	int ans;

	if ((ans = ioctl(fd,AUDIO_PAUSE)) < 0) {
		perror("AUDIO PAUSE: ");
		return -1;
	}

	return 0;
}


int audioContinue(int fd)
{
	int ans;

	if ((ans = ioctl(fd,AUDIO_CONTINUE)) < 0) {
		perror("AUDIO CONTINUE: ");
		return -1;
	}

	return 0;
}

int audioSelectSource(int fd, audio_stream_source_t source)
{
	int ans;

	if ((ans = ioctl(fd,AUDIO_SELECT_SOURCE, source)) < 0) {
		perror("AUDIO SELECT SOURCE: ");
		return -1;
	}

	return 0;
}



int audioSetMute(int fd, boolean state)
{
	int ans;

	if ((ans = ioctl(fd,AUDIO_SET_MUTE, state)) < 0) {
		perror("AUDIO SET MUTE: ");
		return -1;
	}

	return 0;
}

int audioSetAVSync(int fd,boolean state)
{
	int ans;

	if ((ans = ioctl(fd,AUDIO_SET_AV_SYNC, state)) < 0) {
		perror("AUDIO SET AV SYNC: ");
		return -1;
	}

	return 0;
}

int audioSetBypassMode(int fd,boolean mode)
{
	int ans;

	if ((ans = ioctl(fd,AUDIO_SET_BYPASS_MODE, mode)) < 0) {
		printf("AUDIO SET BYPASS MODE not implemented?\n");
		return -1;
	}

	return 0;
}


int audioChannelSelect(int fd, audio_channel_select_t select)
{
	int ans;

	if ((ans = ioctl(fd,AUDIO_CHANNEL_SELECT, select)) < 0) {
		perror("AUDIO CHANNEL SELECT: ");
		return -1;
	}

	return 0;
}

int audioGetStatus(int fd)
{
	struct audio_status stat;
	int ans;

	if ((ans = ioctl(fd,AUDIO_GET_STATUS, &stat)) < 0) {
		perror("AUDIO GET STATUS: ");
		return -1;
	}

	printf("Audio Status:\n");
	printf("  Sync State          : %s\n",
	       (stat.AV_sync_state ? "SYNC" : "NO SYNC"));
	printf("  Mute State          : %s\n",
	       (stat.mute_state ? "muted" : "not muted"));
	printf("  Play State          : ");
	switch ((int)stat.play_state){
	case AUDIO_STOPPED:
		printf("STOPPED (%d)\n",stat.play_state);
		break;
	case AUDIO_PLAYING:
		printf("PLAYING (%d)\n",stat.play_state);
		break;
	case AUDIO_PAUSED:
		printf("PAUSED (%d)\n",stat.play_state);
		break;
	default:
		printf("unknown (%d)\n",stat.play_state);
		break;
	}

	printf("  Stream Source       : ");
	switch((int)stat.stream_source){
	case AUDIO_SOURCE_DEMUX:
		printf("DEMUX (%d)\n",stat.stream_source);
		break;
	case AUDIO_SOURCE_MEMORY:
		printf("MEMORY (%d)\n",stat.stream_source);
		break;
	default:
		printf("unknown (%d)\n",stat.stream_source);
		break;
	}

	printf("  Channel Select      : ");
	switch((int)stat.channel_select){
	case AUDIO_STEREO:
		printf("Stereo (%d)\n",stat.channel_select);
		break;
	case AUDIO_MONO_LEFT:
		printf("Mono left(%d)\n",stat.channel_select);
		break;
	case AUDIO_MONO_RIGHT:
		printf("Mono right (%d)\n",stat.channel_select);
		break;
	default:
		printf("unknown (%d)\n",stat.channel_select);
		break;
	}
	printf("  Bypass Mode         : %s\n",
	       (stat.bypass_mode ? "ON" : "OFF"));

	return 0;

}

#define BUFFY 100000
#define NFD   2
play_file_audio(int filefd, int fd)
{
	char buf[BUFFY];
	int count;
	int written;
	int ch;
	struct pollfd pfd[NFD];
	int stopped = 0;
	boolean mute = false;
	boolean sync = false;

	pfd[0].fd = STDIN_FILENO;
	pfd[0].events = POLLIN;

	pfd[1].fd = fd;
	pfd[1].events = POLLOUT;


	while ( (count = read(filefd,buf,BUFFY)) >= 0  ){
		written = 0;
		while(written < count){
			if (poll(pfd,NFD,1)){
				if (pfd[1].revents & POLLOUT){
					written += write(fd,buf+written,
							count-written);
				}
				if (pfd[0].revents & POLLIN){
					int c = getchar();
					switch(c){
					case 'z':
						audioPause(fd);
						printf("playback paused\n");
						stopped = 1;
						break;

					case 's':
						audioStop(fd);
						printf("playback stopped\n");
						stopped = 1;
						break;

					case 'c':
						audioContinue(fd);
						printf("playback continued\n");
						stopped = 0;
						break;

					case 'p':
						audioPlay(fd);
						printf("playback started\n");
						stopped = 0;
						break;

					case 'm':
						if (mute==false)mute=true;
						else mute=false;
						audioSetMute(fd,mute);
						printf("mute %d\n",mute);
						break;

					case 'a':
						if (sync==false)sync=true;
						else sync=false;
						audioSetAVSync(fd,sync);
						printf("AV sync %d\n",sync);
						stopped = 0;
						break;

					case 'q':
						audioContinue(fd);
						exit(0);
						break;
					}
				}

			}
		}
	}

}


main(int argc, char **argv)
{
	int fd;
	int filefd;
	boolean mute = false;
	boolean sync = false;

	if (argc < 2) return -1;

	if ( (filefd = open(argv[1],O_RDONLY)) < 0){
		perror("File open:");
		return -1;
	}

	if ((fd = open("/dev/ost/audio",O_RDWR|O_NONBLOCK)) < 0){
		perror("AUDIO DEVICE: ");
		return -1;
	}



	audioSetMute(fd,mute);
	audioSetBypassMode(fd,false);
	//audioContinue(fd);
	audioSelectSource(fd,AUDIO_SOURCE_MEMORY);
	audioPlay(fd);
	//sleep(4);
	//audioPause(fd);
	//sleep(3);
	//audioContinue(fd);
	//sleep(3);
	//audioStop(fd);
	//audioChannelSelect(fd,AUDIO_STEREO);
	//audioSetAVSync(fd,sync);
	audioGetStatus(fd);

	play_file_audio(filefd,fd);

	close(fd);
	return 0;


}