audio-mixer.cpp 7.16 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 23 24 25 26 27 28 29 30 31 32
/*
 * Copyright (c) 2010-2020 Belledonne Communications SARL.
 *
 * This file is part of Liblinphone.
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include "streams.h"
#include "mixers.h"

#include "linphone/core.h"
#include "private.h"

#include "mediastreamer2/msvolume.h"

LINPHONE_BEGIN_NAMESPACE

MS2AudioMixer::MS2AudioMixer(MixerSession &session) : StreamMixer(session){
	MSAudioConferenceParams ms_conf_params;
	ms_conf_params.samplerate = lp_config_get_int(mSession.getCCore()->config, "sound", "conference_rate", 16000);
Simon Morlat's avatar
Simon Morlat committed
33 34
	ms_conf_params.active_talker_callback = &MS2AudioMixer::sOnActiveTalkerChanged;
	ms_conf_params.user_data = this;
35 36 37 38
	mConference = ms_audio_conference_new(&ms_conf_params, mSession.getCCore()->factory);
}

MS2AudioMixer::~MS2AudioMixer(){
Simon Morlat's avatar
Simon Morlat committed
39 40 41
	if (mTimer){
		mSession.getCore().destroyTimer(mTimer);
	}
42 43 44 45 46 47 48 49 50
	if (mRecordEndpoint) {
		stopRecording();
	}
	if (mLocalEndpoint){
		removeLocalParticipant();
	}
	ms_audio_conference_destroy(mConference);
}

Simon Morlat's avatar
Simon Morlat committed
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
void MS2AudioMixer::addListener(AudioMixerListener *listener){
	if (mTimer == nullptr){
		// Start the monitoring of the active talker since somebody wants this information.
		mTimer = mSession.getCore().createTimer([this]() -> bool{
				ms_audio_conference_process_events(mConference);
				return true;
			}, 50, "AudioConference events timer");
	}
	mListeners.push_back(listener);
}

void MS2AudioMixer::removeListener(AudioMixerListener *listener){
	mListeners.remove(listener);
}

void MS2AudioMixer::sOnActiveTalkerChanged(MSAudioConference *audioconf, MSAudioEndpoint *ep){
	const MSAudioConferenceParams *params = ms_audio_conference_get_params(audioconf);
	MS2AudioMixer *zis = static_cast<MS2AudioMixer*>(params->user_data);
	zis->onActiveTalkerChanged(ep);
}

void MS2AudioMixer::onActiveTalkerChanged(MSAudioEndpoint *ep){
	StreamsGroup *sg = (StreamsGroup*)ms_audio_endpoint_get_user_data(ep);
	for (auto & l : mListeners){
		l->onActiveTalkerChanged(sg);
	}
}

void MS2AudioMixer::connectEndpoint(Stream *as, MSAudioEndpoint *endpoint, bool muted){
	ms_audio_endpoint_set_user_data(endpoint, &as->getGroup());
81 82 83 84
	ms_audio_conference_add_member(mConference, endpoint);
	ms_audio_conference_mute_member(mConference, endpoint, muted);
}

Simon Morlat's avatar
Simon Morlat committed
85 86
void MS2AudioMixer::disconnectEndpoint(Stream *as, MSAudioEndpoint *endpoint){
	ms_audio_endpoint_set_user_data(endpoint, nullptr);
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
	ms_audio_conference_remove_member(mConference, endpoint);
}


RtpProfile *MS2AudioMixer::sMakeDummyProfile(int samplerate) {
	RtpProfile *prof = rtp_profile_new("dummy");
	PayloadType *pt = payload_type_clone(&payload_type_l16_mono);
	pt->clock_rate = samplerate;
	rtp_profile_set_payload(prof, 0, pt);
	return prof;
}

void MS2AudioMixer::addLocalParticipant(){
	LinphoneCore *core = getSession().getCCore();
	AudioStream *st = audio_stream_new(core->factory, 65000, 65001, FALSE);
	MSSndCard *playcard = core->sound_conf.lsd_card
		? core->sound_conf.lsd_card
		: core->sound_conf.play_sndcard;
	MSSndCard *captcard = core->sound_conf.capt_sndcard;
	const MSAudioConferenceParams *params = ms_audio_conference_get_params(mConference);
	mLocalDummyProfile = sMakeDummyProfile(params->samplerate);
	audio_stream_start_full(st, mLocalDummyProfile,
				"127.0.0.1",
				65000,
				"127.0.0.1",
				65001,
				0,
				40,
				nullptr,
				nullptr,
				playcard,
				captcard,
				linphone_core_echo_cancellation_enabled(core)
				);
	_post_configure_audio_stream(st, core, FALSE);
	mLocalParticipantStream = st;
	mLocalEndpoint = ms_audio_endpoint_get_from_stream(st, FALSE);
	ms_message("Conference: adding local endpoint");
	ms_audio_conference_add_member(mConference, mLocalEndpoint);
}

void MS2AudioMixer::removeLocalParticipant(){
	if (mLocalEndpoint) {
		ms_audio_conference_remove_member(mConference, mLocalEndpoint);
		ms_audio_endpoint_release_from_stream(mLocalEndpoint);
		mLocalEndpoint = nullptr;
		audio_stream_stop(mLocalParticipantStream);
		mLocalParticipantStream = nullptr;
		rtp_profile_destroy(mLocalDummyProfile);
		mLocalDummyProfile = nullptr;
	}
}

void MS2AudioMixer::enableLocalParticipant(bool value){
	/* Create a dummy audiostream in order to extract the local part of it */
	/* network address and ports have no meaning and are not used here. */
	if (value && !mLocalParticipantStream){
		addLocalParticipant();
	}else if (!value && mLocalParticipantStream){
		removeLocalParticipant();
	}
}

void MS2AudioMixer::setRecordPath(const std::string &path){
	mRecordPath = path;
}

void MS2AudioMixer::enableMic(bool value){
	mLocalMicEnabled = value;
Simon Morlat's avatar
Simon Morlat committed
156 157
	if (mLocalEndpoint)
		ms_audio_conference_mute_member(mConference, mLocalEndpoint, !value);
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
}

bool MS2AudioMixer::micEnabled()const{
	return mLocalMicEnabled;
}

void MS2AudioMixer::enableSpeaker(bool value){
}

bool MS2AudioMixer::speakerEnabled()const{
	return false;
}

void MS2AudioMixer::startRecording(){
	if (mRecordPath.empty()){
		lError() << "MS2AudioMixer:startRecording(): no path set.";
		return;
	}
	if (!mRecordEndpoint) {
		mRecordEndpoint = ms_audio_endpoint_new_recorder(getSession().getCCore()->factory);
		ms_audio_conference_add_member(mConference, mRecordEndpoint);
	}
	ms_audio_recorder_endpoint_start(mRecordEndpoint, mRecordPath.c_str());
}

void MS2AudioMixer::stopRecording(){
	if (!mRecordEndpoint) {
		lWarning() << "MS2AudioMixer::stopRecording(): no record currently active";
		return ;
	}
	ms_audio_recorder_endpoint_stop(mRecordEndpoint);
	ms_audio_conference_remove_member(mConference, mRecordEndpoint);
	ms_audio_endpoint_destroy(mRecordEndpoint);
	mRecordEndpoint = nullptr;
}

bool MS2AudioMixer::isRecording(){
	return mRecordEndpoint != nullptr; 
}

float MS2AudioMixer::getPlayVolume(){
	AudioStream *st = mLocalParticipantStream;
	if (st && st->volrecv) {
		float vol = 0;
		ms_filter_call_method(st->volrecv, MS_VOLUME_GET, &vol);
		return vol;
	}
	return LINPHONE_VOLUME_DB_LOWEST;
	
}

float MS2AudioMixer::getRecordVolume(){
	AudioStream *st = mLocalParticipantStream;
	if (st && st->volsend && mLocalMicEnabled) {
		float vol = 0;
		ms_filter_call_method(st->volsend, MS_VOLUME_GET, &vol);
		return vol;
	}
	return LINPHONE_VOLUME_DB_LOWEST;
}

float MS2AudioMixer::getMicGain(){
	return 0.0;
}

void MS2AudioMixer::setMicGain(float value){
}

float MS2AudioMixer::getSpeakerGain(){
	return 0.0;
}

void MS2AudioMixer::setSpeakerGain(float value){
}

void MS2AudioMixer::setRoute(LinphoneAudioRoute route){
}

void MS2AudioMixer::sendDtmf(int dtmf){
}

void MS2AudioMixer::enableEchoCancellation(bool value){
}

bool MS2AudioMixer::echoCancellationEnabled()const{
	return linphone_core_echo_cancellation_enabled(getSession().getCCore());
}

Simon Morlat's avatar
Simon Morlat committed
246 247 248
AudioStream * MS2AudioMixer::getAudioStream(){
	return mLocalParticipantStream;
}
249 250

LINPHONE_END_NAMESPACE