oss.c 13.5 KB
Newer Older
aymeric's avatar
aymeric committed
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
/*
mediastreamer2 library - modular sound and video processing and streaming
Copyright (C) 2006  Simon MORLAT (simon.morlat@linphone.org)

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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "mediastreamer2/mssndcard.h"
#include "mediastreamer2/msfilter.h"

#include <sys/soundcard.h>

#include <errno.h>
#include <assert.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <unistd.h>
31 32

#ifdef HAVE_ALLOCA_H
aymeric's avatar
aymeric committed
33
#include <alloca.h>
34
#endif
aymeric's avatar
aymeric committed
35 36 37 38

MSFilter *ms_oss_read_new(MSSndCard *card);
MSFilter *ms_oss_write_new(MSSndCard *card);

39 40

static int configure_fd(int fd, int bits,int stereo, int rate, int *minsz)
aymeric's avatar
aymeric committed
41 42 43
{
	int p=0,cond=0;
	int i=0;
44
	int min_size=0,blocksize=512;
aymeric's avatar
aymeric committed
45
	int err;
46

aymeric's avatar
aymeric committed
47 48 49 50 51 52 53
	//g_message("opening sound device");
	/* unset nonblocking mode */
	/* We wanted non blocking open but now put it back to normal ; thanks Xine !*/
	fcntl(fd, F_SETFL, fcntl(fd, F_GETFL)&~O_NONBLOCK);

	/* reset is maybe not needed but takes time*/
	/*ioctl(fd, SNDCTL_DSP_RESET, 0); */
54

aymeric's avatar
aymeric committed
55
	p=AFMT_S16_NE;
56

aymeric's avatar
aymeric committed
57 58 59 60 61
	err=ioctl(fd,SNDCTL_DSP_SETFMT,&p);
	if (err<0){
		ms_warning("oss_open: can't set sample format:%s.",strerror(errno));
	}

62

aymeric's avatar
aymeric committed
63 64 65 66 67 68 69 70 71 72 73
	p =  bits;  /* 16 bits */
	err=ioctl(fd, SNDCTL_DSP_SAMPLESIZE, &p);
	if (err<0){
		ms_warning("oss_open: can't set sample size to %i:%s.",bits,strerror(errno));
	}

	p =  rate;  /* rate in khz*/
	err=ioctl(fd, SNDCTL_DSP_SPEED, &p);
	if (err<0){
		ms_warning("oss_open: can't set sample rate to %i:%s.",rate,strerror(errno));
	}
74

aymeric's avatar
aymeric committed
75 76 77 78 79
	p =  stereo;  /* stereo or not */
	err=ioctl(fd, SNDCTL_DSP_STEREO, &p);
	if (err<0){
		ms_warning("oss_open: can't set mono/stereo mode:%s.",strerror(errno));
	}
80

aymeric's avatar
aymeric committed
81 82
	if (rate==16000) blocksize=4096;	/* oss emulation is not very good at 16khz */
	else blocksize=blocksize*(rate/8000);
83

aymeric's avatar
aymeric committed
84 85
	ioctl(fd, SNDCTL_DSP_GETBLKSIZE, &min_size);

86 87 88 89
	/**
	 * first try SNDCTL_DSP_SETFRAGMENT
	 */
	if (min_size>blocksize) {
90
		int size_selector=0;
91
		int frag;
92
		while ((blocksize >> size_selector) != 1)size_selector++; /*compute selector blocksize = 1<< size_selector*/
93
		frag = (2 << 16) | (size_selector);
94
		if (ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &frag) == -1) {
95
			ms_warning("This OSS driver does not support trying SNDCTL_DSP_SETFRAGMENT");
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
			ioctl(fd, SNDCTL_DSP_GETBLKSIZE, &min_size);

			/* try to subdivide BLKSIZE to reach block size if necessary */
			if (min_size>blocksize)
			{
				cond=1;
				p=min_size/blocksize;
				while(cond)
				{
					i=ioctl(fd, SNDCTL_DSP_SUBDIVIDE, &p);
					ms_message("subdivide bloc min_size [%i] block_size [%i]  said error=%i,errno=%i\n",min_size,blocksize,i,errno);
					if ((i!=0) || (p==1)) cond=0;
					else p=p/2;
				}
			}
			ioctl(fd, SNDCTL_DSP_GETBLKSIZE, &min_size);
		} else {
			/*it's working*/
			min_size=1 << (frag&0x0FFFF);
			ms_message("Max fragment=%x, size selector=%x block size=%i",frag>>16,frag&0x0FFFF,min_size);
aymeric's avatar
aymeric committed
116 117
		}
	}
118

aymeric's avatar
aymeric committed
119 120 121 122 123 124 125 126 127 128
	if (min_size>blocksize)
	{
		ms_warning("dsp block size set to %i.",min_size);
	}else{
		/* no need to access the card with less latency than needed*/
		min_size=blocksize;
	}

	ms_message("/dev/dsp opened: rate=%i,bits=%i,stereo=%i blocksize=%i.",
			rate,bits,stereo,min_size);
129

aymeric's avatar
aymeric committed
130 131 132
	/* start recording !!! Alex */
	{
		int fl,res;
133

aymeric's avatar
aymeric committed
134 135 136
		fl=PCM_ENABLE_OUTPUT|PCM_ENABLE_INPUT;
		res=ioctl(fd, SNDCTL_DSP_SETTRIGGER, &fl);
		if (res<0) ms_warning("OSS_TRIGGER: %s",strerror(errno));
137
	}
aymeric's avatar
aymeric committed
138 139 140 141
	*minsz=min_size;
	return fd;
}

142

aymeric's avatar
aymeric committed
143 144 145
typedef struct OssData{
	char *pcmdev;
	char *mixdev;
146 147
	int pcmfd_read;
	int pcmfd_write;
aymeric's avatar
aymeric committed
148 149 150 151 152 153 154 155 156 157 158
	int rate;
	int bits;
	ms_thread_t thread;
	ms_mutex_t mutex;
	queue_t rq;
	MSBufferizer * bufferizer;
	bool_t read_started;
	bool_t write_started;
	bool_t stereo;
} OssData;

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
static void oss_open(OssData* d, int *minsz){
	int fd=open(d->pcmdev,O_RDWR|O_NONBLOCK);
	if (fd>0) {
		d->pcmfd_read=d->pcmfd_write=configure_fd(fd, d->bits, d->stereo, d->rate, minsz);
		return ;
	}
	ms_warning ("Cannot open a single fd in rw mode for [%s] trying to open two",d->pcmdev);

	d->pcmfd_read=open(d->pcmdev,O_RDONLY|O_NONBLOCK);
	if (d->pcmfd_read > 0) {
		d->pcmfd_read=configure_fd(d->pcmfd_read, d->bits, d->stereo, d->rate, minsz);
	} else {
		ms_error("Cannot open fd in ro mode for [%s]",d->pcmdev);
	}
	d->pcmfd_write=open(d->pcmdev,O_WRONLY|O_NONBLOCK);
	if (d->pcmfd_write > 0) {
		d->pcmfd_write=configure_fd(d->pcmfd_write, d->bits, d->stereo, d->rate, minsz);
	} else {
		ms_error("Cannot open fd in wr mode for [%s]",d->pcmdev);
	}
	return ;
}

aymeric's avatar
aymeric committed
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
static void oss_set_level(MSSndCard *card, MSSndCardMixerElem e, int percent)
{
	OssData *d=(OssData*)card->data;
	int p,mix_fd;
	int osscmd;
	if (d->mixdev==NULL) return;
	switch(e){
		case MS_SND_CARD_MASTER:
			osscmd=SOUND_MIXER_VOLUME;
		break;
		case MS_SND_CARD_CAPTURE:
			osscmd=SOUND_MIXER_IGAIN;
		break;
		case MS_SND_CARD_PLAYBACK:
			osscmd=SOUND_MIXER_PCM;
		break;
		default:
			ms_warning("oss_card_set_level: unsupported command.");
			return;
	}
	p=(((int)percent)<<8 | (int)percent);
	mix_fd = open(d->mixdev, O_WRONLY);
	ioctl(mix_fd,MIXER_WRITE(osscmd), &p);
	close(mix_fd);
}

static int oss_get_level(MSSndCard *card, MSSndCardMixerElem e)
{
	OssData *d=(OssData*)card->data;
	int p=0,mix_fd;
	int osscmd;
	if (d->mixdev==NULL) return -1;
	switch(e){
		case MS_SND_CARD_MASTER:
			osscmd=SOUND_MIXER_VOLUME;
		break;
		case MS_SND_CARD_CAPTURE:
			osscmd=SOUND_MIXER_IGAIN;
		break;
		case MS_SND_CARD_PLAYBACK:
			osscmd=SOUND_MIXER_PCM;
		break;
		default:
			ms_warning("oss_card_get_level: unsupported command.");
			return -1;
	}
	mix_fd = open(d->mixdev, O_RDONLY);
	ioctl(mix_fd,MIXER_READ(osscmd), &p);
	close(mix_fd);
	return p>>8;
}

static void oss_set_source(MSSndCard *card, MSSndCardCapture source)
{
	OssData *d=(OssData*)card->data;
	int p=0;
	int mix_fd;
	if (d->mixdev==NULL) return;

	switch(source){
		case MS_SND_CARD_MIC:
			p = 1 << SOUND_MIXER_MIC;
		break;
		case MS_SND_CARD_LINE:
			p = 1 << SOUND_MIXER_LINE;
		break;
	}
249

aymeric's avatar
aymeric committed
250 251 252 253 254 255
	mix_fd = open(d->mixdev, O_WRONLY);
	ioctl(mix_fd, SOUND_MIXER_WRITE_RECSRC, &p);
	close(mix_fd);
}

static void oss_init(MSSndCard *card){
256
	OssData *d=ms_new0(OssData,1);
aymeric's avatar
aymeric committed
257 258
	d->pcmdev=NULL;
	d->mixdev=NULL;
259 260
	d->pcmfd_read=-1;
	d->pcmfd_write=-1;
aymeric's avatar
aymeric committed
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
	d->read_started=FALSE;
	d->write_started=FALSE;
	d->bits=16;
	d->rate=8000;
	d->stereo=FALSE;
	qinit(&d->rq);
	d->bufferizer=ms_bufferizer_new();
	ms_mutex_init(&d->mutex,NULL);
	card->data=d;
}

static void oss_uninit(MSSndCard *card){
	OssData *d=(OssData*)card->data;
	if (d->pcmdev!=NULL) ms_free(d->pcmdev);
	if (d->mixdev!=NULL) ms_free(d->mixdev);
	ms_bufferizer_destroy(d->bufferizer);
	flushq(&d->rq,0);
	ms_mutex_destroy(&d->mutex);
	ms_free(d);
}

#define DSP_NAME "/dev/dsp"
#define MIXER_NAME "/dev/mixer"

static void oss_detect(MSSndCardManager *m);

MSSndCardDesc oss_card_desc={
	.driver_type="OSS",
	.detect=oss_detect,
	.init=oss_init,
	.set_level=oss_set_level,
	.get_level=oss_get_level,
	.set_capture=oss_set_source,
	.create_reader=ms_oss_read_new,
	.create_writer=ms_oss_write_new,
296
	.uninit=oss_uninit
aymeric's avatar
aymeric committed
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
};

static MSSndCard *oss_card_new(const char *pcmdev, const char *mixdev){
	MSSndCard *card=ms_snd_card_new(&oss_card_desc);
	OssData *d=(OssData*)card->data;
	d->pcmdev=ms_strdup(pcmdev);
	d->mixdev=ms_strdup(mixdev);
	card->name=ms_strdup(pcmdev);
	return card;
}

static void oss_detect(MSSndCardManager *m){
	int i;
	char pcmdev[sizeof(DSP_NAME)+3];
	char mixdev[sizeof(MIXER_NAME)+3];
	if (access(DSP_NAME,F_OK)==0){
		MSSndCard *card=oss_card_new(DSP_NAME,MIXER_NAME);
		ms_snd_card_manager_add_card(m,card);
	}
	for(i=0;i<10;i++){
		snprintf(pcmdev,sizeof(pcmdev),"%s%i",DSP_NAME,i);
		snprintf(mixdev,sizeof(mixdev),"%s%i",MIXER_NAME,i);
		if (access(pcmdev,F_OK)==0){
320 321
			MSSndCard *card=oss_card_new(pcmdev,mixdev);
			ms_snd_card_manager_add_card(m,card);
aymeric's avatar
aymeric committed
322 323 324 325 326 327 328 329 330 331 332 333
		}
	}
}

static void * oss_thread(void *p){
	MSSndCard *card=(MSSndCard*)p;
	OssData *d=(OssData*)card->data;
	int bsize=0;
	uint8_t *rtmpbuff=NULL;
	uint8_t *wtmpbuff=NULL;
	int err;
	mblk_t *rm=NULL;
334
	bool_t did_read=FALSE;
335

336 337 338 339 340 341
	oss_open(d,&bsize);
	if (d->pcmfd_read>=0){
		rtmpbuff=(uint8_t*)alloca(bsize);
	}
	if (d->pcmfd_write>=0){
		wtmpbuff=(uint8_t*)alloca(bsize);
aymeric's avatar
aymeric committed
342 343
	}
	while(d->read_started || d->write_started){
344
		did_read=FALSE;
345
		if (d->pcmfd_read>=0){
aymeric's avatar
aymeric committed
346 347
			if (d->read_started){
				if (rm==NULL) rm=allocb(bsize,0);
348 349 350 351 352
				err=read(d->pcmfd_read,rm->b_wptr,bsize);
				if (err<0){
					ms_warning("Fail to read %i bytes from soundcard: %s",
					bsize,strerror(errno));
				}else{
353
					did_read=TRUE;
354
					rm->b_wptr+=err;
355
					ms_mutex_lock(&d->mutex);
356
					putq(&d->rq,rm);
357
					ms_mutex_unlock(&d->mutex);
358
					rm=NULL;
aymeric's avatar
aymeric committed
359 360
				}
			}else {
361
				/* case where we have no reader filtern the read is performed for synchronisation */
362
				int sz = read(d->pcmfd_read,rtmpbuff,bsize);
363 364
				if( sz==-1) ms_warning("sound device read error %s ",strerror(errno));
				else did_read=TRUE;
aymeric's avatar
aymeric committed
365
			}
366 367
		}
		if (d->pcmfd_write>=0){
aymeric's avatar
aymeric committed
368
			if (d->write_started){
369 370 371 372 373 374
				err=ms_bufferizer_read(d->bufferizer,wtmpbuff,bsize);
				if (err==bsize){
					err=write(d->pcmfd_write,wtmpbuff,bsize);
					if (err<0){
						ms_warning("Fail to write %i bytes from soundcard: %s",
						bsize,strerror(errno));
aymeric's avatar
aymeric committed
375 376 377 378 379
					}
				}
			}else {
				int sz;
				memset(wtmpbuff,0,bsize);
380
				sz = write(d->pcmfd_write,wtmpbuff,bsize);
aymeric's avatar
aymeric committed
381 382
				if( sz!=bsize) ms_warning("sound device write returned %i !",sz);
			}
383 384
		}
		if (!did_read) usleep(20000); /*avoid 100%cpu loop for nothing*/
aymeric's avatar
aymeric committed
385
	}
386
	if (d->pcmfd_read==d->pcmfd_write && d->pcmfd_read>=0 ) {
387 388 389 390 391 392 393 394 395 396 397
		close(d->pcmfd_read);
		d->pcmfd_read = d->pcmfd_write =-1;
	} else {
		if (d->pcmfd_read>=0) {
			close(d->pcmfd_read);
			d->pcmfd_read=-1;
		}
		if (d->pcmfd_write>=0) {
			close(d->pcmfd_write);
			d->pcmfd_write=-1;
		}
aymeric's avatar
aymeric committed
398
	}
399

aymeric's avatar
aymeric committed
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
	return NULL;
}

static void oss_start_r(MSSndCard *card){
	OssData *d=(OssData*)card->data;
	if (d->read_started==FALSE && d->write_started==FALSE){
		d->read_started=TRUE;
		ms_thread_create(&d->thread,NULL,oss_thread,card);
	}else d->read_started=TRUE;
}

static void oss_stop_r(MSSndCard *card){
	OssData *d=(OssData*)card->data;
	d->read_started=FALSE;
	if (d->write_started==FALSE){
		ms_thread_join(d->thread,NULL);
	}
}

static void oss_start_w(MSSndCard *card){
	OssData *d=(OssData*)card->data;
	if (d->read_started==FALSE && d->write_started==FALSE){
		d->write_started=TRUE;
		ms_thread_create(&d->thread,NULL,oss_thread,card);
	}else{
		d->write_started=TRUE;
	}
}

static void oss_stop_w(MSSndCard *card){
	OssData *d=(OssData*)card->data;
	d->write_started=FALSE;
	if (d->read_started==FALSE){
		ms_thread_join(d->thread,NULL);
	}
}

static mblk_t *oss_get(MSSndCard *card){
	OssData *d=(OssData*)card->data;
	mblk_t *m;
	ms_mutex_lock(&d->mutex);
	m=getq(&d->rq);
	ms_mutex_unlock(&d->mutex);
	return m;
}

static void oss_put(MSSndCard *card, mblk_t *m){
	OssData *d=(OssData*)card->data;
	ms_mutex_lock(&d->mutex);
	ms_bufferizer_put(d->bufferizer,m);
	ms_mutex_unlock(&d->mutex);
}


static void oss_read_preprocess(MSFilter *f){
	MSSndCard *card=(MSSndCard*)f->data;
	oss_start_r(card);
}

static void oss_read_postprocess(MSFilter *f){
	MSSndCard *card=(MSSndCard*)f->data;
	oss_stop_r(card);
}

static void oss_read_process(MSFilter *f){
	MSSndCard *card=(MSSndCard*)f->data;
	mblk_t *m;
	while((m=oss_get(card))!=NULL){
		ms_queue_put(f->outputs[0],m);
	}
}

static void oss_write_preprocess(MSFilter *f){
	MSSndCard *card=(MSSndCard*)f->data;
	oss_start_w(card);
}

static void oss_write_postprocess(MSFilter *f){
	MSSndCard *card=(MSSndCard*)f->data;
	oss_stop_w(card);
}

static void oss_write_process(MSFilter *f){
	MSSndCard *card=(MSSndCard*)f->data;
	mblk_t *m;
	while((m=ms_queue_get(f->inputs[0]))!=NULL){
		oss_put(card,m);
	}
}

static int set_rate(MSFilter *f, void *arg){
	MSSndCard *card=(MSSndCard*)f->data;
	OssData *d=(OssData*)card->data;
	d->rate=*((int*)arg);
	return 0;
}

497 498 499 500 501 502 503
static int get_rate(MSFilter *f, void *arg){
	MSSndCard *card=(MSSndCard*)f->data;
	OssData *d=(OssData*)card->data;
	*((int*)arg)=d->rate;
	return 0;
}

aymeric's avatar
aymeric committed
504 505 506 507 508 509 510 511 512
static int set_nchannels(MSFilter *f, void *arg){
	MSSndCard *card=(MSSndCard*)f->data;
	OssData *d=(OssData*)card->data;
	d->stereo=(*((int*)arg)==2);
	return 0;
}

static MSFilterMethod oss_methods[]={
	{	MS_FILTER_SET_SAMPLE_RATE	, set_rate	},
513
	{	MS_FILTER_GET_SAMPLE_RATE	, get_rate },
aymeric's avatar
aymeric committed
514 515 516 517 518 519 520
	{	MS_FILTER_SET_NCHANNELS		, set_nchannels	},
	{	0				, NULL		}
};

MSFilterDesc oss_read_desc={
	.id=MS_OSS_READ_ID,
	.name="MSOssRead",
521
	.text="Sound capture filter for OSS drivers",
aymeric's avatar
aymeric committed
522 523 524 525 526 527 528 529 530 531 532 533 534
	.category=MS_FILTER_OTHER,
	.ninputs=0,
	.noutputs=1,
	.preprocess=oss_read_preprocess,
	.process=oss_read_process,
	.postprocess=oss_read_postprocess,
	.methods=oss_methods
};


MSFilterDesc oss_write_desc={
	.id=MS_OSS_WRITE_ID,
	.name="MSOssWrite",
535
	.text="Sound playback filter for OSS drivers",
aymeric's avatar
aymeric committed
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
	.category=MS_FILTER_OTHER,
	.ninputs=1,
	.noutputs=0,
	.preprocess=oss_write_preprocess,
	.process=oss_write_process,
	.postprocess=oss_write_postprocess,
	.methods=oss_methods
};

MSFilter *ms_oss_read_new(MSSndCard *card){
	MSFilter *f=ms_filter_new_from_desc(&oss_read_desc);
	f->data=card;
	return f;
}


MSFilter *ms_oss_write_new(MSSndCard *card){
	MSFilter *f=ms_filter_new_from_desc(&oss_write_desc);
	f->data=card;
	return f;
}

MS_FILTER_DESC_EXPORT(oss_read_desc)
MS_FILTER_DESC_EXPORT(oss_write_desc)