From 24fd131cb5a37a249d7abae63f35ca4226a7fd3f Mon Sep 17 00:00:00 2001 From: Ghislain MARY Date: Wed, 12 Dec 2012 14:32:12 +0100 Subject: [PATCH] Add the play-wav command. --- daemon/Makefile.am | 2 ++ daemon/commands/play-wav.cc | 37 +++++++++++++++++++++++++++++++++++++ daemon/commands/play-wav.h | 12 ++++++++++++ daemon/daemon.cc | 25 +++++++++++++++++++++++-- daemon/daemon.h | 4 ++++ 5 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 daemon/commands/play-wav.cc create mode 100644 daemon/commands/play-wav.h diff --git a/daemon/Makefile.am b/daemon/Makefile.am index 5369b324e..24a9a5b30 100644 --- a/daemon/Makefile.am +++ b/daemon/Makefile.am @@ -20,6 +20,7 @@ linphone_daemon_SOURCES=daemon.cc \ commands/ipv6.cc \ commands/media-encryption.cc \ commands/msfilter-add-fmtp.cc \ + commands/play-wav.cc \ commands/pop-event.cc \ commands/port.cc \ commands/ptime.cc \ @@ -47,6 +48,7 @@ linphone_daemon_SOURCES=daemon.cc \ commands/ipv6.h \ commands/media-encryption.h \ commands/msfilter-add-fmtp.h \ + commands/play-wav.h \ commands/pop-event.h \ commands/port.h \ commands/ptime.h \ diff --git a/daemon/commands/play-wav.cc b/daemon/commands/play-wav.cc new file mode 100644 index 000000000..266341d65 --- /dev/null +++ b/daemon/commands/play-wav.cc @@ -0,0 +1,37 @@ +#include "play-wav.h" + +using namespace std; + +PlayWavCommand::PlayWavCommand() : + DaemonCommand("play-wav", "play-wav ", + "Play an WAV audio file (needs to have enabled the linphone sound daemon (LSD).\n" + " is the WAV file to be played.") { + addExample(new DaemonCommandExample("play-wav /usr/local/share/sounds/linphone/hello8000.wav", + "Status: Ok")); +} + +void playWavFinished(LsdPlayer *p) { + linphone_sound_daemon_release_player(lsd_player_get_daemon(p), p); +} + +void PlayWavCommand::exec(Daemon *app, const char *args) { + LinphoneSoundDaemon *lsd = app->getLSD(); + if (!lsd) { + app->sendResponse(Response("The linphone sound daemon (LSD) is not enabled.", Response::Error)); + return; + } + + string filename; + istringstream ist(args); + ist >> filename; + if (ist.eof() && (filename.length() == 0)) { + app->sendResponse(Response("Missing filename parameter.", Response::Error)); + } else if (ist.fail()) { + app->sendResponse(Response("Incorrect filename parameter.", Response::Error)); + } else { + LsdPlayer *p = linphone_sound_daemon_get_player(lsd); + lsd_player_set_callback(p, playWavFinished); + lsd_player_play(p, filename.c_str()); + app->sendResponse(Response()); + } +} diff --git a/daemon/commands/play-wav.h b/daemon/commands/play-wav.h new file mode 100644 index 000000000..a51832a71 --- /dev/null +++ b/daemon/commands/play-wav.h @@ -0,0 +1,12 @@ +#ifndef COMMAND_PLAY_WAV_H_ +#define COMMAND_PLAY_WAV_H_ + +#include "../daemon.h" + +class PlayWavCommand: public DaemonCommand { +public: + PlayWavCommand(); + virtual void exec(Daemon *app, const char *args); +}; + +#endif //COMMAND_PLAY_WAV_H_ diff --git a/daemon/daemon.cc b/daemon/daemon.cc index 7ce285565..bed3b1afb 100644 --- a/daemon/daemon.cc +++ b/daemon/daemon.cc @@ -31,6 +31,7 @@ #include "commands/ipv6.h" #include "commands/media-encryption.h" #include "commands/msfilter-add-fmtp.h" +#include "commands/play-wav.h" #include "commands/pop-event.h" #include "commands/port.h" #include "commands/ptime.h" @@ -245,7 +246,7 @@ bool DaemonCommand::matches(const char *name) const { } Daemon::Daemon(const char *config_path, const char *factory_config_path, const char *log_file, const char *pipe_name, bool display_video, bool capture_video) : - mLogFile(NULL), mCallIds(0), mProxyIds(0), mAudioStreamIds(0) { + mLSD(0), mLogFile(NULL), mCallIds(0), mProxyIds(0), mAudioStreamIds(0) { ms_mutex_init(&mMutex, NULL); mServerFd = -1; mChildFd = -1; @@ -291,6 +292,10 @@ LinphoneCore *Daemon::getCore() { return mLc; } +LinphoneSoundDaemon *Daemon::getLSD() { + return mLSD; +} + int Daemon::updateCallId(LinphoneCall *call) { int val = (int) (long) linphone_call_get_user_pointer(call); if (val == 0) { @@ -360,6 +365,7 @@ void Daemon::initCommands() { mCommands.push_back(new CallCommand()); mCommands.push_back(new TerminateCommand()); mCommands.push_back(new DtmfCommand()); + mCommands.push_back(new PlayWavCommand()); mCommands.push_back(new PopEventCommand()); mCommands.push_back(new AnswerCommand()); mCommands.push_back(new CallStatusCommand()); @@ -624,10 +630,11 @@ static void printHelp() { "\t--dump-commands-help\tDump the help of every available commands.\n" "\t--dump-commands-html-help\tDump the help of every available commands.\n" "\t--pipe \tCreate an unix server socket to receive commands.\n" - "\t--log \t\tSupply a file where the log will be saved\n" + "\t--log \t\tSupply a file where the log will be saved.\n" "\t--factory-config \tSupply a readonly linphonerc style config file to start with.\n" "\t--config \t\tSupply a linphonerc style config file to start with.\n" "\t--disable-stats-events\t\tDo not automatically raise RTP statistics events.\n" + "\t--enable-lsd\t\tUse the linphone sound daemon.\n" "\t-C\t\t\tenable video capture.\n" "\t-D\t\t\tenable video display.\n"); } @@ -687,6 +694,15 @@ void Daemon::enableStatsEvents(bool enabled){ mUseStatsEvents=enabled; } +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(NULL, 44100, 1); + linphone_core_use_sound_daemon(mLc, mLSD); + } +} + Daemon::~Daemon() { uninitCommands(); @@ -694,6 +710,7 @@ Daemon::~Daemon() { audio_stream_stop(it->second); } + enableLSD(false); linphone_core_destroy(mLc); if (mChildFd != -1) { close(mChildFd); @@ -722,6 +739,7 @@ int main(int argc, char *argv[]) { bool capture_video = false; bool display_video = false; bool nostats=FALSE; + bool lsd_enabled = false; int i; for (i = 1; i < argc; ++i) { @@ -769,10 +787,13 @@ int main(int argc, char *argv[]) { display_video = true; }else if (strcmp(argv[i],"--disable-stats-events")==0){ nostats=TRUE; + } else if (strcmp(argv[i], "--enable-lsd") == 0) { + lsd_enabled = true; } } Daemon app(config_path, factory_config_path, log_file, pipe_name, display_video, capture_video); app.enableStatsEvents(!nostats); + app.enableLSD(lsd_enabled); return app.run(); } ; diff --git a/daemon/daemon.h b/daemon/daemon.h index 5d0078d0e..c56cc7203 100644 --- a/daemon/daemon.h +++ b/daemon/daemon.h @@ -2,6 +2,7 @@ #define DAEMON_H_ #include +#include #include #include @@ -169,6 +170,7 @@ public: void quit(); void sendResponse(const Response &resp); LinphoneCore *getCore(); + LinphoneSoundDaemon *getLSD(); const std::list &getCommandList() const; LinphoneCall *findCall(int id); LinphoneProxyConfig *findProxy(int id); @@ -182,6 +184,7 @@ public: void dumpCommandsHelp(); void dumpCommandsHelpHtml(); void enableStatsEvents(bool enabled); + void enableLSD(bool enabled); private: static void* iterateThread(void *arg); static void callStateChanged(LinphoneCore *lc, LinphoneCall *call, LinphoneCallState state, const char *msg); @@ -199,6 +202,7 @@ private: void initCommands(); void uninitCommands(); LinphoneCore *mLc; + LinphoneSoundDaemon *mLSD; std::list mCommands; std::queue mEventQueue; int mServerFd;