diff --git a/core/AmPromptCollection.cpp b/core/AmPromptCollection.cpp new file mode 100644 index 00000000..28c805ee --- /dev/null +++ b/core/AmPromptCollection.cpp @@ -0,0 +1,151 @@ +/* + * $Id: AmPromptCollection.cpp 288 2007-03-28 16:32:02Z sayer $ + * + * Copyright (C) 2007 iptego GmbH + * + * This file is part of sems, a free SIP media server. + * + * sems 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. + * + * For a license to use the sems software under conditions + * other than those described here, or to purchase support for this + * software, please contact iptel.org by e-mail at the following addresses: + * info@iptel.org + * + * sems 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 "AmPromptCollection.h" +#include "AmUtils.h" +#include "log.h" + +AmPromptCollection::AmPromptCollection() +{ +} + +AmPromptCollection::~AmPromptCollection() +{ + // clean up + for (map::iterator it= + store.begin(); it != store.end();it++) + delete it->second; +} + +int AmPromptCollection::configureModule(AmConfigReader& cfg, + vector >& announcements, + const char* mod_name) { + int res = 0; + for (vector >::iterator it= + announcements.begin(); it != announcements.end(); it++) { + string fname = cfg.getParameter(it->first, ""); + if (fname.empty()){ + WARN("using default file '%s' for '%s' prompt in '%s' module\n", + it->second.c_str(), it->first.c_str(), mod_name); + fname = it->second; + } + + if (0 != setPrompt(it->first, fname, mod_name)) + res = -1; + } + + return res; +} + +int AmPromptCollection::setPrompt(const string& name, + const string& filename, + const char* mod_name) { + if (!file_exists(filename)) { + ERROR("'%s' prompt for module %s does not exist at '%s'.\n", + name.c_str(), mod_name, filename.c_str()); + return -1; + } + + AudioFileEntry* af = new AudioFileEntry(); + if (af->load(filename)) { + ERROR("Could not load '%s' prompt for module %s at '%s'.\n", + name.c_str(), mod_name, filename.c_str()); + delete af; + return -1; + } + DBG("adding prompt '%s' to prompt collection.\n", + name.c_str()); + store[name]=af; + return 0; +} + + + +AudioFileEntry::AudioFileEntry() + : isopen(false) +{ +} + +AudioFileEntry::~AudioFileEntry() { +} + +int AudioFileEntry::load(const std::string& filename) { + int res = cache.load(filename); + isopen = !res; + return res; +} + +AmCachedAudioFile* AudioFileEntry::getAudio(){ + if (!isopen) + return NULL; + return new AmCachedAudioFile(&cache); +} + +int AmPromptCollection::addToPlaylist(const string& name, long sess_id, + AmPlaylist& list, bool front) { + string s = name; + map::iterator it=store.begin(); + + while (it != store.end()) { + if (!strcmp(it->first.c_str(), s.c_str())) + break; + it++; + } + if (it == store.end()) { + WARN("'%s' prompt not found!\n", name.c_str()); + return -1; + } + + DBG("adding '%s' prompt to playlist at the %s'\n", it->first.c_str(), + front ? "front":"back"); + + AmCachedAudioFile* af = it->second->getAudio(); + if (NULL == af) { + return -2; + } + + if (front) + list.addToPlayListFront(new AmPlaylistItem(af,NULL)); + else + list.addToPlaylist(new AmPlaylistItem(af,NULL)); + + items_mut.lock(); + items[sess_id].push_back(af); + items_mut.unlock(); + + return 0; +} + + +void AmPromptCollection::cleanup(long sess_id) { + items_mut.lock(); + for (vector::iterator it = + items[sess_id].begin(); it!=items[sess_id].end(); it++) + delete *it; + items.erase(sess_id); + items_mut.unlock(); +} diff --git a/core/AmPromptCollection.h b/core/AmPromptCollection.h new file mode 100644 index 00000000..043f8b72 --- /dev/null +++ b/core/AmPromptCollection.h @@ -0,0 +1,128 @@ + +/* + * $Id: AmPromptFileCollection.cpp 288 2007-03-28 16:32:02Z sayer $ + * + * Copyright (C) 2007 iptego GmbH + * + * This file is part of sems, a free SIP media server. + * + * sems 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. + * + * For a license to use the sems software under conditions + * other than those described here, or to purchase support for this + * software, please contact iptel.org by e-mail at the following addresses: + * info@iptel.org + * + * sems 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 + */ + +#ifndef AM_PROMPT_COLLECTION_H +#define AM_PROMPT_COLLECTION_H + + +/** + * + * Example how to use: + * + * AM_PROMPT_START; + * AM_PROMPT_ADD("enter_pin", "path/to/default/enter_pin.wav"); + * AM_PROMPT_ADD("ok", "path/to/default/ok.wav"); + * AM_PROMPT_END(prompts, cfg, APP_NAME); + */ + +#define AM_PROMPT_START \ +{ \ + vector > _prompt_names + +#define AM_PROMPT_ADD(_name, _default_file) \ + _prompt_names.push_back(make_pair(_name, _default_file)) + +#define AM_PROMPT_END(_prompts, _cfg, _MOD_NAME) \ + _prompts.configureModule(_cfg, _prompt_names, _MOD_NAME); \ + } + +#include +#include +#include +using std::map; +using std::string; +using std::pair; + +#include "AmCachedAudioFile.h" +#include "AmPlaylist.h" +#include "AmConfigReader.h" + +class AudioFileEntry; + +/** + * \brief manages AmAudioFiles with name for a session. + */ +class AmPromptCollection { + + // loaded files + map store; + + // opened objects + map > items; + // mutex for the above + AmMutex items_mut; + + public: + AmPromptCollection(); + ~AmPromptCollection(); + + /** + * get configuration for announcements from cfg, + * check for file existence + * @param announcements : name, default file for announcement + */ + int configureModule(AmConfigReader& cfg, + vector >& announcements, + const char* mod_name); + /** + * add a prompt with explicit filename + */ + int setPrompt(const string& name, + const string& filename, + const char* mod_name); + /** + * add the announcement identified with @param name + * to the playlist @list + */ + int addToPlaylist(const string& name, long sess_id, + AmPlaylist& list, bool front=false); + /** + * cleanup allocated object of sess_id + */ + void cleanup(long sess_id); +}; + +/** + * AudioFile with filename + */ + +class AudioFileEntry : public AmAudioFile { + AmFileCache cache; + bool isopen; + +public: + AudioFileEntry(); + ~AudioFileEntry(); + + int load(const std::string& filename); + bool isOpen() { return isopen; } + + AmCachedAudioFile* getAudio(); +}; + +#endif