2004-05-22 00:05:20 +00:00
|
|
|
/**
|
|
|
|
* wavefile.cpp
|
|
|
|
* This file is part of the YATE Project http://YATE.null.ro
|
|
|
|
*
|
|
|
|
* Wave file driver (record+playback)
|
2004-11-29 03:56:41 +00:00
|
|
|
*
|
|
|
|
* Yet Another Telephony Engine - a fully featured software PBX and IVR
|
2006-05-27 15:08:43 +00:00
|
|
|
* Copyright (C) 2004-2006 Null Team
|
2004-11-29 03:56:41 +00:00
|
|
|
*
|
|
|
|
* 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
|
2006-05-27 15:08:43 +00:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
|
2004-05-22 00:05:20 +00:00
|
|
|
*/
|
|
|
|
|
2005-03-18 18:16:59 +00:00
|
|
|
#include <yatephone.h>
|
2004-05-22 00:05:20 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
using namespace TelEngine;
|
2006-05-27 14:53:18 +00:00
|
|
|
namespace { // anonymous
|
2004-05-22 00:05:20 +00:00
|
|
|
|
|
|
|
class WaveSource : public ThreadedSource
|
|
|
|
{
|
|
|
|
public:
|
2007-03-19 23:06:06 +00:00
|
|
|
static WaveSource* create(const String& file, CallEndpoint* chan, bool autoclose = true, bool autorepeat = false);
|
2004-05-22 00:05:20 +00:00
|
|
|
~WaveSource();
|
|
|
|
virtual void run();
|
|
|
|
virtual void cleanup();
|
2005-11-04 19:30:47 +00:00
|
|
|
void setNotify(const String& id);
|
2004-05-22 00:05:20 +00:00
|
|
|
private:
|
2007-03-19 23:06:06 +00:00
|
|
|
WaveSource(const char* file, CallEndpoint* chan, bool autoclose);
|
|
|
|
void init(const String& file, bool autorepeat);
|
2004-12-21 04:16:09 +00:00
|
|
|
void detectAuFormat();
|
|
|
|
void detectWavFormat();
|
2005-09-30 22:46:16 +00:00
|
|
|
void detectIlbcFormat();
|
|
|
|
bool computeDataRate();
|
2006-10-09 14:15:14 +00:00
|
|
|
bool notify(DataSource* source, const char* reason = 0);
|
2005-05-14 01:38:58 +00:00
|
|
|
CallEndpoint* m_chan;
|
2004-12-20 04:11:29 +00:00
|
|
|
DataBlock m_data;
|
2004-05-22 00:05:20 +00:00
|
|
|
int m_fd;
|
2005-01-22 02:20:42 +00:00
|
|
|
bool m_swap;
|
2004-05-22 00:05:20 +00:00
|
|
|
unsigned m_brate;
|
2006-12-14 18:09:46 +00:00
|
|
|
long m_repeatPos;
|
2004-05-22 00:05:20 +00:00
|
|
|
unsigned m_total;
|
2005-04-09 22:10:00 +00:00
|
|
|
u_int64_t m_time;
|
2004-09-25 18:22:40 +00:00
|
|
|
String m_id;
|
|
|
|
bool m_autoclose;
|
2006-06-21 21:46:55 +00:00
|
|
|
bool m_autoclean;
|
2005-12-14 13:50:44 +00:00
|
|
|
bool m_nodata;
|
2004-05-22 00:05:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class WaveConsumer : public DataConsumer
|
|
|
|
{
|
|
|
|
public:
|
2007-03-19 23:06:06 +00:00
|
|
|
enum Header {
|
|
|
|
None = 0,
|
|
|
|
Au,
|
|
|
|
Ilbc,
|
|
|
|
};
|
|
|
|
WaveConsumer(const String& file, CallEndpoint* chan, unsigned maxlen, const char* format = 0);
|
2004-05-22 00:05:20 +00:00
|
|
|
~WaveConsumer();
|
2007-03-19 23:06:06 +00:00
|
|
|
virtual bool setFormat(const DataFormat& format);
|
2005-09-06 02:51:09 +00:00
|
|
|
virtual void Consume(const DataBlock& data, unsigned long tStamp);
|
2004-09-25 18:22:40 +00:00
|
|
|
inline void setNotify(const String& id)
|
|
|
|
{ m_id = id; }
|
2004-05-22 00:05:20 +00:00
|
|
|
private:
|
2007-03-19 23:06:06 +00:00
|
|
|
void writeIlbcHeader() const;
|
|
|
|
void writeAuHeader();
|
2005-05-14 01:38:58 +00:00
|
|
|
CallEndpoint* m_chan;
|
2004-05-22 00:05:20 +00:00
|
|
|
int m_fd;
|
2007-03-19 23:06:06 +00:00
|
|
|
bool m_swap;
|
|
|
|
bool m_locked;
|
|
|
|
Header m_header;
|
2004-05-22 00:05:20 +00:00
|
|
|
unsigned m_total;
|
2004-09-07 16:24:40 +00:00
|
|
|
unsigned m_maxlen;
|
2005-04-09 22:10:00 +00:00
|
|
|
u_int64_t m_time;
|
2004-09-25 18:22:40 +00:00
|
|
|
String m_id;
|
2004-05-22 00:05:20 +00:00
|
|
|
};
|
|
|
|
|
2005-04-24 01:08:51 +00:00
|
|
|
class WaveChan : public Channel
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
public:
|
2007-03-19 23:06:06 +00:00
|
|
|
WaveChan(const String& file, bool record, unsigned maxlen, bool autorepeat, const char* format = 0);
|
2004-05-22 00:05:20 +00:00
|
|
|
~WaveChan();
|
|
|
|
};
|
|
|
|
|
2005-11-04 19:30:47 +00:00
|
|
|
class Disconnector : public Thread
|
2004-12-20 04:11:29 +00:00
|
|
|
{
|
|
|
|
public:
|
2006-10-09 14:15:14 +00:00
|
|
|
Disconnector(CallEndpoint* chan, const String& id, DataSource* source, bool disc, const char* reason = 0);
|
2005-11-04 19:30:47 +00:00
|
|
|
virtual ~Disconnector();
|
2005-01-11 17:05:48 +00:00
|
|
|
virtual void run();
|
2005-11-04 19:30:47 +00:00
|
|
|
bool init();
|
2004-12-20 04:11:29 +00:00
|
|
|
private:
|
2006-03-22 18:22:34 +00:00
|
|
|
RefPointer<CallEndpoint> m_chan;
|
2005-11-04 19:30:47 +00:00
|
|
|
Message* m_msg;
|
2006-10-03 16:42:49 +00:00
|
|
|
DataSource* m_source;
|
2005-11-04 19:30:47 +00:00
|
|
|
bool m_disc;
|
2004-12-20 04:11:29 +00:00
|
|
|
};
|
|
|
|
|
2004-09-07 16:24:40 +00:00
|
|
|
class AttachHandler : public MessageHandler
|
|
|
|
{
|
|
|
|
public:
|
2004-12-21 04:16:09 +00:00
|
|
|
AttachHandler() : MessageHandler("chan.attach") { }
|
2004-09-07 16:24:40 +00:00
|
|
|
virtual bool received(Message &msg);
|
|
|
|
};
|
|
|
|
|
2005-05-30 15:13:02 +00:00
|
|
|
class RecordHandler : public MessageHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RecordHandler() : MessageHandler("chan.record") { }
|
|
|
|
virtual bool received(Message &msg);
|
|
|
|
};
|
|
|
|
|
2005-04-24 01:08:51 +00:00
|
|
|
class WaveFileDriver : public Driver
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
public:
|
2005-04-24 01:08:51 +00:00
|
|
|
WaveFileDriver();
|
2004-05-22 00:05:20 +00:00
|
|
|
virtual void initialize();
|
2005-04-24 01:08:51 +00:00
|
|
|
virtual bool msgExecute(Message& msg, String& dest);
|
2004-05-22 00:05:20 +00:00
|
|
|
private:
|
2005-04-24 01:08:51 +00:00
|
|
|
AttachHandler* m_handler;
|
2004-05-22 00:05:20 +00:00
|
|
|
};
|
|
|
|
|
2007-02-22 21:40:21 +00:00
|
|
|
bool s_asyncDelete = true;
|
|
|
|
|
2005-04-24 01:08:51 +00:00
|
|
|
INIT_PLUGIN(WaveFileDriver);
|
|
|
|
|
2007-01-12 22:16:38 +00:00
|
|
|
|
2007-03-19 23:06:06 +00:00
|
|
|
typedef struct {
|
|
|
|
uint32_t sign;
|
|
|
|
uint32_t offs;
|
|
|
|
uint32_t len;
|
|
|
|
uint32_t form;
|
|
|
|
uint32_t freq;
|
|
|
|
uint32_t chan;
|
|
|
|
} AuHeader;
|
|
|
|
|
|
|
|
#define ILBC_HEADER_LEN 9
|
|
|
|
|
|
|
|
|
|
|
|
WaveSource* WaveSource::create(const String& file, CallEndpoint* chan, bool autoclose, bool autorepeat)
|
|
|
|
{
|
|
|
|
WaveSource* tmp = new WaveSource(file,chan,autoclose);
|
|
|
|
tmp->init(file,autorepeat);
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WaveSource::init(const String& file, bool autorepeat)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
2005-01-22 02:20:42 +00:00
|
|
|
if (file == "-") {
|
2005-12-14 13:50:44 +00:00
|
|
|
m_nodata = true;
|
|
|
|
m_brate = 8000;
|
2005-01-22 02:20:42 +00:00
|
|
|
start("WaveSource");
|
|
|
|
return;
|
|
|
|
}
|
2006-07-01 00:02:45 +00:00
|
|
|
m_fd = ::open(file.safe(),O_RDONLY|O_NOCTTY|O_BINARY);
|
2004-12-21 04:16:09 +00:00
|
|
|
if (m_fd < 0) {
|
2005-11-04 19:30:47 +00:00
|
|
|
Debug(DebugWarn,"Opening '%s': error %d: %s",
|
2004-12-21 04:16:09 +00:00
|
|
|
file.c_str(), errno, ::strerror(errno));
|
2005-04-24 01:08:51 +00:00
|
|
|
m_format.clear();
|
2006-10-09 14:15:14 +00:00
|
|
|
notify(this,"error");
|
2004-12-21 04:16:09 +00:00
|
|
|
return;
|
|
|
|
}
|
2005-09-30 22:46:16 +00:00
|
|
|
if (file.endsWith(".gsm"))
|
2005-04-24 01:08:51 +00:00
|
|
|
m_format = "gsm";
|
2005-09-30 22:46:16 +00:00
|
|
|
else if (file.endsWith(".alaw") || file.endsWith(".A"))
|
2005-04-24 01:08:51 +00:00
|
|
|
m_format = "alaw";
|
2005-09-30 22:46:16 +00:00
|
|
|
else if (file.endsWith(".mulaw") || file.endsWith(".u"))
|
2005-04-24 01:08:51 +00:00
|
|
|
m_format = "mulaw";
|
2005-09-30 22:46:16 +00:00
|
|
|
else if (file.endsWith(".ilbc20"))
|
|
|
|
m_format = "ilbc20";
|
|
|
|
else if (file.endsWith(".ilbc30"))
|
|
|
|
m_format = "ilbc30";
|
2004-12-21 04:16:09 +00:00
|
|
|
else if (file.endsWith(".au"))
|
|
|
|
detectAuFormat();
|
|
|
|
else if (file.endsWith(".wav"))
|
|
|
|
detectWavFormat();
|
2005-09-30 22:46:16 +00:00
|
|
|
else if (file.endsWith(".lbc"))
|
|
|
|
detectIlbcFormat();
|
2004-12-21 04:16:09 +00:00
|
|
|
else if (!file.endsWith(".slin"))
|
|
|
|
Debug(DebugMild,"Unknown format for file '%s', assuming signed linear",file.c_str());
|
2006-12-14 18:09:46 +00:00
|
|
|
if (computeDataRate()) {
|
|
|
|
if (autorepeat)
|
|
|
|
m_repeatPos = ::lseek(m_fd,0,SEEK_CUR);
|
2007-02-22 21:40:21 +00:00
|
|
|
asyncDelete(s_asyncDelete);
|
2005-09-30 22:46:16 +00:00
|
|
|
start("WaveSource");
|
2006-12-14 18:09:46 +00:00
|
|
|
}
|
2005-11-04 19:30:47 +00:00
|
|
|
else {
|
2005-09-30 22:46:16 +00:00
|
|
|
Debug(DebugWarn,"Unable to compute data rate for file '%s'",file.c_str());
|
2006-10-09 14:15:14 +00:00
|
|
|
notify(this,"error");
|
2005-11-04 19:30:47 +00:00
|
|
|
}
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
|
|
|
|
2007-03-19 23:06:06 +00:00
|
|
|
WaveSource::WaveSource(const char* file, CallEndpoint* chan, bool autoclose)
|
|
|
|
: m_chan(chan), m_fd(-1), m_swap(false), m_brate(0), m_repeatPos(-1),
|
|
|
|
m_total(0), m_time(0), m_autoclose(autoclose), m_autoclean(false),
|
|
|
|
m_nodata(false)
|
|
|
|
{
|
|
|
|
Debug(&__plugin,DebugAll,"WaveSource::WaveSource(\"%s\",%p) [%p]",file,chan,this);
|
|
|
|
}
|
|
|
|
|
2004-05-22 00:05:20 +00:00
|
|
|
WaveSource::~WaveSource()
|
|
|
|
{
|
2005-05-06 18:13:33 +00:00
|
|
|
Debug(&__plugin,DebugAll,"WaveSource::~WaveSource() [%p] total=%u stamp=%lu",this,m_total,timeStamp());
|
2005-07-20 10:19:07 +00:00
|
|
|
stop();
|
2004-08-08 23:56:18 +00:00
|
|
|
if (m_time) {
|
|
|
|
m_time = Time::now() - m_time;
|
|
|
|
if (m_time) {
|
2005-04-09 22:10:00 +00:00
|
|
|
m_time = (m_total*(u_int64_t)1000000 + m_time/2) / m_time;
|
2005-05-06 18:13:33 +00:00
|
|
|
Debug(&__plugin,DebugInfo,"WaveSource rate=" FMT64U " b/s",m_time);
|
2004-08-08 23:56:18 +00:00
|
|
|
}
|
|
|
|
}
|
2004-05-22 00:05:20 +00:00
|
|
|
if (m_fd >= 0) {
|
|
|
|
::close(m_fd);
|
|
|
|
m_fd = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-21 04:16:09 +00:00
|
|
|
void WaveSource::detectAuFormat()
|
|
|
|
{
|
2007-03-19 23:06:06 +00:00
|
|
|
AuHeader header;
|
2004-12-21 04:16:09 +00:00
|
|
|
if ((::read(m_fd,&header,sizeof(header)) != sizeof(header)) ||
|
|
|
|
(ntohl(header.sign) != 0x2E736E64)) {
|
|
|
|
Debug(DebugMild,"Invalid .au file header, assuming raw signed linear");
|
|
|
|
::lseek(m_fd,0,SEEK_SET);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
::lseek(m_fd,ntohl(header.offs),SEEK_SET);
|
|
|
|
int samp = ntohl(header.freq);
|
|
|
|
int chan = ntohl(header.chan);
|
|
|
|
m_brate = samp;
|
|
|
|
switch (ntohl(header.form)) {
|
|
|
|
case 1:
|
2005-04-24 01:08:51 +00:00
|
|
|
m_format = "mulaw";
|
2004-12-21 04:16:09 +00:00
|
|
|
break;
|
|
|
|
case 27:
|
2005-04-24 01:08:51 +00:00
|
|
|
m_format = "alaw";
|
2004-12-21 04:16:09 +00:00
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
m_brate *= 2;
|
2005-01-22 02:20:42 +00:00
|
|
|
m_swap = true;
|
2004-12-21 04:16:09 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Debug(DebugMild,"Unknown .au format 0x%0X, assuming signed linear",ntohl(header.form));
|
|
|
|
}
|
|
|
|
if (samp != 8000)
|
2006-12-14 18:09:46 +00:00
|
|
|
m_format << "/" << samp;
|
|
|
|
if (chan > 1)
|
2004-12-21 04:16:09 +00:00
|
|
|
m_format = String(chan) + "*" + m_format;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WaveSource::detectWavFormat()
|
|
|
|
{
|
|
|
|
Debug(DebugMild,".wav not supported yet, assuming raw signed linear");
|
|
|
|
}
|
|
|
|
|
2005-09-30 22:46:16 +00:00
|
|
|
void WaveSource::detectIlbcFormat()
|
|
|
|
{
|
|
|
|
char header[ILBC_HEADER_LEN+1];
|
|
|
|
if (::read(m_fd,&header,ILBC_HEADER_LEN) == ILBC_HEADER_LEN) {
|
|
|
|
header[ILBC_HEADER_LEN] = '\0';
|
|
|
|
if (::strcmp(header,"#!iLBC20\n") == 0) {
|
|
|
|
m_format = "ilbc20";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (::strcmp(header,"#!iLBC30\n") == 0) {
|
|
|
|
m_format = "ilbc30";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Debug(DebugMild,"Invalid iLBC file, assuming raw signed linear");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WaveSource::computeDataRate()
|
|
|
|
{
|
|
|
|
if (m_brate)
|
|
|
|
return true;
|
|
|
|
const FormatInfo* info = m_format.getInfo();
|
|
|
|
if (!info)
|
|
|
|
return false;
|
|
|
|
m_brate = info->dataRate();
|
|
|
|
return (m_brate != 0);
|
|
|
|
}
|
|
|
|
|
2004-05-22 00:05:20 +00:00
|
|
|
void WaveSource::run()
|
|
|
|
{
|
2005-09-06 02:51:09 +00:00
|
|
|
unsigned long ts = 0;
|
2004-05-22 00:05:20 +00:00
|
|
|
int r = 0;
|
2005-09-02 16:39:00 +00:00
|
|
|
// wait until at least one consumer is attached
|
|
|
|
while (!r) {
|
|
|
|
m_mutex.lock();
|
|
|
|
r = m_consumers.count();
|
|
|
|
m_mutex.unlock();
|
|
|
|
Thread::yield(true);
|
2006-01-19 21:13:49 +00:00
|
|
|
if (!alive())
|
|
|
|
return;
|
2005-09-02 16:39:00 +00:00
|
|
|
}
|
2006-12-14 18:09:46 +00:00
|
|
|
unsigned int blen = (m_brate*20)/1000;
|
2006-10-03 16:42:49 +00:00
|
|
|
DDebug(&__plugin,DebugAll,"Consumer found, starting to play data with rate %d [%p]",m_brate,this);
|
2006-12-14 18:09:46 +00:00
|
|
|
m_data.assign(0,blen);
|
2005-09-02 16:39:00 +00:00
|
|
|
// start counting time from now
|
2005-04-09 22:10:00 +00:00
|
|
|
u_int64_t tpos = Time::now();
|
2004-08-08 23:56:18 +00:00
|
|
|
m_time = tpos;
|
2004-05-22 00:05:20 +00:00
|
|
|
do {
|
2005-01-22 02:20:42 +00:00
|
|
|
r = (m_fd >= 0) ? ::read(m_fd,m_data.data(),m_data.length()) : m_data.length();
|
2004-05-22 00:05:20 +00:00
|
|
|
if (r < 0) {
|
|
|
|
if (errno == EINTR) {
|
|
|
|
r = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2006-12-14 18:09:46 +00:00
|
|
|
if (!r) {
|
|
|
|
if (m_repeatPos >= 0) {
|
|
|
|
DDebug(&__plugin,DebugAll,"Autorepeating from offset %ld [%p]",
|
|
|
|
m_repeatPos,this);
|
|
|
|
::lseek(m_fd,m_repeatPos,SEEK_SET);
|
|
|
|
m_data.assign(0,blen);
|
|
|
|
r = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2004-12-20 04:11:29 +00:00
|
|
|
if (r < (int)m_data.length())
|
|
|
|
m_data.assign(m_data.data(),r);
|
2005-01-22 02:20:42 +00:00
|
|
|
if (m_swap) {
|
|
|
|
uint16_t* p = (uint16_t*)m_data.data();
|
|
|
|
for (int i = 0; i < r; i+= 2) {
|
|
|
|
*p = ntohs(*p);
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
}
|
2005-04-09 22:10:00 +00:00
|
|
|
int64_t dly = tpos - Time::now();
|
2004-05-22 00:05:20 +00:00
|
|
|
if (dly > 0) {
|
2005-05-06 18:13:33 +00:00
|
|
|
XDebug(&__plugin,DebugAll,"WaveSource sleeping for " FMT64 " usec",dly);
|
2005-04-09 22:10:00 +00:00
|
|
|
Thread::usleep((unsigned long)dly);
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
2006-10-03 17:10:56 +00:00
|
|
|
if (!alive()) {
|
|
|
|
m_autoclose = false;
|
2007-03-05 13:48:19 +00:00
|
|
|
// if this is a zombie it surely has no owner anymore
|
|
|
|
m_chan = 0;
|
2006-10-09 14:15:14 +00:00
|
|
|
notify(0,"replaced");
|
2006-10-03 16:42:49 +00:00
|
|
|
return;
|
2006-10-03 17:10:56 +00:00
|
|
|
}
|
2005-09-06 02:51:09 +00:00
|
|
|
Forward(m_data,ts);
|
|
|
|
ts += m_data.length()*8000/m_brate;
|
2004-05-22 00:05:20 +00:00
|
|
|
m_total += r;
|
2005-04-09 22:10:00 +00:00
|
|
|
tpos += (r*(u_int64_t)1000000/m_brate);
|
2004-05-22 00:05:20 +00:00
|
|
|
} while (r > 0);
|
2006-10-03 16:42:49 +00:00
|
|
|
Debug(&__plugin,DebugAll,"WaveSource '%s' end of data (%u played) chan=%p [%p]",m_id.c_str(),m_total,m_chan,this);
|
2006-06-21 21:46:55 +00:00
|
|
|
// at cleanup time deref the data source if we start no disconnector thread
|
2006-10-09 14:15:14 +00:00
|
|
|
m_autoclean = !notify(this,"eof");
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WaveSource::cleanup()
|
|
|
|
{
|
2007-03-05 13:48:19 +00:00
|
|
|
Debug(&__plugin,DebugAll,"WaveSource cleanup, total=%u, alive=%s, autoclean=%s [%p]",
|
|
|
|
m_total,String::boolText(alive()),String::boolText(m_autoclean),this);
|
2006-06-28 15:43:23 +00:00
|
|
|
if (m_autoclean) {
|
2007-01-12 22:16:38 +00:00
|
|
|
asyncDelete(false);
|
2006-06-28 15:43:23 +00:00
|
|
|
if (m_chan && (m_chan->getSource() == this))
|
|
|
|
m_chan->setSource();
|
|
|
|
else
|
|
|
|
deref();
|
2007-01-12 22:16:38 +00:00
|
|
|
return;
|
2006-06-28 15:43:23 +00:00
|
|
|
}
|
2007-01-12 22:16:38 +00:00
|
|
|
ThreadedSource::cleanup();
|
2005-11-04 19:30:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WaveSource::setNotify(const String& id)
|
|
|
|
{
|
|
|
|
m_id = id;
|
2005-12-14 13:50:44 +00:00
|
|
|
if ((m_fd < 0) && !m_nodata)
|
2006-10-03 17:10:56 +00:00
|
|
|
notify(this);
|
2005-11-04 19:30:47 +00:00
|
|
|
}
|
|
|
|
|
2006-10-09 14:15:14 +00:00
|
|
|
bool WaveSource::notify(DataSource* source, const char* reason)
|
2005-11-04 19:30:47 +00:00
|
|
|
{
|
2007-03-19 23:06:06 +00:00
|
|
|
if (!m_chan) {
|
|
|
|
if (m_id) {
|
|
|
|
DDebug(&__plugin,DebugAll,"WaveSource enqueueing notify message [%p]",this);
|
|
|
|
Message* m = new Message("chan.notify");
|
|
|
|
m->addParam("targetid",m_id);
|
|
|
|
if (reason)
|
|
|
|
m->addParam("reason",reason);
|
|
|
|
Engine::enqueue(m);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2005-11-04 19:30:47 +00:00
|
|
|
if (m_id || m_autoclose) {
|
2006-10-09 14:15:14 +00:00
|
|
|
DDebug(&__plugin,DebugInfo,"Preparing '%s' disconnector for '%s' chan '%s' source=%p [%p]",
|
|
|
|
reason,m_id.c_str(),(m_chan ? m_chan->id().c_str() : ""),source,this);
|
|
|
|
Disconnector *disc = new Disconnector(m_chan,m_id,source,m_autoclose,reason);
|
2006-06-21 21:46:55 +00:00
|
|
|
return disc->init();
|
2005-11-04 19:30:47 +00:00
|
|
|
}
|
2006-06-21 21:46:55 +00:00
|
|
|
return false;
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
|
|
|
|
2007-01-12 22:16:38 +00:00
|
|
|
|
2007-03-19 23:06:06 +00:00
|
|
|
WaveConsumer::WaveConsumer(const String& file, CallEndpoint* chan, unsigned maxlen, const char* format)
|
|
|
|
: m_chan(chan), m_fd(-1), m_swap(false), m_locked(false), m_header(None),
|
|
|
|
m_total(0), m_maxlen(maxlen), m_time(0)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
2007-03-19 23:06:06 +00:00
|
|
|
Debug(&__plugin,DebugAll,"WaveConsumer::WaveConsumer(\"%s\",%p,%u,\"%s\") [%p]",
|
|
|
|
file.c_str(),chan,maxlen,format,this);
|
|
|
|
if (format) {
|
|
|
|
m_locked = true;
|
|
|
|
m_format = format;
|
|
|
|
}
|
2005-01-22 02:20:42 +00:00
|
|
|
if (file == "-")
|
|
|
|
return;
|
|
|
|
else if (file.endsWith(".gsm"))
|
2005-04-24 01:08:51 +00:00
|
|
|
m_format = "gsm";
|
2004-12-21 04:16:09 +00:00
|
|
|
else if (file.endsWith(".alaw") || file.endsWith(".A"))
|
2005-04-24 01:08:51 +00:00
|
|
|
m_format = "alaw";
|
2004-12-21 04:16:09 +00:00
|
|
|
else if (file.endsWith(".mulaw") || file.endsWith(".u"))
|
2005-04-24 01:08:51 +00:00
|
|
|
m_format = "mulaw";
|
2005-09-30 22:46:16 +00:00
|
|
|
else if (file.endsWith(".ilbc20"))
|
|
|
|
m_format = "ilbc20";
|
|
|
|
else if (file.endsWith(".ilbc30"))
|
|
|
|
m_format = "ilbc30";
|
2007-03-19 23:06:06 +00:00
|
|
|
else if (file.endsWith(".lbc"))
|
|
|
|
m_header=Ilbc;
|
|
|
|
else if (file.endsWith(".au"))
|
|
|
|
m_header=Au;
|
2006-07-01 00:02:45 +00:00
|
|
|
m_fd = ::open(file.safe(),O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY|O_BINARY,S_IRUSR|S_IWUSR);
|
2004-05-22 00:05:20 +00:00
|
|
|
if (m_fd < 0)
|
2005-11-04 19:30:47 +00:00
|
|
|
Debug(DebugWarn,"Creating '%s': error %d: %s",
|
2004-09-07 16:24:40 +00:00
|
|
|
file.c_str(), errno, ::strerror(errno));
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WaveConsumer::~WaveConsumer()
|
|
|
|
{
|
2005-05-06 18:13:33 +00:00
|
|
|
Debug(&__plugin,DebugAll,"WaveConsumer::~WaveConsumer() [%p] total=%u stamp=%lu",this,m_total,timeStamp());
|
2004-08-08 23:56:18 +00:00
|
|
|
if (m_time) {
|
|
|
|
m_time = Time::now() - m_time;
|
|
|
|
if (m_time) {
|
2005-04-09 22:10:00 +00:00
|
|
|
m_time = (m_total*(u_int64_t)1000000 + m_time/2) / m_time;
|
2005-05-06 18:13:33 +00:00
|
|
|
Debug(&__plugin,DebugInfo,"WaveConsumer rate=" FMT64U " b/s",m_time);
|
2004-08-08 23:56:18 +00:00
|
|
|
}
|
|
|
|
}
|
2004-05-22 00:05:20 +00:00
|
|
|
if (m_fd >= 0) {
|
|
|
|
::close(m_fd);
|
|
|
|
m_fd = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-19 23:06:06 +00:00
|
|
|
void WaveConsumer::writeIlbcHeader() const
|
|
|
|
{
|
|
|
|
if (m_format == "ilbc20")
|
|
|
|
::write(m_fd,"#!iLBC20\n",ILBC_HEADER_LEN);
|
|
|
|
else if (m_format == "ilbc30")
|
|
|
|
::write(m_fd,"#!iLBC30\n",ILBC_HEADER_LEN);
|
|
|
|
else
|
|
|
|
Debug(DebugMild,"Invalid iLBC format '%s', not writing header",m_format.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void WaveConsumer::writeAuHeader()
|
|
|
|
{
|
|
|
|
AuHeader header;
|
2007-04-06 13:37:56 +00:00
|
|
|
String fmt = m_format;
|
|
|
|
int chans = 1;
|
|
|
|
int rate = 8000;
|
|
|
|
int sep = fmt.find('*');
|
|
|
|
if (sep > 0)
|
|
|
|
fmt >> chans >> "*";
|
|
|
|
sep = fmt.find('/');
|
|
|
|
if (sep > 0) {
|
|
|
|
rate = fmt.substr(sep+1).toInteger(rate);
|
|
|
|
fmt.assign(fmt,sep);
|
|
|
|
}
|
|
|
|
if (fmt == "slin") {
|
2007-03-19 23:06:06 +00:00
|
|
|
m_swap = true;
|
|
|
|
header.form = ntohl(3);
|
|
|
|
}
|
2007-04-06 13:37:56 +00:00
|
|
|
else if (fmt == "mulaw")
|
2007-03-19 23:06:06 +00:00
|
|
|
header.form = ntohl(1);
|
2007-04-06 13:37:56 +00:00
|
|
|
else if (fmt == "alaw")
|
2007-03-19 23:06:06 +00:00
|
|
|
header.form = ntohl(27);
|
|
|
|
else {
|
|
|
|
Debug(DebugMild,"Invalid au format '%s', not writing header",m_format.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
header.sign = htonl(0x2E736E64);
|
|
|
|
header.offs = htonl(sizeof(header));
|
2007-04-06 13:37:56 +00:00
|
|
|
header.freq = ntohl(rate);
|
|
|
|
header.chan = ntohl(chans);
|
2007-03-19 23:06:06 +00:00
|
|
|
header.len = 0;
|
|
|
|
::write(m_fd,&header,sizeof(header));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WaveConsumer::setFormat(const DataFormat& format)
|
|
|
|
{
|
|
|
|
if (m_locked || (format == "slin"))
|
|
|
|
return false;
|
|
|
|
bool ok = false;
|
|
|
|
switch (m_header) {
|
|
|
|
case Ilbc:
|
|
|
|
if ((format == "ilbc20") || (format == "ilbc30"))
|
|
|
|
ok = true;
|
|
|
|
break;
|
|
|
|
case Au:
|
2007-04-06 16:08:38 +00:00
|
|
|
if ((format.find("mulaw") >= 0) || (format.find("alaw") >= 0) || (format.find("slin") >= 0))
|
2007-03-19 23:06:06 +00:00
|
|
|
ok = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ok) {
|
|
|
|
DDebug(DebugInfo,"WaveConsumer new format '%s'",format.c_str());
|
|
|
|
m_format = format;
|
|
|
|
m_locked = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-09-06 02:51:09 +00:00
|
|
|
void WaveConsumer::Consume(const DataBlock& data, unsigned long tStamp)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
2004-09-07 16:24:40 +00:00
|
|
|
if (!data.null()) {
|
2004-08-08 23:56:18 +00:00
|
|
|
if (!m_time)
|
|
|
|
m_time = Time::now();
|
2007-03-19 23:06:06 +00:00
|
|
|
if (m_fd >= 0) {
|
|
|
|
switch (m_header) {
|
|
|
|
case Ilbc:
|
|
|
|
writeIlbcHeader();
|
|
|
|
break;
|
|
|
|
case Au:
|
|
|
|
writeAuHeader();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
m_header = None;
|
|
|
|
if (m_swap) {
|
|
|
|
unsigned int n = data.length();
|
|
|
|
DataBlock swapped(0,n);
|
|
|
|
const uint16_t* s = (const uint16_t*)data.data();
|
|
|
|
uint16_t* d = (uint16_t*)swapped.data();
|
|
|
|
for (unsigned int i = 0; i < n; i+= 2)
|
|
|
|
*d++ = ntohs(*s++);
|
|
|
|
::write(m_fd,swapped.data(),swapped.length());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
::write(m_fd,data.data(),data.length());
|
|
|
|
}
|
2004-05-22 00:05:20 +00:00
|
|
|
m_total += data.length();
|
2004-09-07 16:24:40 +00:00
|
|
|
if (m_maxlen && (m_total >= m_maxlen)) {
|
|
|
|
m_maxlen = 0;
|
|
|
|
if (m_fd >= 0) {
|
|
|
|
::close(m_fd);
|
|
|
|
m_fd = -1;
|
|
|
|
}
|
2004-12-20 04:11:29 +00:00
|
|
|
if (m_chan) {
|
2006-10-09 14:15:14 +00:00
|
|
|
DDebug(&__plugin,DebugInfo,"Preparing 'maxlen' disconnector for '%s' chan '%s' in consumer [%p]",
|
|
|
|
m_id.c_str(),(m_chan ? m_chan->id().c_str() : ""),this);
|
|
|
|
Disconnector *disc = new Disconnector(m_chan,m_id,0,false,"maxlen");
|
2005-01-11 17:05:48 +00:00
|
|
|
m_chan = 0;
|
2005-11-04 19:30:47 +00:00
|
|
|
disc->init();
|
2004-12-20 04:11:29 +00:00
|
|
|
}
|
2004-09-07 16:24:40 +00:00
|
|
|
}
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-12 22:16:38 +00:00
|
|
|
|
2006-10-09 14:15:14 +00:00
|
|
|
Disconnector::Disconnector(CallEndpoint* chan, const String& id, DataSource* source, bool disc, const char* reason)
|
2005-11-04 19:30:47 +00:00
|
|
|
: m_chan(chan), m_msg(0), m_source(source), m_disc(disc)
|
2005-01-11 17:05:48 +00:00
|
|
|
{
|
2005-11-04 19:30:47 +00:00
|
|
|
if (id) {
|
2007-03-19 23:06:06 +00:00
|
|
|
Message* m = new Message("chan.notify");
|
2006-10-09 14:15:14 +00:00
|
|
|
if (chan)
|
|
|
|
m->addParam("id",chan->id());
|
2005-11-04 19:30:47 +00:00
|
|
|
m->addParam("targetid",id);
|
2006-10-09 14:15:14 +00:00
|
|
|
if (reason)
|
|
|
|
m->addParam("reason",reason);
|
2005-11-04 19:30:47 +00:00
|
|
|
m->userData(chan);
|
|
|
|
m_msg = m;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Disconnector::~Disconnector()
|
|
|
|
{
|
2006-10-03 17:10:56 +00:00
|
|
|
if (m_msg) {
|
|
|
|
DDebug(&__plugin,DebugAll,"Disconnector enqueueing notify message [%p]",this);
|
2005-11-04 19:30:47 +00:00
|
|
|
Engine::enqueue(m_msg);
|
2006-10-03 17:10:56 +00:00
|
|
|
}
|
2005-11-04 19:30:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Disconnector::init()
|
|
|
|
{
|
|
|
|
if (error()) {
|
|
|
|
Debug(DebugFail,"Error creating disconnector thread %p",this);
|
|
|
|
delete this;
|
|
|
|
return false;
|
|
|
|
}
|
2006-06-21 21:46:55 +00:00
|
|
|
return startup();
|
2005-11-04 19:30:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Disconnector::run()
|
|
|
|
{
|
2006-10-03 17:10:56 +00:00
|
|
|
DDebug(&__plugin,DebugAll,"Disconnector::run() chan=%p msg=%p source=%s disc=%s [%p]",
|
|
|
|
(void*)m_chan,m_msg,String::boolText(m_source),String::boolText(m_disc),this);
|
2005-11-04 19:30:47 +00:00
|
|
|
if (!m_chan)
|
|
|
|
return;
|
|
|
|
if (m_source) {
|
2006-10-03 16:42:49 +00:00
|
|
|
if (m_chan->getSource() == m_source)
|
|
|
|
m_chan->setSource();
|
|
|
|
else
|
|
|
|
Debug(&__plugin,DebugMild,"Source %p in channel %p was replaced with %p",
|
|
|
|
m_source,(void*)m_chan,m_chan->getSource());
|
2005-11-04 19:30:47 +00:00
|
|
|
if (m_disc)
|
|
|
|
m_chan->disconnect("eof");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (m_msg)
|
|
|
|
m_chan->setConsumer();
|
|
|
|
else
|
|
|
|
m_chan->disconnect();
|
2005-01-11 17:05:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-12 22:16:38 +00:00
|
|
|
|
2007-03-19 23:06:06 +00:00
|
|
|
WaveChan::WaveChan(const String& file, bool record, unsigned maxlen, bool autorepeat, const char* format)
|
2005-04-24 01:08:51 +00:00
|
|
|
: Channel(__plugin)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
2005-05-06 18:13:33 +00:00
|
|
|
Debug(this,DebugAll,"WaveChan::WaveChan(%s) [%p]",(record ? "record" : "play"),this);
|
2004-05-22 00:05:20 +00:00
|
|
|
if (record) {
|
2007-03-19 23:06:06 +00:00
|
|
|
setConsumer(new WaveConsumer(file,this,maxlen,format));
|
2004-05-22 00:05:20 +00:00
|
|
|
getConsumer()->deref();
|
|
|
|
}
|
|
|
|
else {
|
2007-03-19 23:06:06 +00:00
|
|
|
setSource(WaveSource::create(file,this,true,autorepeat));
|
2004-05-22 00:05:20 +00:00
|
|
|
getSource()->deref();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WaveChan::~WaveChan()
|
|
|
|
{
|
2005-05-06 18:13:33 +00:00
|
|
|
Debug(this,DebugAll,"WaveChan::~WaveChan() %s [%p]",id().c_str(),this);
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
|
|
|
|
2007-01-12 22:16:38 +00:00
|
|
|
|
2004-09-07 16:24:40 +00:00
|
|
|
bool AttachHandler::received(Message &msg)
|
|
|
|
{
|
2007-03-19 23:06:06 +00:00
|
|
|
int more = 4;
|
2004-09-07 16:24:40 +00:00
|
|
|
String src(msg.getValue("source"));
|
|
|
|
if (src.null())
|
|
|
|
more--;
|
|
|
|
else {
|
|
|
|
Regexp r("^wave/\\([^/]*\\)/\\(.*\\)$");
|
|
|
|
if (src.matches(r)) {
|
|
|
|
if (src.matchString(1) == "play") {
|
|
|
|
src = src.matchString(2);
|
|
|
|
more--;
|
|
|
|
}
|
|
|
|
else {
|
2004-10-22 02:45:19 +00:00
|
|
|
Debug(DebugWarn,"Could not attach source with method '%s', use 'play'",
|
2004-09-07 16:24:40 +00:00
|
|
|
src.matchString(1).c_str());
|
2007-03-19 23:06:06 +00:00
|
|
|
src.clear();
|
2004-09-07 16:24:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2007-03-19 23:06:06 +00:00
|
|
|
src.clear();
|
2004-09-07 16:24:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String cons(msg.getValue("consumer"));
|
|
|
|
if (cons.null())
|
|
|
|
more--;
|
|
|
|
else {
|
|
|
|
Regexp r("^wave/\\([^/]*\\)/\\(.*\\)$");
|
|
|
|
if (cons.matches(r)) {
|
|
|
|
if (cons.matchString(1) == "record") {
|
|
|
|
cons = cons.matchString(2);
|
|
|
|
more--;
|
|
|
|
}
|
|
|
|
else {
|
2004-10-22 02:45:19 +00:00
|
|
|
Debug(DebugWarn,"Could not attach consumer with method '%s', use 'record'",
|
2004-09-07 16:24:40 +00:00
|
|
|
cons.matchString(1).c_str());
|
2007-03-19 23:06:06 +00:00
|
|
|
cons.clear();
|
2004-09-07 16:24:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2007-03-19 23:06:06 +00:00
|
|
|
cons.clear();
|
2004-09-07 16:24:40 +00:00
|
|
|
}
|
2005-09-28 22:06:11 +00:00
|
|
|
|
|
|
|
String ovr(msg.getValue("override"));
|
|
|
|
if (ovr.null())
|
|
|
|
more--;
|
|
|
|
else {
|
|
|
|
Regexp r("^wave/\\([^/]*\\)/\\(.*\\)$");
|
|
|
|
if (ovr.matches(r)) {
|
|
|
|
if (ovr.matchString(1) == "play") {
|
|
|
|
ovr = ovr.matchString(2);
|
|
|
|
more--;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Debug(DebugWarn,"Could not attach override with method '%s', use 'play'",
|
|
|
|
ovr.matchString(1).c_str());
|
2007-03-19 23:06:06 +00:00
|
|
|
ovr.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ovr.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
String repl(msg.getValue("replace"));
|
|
|
|
if (repl.null())
|
|
|
|
more--;
|
|
|
|
else {
|
|
|
|
Regexp r("^wave/\\([^/]*\\)/\\(.*\\)$");
|
|
|
|
if (repl.matches(r)) {
|
|
|
|
if (repl.matchString(1) == "play") {
|
|
|
|
repl = repl.matchString(2);
|
|
|
|
more--;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Debug(DebugWarn,"Could not attach replacement with method '%s', use 'play'",
|
|
|
|
repl.matchString(1).c_str());
|
|
|
|
repl.clear();
|
2005-09-28 22:06:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2007-03-19 23:06:06 +00:00
|
|
|
repl.clear();
|
2005-09-28 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2007-03-19 23:06:06 +00:00
|
|
|
if (src.null() && cons.null() && ovr.null() && repl.null())
|
2004-09-07 16:24:40 +00:00
|
|
|
return false;
|
|
|
|
|
2005-09-28 22:06:11 +00:00
|
|
|
// if single attach was requested we can return true if everything is ok
|
|
|
|
bool ret = msg.getBoolValue("single");
|
|
|
|
|
2004-09-07 16:24:40 +00:00
|
|
|
String ml(msg.getValue("maxlen"));
|
|
|
|
unsigned maxlen = ml.toInteger(0);
|
2005-05-14 01:38:58 +00:00
|
|
|
CallEndpoint *ch = static_cast<CallEndpoint*>(msg.userData());
|
2005-04-24 01:08:51 +00:00
|
|
|
if (!ch) {
|
2004-09-07 16:24:40 +00:00
|
|
|
if (!src.null())
|
2004-10-22 02:45:19 +00:00
|
|
|
Debug(DebugWarn,"Wave source '%s' attach request with no data channel!",src.c_str());
|
2004-09-07 16:24:40 +00:00
|
|
|
if (!cons.null())
|
2004-10-22 02:45:19 +00:00
|
|
|
Debug(DebugWarn,"Wave consumer '%s' attach request with no data channel!",cons.c_str());
|
2005-09-28 22:06:11 +00:00
|
|
|
if (!ovr.null())
|
|
|
|
Debug(DebugWarn,"Wave override '%s' attach request with no data channel!",ovr.c_str());
|
2004-09-07 16:24:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!src.null()) {
|
2007-03-19 23:06:06 +00:00
|
|
|
WaveSource* s = WaveSource::create(src,ch,false,msg.getBoolValue("autorepeat"));
|
2005-04-24 01:08:51 +00:00
|
|
|
ch->setSource(s);
|
2005-11-04 19:30:47 +00:00
|
|
|
s->setNotify(msg.getValue("notify"));
|
2004-09-07 16:24:40 +00:00
|
|
|
s->deref();
|
2005-09-28 22:06:11 +00:00
|
|
|
msg.clearParam("source");
|
2004-09-07 16:24:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!cons.null()) {
|
2007-03-19 23:06:06 +00:00
|
|
|
WaveConsumer* c = new WaveConsumer(cons,ch,maxlen,msg.getValue("format"));
|
2004-09-25 18:22:40 +00:00
|
|
|
c->setNotify(msg.getValue("notify"));
|
2005-04-24 01:08:51 +00:00
|
|
|
ch->setConsumer(c);
|
2004-09-07 16:24:40 +00:00
|
|
|
c->deref();
|
2005-09-28 22:06:11 +00:00
|
|
|
msg.clearParam("consumer");
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!ovr.null()) {
|
|
|
|
DataConsumer* c = ch->getConsumer();
|
|
|
|
if (!c) {
|
|
|
|
Debug(DebugWarn,"Wave override '%s' attach request with no consumer!",ovr.c_str());
|
|
|
|
ret = false;
|
|
|
|
break;
|
|
|
|
}
|
2007-03-19 23:06:06 +00:00
|
|
|
WaveSource* s = WaveSource::create(ovr,0,false,msg.getBoolValue("autorepeat"));
|
2005-09-28 22:06:11 +00:00
|
|
|
s->setNotify(msg.getValue("notify"));
|
|
|
|
if (DataTranslator::attachChain(s,c,true))
|
|
|
|
msg.clearParam("override");
|
|
|
|
else {
|
|
|
|
Debug(DebugWarn,"Failed to override attach wave '%s' to consumer %p",ovr.c_str(),c);
|
|
|
|
s->deref();
|
|
|
|
ret = false;
|
|
|
|
}
|
|
|
|
break;
|
2004-09-07 16:24:40 +00:00
|
|
|
}
|
|
|
|
|
2007-03-19 23:06:06 +00:00
|
|
|
while (!repl.null()) {
|
|
|
|
DataConsumer* c = ch->getConsumer();
|
|
|
|
if (!c) {
|
|
|
|
Debug(DebugWarn,"Wave replacement '%s' attach request with no consumer!",repl.c_str());
|
|
|
|
ret = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
WaveSource* s = WaveSource::create(repl,0,false,msg.getBoolValue("autorepeat"));
|
|
|
|
s->setNotify(msg.getValue("notify"));
|
|
|
|
if (DataTranslator::attachChain(s,c,false))
|
|
|
|
msg.clearParam("replace");
|
|
|
|
else {
|
|
|
|
Debug(DebugWarn,"Failed to replacement attach wave '%s' to consumer %p",repl.c_str(),c);
|
|
|
|
s->deref();
|
|
|
|
ret = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-09-07 16:24:40 +00:00
|
|
|
// Stop dispatching if we handled all requested
|
2005-09-28 22:06:11 +00:00
|
|
|
return ret && !more;
|
2004-09-07 16:24:40 +00:00
|
|
|
}
|
|
|
|
|
2007-01-12 22:16:38 +00:00
|
|
|
|
2005-05-30 15:13:02 +00:00
|
|
|
bool RecordHandler::received(Message &msg)
|
|
|
|
{
|
|
|
|
int more = 2;
|
|
|
|
|
|
|
|
String c1(msg.getValue("call"));
|
|
|
|
if (c1.null())
|
|
|
|
more--;
|
|
|
|
else {
|
|
|
|
Regexp r("^wave/\\([^/]*\\)/\\(.*\\)$");
|
|
|
|
if (c1.matches(r)) {
|
|
|
|
if (c1.matchString(1) == "record") {
|
|
|
|
c1 = c1.matchString(2);
|
|
|
|
more--;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Debug(DebugWarn,"Could not attach call recorder with method '%s', use 'record'",
|
|
|
|
c1.matchString(1).c_str());
|
|
|
|
c1 = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
c1 = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
String c2(msg.getValue("peer"));
|
|
|
|
if (c2.null())
|
|
|
|
more--;
|
|
|
|
else {
|
|
|
|
Regexp r("^wave/\\([^/]*\\)/\\(.*\\)$");
|
|
|
|
if (c2.matches(r)) {
|
|
|
|
if (c2.matchString(1) == "record") {
|
|
|
|
c2 = c2.matchString(2);
|
|
|
|
more--;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Debug(DebugWarn,"Could not attach peer recorder with method '%s', use 'record'",
|
|
|
|
c2.matchString(1).c_str());
|
|
|
|
c2 = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
c2 = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c1.null() && c2.null())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
String ml(msg.getValue("maxlen"));
|
|
|
|
unsigned maxlen = ml.toInteger(0);
|
|
|
|
|
2005-06-14 20:53:20 +00:00
|
|
|
CallEndpoint *ch = static_cast<CallEndpoint*>(msg.userObject("CallEndpoint"));
|
|
|
|
DataEndpoint *de = static_cast<DataEndpoint*>(msg.userObject("DataEndpoint"));
|
2005-05-30 15:13:02 +00:00
|
|
|
if (ch && !de)
|
|
|
|
de = ch->setEndpoint();
|
|
|
|
|
|
|
|
if (!de) {
|
|
|
|
if (!c1.null())
|
|
|
|
Debug(DebugWarn,"Wave source '%s' call record with no data channel!",c1.c_str());
|
|
|
|
if (!c2.null())
|
|
|
|
Debug(DebugWarn,"Wave source '%s' peer record with no data channel!",c2.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!c1.null()) {
|
2007-03-19 23:06:06 +00:00
|
|
|
WaveConsumer* c = new WaveConsumer(c1,ch,maxlen,msg.getValue("format"));
|
2005-05-30 15:13:02 +00:00
|
|
|
c->setNotify(msg.getValue("notify"));
|
|
|
|
de->setCallRecord(c);
|
|
|
|
c->deref();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!c2.null()) {
|
2007-03-19 23:06:06 +00:00
|
|
|
WaveConsumer* c = new WaveConsumer(c2,ch,maxlen,msg.getValue("format"));
|
2005-05-30 15:13:02 +00:00
|
|
|
c->setNotify(msg.getValue("notify"));
|
|
|
|
de->setPeerRecord(c);
|
|
|
|
c->deref();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop dispatching if we handled all requested
|
|
|
|
return !more;
|
|
|
|
}
|
|
|
|
|
2007-01-12 22:16:38 +00:00
|
|
|
|
2005-04-24 01:08:51 +00:00
|
|
|
bool WaveFileDriver::msgExecute(Message& msg, String& dest)
|
|
|
|
{
|
|
|
|
Regexp r("^\\([^/]*\\)/\\(.*\\)$");
|
|
|
|
if (!dest.matches(r))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool meth = false;
|
|
|
|
if (dest.matchString(1) == "record")
|
|
|
|
meth = true;
|
|
|
|
else if (dest.matchString(1) != "play") {
|
|
|
|
Debug(DebugWarn,"Invalid wavefile method '%s', use 'record' or 'play'",
|
|
|
|
dest.matchString(1).c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
String ml(msg.getValue("maxlen"));
|
|
|
|
unsigned maxlen = ml.toInteger(0);
|
2005-05-05 20:37:30 +00:00
|
|
|
CallEndpoint* ch = static_cast<CallEndpoint*>(msg.userData());
|
2005-04-24 01:08:51 +00:00
|
|
|
if (ch) {
|
2005-05-06 18:13:33 +00:00
|
|
|
Debug(this,DebugInfo,"%s wave file '%s'", (meth ? "Record to" : "Play from"),
|
2005-04-24 01:08:51 +00:00
|
|
|
dest.matchString(2).c_str());
|
2007-03-19 23:06:06 +00:00
|
|
|
WaveChan *c = new WaveChan(dest.matchString(2),meth,maxlen,msg.getBoolValue("autorepeat"),msg.getValue("format"));
|
2005-09-02 16:39:00 +00:00
|
|
|
if (ch->connect(c,msg.getValue("reason"))) {
|
2005-06-14 12:36:03 +00:00
|
|
|
msg.setParam("peerid",c->id());
|
2005-04-24 01:08:51 +00:00
|
|
|
c->deref();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
c->destruct();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Message m("call.route");
|
2005-05-05 20:37:30 +00:00
|
|
|
m.addParam("module",name());
|
|
|
|
String callto(msg.getValue("direct"));
|
|
|
|
if (callto.null()) {
|
|
|
|
const char *targ = msg.getValue("target");
|
|
|
|
if (!targ) {
|
|
|
|
Debug(DebugWarn,"Wave outgoing call with no target!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
callto = msg.getValue("caller");
|
|
|
|
if (callto.null())
|
|
|
|
callto << prefix() << dest;
|
|
|
|
m.addParam("called",targ);
|
|
|
|
m.addParam("caller",callto);
|
|
|
|
if (!Engine::dispatch(m)) {
|
|
|
|
Debug(DebugWarn,"Wave outgoing call but no route!");
|
|
|
|
return false;
|
2005-04-24 01:08:51 +00:00
|
|
|
}
|
2005-05-05 20:37:30 +00:00
|
|
|
callto = m.retValue();
|
|
|
|
m.retValue().clear();
|
2005-04-24 01:08:51 +00:00
|
|
|
}
|
2005-05-05 20:37:30 +00:00
|
|
|
m = "call.execute";
|
|
|
|
m.addParam("callto",callto);
|
2007-03-19 23:06:06 +00:00
|
|
|
WaveChan *c = new WaveChan(dest.matchString(2),meth,maxlen,msg.getBoolValue("autorepeat"),msg.getValue("format"));
|
2006-02-28 14:39:58 +00:00
|
|
|
m.setParam("id",c->id());
|
2005-05-05 20:37:30 +00:00
|
|
|
m.userData(c);
|
|
|
|
if (Engine::dispatch(m)) {
|
2006-02-28 14:39:58 +00:00
|
|
|
msg.setParam("id",c->id());
|
2005-05-05 20:37:30 +00:00
|
|
|
c->deref();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Debug(DebugWarn,"Wave outgoing call not accepted!");
|
|
|
|
c->destruct();
|
2005-04-24 01:08:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
WaveFileDriver::WaveFileDriver()
|
|
|
|
: Driver("wave","misc"), m_handler(0)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
Output("Loaded module WaveFile");
|
|
|
|
}
|
|
|
|
|
2005-04-24 01:08:51 +00:00
|
|
|
void WaveFileDriver::initialize()
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
Output("Initializing module WaveFile");
|
2005-04-24 01:08:51 +00:00
|
|
|
setup();
|
2007-02-22 21:40:21 +00:00
|
|
|
s_asyncDelete = Engine::config().getBoolValue("hacks","asyncdelete",true);
|
2004-05-22 00:05:20 +00:00
|
|
|
if (!m_handler) {
|
2005-04-24 01:08:51 +00:00
|
|
|
m_handler = new AttachHandler;
|
2004-05-22 00:05:20 +00:00
|
|
|
Engine::install(m_handler);
|
2005-05-30 15:13:02 +00:00
|
|
|
Engine::install(new RecordHandler);
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-27 14:53:18 +00:00
|
|
|
}; // anonymous namespace
|
|
|
|
|
2004-05-22 00:05:20 +00:00
|
|
|
/* vi: set ts=8 sw=4 sts=4 noet: */
|