daemon.cc 31.9 KB
Newer Older
Sandrine Avakian's avatar
Sandrine Avakian committed
/*
 * Copyright (c) 2010-2022 Belledonne Communications SARL.
 * (see https://gitlab.linphone.org/BC/public/liblinphone).
Simon Morlat's avatar
Simon Morlat committed
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
Simon Morlat's avatar
Simon Morlat committed
 *
 * 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 Affero General Public License for more details.
 * You should have received a copy of the GNU Affero General Public License
Simon Morlat's avatar
Simon Morlat committed
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
Sandrine Avakian's avatar
Sandrine Avakian committed

Ghislain MARY's avatar
Ghislain MARY committed
#include <cstdio>
#ifndef _WIN32
Ghislain MARY's avatar
Ghislain MARY committed
#include <sys/ioctl.h>
Yann Diorcet's avatar
Yann Diorcet committed
#include <algorithm>
#include <functional>
Ghislain MARY's avatar
Ghislain MARY committed
#include <limits>
#include <readline/history.h>
#ifndef _WIN32
#include <poll.h>
Yann Diorcet's avatar
Yann Diorcet committed

#include "commands/adaptive-jitter-compensation.h"
#include "commands/answer.h"
#include "commands/audio-codec-get.h"
#include "commands/audio-codec-move.h"
Yann Diorcet's avatar
Yann Diorcet committed
#include "commands/audio-codec-set.h"
#include "commands/audio-codec-toggle.h"
#include "commands/audio-stream-start.h"
Guillaume Beraudo's avatar
Guillaume Beraudo committed
#include "commands/audio-stream-stats.h"
#include "commands/auth-infos-clear.h"
#include "commands/call-mute.h"
#include "commands/call-resume.h"
#include "commands/call-stats.h"
#include "commands/call-status.h"
#include "commands/call-transfer.h"
#include "commands/call.h"
#include "commands/cn.h"
#include "commands/conference.h"
#include "commands/dtmf.h"
#include "commands/echo.h"
#include "commands/firewall-policy.h"
#include "commands/help.h"
Ghislain MARY's avatar
Ghislain MARY committed
#include "commands/ipv6.h"
#include "commands/media-encryption.h"
#include "commands/msfilter-add-fmtp.h"
Ghislain MARY's avatar
Ghislain MARY committed
#include "commands/play-wav.h"
#include "commands/pop-event.h"
Ghislain MARY's avatar
Ghislain MARY committed
#include "commands/port.h"
#include "commands/ptime.h"
#include "commands/register-status.h"
#include "commands/terminate.h"
#include "commands/unregister.h"
Ghislain MARY's avatar
Ghislain MARY committed
#include "commands/version.h"
#include "commands/video.h"
#include "daemon.h"
Guillaume Beraudo's avatar
Guillaume Beraudo committed
#include "private.h"
using namespace std;
Yann Diorcet's avatar
Yann Diorcet committed

#define INT_TO_VOIDPTR(i) ((void *)(intptr_t)(i))
Ghislain MARY's avatar
Ghislain MARY committed
#define VOIDPTR_TO_INT(p) ((int)(intptr_t)(p))

#ifndef WIN32
#else
#include <windows.h>
void usleep(int waitTime) {
}
#endif

#ifdef HAVE_READLINE
#define LICENCE_GPL
#else
#define LICENCE_COMMERCIAL
#endif

const char *const ice_state_str[] = {
    "Not activated",        /* LinphoneIceStateNotActivated */
    "Failed",               /* LinphoneIceStateFailed */
    "In progress",          /* LinphoneIceStateInProgress */
    "Host connection",      /* LinphoneIceStateHostConnection */
    "Reflexive connection", /* LinphoneIceStateReflexiveConnection */
    "Relayed connection"    /* LinphoneIceStateRelayConnection */
void *Daemon::iterateThread(void *arg) {
	while (daemon->mRunning) {
		ms_mutex_lock(&daemon->mMutex);
		daemon->iterate();
		ms_mutex_unlock(&daemon->mMutex);
		usleep(20000);
	}
	return 0;
}

CallEvent::CallEvent(Daemon *daemon, LinphoneCall *call, LinphoneCallState state) : Event("call-state-changed") {
	LinphoneCallLog *callLog = linphone_call_get_call_log(call);
Simon Morlat's avatar
Simon Morlat committed
	const LinphoneAddress *fromAddr = linphone_call_log_get_from_address(callLog);
Yann Diorcet's avatar
Yann Diorcet committed
	ostringstream ostr;
	ostr << "Event: " << linphone_call_state_to_string(state) << "\n";
	ostr << "From: " << fromStr << "\n";
Yann Diorcet's avatar
Yann Diorcet committed
	ostr << "Id: " << daemon->updateCallId(call) << "\n";
Yann Diorcet's avatar
Yann Diorcet committed
}

DtmfEvent::DtmfEvent(Daemon *daemon, LinphoneCall *call, int dtmf) : Event("receiving-tone") {
Yann Diorcet's avatar
Yann Diorcet committed
	ostringstream ostr;
	char *remote = linphone_call_get_remote_address_as_string(call);
Yann Diorcet's avatar
Yann Diorcet committed
	ostr << "From: " << remote << "\n";
Yann Diorcet's avatar
Yann Diorcet committed
	ostr << "Id: " << daemon->updateCallId(call) << "\n";
Yann Diorcet's avatar
Yann Diorcet committed
	ms_free(remote);
}

Guillaume Beraudo's avatar
Guillaume Beraudo committed
static ostream &printCallStatsHelper(ostream &ostr, const LinphoneCallStats *stats, const string &prefix) {
	ostr << prefix << "ICE state: " << ice_state_str[linphone_call_stats_get_ice_state(stats)] << "\n";
	ostr << prefix << "RoundTripDelay: " << linphone_call_stats_get_round_trip_delay(stats) << "\n";
	//	ostr << prefix << "Jitter: " << stats->jitter_stats.jitter << "\n";
	//	ostr << prefix << "MaxJitter: " << stats->jitter_stats.max_jitter << "\n";
	//	ostr << prefix << "SumJitter: " << stats->jitter_stats.sum_jitter << "\n";
	//	ostr << prefix << "MaxJitterTs: " << stats->jitter_stats.max_jitter_ts << "\n";
	ostr << prefix << "JitterBufferSizeMs: " << linphone_call_stats_get_jitter_buffer_size_ms(stats) << "\n";
	ostr << prefix << "Received-InterarrivalJitter: " << linphone_call_stats_get_receiver_interarrival_jitter(stats)
	     << "\n";
	ostr << prefix << "Received-FractionLost: " << linphone_call_stats_get_receiver_loss_rate(stats) << "\n";
	ostr << prefix << "Sent-InterarrivalJitter: " << linphone_call_stats_get_sender_interarrival_jitter(stats) << "\n";
	ostr << prefix << "Sent-FractionLost: " << linphone_call_stats_get_sender_loss_rate(stats) << "\n";
Guillaume Beraudo's avatar
Guillaume Beraudo committed
	return ostr;
}

CallStatsEvent::CallStatsEvent(Daemon *daemon, LinphoneCall *call, const LinphoneCallStats *stats)
    : Event("call-stats") {
Guillaume Beraudo's avatar
Guillaume Beraudo committed
	const LinphoneCallParams *callParams = linphone_call_get_current_params(call);
	const char *prefix = "";

	ostringstream ostr;
	ostr << "Id: " << daemon->updateCallId(call) << "\n";
	ostr << "Type: ";
	if (linphone_call_stats_get_type(stats) == LINPHONE_CALL_STATS_AUDIO) {
		ostr << "Audio";
Guillaume Beraudo's avatar
Guillaume Beraudo committed
	} else {
Guillaume Beraudo's avatar
Guillaume Beraudo committed
	}
Guillaume Beraudo's avatar
Guillaume Beraudo committed
	printCallStatsHelper(ostr, stats, prefix);
	if (linphone_call_stats_get_type(stats) == LINPHONE_CALL_STATS_AUDIO) {
		const LinphonePayloadType *audioCodec = linphone_call_params_get_used_audio_payload_type(callParams);
Yann Diorcet's avatar
Yann Diorcet committed
		ostr << PayloadTypeResponse(linphone_call_get_core(call), audioCodec, -1, prefix, false).getBody() << "\n";
	} else {
		const LinphonePayloadType *videoCodec = linphone_call_params_get_used_video_payload_type(callParams);
Yann Diorcet's avatar
Yann Diorcet committed
		ostr << PayloadTypeResponse(linphone_call_get_core(call), videoCodec, -1, prefix, false).getBody() << "\n";
AudioStreamStatsEvent::AudioStreamStatsEvent(Daemon *daemon, AudioStream *stream, const LinphoneCallStats *stats)
    : Event("audio-stream-stats") {
Guillaume Beraudo's avatar
Guillaume Beraudo committed
	const char *prefix = "";
Guillaume Beraudo's avatar
Guillaume Beraudo committed
	ostringstream ostr;
	ostr << "Id: " << daemon->updateAudioStreamId(stream) << "\n";
	ostr << "Type: ";
	if (linphone_call_stats_get_type(stats) == LINPHONE_CALL_STATS_AUDIO) {
		ostr << "Audio";
Guillaume Beraudo's avatar
Guillaume Beraudo committed
	} else {
Guillaume Beraudo's avatar
Guillaume Beraudo committed
	}
Guillaume Beraudo's avatar
Guillaume Beraudo committed
	printCallStatsHelper(ostr, stats, prefix);
CallPlayingStatsEvent::CallPlayingStatsEvent(BCTBX_UNUSED(Daemon *daemon), int id) : Event("call-playing-complete") {
	ostringstream ostr;
	ostr << "Id: " << id << "\n";

PayloadTypeResponse::PayloadTypeResponse(BCTBX_UNUSED(LinphoneCore *core),
                                         const LinphonePayloadType *payloadType,
                                         int index,
                                         const string &prefix,
                                         bool enabled_status) {
Yann Diorcet's avatar
Yann Diorcet committed
	ostringstream ostr;
Yann Diorcet's avatar
Yann Diorcet committed
	if (payloadType != NULL) {
		if (index >= 0) ostr << prefix << "Index: " << index << "\n";
		const char *recv_fmtp = linphone_payload_type_get_recv_fmtp(payloadType);
		const char *send_fmtp = linphone_payload_type_get_send_fmtp(payloadType);
		ostr << prefix << "Payload-type-number: " << linphone_payload_type_get_number(payloadType) << "\n";
		ostr << prefix << "Clock-rate: " << linphone_payload_type_get_clock_rate(payloadType) << "\n";
		ostr << prefix << "Bitrate: " << linphone_payload_type_get_normal_bitrate(payloadType) << "\n";
		ostr << prefix << "Mime: " << linphone_payload_type_get_mime_type(payloadType) << "\n";
		ostr << prefix << "Channels: " << linphone_payload_type_get_channels(payloadType) << "\n";
		ostr << prefix << "Recv-fmtp: " << (recv_fmtp ? recv_fmtp : "") << "\n";
		ostr << prefix << "Send-fmtp: " << (send_fmtp ? send_fmtp : "") << "\n";
Yann Diorcet's avatar
Yann Diorcet committed
		if (enabled_status)
			ostr << prefix << "Enabled: " << (linphone_payload_type_enabled(payloadType) == TRUE ? "true" : "false")
Yann Diorcet's avatar
Yann Diorcet committed
		setBody(ostr.str().c_str());
	}
PayloadTypeParser::PayloadTypeParser(LinphoneCore *core, const string &mime_type, bool accept_all)
    : mAll(false), mSuccesful(true), mPayloadType(NULL), mPosition(-1) {
	int number = -1;
	if (accept_all && (mime_type.compare("ALL") == 0)) {
		mAll = true;
		return;
	}
		if (sscanf(mime_type.c_str(), "%63[^/]/%u/%u", type, &rate, &channels) != 3) {
		mPayloadType = linphone_core_get_payload_type(core, type, rate, channels);
		if (mPayloadType) {
			bctbx_list_t *codecs = linphone_core_get_audio_payload_types(core);
			bctbx_list_t *elem;
			int index = 0;
			for (elem = codecs; elem != NULL; elem = elem->next, ++index) {
				if (linphone_payload_type_weak_equals((LinphonePayloadType *)elem->data, mPayloadType)) {
					mPosition = index;
					break;
				}
			}
			bctbx_list_free_with_data(codecs, (bctbx_list_free_func)linphone_payload_type_unref);
		}
		bctbx_list_t *codecs = linphone_core_get_audio_payload_types(core);
		bctbx_list_t *elem;
		for (elem = codecs; elem != NULL; elem = elem->next) {
			if (number == linphone_payload_type_get_number((LinphonePayloadType *)elem->data)) {
				mPayloadType = linphone_payload_type_ref((LinphonePayloadType *)elem->data);
		bctbx_list_free_with_data(codecs, (bctbx_list_free_func)linphone_payload_type_unref);
DaemonCommandExample::DaemonCommandExample(const string &command, const string &output)
    : mCommand(command), mOutput(output) {
}
Ghislain MARY's avatar
Ghislain MARY committed

DaemonCommand::DaemonCommand(const string &name, const string &proto, const string &description)
    : mName(name), mProto(proto), mDescription(description) {
Ghislain MARY's avatar
Ghislain MARY committed
}

void DaemonCommand::addExample(std::unique_ptr<const DaemonCommandExample> &&example) {
johan's avatar
johan committed
	mExamples.emplace_back(std::move(example));
Ghislain MARY's avatar
Ghislain MARY committed
}

const string DaemonCommand::getHelp() const {
	ostringstream ost;
	ost << getProto() << endl << endl;
	ost << "Description:" << endl << getDescription() << endl << endl;
	int c = 1;
	for (const auto &example : getExamples()) {
		ost << "Example " << c++ << ":" << endl;
		ost << ">" << example->getCommand() << endl;
		ost << example->getOutput() << endl;
Ghislain MARY's avatar
Ghislain MARY committed
		ost << endl;
	}
	return ost.str();
Yann Diorcet's avatar
Yann Diorcet committed
}

bool DaemonCommand::matches(const string &name) const {
	return mName.compare(name) == 0;
Yann Diorcet's avatar
Yann Diorcet committed
}

Daemon::Daemon(const char *config_path,
               const char *factory_config_path,
               const char *log_file,
               const char *pipe_path,
               bool display_video,
               bool capture_video)
    : mLSD(0), mLogFile(NULL), mAutoVideo(0), mCallIds(0), mProxyIds(0), mAudioStreamIds(0) {
	ms_mutex_init(&mMutex, NULL);
	mServerFd = (bctbx_pipe_t)-1;
	mChildFd = (bctbx_pipe_t)-1;
	if (pipe_path == NULL) {
#ifdef HAVE_READLINE
		const char *homedir = getenv("HOME");
		rl_readline_name = (char *)"daemon";
		if (homedir == NULL) homedir = ".";
		mHistfile = string(homedir) + string("/.linphone_history");
		read_history(mHistfile.c_str());
		setlinebuf(stdout);
#endif
Yann Diorcet's avatar
Yann Diorcet committed
	} else {
		mServerFd = bctbx_server_pipe_create_by_path(pipe_path);
#ifndef _WIN32
Yann Diorcet's avatar
Yann Diorcet committed
		listen(mServerFd, 2);
		fprintf(stdout, "Server unix socket created, path=%s fd=%i\n", pipe_path, (int)mServerFd);
		fprintf(stdout, "Named pipe  created, path=%s fd=%p\n", pipe_path, mServerFd);
	if (log_file != NULL) {
		mLogFile = fopen(log_file, "a+");
		linphone_core_enable_logs(mLogFile);
	} else {
		linphone_core_disable_logs();
	}
	LinphoneCoreVTable vtable;
	memset(&vtable, 0, sizeof(vtable));
Yann Diorcet's avatar
Yann Diorcet committed
	vtable.call_state_changed = callStateChanged;
Yann Diorcet's avatar
Yann Diorcet committed
	vtable.call_stats_updated = callStatsUpdated;
Yann Diorcet's avatar
Yann Diorcet committed
	vtable.dtmf_received = dtmfReceived;
	vtable.message_received = messageReceived;
	mLc = linphone_core_new(&vtable, config_path, factory_config_path, this);
Yann Diorcet's avatar
Yann Diorcet committed
	linphone_core_set_user_data(mLc, this);
	linphone_core_enable_video_capture(mLc, capture_video);
	linphone_core_enable_video_display(mLc, display_video);
Ronan's avatar
Ronan committed

	for (const bctbx_list_t *proxy = linphone_core_get_proxy_config_list(mLc); proxy != NULL;
	     proxy = bctbx_list_next(proxy)) {
		updateProxyId((LinphoneProxyConfig *)bctbx_list_get_data(proxy));
	}
Ronan's avatar
Ronan committed

Yann Diorcet's avatar
Yann Diorcet committed
	initCommands();
Yann Diorcet's avatar
Yann Diorcet committed
}

const list<DaemonCommand *> &Daemon::getCommandList() const {
Yann Diorcet's avatar
Yann Diorcet committed
	return mCommands;
}

Yann Diorcet's avatar
Yann Diorcet committed
LinphoneCore *Daemon::getCore() {
Yann Diorcet's avatar
Yann Diorcet committed
	return mLc;
}

Ghislain MARY's avatar
Ghislain MARY committed
LinphoneSoundDaemon *Daemon::getLSD() {
	return mLSD;
}

Yann Diorcet's avatar
Yann Diorcet committed
int Daemon::updateCallId(LinphoneCall *call) {
	int val = VOIDPTR_TO_INT(linphone_call_get_user_data(call));
Yann Diorcet's avatar
Yann Diorcet committed
	if (val == 0) {
		linphone_call_set_user_data(call, INT_TO_VOIDPTR(++mCallIds));
Yann Diorcet's avatar
Yann Diorcet committed
	}
	return val;
Yann Diorcet's avatar
Yann Diorcet committed
}

Yann Diorcet's avatar
Yann Diorcet committed
LinphoneCall *Daemon::findCall(int id) {
	const bctbx_list_t *elem = linphone_core_get_calls(mLc);
Yann Diorcet's avatar
Yann Diorcet committed
	for (; elem != NULL; elem = elem->next) {
		LinphoneCall *call = (LinphoneCall *)elem->data;
		if (VOIDPTR_TO_INT(linphone_call_get_user_data(call)) == id) return call;
Yann Diorcet's avatar
Yann Diorcet committed
	}
	return NULL;
}

Yann Diorcet's avatar
Yann Diorcet committed
int Daemon::updateProxyId(LinphoneProxyConfig *cfg) {
Ghislain MARY's avatar
Ghislain MARY committed
	int val = VOIDPTR_TO_INT(linphone_proxy_config_get_user_data(cfg));
Yann Diorcet's avatar
Yann Diorcet committed
	if (val == 0) {
Ghislain MARY's avatar
Ghislain MARY committed
		linphone_proxy_config_set_user_data(cfg, INT_TO_VOIDPTR(++mProxyIds));
Yann Diorcet's avatar
Yann Diorcet committed
	}
	return val;
Yann Diorcet's avatar
Yann Diorcet committed
LinphoneProxyConfig *Daemon::findProxy(int id) {
	const bctbx_list_t *elem = linphone_core_get_proxy_config_list(mLc);
Yann Diorcet's avatar
Yann Diorcet committed
	for (; elem != NULL; elem = elem->next) {
		LinphoneProxyConfig *proxy = (LinphoneProxyConfig *)elem->data;
		if (VOIDPTR_TO_INT(linphone_proxy_config_get_user_data(proxy)) == id) return proxy;
Yann Diorcet's avatar
Yann Diorcet committed
	}
	return NULL;
}

LinphoneAuthInfo *Daemon::findAuthInfo(int id) {
	const bctbx_list_t *elem = linphone_core_get_auth_info_list(mLc);
	if (elem == NULL || id < 1 || (unsigned int)id > bctbx_list_size(elem)) {
		return NULL;
	}
	while (id > 1) {
		elem = elem->next;
		--id;
	}
Yann Diorcet's avatar
Yann Diorcet committed
int Daemon::updateAudioStreamId(AudioStream *audio_stream) {
	for (map<int, AudioStreamAndOther *>::iterator it = mAudioStreams.begin(); it != mAudioStreams.end(); ++it) {
		if (it->second->stream == audio_stream) return it->first;
	++mAudioStreamIds;
	mAudioStreams.insert(make_pair(mAudioStreamIds, new AudioStreamAndOther(audio_stream)));
	return mAudioStreamIds;
Guillaume Beraudo's avatar
Guillaume Beraudo committed
AudioStreamAndOther *Daemon::findAudioStreamAndOther(int id) {
	map<int, AudioStreamAndOther *>::iterator it = mAudioStreams.find(id);
	if (it != mAudioStreams.end()) return it->second;
Yann Diorcet's avatar
Yann Diorcet committed
	return NULL;
}

Guillaume Beraudo's avatar
Guillaume Beraudo committed
AudioStream *Daemon::findAudioStream(int id) {
	map<int, AudioStreamAndOther *>::iterator it = mAudioStreams.find(id);
	if (it != mAudioStreams.end()) return it->second->stream;
Guillaume Beraudo's avatar
Guillaume Beraudo committed
	return NULL;
}

void Daemon::removeAudioStream(int id) {
	map<int, AudioStreamAndOther *>::iterator it = mAudioStreams.find(id);
Guillaume Beraudo's avatar
Guillaume Beraudo committed
	if (it != mAudioStreams.end()) {
Guillaume Beraudo's avatar
Guillaume Beraudo committed
	}
static bool compareCommands(const DaemonCommand *command1, const DaemonCommand *command2) {
	return (command1->getProto() < command2->getProto());
}

Yann Diorcet's avatar
Yann Diorcet committed
void Daemon::initCommands() {
Yann Diorcet's avatar
Yann Diorcet committed
	mCommands.push_back(new RegisterCommand());
	mCommands.push_back(new ContactCommand());
Yann Diorcet's avatar
Yann Diorcet committed
	mCommands.push_back(new RegisterStatusCommand());
	mCommands.push_back(new RegisterInfoCommand());
Yann Diorcet's avatar
Yann Diorcet committed
	mCommands.push_back(new UnregisterCommand());
	mCommands.push_back(new AuthInfosClearCommand());
Yann Diorcet's avatar
Yann Diorcet committed
	mCommands.push_back(new CallCommand());
	mCommands.push_back(new TerminateCommand());
	mCommands.push_back(new DtmfCommand());
Ghislain MARY's avatar
Ghislain MARY committed
	mCommands.push_back(new PlayWavCommand());
Yann Diorcet's avatar
Yann Diorcet committed
	mCommands.push_back(new PopEventCommand());
	mCommands.push_back(new AnswerCommand());
Yann Diorcet's avatar
Yann Diorcet committed
	mCommands.push_back(new CallStatusCommand());
Yann Diorcet's avatar
Yann Diorcet committed
	mCommands.push_back(new CallStatsCommand());
	mCommands.push_back(new CallPauseCommand());
	mCommands.push_back(new CallMuteCommand());
	mCommands.push_back(new CallResumeCommand());
	mCommands.push_back(new CallTransferCommand());
	mCommands.push_back(new Video());
	mCommands.push_back(new Video::Preview());
	mCommands.push_back(new VideoSource());
	mCommands.push_back(new VideoSourceGet());
	mCommands.push_back(new VideoSourceList());
	mCommands.push_back(new VideoSourceSet());
	mCommands.push_back(new VideoSourceReload());
	mCommands.push_back(new VideoDisplayGet());
	mCommands.push_back(new VideoDisplaySet());
	mCommands.push_back(new AutoVideo());
	mCommands.push_back(new ConferenceCommand());
Yann Diorcet's avatar
Yann Diorcet committed
	mCommands.push_back(new AudioCodecGetCommand());
	mCommands.push_back(new AudioCodecEnableCommand());
	mCommands.push_back(new AudioCodecDisableCommand());
	mCommands.push_back(new AudioCodecMoveCommand());
Yann Diorcet's avatar
Yann Diorcet committed
	mCommands.push_back(new AudioCodecSetCommand());
Yann Diorcet's avatar
Yann Diorcet committed
	mCommands.push_back(new AudioStreamStartCommand());
	mCommands.push_back(new AudioStreamStopCommand());
Guillaume Beraudo's avatar
Guillaume Beraudo committed
	mCommands.push_back(new AudioStreamStatsCommand());
	mCommands.push_back(new MSFilterAddFmtpCommand());
Yann Diorcet's avatar
Yann Diorcet committed
	mCommands.push_back(new PtimeCommand());
Ghislain MARY's avatar
Ghislain MARY committed
	mCommands.push_back(new IPv6Command());
	mCommands.push_back(new FirewallPolicyCommand());
	mCommands.push_back(new MediaEncryptionCommand());
Ghislain MARY's avatar
Ghislain MARY committed
	mCommands.push_back(new PortCommand());
	mCommands.push_back(new AdaptiveBufferCompensationCommand());
Simon Morlat's avatar
Simon Morlat committed
	mCommands.push_back(new JitterBufferCommand());
	mCommands.push_back(new JitterBufferResetCommand());
Ghislain MARY's avatar
Ghislain MARY committed
	mCommands.push_back(new VersionCommand());
Yann Diorcet's avatar
Yann Diorcet committed
	mCommands.push_back(new QuitCommand());
	mCommands.push_back(new HelpCommand());
	mCommands.push_back(new ConfigGetCommand());
	mCommands.push_back(new ConfigSetCommand());
	mCommands.push_back(new NetsimCommand());
Simon Morlat's avatar
Simon Morlat committed
	mCommands.push_back(new CNCommand());
	mCommands.push_back(new IncallPlayerStartCommand());
	mCommands.push_back(new IncallPlayerStopCommand());
	mCommands.push_back(new IncallPlayerPauseCommand());
	mCommands.push_back(new IncallPlayerResumeCommand());
	mCommands.push_back(new MessageCommand());
	mCommands.push_back(new EchoCalibrationCommand());
Yann Diorcet's avatar
Yann Diorcet committed
}

void Daemon::uninitCommands() {
	while (!mCommands.empty()) {
		delete mCommands.front();
		mCommands.pop_front();
	}
Yann Diorcet's avatar
Yann Diorcet committed
}

Yann Diorcet's avatar
Yann Diorcet committed
bool Daemon::pullEvent() {
	bool status = false;
Yann Diorcet's avatar
Yann Diorcet committed
	ostringstream ostr;
	size_t size = mEventQueue.size();
	ostr << "Size: " << size << "\n"; // size is the number items remaining in the queue after popping the event.
Yann Diorcet's avatar
Yann Diorcet committed
	if (!mEventQueue.empty()) {
Yann Diorcet's avatar
Yann Diorcet committed
		mEventQueue.pop();
Yann Diorcet's avatar
Yann Diorcet committed
		status = true;
Yann Diorcet's avatar
Yann Diorcet committed
	}
Yann Diorcet's avatar
Yann Diorcet committed
	sendResponse(Response(ostr.str().c_str(), Response::Ok));
	return status;
Yann Diorcet's avatar
Yann Diorcet committed
}

void Daemon::callStateChanged(LinphoneCall *call, LinphoneCallState state, BCTBX_UNUSED(const char *msg)) {
	queueEvent(new CallEvent(this, call, state));
	if (state == LinphoneCallIncomingReceived && mAutoAnswer) {
		linphone_call_accept(call);
	}
Yann Diorcet's avatar
Yann Diorcet committed
}

void Daemon::messageReceived(BCTBX_UNUSED(LinphoneChatRoom *cr), LinphoneChatMessage *msg) {
	queueEvent(new IncomingMessageEvent(msg));
}

Yann Diorcet's avatar
Yann Diorcet committed
void Daemon::callStatsUpdated(LinphoneCall *call, const LinphoneCallStats *stats) {
	if (mUseStatsEvents) {
		/* don't queue periodical updates (3 per seconds for just bandwidth updates) */
		if (!(_linphone_call_stats_get_updated(stats) & LINPHONE_CALL_STATS_PERIODICAL_UPDATE)) {
			queueEvent(new CallStatsEvent(this, call, stats));
void Daemon::callPlayingComplete(int id) {
	queueEvent(new CallPlayingStatsEvent(this, id));
Yann Diorcet's avatar
Yann Diorcet committed
void Daemon::dtmfReceived(LinphoneCall *call, int dtmf) {
	queueEvent(new DtmfEvent(this, call, dtmf));
Yann Diorcet's avatar
Yann Diorcet committed
}

Yann Diorcet's avatar
Yann Diorcet committed
void Daemon::callStateChanged(LinphoneCore *lc, LinphoneCall *call, LinphoneCallState state, const char *msg) {
	Daemon *app = (Daemon *)linphone_core_get_user_data(lc);
Yann Diorcet's avatar
Yann Diorcet committed
	app->callStateChanged(call, state, msg);
Yann Diorcet's avatar
Yann Diorcet committed
}
Yann Diorcet's avatar
Yann Diorcet committed
void Daemon::callStatsUpdated(LinphoneCore *lc, LinphoneCall *call, const LinphoneCallStats *stats) {
	Daemon *app = (Daemon *)linphone_core_get_user_data(lc);
Yann Diorcet's avatar
Yann Diorcet committed
	app->callStatsUpdated(call, stats);
}
Yann Diorcet's avatar
Yann Diorcet committed
void Daemon::dtmfReceived(LinphoneCore *lc, LinphoneCall *call, int dtmf) {
	Daemon *app = (Daemon *)linphone_core_get_user_data(lc);
Yann Diorcet's avatar
Yann Diorcet committed
	app->dtmfReceived(call, dtmf);
}
Yann Diorcet's avatar
Yann Diorcet committed

void Daemon::messageReceived(LinphoneCore *lc, LinphoneChatRoom *cr, LinphoneChatMessage *msg) {
	Daemon *app = (Daemon *)linphone_core_get_user_data(lc);
Guillaume Beraudo's avatar
Guillaume Beraudo committed
void Daemon::iterateStreamStats() {
	for (map<int, AudioStreamAndOther *>::iterator it = mAudioStreams.begin(); it != mAudioStreams.end(); ++it) {
Guillaume Beraudo's avatar
Guillaume Beraudo committed
		OrtpEvent *ev;
		while (it->second->queue && (NULL != (ev = ortp_ev_queue_get(it->second->queue)))) {
			OrtpEventType evt = ortp_event_get_type(ev);
Guillaume Beraudo's avatar
Guillaume Beraudo committed
			if (evt == ORTP_EVENT_RTCP_PACKET_RECEIVED || evt == ORTP_EVENT_RTCP_PACKET_EMITTED) {
				linphone_call_stats_fill(it->second->stats, &it->second->stream->ms, ev);
				if (mUseStatsEvents)
					mEventQueue.push(new AudioStreamStatsEvent(this, it->second->stream, it->second->stats));
Guillaume Beraudo's avatar
Guillaume Beraudo committed
			}
			ortp_event_destroy(ev);
		}
	}
}

Yann Diorcet's avatar
Yann Diorcet committed
void Daemon::iterate() {
Yann Diorcet's avatar
Yann Diorcet committed
	linphone_core_iterate(mLc);
Guillaume Beraudo's avatar
Guillaume Beraudo committed
	iterateStreamStats();
	if (mChildFd == (bctbx_pipe_t)-1) {
Yann Diorcet's avatar
Yann Diorcet committed
		if (!mEventQueue.empty()) {
Yann Diorcet's avatar
Yann Diorcet committed
			mEventQueue.pop();
			fprintf(stdout, "\n%s\n", r->toBuf().c_str());
Yann Diorcet's avatar
Yann Diorcet committed
			fflush(stdout);
			delete r;
		}
	}
}

void Daemon::execCommand(const string &command) {
	istringstream ist(command);
	string name;
	ist >> name;
	stringbuf argsbuf;
	ist.get(argsbuf);
	string args = argsbuf.str();
	if (!args.empty() && (args[0] == ' ')) args.erase(0, 1);
	    find_if(mCommands.begin(), mCommands.end(), [&name](const DaemonCommand *dc) { return dc->matches(name); });
Yann Diorcet's avatar
Yann Diorcet committed
	if (it != mCommands.end()) {
Yann Diorcet's avatar
Yann Diorcet committed
		(*it)->exec(this, args);
Yann Diorcet's avatar
Yann Diorcet committed
	} else {
Yann Diorcet's avatar
Yann Diorcet committed
		sendResponse(Response("Unknown command."));
	}
}

Yann Diorcet's avatar
Yann Diorcet committed
void Daemon::sendResponse(const Response &resp) {
	string buf = resp.toBuf();
	if (mChildFd != (bctbx_pipe_t)-1) {
		if (bctbx_pipe_write(mChildFd, (uint8_t *)buf.c_str(), (int)buf.size()) == -1) {
Yann Diorcet's avatar
Yann Diorcet committed
			ms_error("Fail to write to pipe: %s", strerror(errno));
Yann Diorcet's avatar
Yann Diorcet committed
		}
Yann Diorcet's avatar
Yann Diorcet committed
	} else {
		cout << buf << flush;
Yann Diorcet's avatar
Yann Diorcet committed
	}
}

string Daemon::readPipe() {
	char buffer[32768];
	memset(buffer, '\0', sizeof(buffer));
#ifdef _WIN32
	if (mChildFd == (bctbx_pipe_t)-1) {
		mChildFd = bctbx_server_pipe_accept_client(mServerFd);
		ms_message("Client accepted");
	}
	if (mChildFd != (bctbx_pipe_t)-1) {
		int ret = bctbx_pipe_read(mChildFd, (uint8_t *)buffer, sizeof(buffer));
		if (ret == -1) {
			ms_error("Fail to read from pipe: %s", strerror(errno));
			mChildFd = (bctbx_pipe_t)-1;
		} else {
			if (ret == 0) {
				ms_message("Client disconnected");
				mChildFd = (bctbx_pipe_t)-1;
				return "";
			buffer[ret] = '\0';
			return buffer;
		}
	}
#else
Yann Diorcet's avatar
Yann Diorcet committed
	int nfds = 1;
	memset(&pfd[0], 0, sizeof(pfd));
	if (mServerFd != (bctbx_pipe_t)-1) {
Yann Diorcet's avatar
Yann Diorcet committed
		pfd[0].events = POLLIN;
		pfd[0].fd = mServerFd;
Yann Diorcet's avatar
Yann Diorcet committed
	}
	if (mChildFd != (bctbx_pipe_t)-1) {
Yann Diorcet's avatar
Yann Diorcet committed
		pfd[1].events = POLLIN;
		pfd[1].fd = mChildFd;
Yann Diorcet's avatar
Yann Diorcet committed
		nfds++;
	}
	int err = poll(pfd, (nfds_t)nfds, 50);
Yann Diorcet's avatar
Yann Diorcet committed
	if (err > 0) {
		if (mServerFd != (bctbx_pipe_t)-1 && (pfd[0].revents & POLLIN)) {
Yann Diorcet's avatar
Yann Diorcet committed
			struct sockaddr_storage addr;
Yann Diorcet's avatar
Yann Diorcet committed
			socklen_t addrlen = sizeof(addr);
			int childfd = accept(mServerFd, (struct sockaddr *)&addr, &addrlen);
Yann Diorcet's avatar
Yann Diorcet committed
			if (childfd != -1) {
				if (mChildFd != (bctbx_pipe_t)-1) {
Yann Diorcet's avatar
Yann Diorcet committed
					ms_error("Cannot accept two client at the same time");
					close(childfd);
Yann Diorcet's avatar
Yann Diorcet committed
				} else {
					mChildFd = (bctbx_pipe_t)childfd;
					return "";
Yann Diorcet's avatar
Yann Diorcet committed
				}
			}
		}
		if (mChildFd != (bctbx_pipe_t)-1 && (pfd[1].revents & POLLIN)) {
Yann Diorcet's avatar
Yann Diorcet committed
			int ret;
			if ((ret = bctbx_pipe_read(mChildFd, (uint8_t *)buffer, sizeof(buffer))) == -1) {
Yann Diorcet's avatar
Yann Diorcet committed
				ms_error("Fail to read from pipe: %s", strerror(errno));
Yann Diorcet's avatar
Yann Diorcet committed
			} else {
				if (ret == 0) {
Yann Diorcet's avatar
Yann Diorcet committed
					ms_message("Client disconnected");
					bctbx_server_pipe_close_client(mChildFd);
					mChildFd = (bctbx_pipe_t)-1;
					return "";
Yann Diorcet's avatar
Yann Diorcet committed
				}
				buffer[ret] = '\0';
Yann Diorcet's avatar
Yann Diorcet committed
				return buffer;
			}
		}
	}
	return "";
Yann Diorcet's avatar
Yann Diorcet committed
}

Ghislain MARY's avatar
Ghislain MARY committed
void Daemon::dumpCommandsHelp() {
	int cols = 80;
#ifdef TIOCGSIZE
	struct ttysize ts;
	ioctl(STDIN_FILENO, TIOCGSIZE, &ts);
	cols = ts.ts_cols;
#elif defined(TIOCGWINSZ)
	struct winsize ts;
	ioctl(STDIN_FILENO, TIOCGWINSZ, &ts);
	cols = ts.ws_col;
#endif

	cout << endl;
	for (list<DaemonCommand *>::iterator it = mCommands.begin(); it != mCommands.end(); ++it) {
Ghislain MARY's avatar
Ghislain MARY committed
		cout << setfill('-') << setw(cols) << "-" << endl << endl;
		cout << (*it)->getHelp();
	}
}

static string htmlEscape(const string &orig) {
	string ret = orig;
	size_t pos;
Ghislain MARY's avatar
Ghislain MARY committed

	while (1) {
		pos = ret.find('<');
		if (pos != string::npos) {
			ret.erase(pos, 1);
			ret.insert(pos, "&lt");
			continue;
		}
		pos = ret.find('>');
		if (pos != string::npos) {
			ret.erase(pos, 1);
			ret.insert(pos, "&gt");
			continue;
		}
		break;
	}
	while (1) {
		pos = ret.find('\n');
		if (pos != string::npos) {
			ret.erase(pos, 1);
			ret.insert(pos, "<br>");
			continue;
		}
		break;
	}
	return ret;
}

	cout << endl;
	cout << "<!DOCTYPE html><html><body>" << endl;
	cout << "<h1>List of linphone-daemon commands.</h1>" << endl;
	for (const auto &cmd : mCommands) {
		cout << "<h2>" << htmlEscape(cmd->getProto()) << "</h2>" << endl;
		cout << "<h3>"
		     << "Description"
		     << "</h3>" << endl;
		cout << "<p>" << htmlEscape(cmd->getDescription()) << "</p>" << endl;
		cout << "<h3>"
		     << "Examples"
		     << "</h3>" << endl;
		const auto &examples = cmd->getExamples();
		cout << "<p><i>";
		for (const auto &example : examples) {
			cout << "<b>" << htmlEscape("Linphone-daemon>") << htmlEscape(example->getCommand()) << "</b><br>" << endl;
			cout << htmlEscape(example->getOutput()) << "<br>" << endl;
			cout << "<br><br>";
Ghislain MARY's avatar
Ghislain MARY committed

Yann Diorcet's avatar
Yann Diorcet committed
static void printHelp() {
	cout << "daemon-linphone [<options>]" << endl
	     <<
#if defined(LICENCE_GPL) || defined(LICENCE_COMMERCIAL)
#endif
#ifdef LICENCE_COMMERCIAL
	    "where options are :" << endl
	     << "\t--help                     Print this notice." << endl
	     << "\t--dump-commands-help       Dump the help of every available commands." << endl
	     << "\t--dump-commands-html-help  Dump the help of every available commands." << endl
	     << "\t--pipe <pipepath>          Create an unix server socket in the specified path to receive commands from. "
	        "For Windows just use a name instead of a path."
	     << endl
	     << "\t--log <path>               Supply a file where the log will be saved." << endl
	     << "\t--factory-config <path>    Supply a readonly linphonerc style config file to start with." << endl
	     << "\t--config <path>            Supply a linphonerc style config file to start with." << endl
	     << "\t--disable-stats-events     Do not automatically raise RTP statistics events." << endl
	     << "\t--enable-lsd               Use the linphone sound daemon." << endl
	     << "\t-C                         Enable video capture." << endl
	     << "\t-D                         Enable video display." << endl
	     << "\t--auto-answer              Automatically answer incoming calls." << endl;
Yann Diorcet's avatar
Yann Diorcet committed
}

void Daemon::startThread() {
	ms_thread_create(&this->mThread, NULL, Daemon::iterateThread, this);
}

#ifdef max
#undef max
#endif

string Daemon::readLine(const string &prompt, bool *eof) {
	*eof = false;
	return readline(prompt.c_str());
		return "";
	stringbuf outbuf;
	cin.get(outbuf);
	cin.clear();
Ronan's avatar
Ronan committed
	cin.ignore(numeric_limits<streamsize>::max(), '\n');
	return outbuf.str();
Yann Diorcet's avatar
Yann Diorcet committed
int Daemon::run() {
	const string prompt("daemon-linphone>");
Yann Diorcet's avatar
Yann Diorcet committed
	mRunning = true;
Yann Diorcet's avatar
Yann Diorcet committed
	while (mRunning) {
		string line;
		if (mServerFd == (bctbx_pipe_t)-1) {
			line = readLine(prompt, &eof);
			if (!line.empty()) {
				add_history(line.c_str());
Yann Diorcet's avatar
Yann Diorcet committed
			}
Yann Diorcet's avatar
Yann Diorcet committed
		} else {
			line = readPipe();
		if (!line.empty()) {
			execCommand(line);
Guillaume Beraudo's avatar
Guillaume Beraudo committed
			mRunning = false; // ctrl+d
			cout << "Quitting..." << endl;
		}
Yann Diorcet's avatar
Yann Diorcet committed
	}
Yann Diorcet's avatar
Yann Diorcet committed
	return 0;
}

void Daemon::stopThread() {
	void *ret;
	ms_thread_join(mThread, &ret);
}

Yann Diorcet's avatar
Yann Diorcet committed
void Daemon::quit() {
	mRunning = false;
Yann Diorcet's avatar
Yann Diorcet committed
}

void Daemon::enableStatsEvents(bool enabled) {
	mUseStatsEvents = enabled;
Simon Morlat's avatar
Simon Morlat committed
}

void Daemon::enableAutoAnswer(bool enabled) {
Ghislain MARY's avatar
Ghislain MARY committed
void Daemon::enableLSD(bool enabled) {
	if (mLSD) linphone_sound_daemon_destroy(mLSD);
	linphone_core_use_sound_daemon(mLc, NULL);
	if (enabled) {
		mLSD = linphone_sound_daemon_new(mLc->factory, NULL, 44100, 1);
Ghislain MARY's avatar
Ghislain MARY committed
		linphone_core_use_sound_daemon(mLc, mLSD);
	}
}

Yann Diorcet's avatar
Yann Diorcet committed
Daemon::~Daemon() {
	uninitCommands();
Ronan's avatar
Ronan committed
	for (map<int, AudioStreamAndOther *>::iterator it = mAudioStreams.begin(); it != mAudioStreams.end(); ++it) {
Guillaume Beraudo's avatar
Guillaume Beraudo committed
		audio_stream_stop(it->second->stream);
Ghislain MARY's avatar
Ghislain MARY committed
	enableLSD(false);
Simon Morlat's avatar
Simon Morlat committed
	linphone_core_unref(mLc);
	if (mChildFd != (bctbx_pipe_t)-1) {
		bctbx_server_pipe_close_client(mChildFd);
Yann Diorcet's avatar
Yann Diorcet committed
	}
	if (mServerFd != (bctbx_pipe_t)-1) {
		bctbx_server_pipe_close(mServerFd);
Yann Diorcet's avatar
Yann Diorcet committed
	}
	if (mLogFile != NULL) {
		linphone_core_enable_logs(NULL);
		fclose(mLogFile);
	}

	ms_mutex_destroy(&mMutex);

#ifdef HAVE_READLINE
Yann Diorcet's avatar
Yann Diorcet committed
	stifle_history(30);
	write_history(mHistfile.c_str());
Yann Diorcet's avatar
Yann Diorcet committed
}

static Daemon *the_app = NULL;

static void sighandler(BCTBX_UNUSED(int signum)) {
	if (the_app) {
extern "C" int apple_main(int argc, char **argv) {
Yann Diorcet's avatar
Yann Diorcet committed
int main(int argc, char *argv[]) {
Yann Diorcet's avatar
Yann Diorcet committed
	const char *config_path = NULL;
	const char *factory_config_path = NULL;
	const char *pipe_path = NULL;
	const char *log_file = NULL;
Yann Diorcet's avatar
Yann Diorcet committed
	bool capture_video = false;
	bool display_video = false;
Ghislain MARY's avatar
Ghislain MARY committed
	bool stats_enabled = true;
Ghislain MARY's avatar
Ghislain MARY committed
	bool lsd_enabled = false;
	bool auto_answer = false;
Yann Diorcet's avatar
Yann Diorcet committed
	int i;

Yann Diorcet's avatar
Yann Diorcet committed
	for (i = 1; i < argc; ++i) {
		if (strcmp(argv[i], "--help") == 0) {
Yann Diorcet's avatar
Yann Diorcet committed
			printHelp();
			return 0;
Ghislain MARY's avatar
Ghislain MARY committed
		} else if (strcmp(argv[i], "--dump-commands-help") == 0) {
			Daemon app(NULL, NULL, NULL, NULL, false, false);
			app.dumpCommandsHelp();
			return 0;
		} else if (strcmp(argv[i], "--dump-commands-html-help") == 0) {
			Daemon app(NULL, NULL, NULL, NULL, false, false);
			app.dumpCommandsHelpHtml();