lsd.c 9.27 KB
Newer Older
Simon Morlat's avatar
Simon Morlat committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
linphone
Copyright (C) 2010 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
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
Simon Morlat's avatar
Simon Morlat committed
18 19 20 21 22 23 24
*/

/* Linphone Sound Daemon: is a lightweight utility to play sounds to speaker during a conversation.
 This is useful for embedded platforms, where sound apis are not performant enough to allow
 simultaneous sound access.
*/

25
#include "linphone/core_utils.h"
26
#include "private.h"
27
#include "mediastreamer2/msticker.h"
Simon Morlat's avatar
Simon Morlat committed
28
#include "mediastreamer2/mssndcard.h"
Simon Morlat's avatar
Simon Morlat committed
29 30 31
#include "mediastreamer2/msaudiomixer.h"
#include "mediastreamer2/mschanadapter.h"
#include "mediastreamer2/msfileplayer.h"
32
#include "mediastreamer2/msitc.h"
Simon Morlat's avatar
Simon Morlat committed
33

Simon Morlat's avatar
Simon Morlat committed
34

35
static struct _MSSndCard *linphone_sound_daemon_get_proxy_card(LinphoneSoundDaemon *obj);
Simon Morlat's avatar
Simon Morlat committed
36 37 38 39 40 41 42 43 44 45

#define MAX_BRANCHES 10


struct _LsdPlayer{
	struct _LinphoneSoundDaemon *lsd;
	MSFilter *player;
	MSFilter *rateconv;
	MSFilter *chanadapter;
	LsdEndOfPlayCallback eop_cb;
Simon Morlat's avatar
Simon Morlat committed
46
	int mixer_pin;
Simon Morlat's avatar
Simon Morlat committed
47
	void *user_data;
48 49
	bool_t loop;
	bool_t pad[3];
Simon Morlat's avatar
Simon Morlat committed
50 51 52 53 54 55 56
};

struct _LinphoneSoundDaemon {
	int out_rate;
	int out_nchans;
	MSFilter *mixer;
	MSFilter *soundout;
57 58
	MSTicker *ticker;
	MSSndCard *proxycard;
Simon Morlat's avatar
Simon Morlat committed
59 60 61
	LsdPlayer branches[MAX_BRANCHES];
};

62 63
static MSFilter *create_writer(MSSndCard *c){
	LinphoneSoundDaemon *lsd=(LinphoneSoundDaemon*)c->data;
64
	MSFilter *itcsink=ms_factory_create_filter(ms_snd_card_get_factory(c), MS_ITC_SINK_ID);
65 66
	ms_filter_call_method(itcsink,MS_ITC_SINK_CONNECT,lsd->branches[0].player);
	return itcsink;
67 68 69 70 71 72 73 74 75 76 77 78 79
}

static MSSndCardDesc proxycard={
	"Linphone Sound Daemon",
	/*detect*/ NULL,
	/*init*/ NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	/*create_reader*/ NULL,
	create_writer,
80 81 82 83
	NULL, /*uninit*/
	NULL, /* duplicate */
	NULL, /*unload */
	NULL /*usage_hint */
84 85
};

Simon Morlat's avatar
Simon Morlat committed
86 87
LsdPlayer *linphone_sound_daemon_get_player(LinphoneSoundDaemon *obj){
	int i;
88
	for(i=1;i<MAX_BRANCHES;++i){
Simon Morlat's avatar
Simon Morlat committed
89 90 91 92 93
		LsdPlayer *b=&obj->branches[i];
		MSFilter *p=b->player;
		int state;
		ms_filter_call_method(p,MS_PLAYER_GET_STATE,&state);
		if (state==MSPlayerClosed){
Simon Morlat's avatar
Simon Morlat committed
94
			lsd_player_set_gain(b,1);
95
			lsd_player_enable_loop (b,FALSE);
Simon Morlat's avatar
Simon Morlat committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
			return b;
		}
	}
	ms_warning("No more free players !");
	return NULL;
}

void linphone_sound_daemon_release_player(LinphoneSoundDaemon *obj, LsdPlayer * player){
	int state;
	ms_filter_call_method(player->player,MS_PLAYER_GET_STATE,&state);
	if (state!=MSPlayerClosed){
		ms_filter_call_method(player->player,MS_PLAYER_CLOSE,&state);
	}
}

Simon Morlat's avatar
Simon Morlat committed
111 112 113 114
LinphoneSoundDaemon *lsd_player_get_daemon(const LsdPlayer *p){
	return p->lsd;
}

115
LinphoneStatus lsd_player_stop(LsdPlayer *p){
Simon Morlat's avatar
Simon Morlat committed
116 117 118 119
	ms_filter_call_method_noarg(p->player,MS_PLAYER_PAUSE);
	return 0;
}

120
static void lsd_player_init(MSFactory* factory, LsdPlayer *p, MSConnectionPoint mixer, MSFilterId playerid, LinphoneSoundDaemon *lsd){
Simon Morlat's avatar
Simon Morlat committed
121
	MSConnectionHelper h;
122 123 124
	p->player=ms_factory_create_filter(factory, playerid);
	p->rateconv=ms_factory_create_filter(factory,MS_RESAMPLE_ID);
	p->chanadapter=ms_factory_create_filter(factory,MS_CHANNEL_ADAPTER_ID);
Simon Morlat's avatar
Simon Morlat committed
125
	
Simon Morlat's avatar
Simon Morlat committed
126 127 128 129 130
	ms_connection_helper_start(&h);
	ms_connection_helper_link(&h,p->player,-1,0);
	ms_connection_helper_link(&h,p->rateconv,0,0);
	ms_connection_helper_link(&h,p->chanadapter,0,0);
	ms_connection_helper_link(&h,mixer.filter,mixer.pin,-1);
Simon Morlat's avatar
Simon Morlat committed
131
	p->mixer_pin=mixer.pin;
132
	p->loop=FALSE;
Simon Morlat's avatar
Simon Morlat committed
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
	p->lsd=lsd;
}

static void lsd_player_uninit(LsdPlayer *p, MSConnectionPoint mixer){
	MSConnectionHelper h;

	ms_connection_helper_start(&h);
	ms_connection_helper_unlink (&h,p->player,-1,0);
	ms_connection_helper_unlink(&h,p->rateconv,0,0);
	ms_connection_helper_unlink(&h,p->chanadapter,0,0);
	ms_connection_helper_unlink(&h,mixer.filter,mixer.pin,-1);

	ms_filter_destroy(p->player);
	ms_filter_destroy(p->rateconv);
	ms_filter_destroy(p->chanadapter);
}

void lsd_player_set_callback(LsdPlayer *p, LsdEndOfPlayCallback cb){
	p->eop_cb=cb;
}

void lsd_player_set_user_pointer(LsdPlayer *p, void *up){
	p->user_data=up;
}

158
void *lsd_player_get_user_pointer(const LsdPlayer *p){
Simon Morlat's avatar
Simon Morlat committed
159 160 161
	return p->user_data;
}

162
static void lsd_player_on_eop(void * userdata, MSFilter *f, unsigned int id, void *arg){
163 164 165
	LsdPlayer *p=(LsdPlayer *)userdata;
	if (p->eop_cb!=NULL)
		p->eop_cb(p);
Simon Morlat's avatar
Simon Morlat committed
166 167
}

168
static void lsd_player_configure(LsdPlayer *b){
Simon Morlat's avatar
Simon Morlat committed
169 170
	int rate,chans;
	LinphoneSoundDaemon *lsd=b->lsd;
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187

	if (ms_filter_get_id(b->player)==MS_ITC_SOURCE_ID)
		ms_message("Configuring branch coming from audio call...");
	
	ms_filter_call_method(b->player,MS_FILTER_GET_SAMPLE_RATE,&rate);
	ms_filter_call_method(b->player,MS_FILTER_GET_NCHANNELS,&chans);
	
	
	ms_filter_call_method(b->rateconv,MS_FILTER_SET_SAMPLE_RATE,&rate);
	ms_filter_call_method(b->rateconv,MS_FILTER_SET_NCHANNELS,&chans);
	ms_filter_call_method(b->rateconv,MS_FILTER_SET_OUTPUT_SAMPLE_RATE,&lsd->out_rate);

	ms_filter_call_method(b->chanadapter,MS_FILTER_SET_NCHANNELS,&chans);
	ms_filter_call_method(b->chanadapter,MS_CHANNEL_ADAPTER_SET_OUTPUT_NCHANNELS,&lsd->out_nchans);
	ms_message("player configured for rate=%i, channels=%i",rate,chans);
}

188
LinphoneStatus lsd_player_play(LsdPlayer *b, const char *filename ){
189
	int state;
Simon Morlat's avatar
Simon Morlat committed
190 191 192 193 194 195 196
	
	ms_filter_call_method(b->player,MS_PLAYER_GET_STATE,&state);
	if (state!=MSPlayerClosed){
		ms_filter_call_method_noarg(b->player,MS_PLAYER_CLOSE);
	}
	
	if (ms_filter_call_method(b->player,MS_PLAYER_OPEN,(void*)filename)!=0){
197
		ms_warning("Could not play %s",filename);
Simon Morlat's avatar
Simon Morlat committed
198 199
		return -1;
	}
200
	ms_filter_add_notify_callback (b->player,lsd_player_on_eop,b,FALSE);
201
	lsd_player_configure(b);
202
	ms_filter_call_method_noarg (b->player,MS_PLAYER_START);
Simon Morlat's avatar
Simon Morlat committed
203 204 205
	return 0;
}

Simon Morlat's avatar
Simon Morlat committed
206 207 208 209
void lsd_player_enable_loop(LsdPlayer *p, bool_t loopmode){
	if (ms_filter_get_id(p->player)==MS_FILE_PLAYER_ID){
		int arg=loopmode ? 0 : -1;
		ms_filter_call_method(p->player,MS_FILE_PLAYER_LOOP,&arg);
210
		p->loop=loopmode;
Simon Morlat's avatar
Simon Morlat committed
211 212 213
	}
}

214 215 216 217
bool_t lsd_player_loop_enabled(const LsdPlayer *p){
	return p->loop;
}

Simon Morlat's avatar
Simon Morlat committed
218 219 220
void lsd_player_set_gain(LsdPlayer *p, float gain){
	MSAudioMixerCtl gainctl;
	gainctl.pin=p->mixer_pin;
221
	gainctl.param.gain=gain;
Simon Morlat's avatar
Simon Morlat committed
222 223
	ms_filter_call_method(p->lsd->mixer,MS_AUDIO_MIXER_SET_INPUT_GAIN,&gainctl);
}
Simon Morlat's avatar
Simon Morlat committed
224

225
LinphoneSoundDaemon * linphone_sound_daemon_new(MSFactory* factory, const char *cardname, int rate, int nchannels){
Simon Morlat's avatar
Simon Morlat committed
226 227 228
	int i;
	MSConnectionPoint mp;
	LinphoneSoundDaemon *lsd;
229
	MSSndCard *card=ms_snd_card_manager_get_card(ms_factory_get_snd_card_manager(factory),
Simon Morlat's avatar
Simon Morlat committed
230 231 232
	                                             cardname);
	if (card==NULL){
		card=ms_snd_card_manager_get_default_playback_card (
233
		                                                   ms_factory_get_snd_card_manager(factory));
Simon Morlat's avatar
Simon Morlat committed
234 235 236 237 238 239 240 241
		if (card==NULL){
			ms_error("linphone_sound_daemon_new(): No playback soundcard available");
			return NULL;
		}
	}
	
	lsd=ms_new0(LinphoneSoundDaemon,1);
	lsd->soundout=ms_snd_card_create_writer(card);
242
	lsd->mixer=ms_factory_create_filter(ms_snd_card_get_factory(card),MS_AUDIO_MIXER_ID);
243 244
	lsd->out_rate=rate;
	lsd->out_nchans=nchannels;
Simon Morlat's avatar
Simon Morlat committed
245 246
	ms_filter_call_method(lsd->soundout,MS_FILTER_SET_SAMPLE_RATE,&lsd->out_rate);
	ms_filter_call_method(lsd->soundout,MS_FILTER_SET_NCHANNELS,&lsd->out_nchans);
247 248
	ms_filter_call_method(lsd->mixer,MS_FILTER_SET_SAMPLE_RATE,&lsd->out_rate);
	ms_filter_call_method(lsd->mixer,MS_FILTER_SET_NCHANNELS,&lsd->out_nchans);
Simon Morlat's avatar
Simon Morlat committed
249

Simon Morlat's avatar
Simon Morlat committed
250
	mp.filter=lsd->mixer;
Simon Morlat's avatar
Simon Morlat committed
251 252
	mp.pin=0;

253
	lsd_player_init(factory, &lsd->branches[0],mp,MS_ITC_SOURCE_ID,lsd);
254
	ms_filter_add_notify_callback(lsd->branches[0].player,(MSFilterNotifyFunc)lsd_player_configure,&lsd->branches[0],FALSE);
Simon Morlat's avatar
Simon Morlat committed
255 256
	for(i=1;i<MAX_BRANCHES;++i){
		mp.pin=i;
257
		lsd_player_init(factory,&lsd->branches[i],mp,MS_FILE_PLAYER_ID,lsd);
Simon Morlat's avatar
Simon Morlat committed
258
	}
259 260 261 262 263 264
	ms_filter_link(lsd->mixer,0,lsd->soundout,0);
	lsd->ticker=ms_ticker_new();
	ms_ticker_attach(lsd->ticker,lsd->soundout);

	lsd->proxycard=ms_snd_card_new(&proxycard);
	lsd->proxycard->data=lsd;
Simon Morlat's avatar
Simon Morlat committed
265
	ms_message("LinphoneSoundDaemon started with rate=%i, nchannels=%i",rate,nchannels);
Simon Morlat's avatar
Simon Morlat committed
266 267 268
	return lsd;
}

269 270 271 272 273 274 275
void linphone_sound_daemon_stop_all_players(LinphoneSoundDaemon *obj){
	int i;
	for(i=1;i<MAX_BRANCHES;++i){
		lsd_player_stop(&obj->branches[i]);
	}
}

276 277 278 279 280 281 282
void linphone_sound_daemon_release_all_players(LinphoneSoundDaemon *obj){
	int i;
	for(i=1;i<MAX_BRANCHES;++i){
		linphone_sound_daemon_release_player(obj,&obj->branches[i]);
	}
}

283 284 285 286 287 288 289
void linphone_sound_daemon_destroy(LinphoneSoundDaemon *obj){
	int i;
	MSConnectionPoint mp;
	ms_ticker_detach(obj->ticker,obj->soundout);
	mp.filter=obj->mixer;
	for(i=0;i<MAX_BRANCHES;++i){
		mp.pin=i;
290
		if (i!=0) linphone_sound_daemon_release_player(obj,&obj->branches[i]);
291 292
		lsd_player_uninit (&obj->branches[i],mp);
	}
293
	ms_filter_unlink(obj->mixer,0,obj->soundout,0);
294 295 296 297 298 299 300 301
	ms_ticker_destroy(obj->ticker);
	ms_filter_destroy(obj->soundout);
	ms_filter_destroy(obj->mixer);
}

MSSndCard *linphone_sound_daemon_get_proxy_card(LinphoneSoundDaemon *lsd){
	return lsd->proxycard;
}
302 303

void linphone_core_use_sound_daemon(LinphoneCore *lc, LinphoneSoundDaemon *lsd){
Simon Morlat's avatar
Simon Morlat committed
304 305 306 307 308
	if (lsd!=NULL){
		lc->sound_conf.lsd_card=linphone_sound_daemon_get_proxy_card (lsd);
	}else {
		lc->sound_conf.lsd_card=NULL;
	}
309
}