audioconference.c 8.56 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
mediastreamer2 library - modular sound and video processing and streaming
Copyright (C) 2011 Belledonne Communications SARL
Author: 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/msconference.h"
#include "mediastreamer2/msaudiomixer.h"
Ghislain MARY's avatar
Ghislain MARY committed
23
#include "private.h"
24

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
struct _MSAudioConference{
	MSTicker *ticker;
	MSFilter *mixer;
	MSAudioConferenceParams params;
	int nmembers;
};

struct _MSAudioEndpoint{
	AudioStream *st;
	MSFilter *in_resampler,*out_resampler;
	MSCPoint out_cut_point;
	MSCPoint in_cut_point;
	MSCPoint in_cut_point_prev;
	MSCPoint mixer_in;
	MSCPoint mixer_out;
	MSAudioConference *conference;
41 42
	MSFilter *recorder; /* in case it is a recorder endpoint*/
	MSFilter *player; /* not used at the moment, but we need it so that there is a source connected to the mixer*/
43 44 45
	int pin;
	int samplerate;
};
46

47

Simon Morlat's avatar
Simon Morlat committed
48

49
MSAudioConference * ms_audio_conference_new(const MSAudioConferenceParams *params){
50
	MSAudioConference *obj=ms_new0(MSAudioConference,1);
Simon Morlat's avatar
Simon Morlat committed
51
	int tmp=1;
52
	obj->ticker=ms_ticker_new();
53 54
	ms_ticker_set_name(obj->ticker,"Audio conference MSTicker");
	ms_ticker_set_priority(obj->ticker,__ms_get_default_prio(FALSE));
55
	obj->mixer=ms_filter_new(MS_AUDIO_MIXER_ID);
56
	obj->params=*params;
Simon Morlat's avatar
Simon Morlat committed
57
	ms_filter_call_method(obj->mixer,MS_AUDIO_MIXER_ENABLE_CONFERENCE_MODE,&tmp);
58
	ms_filter_call_method(obj->mixer,MS_FILTER_SET_SAMPLE_RATE,&obj->params.samplerate);
59 60 61
	return obj;
}

62 63 64 65
const MSAudioConferenceParams *ms_audio_conference_get_params(MSAudioConference *obj){
	return &obj->params;
}

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
static MSCPoint just_before(MSFilter *f){
	MSQueue *q;
	MSCPoint pnull={0};
	if ((q=f->inputs[0])!=NULL){
		return q->prev;
	}
	ms_fatal("No filter before %s",f->desc->name);
	return pnull;
}

static MSCPoint just_after(MSFilter *f){
	MSQueue *q;
	MSCPoint pnull={0};
	if ((q=f->outputs[0])!=NULL){
		return q->next;
	}
	ms_fatal("No filter after %s",f->desc->name);
	return pnull;
}

Simon Morlat's avatar
Simon Morlat committed
86
static void cut_audio_stream_graph(MSAudioEndpoint *ep, bool_t is_remote){
87 88 89
	AudioStream *st=ep->st;

	/*stop the audio graph*/
Ghislain MARY's avatar
Ghislain MARY committed
90 91
	ms_ticker_detach(st->ms.ticker,st->soundread);
	if (!st->ec) ms_ticker_detach(st->ms.ticker,st->soundwrite);
92 93 94 95 96 97

	ep->in_cut_point_prev.pin=0;
	if (is_remote){
		/*we would like to keep the volrecv (MSVolume filter) in the graph to measure the output level*/
		ep->in_cut_point_prev.filter=st->volrecv;
	}else{
98
		ep->in_cut_point_prev.filter=st->plc ? st->plc : st->ms.decoder;
99 100 101
	}
	ep->in_cut_point=just_after(ep->in_cut_point_prev.filter);
	ms_filter_unlink(ep->in_cut_point_prev.filter,ep->in_cut_point_prev.pin,ep->in_cut_point.filter, ep->in_cut_point.pin);
102

Ghislain MARY's avatar
Ghislain MARY committed
103 104
	ep->out_cut_point=just_before(st->ms.encoder);
	ms_filter_unlink(ep->out_cut_point.filter,ep->out_cut_point.pin,st->ms.encoder,0);
Simon Morlat's avatar
Simon Morlat committed
105

Ghislain MARY's avatar
Ghislain MARY committed
106
	ms_filter_call_method(st->ms.rtpsend,MS_FILTER_GET_SAMPLE_RATE,&ep->samplerate);
107

Simon Morlat's avatar
Simon Morlat committed
108
	if (is_remote){
109 110
		ep->mixer_in.filter=ep->in_cut_point_prev.filter;
		ep->mixer_in.pin=ep->in_cut_point_prev.pin;
Ghislain MARY's avatar
Ghislain MARY committed
111
		ep->mixer_out.filter=st->ms.encoder;
Simon Morlat's avatar
Simon Morlat committed
112 113 114 115 116
		ep->mixer_out.pin=0;
	}else{
		ep->mixer_in=ep->out_cut_point;
		ep->mixer_out=ep->in_cut_point;
	}
117 118
}

Simon Morlat's avatar
Simon Morlat committed
119

120 121
static void redo_audio_stream_graph(MSAudioEndpoint *ep){
	AudioStream *st=ep->st;
122
	ms_filter_link(ep->in_cut_point_prev.filter,ep->in_cut_point_prev.pin,ep->in_cut_point.filter,ep->in_cut_point.pin);
Ghislain MARY's avatar
Ghislain MARY committed
123 124
	ms_filter_link(ep->out_cut_point.filter,ep->out_cut_point.pin,st->ms.encoder,0);
	ms_ticker_attach(st->ms.ticker,st->soundread);
125
	if (!st->ec)
Ghislain MARY's avatar
Ghislain MARY committed
126
		ms_ticker_attach(st->ms.ticker,st->soundwrite);
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
}

static int find_free_pin(MSFilter *mixer){
	int i;
	for(i=0;i<mixer->desc->ninputs;++i){
		if (mixer->inputs[i]==NULL){
			return i;
		}
	}
	ms_fatal("No more free pin in mixer filter");
	return -1;
}

static void plumb_to_conf(MSAudioEndpoint *ep){
	MSAudioConference *conf=ep->conference;
142
	int in_rate=ep->samplerate,out_rate=ep->samplerate;
143 144 145 146 147 148 149 150 151
	
	if (ep->samplerate!=-1){
		out_rate=in_rate=ep->samplerate;
	}else in_rate=out_rate=conf->params.samplerate;
	
	if (ep->recorder){
		ms_filter_call_method(ep->recorder,MS_FILTER_SET_SAMPLE_RATE,&conf->params.samplerate);
	}
	
152 153
	ep->pin=find_free_pin(conf->mixer);
	
154 155 156 157 158 159 160 161
	if (ep->mixer_in.filter){
		ms_filter_link(ep->mixer_in.filter,ep->mixer_in.pin,ep->in_resampler,0);
		ms_filter_link(ep->in_resampler,0,conf->mixer,ep->pin);
	}
	if (ep->mixer_out.filter){
		ms_filter_link(conf->mixer,ep->pin,ep->out_resampler,0);
		ms_filter_link(ep->out_resampler,0,ep->mixer_out.filter,ep->mixer_out.pin);
	}
162 163

	/*configure resamplers*/
164 165
	ms_filter_call_method(ep->in_resampler,MS_FILTER_SET_OUTPUT_SAMPLE_RATE,&conf->params.samplerate);
	ms_filter_call_method(ep->out_resampler,MS_FILTER_SET_SAMPLE_RATE,&conf->params.samplerate);
166 167 168
	ms_filter_call_method(ep->in_resampler,MS_FILTER_SET_SAMPLE_RATE,&in_rate);
	ms_filter_call_method(ep->out_resampler,MS_FILTER_SET_OUTPUT_SAMPLE_RATE,&out_rate);
	
169 170 171
}

void ms_audio_conference_add_member(MSAudioConference *obj, MSAudioEndpoint *ep){
Simon Morlat's avatar
Simon Morlat committed
172
	/* now connect to the mixer */
173 174 175 176 177 178 179 180 181 182
	ep->conference=obj;
	if (obj->nmembers>0) ms_ticker_detach(obj->ticker,obj->mixer);
	plumb_to_conf(ep);
	ms_ticker_attach(obj->ticker,obj->mixer);
	obj->nmembers++;
}

static void unplumb_from_conf(MSAudioEndpoint *ep){
	MSAudioConference *conf=ep->conference;
	
183 184 185 186 187 188 189 190
	if (ep->mixer_in.filter){
		ms_filter_unlink(ep->mixer_in.filter,ep->mixer_in.pin,ep->in_resampler,0);
		ms_filter_unlink(ep->in_resampler,0,conf->mixer,ep->pin);
	}
	if (ep->mixer_out.filter){
		ms_filter_unlink(conf->mixer,ep->pin,ep->out_resampler,0);
		ms_filter_unlink(ep->out_resampler,0,ep->mixer_out.filter,ep->mixer_out.pin);
	}
191 192 193 194 195 196 197 198 199 200 201
}

void ms_audio_conference_remove_member(MSAudioConference *obj, MSAudioEndpoint *ep){
	ms_ticker_detach(obj->ticker,obj->mixer);
	unplumb_from_conf(ep);
	ep->conference=NULL;
	obj->nmembers--;
	if (obj->nmembers>0) ms_ticker_attach(obj->ticker,obj->mixer);
}

void ms_audio_conference_mute_member(MSAudioConference *obj, MSAudioEndpoint *ep, bool_t muted){
202 203 204 205
	MSAudioMixerCtl ctl={0};
	ctl.pin=ep->pin;
	ctl.param.active=!muted;
	ms_filter_call_method(ep->conference->mixer, MS_AUDIO_MIXER_SET_ACTIVE, &ctl);
206 207
}

208 209 210 211 212
int ms_audio_conference_get_size(MSAudioConference *obj){
	return obj->nmembers;
}


213 214 215 216 217 218
void ms_audio_conference_destroy(MSAudioConference *obj){
	ms_ticker_destroy(obj->ticker);
	ms_filter_destroy(obj->mixer);
	ms_free(obj);
}

Simon Morlat's avatar
Simon Morlat committed
219
MSAudioEndpoint *ms_audio_endpoint_new(void){
220
	MSAudioEndpoint *ep=ms_new0(MSAudioEndpoint,1);
221 222
	ep->in_resampler=ms_filter_new(MS_RESAMPLE_ID);
	ep->out_resampler=ms_filter_new(MS_RESAMPLE_ID);
223
	ep->samplerate=8000;
224 225 226
	return ep;
}

Simon Morlat's avatar
Simon Morlat committed
227 228
MSAudioEndpoint * ms_audio_endpoint_get_from_stream(AudioStream *st, bool_t is_remote){
	MSAudioEndpoint *ep=ms_audio_endpoint_new();
229
	ep->st=st;
Simon Morlat's avatar
Simon Morlat committed
230 231 232 233 234 235 236 237 238
	cut_audio_stream_graph(ep,is_remote);
	return ep;
}

void ms_audio_endpoint_release_from_stream(MSAudioEndpoint *obj){
	redo_audio_stream_graph(obj);
	ms_audio_endpoint_destroy(obj);
}

239 240 241
void ms_audio_endpoint_destroy(MSAudioEndpoint *ep){
	if (ep->in_resampler) ms_filter_destroy(ep->in_resampler);
	if (ep->out_resampler) ms_filter_destroy(ep->out_resampler);
242 243
	if (ep->recorder) ms_filter_destroy(ep->recorder);
	if (ep->player) ms_filter_destroy(ep->player);
244 245 246
	ms_free(ep);
}

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
MSAudioEndpoint * ms_audio_endpoint_new_recorder(){
	MSAudioEndpoint *ep=ms_audio_endpoint_new();
	ep->recorder=ms_filter_new(MS_FILE_REC_ID);
	ep->player=ms_filter_new(MS_FILE_PLAYER_ID);
	ep->mixer_out.filter=ep->recorder;
	ep->mixer_in.filter=ep->player;
	ep->samplerate=-1;
	return ep;
}

int ms_audio_recorder_endpoint_start(MSAudioEndpoint *ep, const char *path){
	int err;
	MSRecorderState state;
	if (!ep->recorder){
		ms_error("This endpoint isn't a recorder endpoint.");
		return -1;
	}
	ms_filter_call_method(ep->recorder,MS_RECORDER_GET_STATE,&state);
	if (state!=MSRecorderClosed)
		ms_filter_call_method_noarg(ep->recorder,MS_RECORDER_CLOSE);
	err=ms_filter_call_method(ep->recorder,MS_RECORDER_OPEN,(void*)path);
	if (err==-1) return -1;
	return ms_filter_call_method_noarg(ep->recorder,MS_RECORDER_START);
}

int ms_audio_recorder_endpoint_stop(MSAudioEndpoint *ep){
	if (!ep->recorder){
		return -1;
	}
	return ms_filter_call_method_noarg(ep->recorder,MS_RECORDER_CLOSE);
Guillaume Beraudo's avatar
Guillaume Beraudo committed
277
}
278