audioconference.c 8.76 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"
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*/
90 91
	ms_ticker_detach(st->ms.sessions.ticker,st->soundread);
	if (!st->ec) ms_ticker_detach(st->ms.sessions.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

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

106 107
	if (ms_filter_has_method(st->ms.encoder,MS_FILTER_GET_SAMPLE_RATE)){
		ms_filter_call_method(st->ms.encoder,MS_FILTER_GET_SAMPLE_RATE,&ep->samplerate);
108 109 110
	}else{
		ms_filter_call_method(st->ms.rtpsend,MS_FILTER_GET_SAMPLE_RATE,&ep->samplerate);
	}
111

Simon Morlat's avatar
Simon Morlat committed
112
	if (is_remote){
113 114
		ep->mixer_in.filter=ep->in_cut_point_prev.filter;
		ep->mixer_in.pin=ep->in_cut_point_prev.pin;
115
		ep->mixer_out.filter=st->ms.encoder;
Simon Morlat's avatar
Simon Morlat committed
116 117 118 119 120
		ep->mixer_out.pin=0;
	}else{
		ep->mixer_in=ep->out_cut_point;
		ep->mixer_out=ep->in_cut_point;
	}
121 122
}

Simon Morlat's avatar
Simon Morlat committed
123

124 125
static void redo_audio_stream_graph(MSAudioEndpoint *ep){
	AudioStream *st=ep->st;
126
	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);
127
	ms_filter_link(ep->out_cut_point.filter,ep->out_cut_point.pin,st->ms.encoder,0);
128
	ms_ticker_attach(st->ms.sessions.ticker,st->soundread);
129
	if (!st->ec)
130
		ms_ticker_attach(st->ms.sessions.ticker,st->soundwrite);
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
}

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;
146
	int in_rate=ep->samplerate,out_rate=ep->samplerate;
147 148 149 150 151 152 153 154 155
	
	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);
	}
	
156 157
	ep->pin=find_free_pin(conf->mixer);
	
158 159 160 161 162 163 164 165
	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);
	}
166 167

	/*configure resamplers*/
168 169
	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);
170 171 172
	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);
	
173 174 175
}

void ms_audio_conference_add_member(MSAudioConference *obj, MSAudioEndpoint *ep){
Simon Morlat's avatar
Simon Morlat committed
176
	/* now connect to the mixer */
177 178 179 180 181 182 183 184 185 186
	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;
	
187 188 189 190 191 192 193 194
	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);
	}
195 196 197 198 199 200 201 202 203 204 205
}

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){
206 207 208 209
	MSAudioMixerCtl ctl={0};
	ctl.pin=ep->pin;
	ctl.param.active=!muted;
	ms_filter_call_method(ep->conference->mixer, MS_AUDIO_MIXER_SET_ACTIVE, &ctl);
210 211
}

212 213 214 215 216
int ms_audio_conference_get_size(MSAudioConference *obj){
	return obj->nmembers;
}


217 218 219 220 221 222
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
223
MSAudioEndpoint *ms_audio_endpoint_new(void){
224
	MSAudioEndpoint *ep=ms_new0(MSAudioEndpoint,1);
225 226
	ep->in_resampler=ms_filter_new(MS_RESAMPLE_ID);
	ep->out_resampler=ms_filter_new(MS_RESAMPLE_ID);
227
	ep->samplerate=8000;
228 229 230
	return ep;
}

Simon Morlat's avatar
Simon Morlat committed
231 232
MSAudioEndpoint * ms_audio_endpoint_get_from_stream(AudioStream *st, bool_t is_remote){
	MSAudioEndpoint *ep=ms_audio_endpoint_new();
233
	ep->st=st;
Simon Morlat's avatar
Simon Morlat committed
234 235 236 237 238 239 240 241 242
	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);
}

243 244 245
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);
246 247
	if (ep->recorder) ms_filter_destroy(ep->recorder);
	if (ep->player) ms_filter_destroy(ep->player);
248 249 250
	ms_free(ep);
}

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
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);
281
}
282