Yate
yatephone.h
1 
22 #ifndef __YATEPHONE_H
23 #define __YATEPHONE_H
24 
25 #ifndef __cplusplus
26 #error C++ is required
27 #endif
28 
29 #include <yatengine.h>
30 
34 namespace TelEngine {
35 
39 struct YATE_API ImageInfo {
43  int width;
44 
48  int height;
49 
53  int depth;
54 };
55 
59 struct YATE_API FormatInfo {
63  const char* name;
64 
68  const char* type;
69 
73  int frameSize;
74 
78  int frameTime;
79 
84 
89 
93  bool converter;
94 
100  int guessSamples(int len) const;
101 
106  int dataRate() const;
107 
111  inline FormatInfo()
112  : name(0), type("audio"),
113  frameSize(0), frameTime(0),
114  sampleRate(8000), numChannels(1),
115  converter(false)
116  { }
117 
121  inline explicit FormatInfo(const char* _name, int fsize = 0, int ftime = 10000,
122  const char* _type = "audio", int srate = 8000, int nchan = 1, bool convert = false)
123  : name(_name), type(_type),
124  frameSize(fsize), frameTime(ftime),
125  sampleRate(srate), numChannels(nchan),
126  converter(convert)
127  { }
128 };
129 
130 class DataEndpoint;
131 class CallEndpoint;
132 class Driver;
133 
138 struct YATE_API TranslatorCaps {
140  const FormatInfo* src;
142  const FormatInfo* dest;
144  int cost;
145 };
146 
151 class YATE_API FormatRepository
152 {
153  YNOCOPY(FormatRepository); // no automatic copies please
154 private:
156 public:
162  static const FormatInfo* getFormat(const String& name);
163 
175  static const FormatInfo* addFormat(const String& name, int fsize, int ftime, const String& type = "audio", int srate = 8000, int nchan = 1);
176 };
177 
182 class YATE_API DataFormat : public NamedList
183 {
184 public:
188  inline DataFormat()
189  : NamedList((const char*)0), m_parsed(0)
190  { }
191 
196  inline DataFormat(const char* value)
197  : NamedList(value), m_parsed(0)
198  { }
199 
204  inline DataFormat(const DataFormat& value)
205  : NamedList(value), m_parsed(value.getInfo())
206  { }
207 
212  inline DataFormat(const String& value)
213  : NamedList(value), m_parsed(0)
214  { }
215 
220  inline DataFormat(const NamedList& value)
221  : NamedList(value), m_parsed(0)
222  { }
223 
228  inline DataFormat(const String* value)
229  : NamedList(value ? value->c_str() : (const char*)0), m_parsed(0)
230  { }
231 
236  inline explicit DataFormat(const FormatInfo* format)
237  : NamedList(format ? format->name : (const char*)0), m_parsed(format)
238  { }
239 
243  inline DataFormat& operator=(const DataFormat& value)
244  { NamedList::operator=(value); m_parsed = value.getInfo(); return *this; }
245 
250  const FormatInfo* getInfo() const;
251 
257  inline int frameSize(int defValue = 0) const
258  { return getInfo() ? getInfo()->frameSize : defValue; }
259 
265  inline int frameTime(int defValue = 0) const
266  { return getInfo() ? getInfo()->frameTime : defValue; }
267 
274  inline int sampleRate(int defValue = 0) const
275  { return getInfo() ? getInfo()->sampleRate : defValue; }
276 
282  inline int numChannels(int defValue = 1) const
283  { return getInfo() ? getInfo()->numChannels : defValue; }
284 
285 protected:
289  virtual void changed();
290 
291 private:
292  mutable const FormatInfo* m_parsed;
293 };
294 
298 class YATE_API DataNode : public RefObject
299 {
300  friend class DataEndpoint;
301  YNOCOPY(DataNode); // no automatic copies please
302 public:
306  enum DataFlags {
307  DataStart = 0x0001,
308  DataEnd = 0x0002,
309  DataMark = 0x0004,
310  DataSilent = 0x0008,
311  DataMissed = 0x0010,
312  DataError = 0x0020,
313  DataPrivate = 0x0100
314  };
315 
320  inline explicit DataNode(const char* format = 0)
321  : m_format(format), m_timestamp(0)
322  { }
323 
329  virtual int costFormat(const DataFormat& format)
330  { return -1; }
331 
337  virtual bool setFormat(const DataFormat& format)
338  { return false; }
339 
344  inline const DataFormat& getFormat() const
345  { return m_format; }
346 
351  inline unsigned long timeStamp() const
352  { return m_timestamp; }
353 
358  virtual bool valid() const
359  { return true; }
360 
366  virtual bool control(NamedList& params)
367  { return false; }
368 
373  inline static unsigned long invalidStamp()
374  { return (unsigned long)-1; }
375 
381  virtual void attached(bool added)
382  { }
383 
384 protected:
385  DataFormat m_format;
386  unsigned long m_timestamp;
387 };
388 
389 class DataSource;
390 class DataTranslator;
391 class TranslatorFactory;
392 class ThreadedSourcePrivate;
393 
397 class YATE_API DataConsumer : public DataNode
398 {
399  friend class DataSource;
400 
401 public:
406  inline explicit DataConsumer(const char* format = "slin")
407  : DataNode(format),
408  m_source(0), m_override(0),
409  m_regularTsDelta(0), m_overrideTsDelta(0), m_lastTsTime(0)
410  { }
411 
415  virtual void destroyed();
416 
422  virtual void* getObject(const String& name) const;
423 
433  virtual unsigned long Consume(const DataBlock& data, unsigned long tStamp, unsigned long flags) = 0;
434 
439  inline DataSource* getConnSource() const
440  { return m_source; }
441 
446  inline DataSource* getOverSource() const
447  { return m_override; }
448 
453  virtual DataSource* getTransSource() const
454  { return 0; }
455 
456 protected:
462  virtual bool synchronize(DataSource* source);
463 
464 private:
465  unsigned long Consume(const DataBlock& data, unsigned long tStamp,
466  unsigned long flags, DataSource* source);
467  DataSource* m_source;
468  DataSource* m_override;
469  long m_regularTsDelta;
470  long m_overrideTsDelta;
471  u_int64_t m_lastTsTime;
472 };
473 
477 class YATE_API DataSource : public DataNode, public Mutex
478 {
479  friend class DataTranslator;
480  YNOCOPY(DataSource); // no automatic copies please
481 public:
486  inline explicit DataSource(const char* format = "slin")
487  : DataNode(format), Mutex(false,"DataSource"),
488  m_nextStamp(invalidStamp()), m_translator(0) { }
489 
493  virtual void destroyed();
494 
500  virtual void* getObject(const String& name) const;
501 
506  virtual bool valid() const;
507 
513  virtual bool control(NamedList& params);
514 
522  unsigned long Forward(const DataBlock& data, unsigned long tStamp = invalidStamp(),
523  unsigned long flags = 0);
524 
531  bool attach(DataConsumer* consumer, bool override = false);
532 
538  bool detach(DataConsumer* consumer);
539 
543  void clear();
544 
550  { return m_translator; }
551 
556  void synchronize(unsigned long tStamp);
557 
562  inline unsigned long nextStamp() const
563  { return m_nextStamp; }
564 
565 protected:
566  unsigned long m_nextStamp;
567  ObjList m_consumers;
568 private:
569  inline void setTranslator(DataTranslator* translator) {
570  Lock mylock(this);
571  m_translator = translator;
572  }
573  bool detachInternal(DataConsumer* consumer);
574  DataTranslator* m_translator;
575 };
576 
581 class YATE_API ThreadedSource : public DataSource
582 {
583  friend class ThreadedSourcePrivate;
584 public:
588  virtual void destroyed();
589 
596  bool start(const char* name = "ThreadedSource", Thread::Priority prio = Thread::Normal);
597 
601  void stop();
602 
607  Thread* thread() const;
608 
613  bool running() const;
614 
615 protected:
620  inline explicit ThreadedSource(const char* format = "slin")
621  : DataSource(format), m_thread(0)
622  { }
623 
627  virtual void run() = 0;
628 
633  virtual void cleanup();
634 
640  bool looping(bool runConsumers = false) const;
641 
642 private:
643  ThreadedSourcePrivate* m_thread;
644 };
645 
651 class YATE_API DataTranslator : public DataConsumer
652 {
653  friend class TranslatorFactory;
654 public:
660  DataTranslator(const char* sFormat, const char* dFormat);
661 
668  explicit DataTranslator(const char* sFormat, DataSource* source = 0);
669 
673  ~DataTranslator();
674 
680  virtual void* getObject(const String& name) const;
681 
686  virtual bool valid() const
687  { return m_tsource && m_tsource->valid(); }
688 
693  virtual DataSource* getTransSource() const
694  { return m_tsource; }
695 
700  DataTranslator* getFirstTranslator();
701 
706  const DataTranslator* getFirstTranslator() const;
707 
716  static ObjList* srcFormats(const DataFormat& dFormat = "slin", int maxCost = -1, unsigned int maxLen = 0, ObjList* lst = 0);
717 
726  static ObjList* destFormats(const DataFormat& sFormat = "slin", int maxCost = -1, unsigned int maxLen = 0, ObjList* lst = 0);
727 
736  static ObjList* allFormats(const ObjList* formats, bool existing = true, bool sameRate = true, bool sameChans = true);
737 
746  static ObjList* allFormats(const String& formats, bool existing = true, bool sameRate = true, bool sameChans = true);
747 
754  static bool canConvert(const DataFormat& fmt1, const DataFormat& fmt2 = "slin");
755 
762  static int cost(const DataFormat& sFormat, const DataFormat& dFormat);
763 
770  static DataTranslator* create(const DataFormat& sFormat, const DataFormat& dFormat);
771 
779  static bool attachChain(DataSource* source, DataConsumer* consumer, bool override = false);
780 
787  static bool detachChain(DataSource* source, DataConsumer* consumer);
788 
793  static void setMaxChain(unsigned int maxChain);
794 
795 protected:
800  inline ObjList* getConsumers() const
801  { return m_tsource ? m_tsource->m_consumers.skipNull() : 0; }
802 
808  virtual bool synchronize(DataSource* source);
809 
814  static void install(TranslatorFactory* factory);
815 
820  static void uninstall(TranslatorFactory* factory);
821 
822 private:
823  DataTranslator(); // No default constructor please
824  static void compose();
825  static void compose(TranslatorFactory* factory);
826  static bool canConvert(const FormatInfo* fmt1, const FormatInfo* fmt2);
827  DataSource* m_tsource;
828  static Mutex s_mutex;
829  static ObjList s_factories;
830  static unsigned int s_maxChain;
831 };
832 
838 class YATE_API TranslatorFactory : public GenObject
839 {
840  YNOCOPY(TranslatorFactory); // no automatic copies please
841 protected:
846  inline explicit TranslatorFactory(const char* name = 0)
847  : m_name(name ? name : "?")
848  { m_counter = Thread::getCurrentObjCounter(true); DataTranslator::install(this); }
849 
850 public:
854  virtual ~TranslatorFactory();
855 
860  virtual void removed(const TranslatorFactory* factory);
861 
868  virtual DataTranslator* create(const DataFormat& sFormat, const DataFormat& dFormat) = 0;
869 
874  virtual const TranslatorCaps* getCapabilities() const = 0;
875 
882  virtual bool converts(const DataFormat& sFormat, const DataFormat& dFormat) const;
883 
888  virtual unsigned int length() const;
889 
895  virtual bool intermediate(const FormatInfo* info) const;
896 
901  virtual const FormatInfo* intermediate() const;
902 
907  virtual const char* name() const
908  { return m_name; }
909 
914  inline NamedCounter* objectsCounter() const
915  { return m_counter; }
916 
917 private:
918  const char* m_name;
919  NamedCounter* m_counter;
920 };
921 
927 class YATE_API DataEndpoint : public RefObject
928 {
929  YNOCOPY(DataEndpoint); // no automatic copies please
930 public:
934  explicit DataEndpoint(CallEndpoint* call = 0, const char* name = "audio");
935 
939  virtual void destroyed();
940 
946  virtual void* getObject(const String& name) const;
947 
952  virtual const String& toString() const;
953 
958  Mutex* mutex() const;
959 
964  static Mutex& commonMutex();
965 
971  bool connect(DataEndpoint* peer);
972 
977  bool disconnect();
978 
983  void setSource(DataSource* source = 0);
984 
989  inline DataSource* getSource() const
990  { return m_source; }
991 
996  void setConsumer(DataConsumer* consumer = 0);
997 
1002  inline DataConsumer* getConsumer() const
1003  { return m_consumer; }
1004 
1010  void setPeerRecord(DataConsumer* consumer = 0);
1011 
1016  inline DataConsumer* getPeerRecord() const
1017  { return m_peerRecord; }
1018 
1024  void setCallRecord(DataConsumer* consumer = 0);
1025 
1030  inline DataConsumer* getCallRecord() const
1031  { return m_callRecord; }
1032 
1038  bool clearData(DataNode* node);
1039 
1045  bool addSniffer(DataConsumer* sniffer);
1046 
1052  bool delSniffer(DataConsumer* sniffer);
1053 
1060  DataConsumer* getSniffer(const String& name, bool ref = false);
1061 
1065  void clearSniffers();
1066 
1071  inline DataEndpoint* getPeer() const
1072  { return m_peer; }
1073 
1078  inline CallEndpoint* getCall() const
1079  { return m_call; }
1080 
1085  inline const String& name() const
1086  { return m_name; }
1087 
1093  inline void clearCall(const CallEndpoint* call)
1094  { if (call == m_call) m_call = 0; }
1095 
1101  virtual bool control(NamedList& params);
1102 
1103 protected:
1109  virtual bool nativeConnect(DataEndpoint* peer)
1110  { return false; }
1111 
1112 private:
1113  String m_name;
1114  DataSource* m_source;
1115  DataConsumer* m_consumer;
1116  DataEndpoint* m_peer;
1117  CallEndpoint* m_call;
1118  DataConsumer* m_peerRecord;
1119  DataConsumer* m_callRecord;
1120  ObjList m_sniffers;
1121 };
1122 
1127 class YATE_API CallEndpoint : public RefObject
1128 {
1129  friend class DataEndpoint;
1130  YNOCOPY(CallEndpoint); // no automatic copies please
1131 private:
1132  CallEndpoint* m_peer;
1133  const void* m_lastPeer;
1134  String m_id;
1135  String m_lastPeerId;
1136 
1137 protected:
1138  ObjList m_data;
1139  Mutex* m_mutex;
1140 
1141 public:
1145  virtual void destroyed();
1146 
1152  virtual void* getObject(const String& name) const;
1153 
1158  virtual const String& toString() const
1159  { return m_id; }
1160 
1165  inline const String& id() const
1166  { return m_id; }
1167 
1172  inline CallEndpoint* getPeer() const
1173  { return m_peer; }
1174 
1180  bool getPeerId(String& id) const;
1181 
1186  String getPeerId() const;
1187 
1193  bool getLastPeerId(String& id) const;
1194 
1198  void setLastPeerId();
1199 
1204  inline Mutex* mutex() const
1205  { return m_mutex; }
1206 
1211  static Mutex& commonMutex();
1212 
1220  bool connect(CallEndpoint* peer, const char* reason = 0, bool notify = true);
1221 
1229  inline bool disconnect(const char* reason = 0, bool notify = true, const NamedList* params = 0)
1230  { return disconnect(false,reason,notify,params); }
1231 
1238  inline bool disconnect(const char* reason, const NamedList& params)
1239  { return disconnect(false,reason,true,&params); }
1240 
1246  DataEndpoint* getEndpoint(const String& type = CallEndpoint::audioType()) const;
1247 
1253  DataEndpoint* setEndpoint(const String& type = CallEndpoint::audioType());
1254 
1259  void clearEndpoint(const String& type = String::empty());
1260 
1266  void setSource(DataSource* source = 0, const String& type = CallEndpoint::audioType());
1267 
1273  DataSource* getSource(const String& type = CallEndpoint::audioType()) const;
1274 
1280  void setConsumer(DataConsumer* consumer = 0, const String& type = CallEndpoint::audioType());
1281 
1287  DataConsumer* getConsumer(const String& type = CallEndpoint::audioType()) const;
1288 
1295  bool clearData(DataNode* node, const String& type = CallEndpoint::audioType());
1296 
1301  static const String& audioType();
1302 
1303 protected:
1307  CallEndpoint(const char* id = 0);
1308 
1313  virtual void connected(const char* reason) { }
1314 
1320  virtual void disconnected(bool final, const char* reason) { }
1321 
1326  virtual void setDisconnect(const NamedList* params) { }
1327 
1335  void setPeer(CallEndpoint* peer, const char* reason = 0, bool notify = true, const NamedList* params = 0);
1336 
1341  void setEndpoint(DataEndpoint* endPoint);
1342 
1347  virtual void setId(const char* newId);
1348 
1349 private:
1350  bool disconnect(bool final, const char* reason, bool notify, const NamedList* params);
1351 };
1352 
1357 class YATE_API Module : public Plugin, public Mutex, public MessageReceiver
1358 {
1359  YNOCOPY(Module); // no automatic copies please
1360 private:
1361  bool m_init;
1362  int m_relays;
1363  String m_type;
1364  Regexp m_filter;
1365  u_int64_t m_changed;
1366  static unsigned int s_delay;
1367 
1368 public:
1374  virtual void* getObject(const String& name) const;
1375 
1380  inline const String& type() const
1381  { return m_type; }
1382 
1387  void changed();
1388 
1393  inline static unsigned int updateDelay()
1394  { return s_delay; }
1395 
1400  inline static void updateDelay(unsigned int delay)
1401  { s_delay = delay; }
1402 
1407  inline bool filterInstalled() const
1408  { return !m_filter.null(); }
1409 
1415  bool filterDebug(const String& item) const;
1416 
1424  static bool itemComplete(String& itemList, const String& item, const String& partWord);
1425 
1426 protected:
1430  enum {
1431  // Module messages
1432  Status = 0x00000001,
1433  Timer = 0x00000002,
1434  Level = 0x00000004,
1435  Command = 0x00000008,
1436  Help = 0x00000010,
1437  Halt = 0x00000020,
1438  Route = 0x00000040,
1439  Stop = 0x00000080,
1440  // Driver messages
1441  Execute = 0x00000100,
1442  Drop = 0x00000200,
1443  // Channel messages
1444  Locate = 0x00000400,
1445  Masquerade = 0x00000800,
1446  Ringing = 0x00001000,
1447  Answered = 0x00002000,
1448  Tone = 0x00004000,
1449  Text = 0x00008000,
1450  Progress = 0x00010000,
1451  Update = 0x00020000,
1452  Transfer = 0x00040000,
1453  Control = 0x00080000,
1454  // Instant messaging related
1455  MsgExecute = 0x00100000,
1456  // Last possible public ID
1457  PubLast = 0x00ffffff,
1458  // Private messages base ID
1459  Private = 0x01000000
1460  } RelayID;
1461 
1467  static const char* messageName(int id);
1468 
1474  static inline int relayId(const char* name)
1475  { return lookup(name,s_messages); }
1476 
1483  Module(const char* name, const char* type = 0, bool earlyInit = false);
1484 
1488  virtual ~Module();
1489 
1493  virtual void initialize();
1494 
1498  void setup();
1499 
1505  inline bool relayInstalled(int id) const
1506  { return (id & m_relays) != 0; }
1507 
1514  bool installRelay(int id, unsigned priority = 100);
1515 
1522  bool installRelay(const char* name, unsigned priority = 100);
1523 
1531  bool installRelay(int id, const char* name, unsigned priority = 100);
1532 
1538  bool installRelay(MessageRelay* relay);
1539 
1546  bool uninstallRelay(MessageRelay* relay, bool delRelay = true);
1547 
1554  bool uninstallRelay(int id, bool delRelay = true);
1555 
1560  bool uninstallRelays();
1561 
1568  virtual bool received(Message &msg, int id);
1569 
1574  virtual void genUpdate(Message& msg);
1575 
1580  virtual void msgTimer(Message& msg);
1581 
1586  virtual void msgStatus(Message& msg);
1587 
1593  virtual bool msgRoute(Message& msg);
1594 
1601  virtual bool msgCommand(Message& msg);
1602 
1607  virtual void statusModule(String& str);
1608 
1613  virtual void statusParams(String& str);
1614 
1619  virtual void statusDetail(String& str);
1620 
1627  virtual bool commandExecute(String& retVal, const String& line);
1628 
1636  virtual bool commandComplete(Message& msg, const String& partLine, const String& partWord);
1637 
1643  virtual bool setDebug(Message& msg, const String& target);
1644 
1645 private:
1646  Module(); // no default constructor please
1647  static TokenDict s_messages[];
1648  ObjList m_relayList;
1649 };
1650 
1655 class YATE_API Channel : public CallEndpoint, public DebugEnabler, public MessageNotifier
1656 {
1657  friend class Driver;
1658  friend class Router;
1659  YNOCOPY(Channel); // no automatic copies please
1660 private:
1661  NamedList m_parameters;
1662  NamedList* m_chanParams; // Channel parameters to be set in all messages
1663  Driver* m_driver;
1664  bool m_outgoing;
1665  u_int64_t m_timeout;
1666  u_int64_t m_maxcall;
1667  u_int64_t m_maxPDD; // Timeout while waiting for some progress on outgoing calls
1668  u_int64_t m_dtmfTime;
1669  unsigned int m_toutAns;
1670  unsigned int m_dtmfSeq;
1671  String m_dtmfText;
1672  String m_dtmfDetected;
1673 
1674 protected:
1675  String m_status;
1676  String m_address;
1677  String m_targetid;
1678  String m_billid;
1679  bool m_answered;
1680 
1681 public:
1685  virtual ~Channel();
1686 
1692  virtual void* getObject(const String& name) const;
1693 
1698  static Mutex& paramMutex();
1699 
1705  virtual void complete(Message& msg, bool minimal = false) const;
1706 
1714  Message* message(const char* name, bool minimal = false, bool data = false);
1715 
1726  Message* message(const char* name, const NamedList* original, const char* params = 0, bool minimal = false, bool data = false);
1727 
1738  inline Message* message(const char* name, const NamedList& original, const char* params = 0, bool minimal = false, bool data = false)
1739  { return message(name,&original,params,minimal,data); }
1740 
1746  virtual bool msgProgress(Message& msg);
1747 
1753  virtual bool msgRinging(Message& msg);
1754 
1760  virtual bool msgAnswered(Message& msg);
1761 
1768  virtual bool msgTone(Message& msg, const char* tone);
1769 
1776  virtual bool msgText(Message& msg, const char* text);
1777 
1784  virtual bool msgDrop(Message& msg, const char* reason);
1785 
1791  virtual bool msgTransfer(Message& msg);
1792 
1798  virtual bool msgUpdate(Message& msg);
1799 
1805  virtual bool msgMasquerade(Message& msg);
1806 
1811  virtual void msgStatus(Message& msg);
1812 
1818  virtual bool msgControl(Message& msg);
1819 
1825  virtual void checkTimers(Message& msg, const Time& tmr);
1826 
1833  virtual bool callPrerouted(Message& msg, bool handled);
1834 
1840  virtual bool callRouted(Message& msg);
1841 
1846  virtual void callAccept(Message& msg);
1847 
1854  virtual void callRejected(const char* error, const char* reason = 0, const Message* msg = 0);
1855 
1861  virtual void callConnect(Message& msg);
1862 
1867  virtual bool setDebug(Message& msg);
1868 
1876  inline String& getStatus(String& buf, bool append = true) const {
1877  Lock lck(chanDataMutex());
1878  if (append)
1879  buf += m_status;
1880  else
1881  buf = m_status;
1882  return buf;
1883  }
1884 
1892  inline void putStatus(NamedList& list, const char* param = "status", bool append = true) const {
1893  NamedString* ns = new NamedString(param);
1894  getStatus(*ns);
1895  if (append)
1896  list.addParam(ns);
1897  else
1898  list.setParam(ns);
1899  }
1900 
1905  inline const String& address() const
1906  { return m_address; }
1907 
1912  inline bool isOutgoing() const
1913  { return m_outgoing; }
1914 
1919  inline bool isIncoming() const
1920  { return !m_outgoing; }
1921 
1926  inline bool isAnswered() const
1927  { return m_answered; }
1928 
1933  const char* direction() const;
1934 
1939  inline Driver* driver() const
1940  { return m_driver; }
1941 
1946  inline u_int64_t timeout() const
1947  { return m_timeout; }
1948 
1953  inline void timeout(u_int64_t tout)
1954  { m_timeout = tout; }
1955 
1960  inline u_int64_t maxcall() const
1961  { return m_maxcall; }
1962 
1967  inline void maxcall(u_int64_t tout)
1968  { m_maxcall = tout; }
1969 
1975  inline void setMaxcall(const Message& msg, int defTout = -1)
1976  { setMaxcall(&msg,defTout); }
1977 
1983  void setMaxcall(const Message* msg, int defTout = -1);
1984 
1990  inline u_int64_t maxPDD() const
1991  { return m_maxPDD; }
1992 
1998  inline void maxPDD(u_int64_t tout)
1999  { m_maxPDD = tout; }
2000 
2006  void setMaxPDD(const Message& msg);
2007 
2013  inline const String& targetid() const
2014  { return m_targetid; }
2015 
2021  inline const String& billid() const
2022  { return m_billid; }
2023 
2028  void initChan();
2029 
2036  bool startRouter(Message* msg);
2037 
2042  static unsigned int allocId();
2043 
2048  void filterDebug(const String& item);
2049 
2054  inline const NamedList& parameters() const
2055  { return m_parameters; }
2056 
2062  inline void setChanParams(const NamedList& list, bool in = false) {
2063  const String& pref = in ? list[YSTRING("ichanparams-prefix")] : list[YSTRING("chanparams-prefix")];
2064  if (!pref)
2065  return;
2066  Lock lck(paramMutex());
2067  if (!m_chanParams)
2068  m_chanParams = new NamedList("");
2069  m_chanParams->copySubParams(list,pref,true,true);
2070  }
2071 
2076  inline void copyChanParams(NamedList& list) const {
2077  if (!m_chanParams)
2078  return;
2079  Lock lck(paramMutex());
2080  list.copyParams(*m_chanParams);
2081  }
2082 
2088  virtual void dispatched(const Message& msg, bool handled);
2089 
2090 protected:
2094  Channel(Driver* driver, const char* id = 0, bool outgoing = false);
2095 
2099  Channel(Driver& driver, const char* id = 0, bool outgoing = false);
2100 
2105  void cleanup();
2106 
2110  void dropChan();
2111 
2116  virtual void zeroRefs();
2117 
2122  virtual void connected(const char* reason);
2123 
2129  virtual void disconnected(bool final, const char* reason);
2130 
2135  virtual void setDisconnect(const NamedList* params);
2136 
2142  virtual void endDisconnect(const Message& msg, bool handled);
2143 
2148  virtual void setId(const char* newId);
2149 
2155  virtual Message* getDisconnect(const char* reason);
2156 
2162  void status(const char* newstat);
2163 
2169  inline const String& getStatus() const
2170  { return m_status; }
2171 
2176  virtual void statusParams(String& str);
2177 
2182  inline void setOutgoing(bool outgoing = true)
2183  { m_outgoing = outgoing; }
2184 
2190  bool dtmfSequence(Message& msg);
2191 
2197  bool dtmfEnqueue(Message* msg);
2198 
2205  bool dtmfInband(const char* tone);
2206 
2213  bool toneDetect(const char* sniffer = 0);
2214 
2220  { return m_parameters; }
2221 
2222 private:
2223  void init();
2224  Channel(); // no default constructor please
2225  static Mutex s_chanDataMutex;
2226  // Just in case we are going to (re)move the channel data mutex!
2227  static inline Mutex* chanDataMutex()
2228  { return &s_chanDataMutex; }
2229 };
2230 
2235 class YATE_API Driver : public Module
2236 {
2237  friend class Router;
2238  friend class Channel;
2239 
2240 private:
2241  bool m_init;
2242  bool m_varchan;
2243  String m_prefix;
2244  ObjList m_chans;
2245  int m_routing;
2246  int m_routed;
2247  int m_total;
2248  unsigned int m_nextid;
2249  int m_timeout;
2250  int m_maxroute;
2251  int m_maxchans;
2252  int m_chanCount;
2253  bool m_dtmfDups;
2254  volatile bool m_doExpire;
2255 
2256 public:
2262  virtual void* getObject(const String& name) const;
2263 
2268  inline const String& prefix() const
2269  { return m_prefix; }
2270 
2275  inline bool varchan() const
2276  { return m_varchan; }
2277 
2282  inline ObjList& channels()
2283  { return m_chans; }
2284 
2290  virtual Channel* find(const String& id) const;
2291 
2296  virtual bool isBusy() const;
2297 
2302  virtual void dropAll(Message &msg);
2303 
2309  virtual bool canAccept(bool routers = true);
2310 
2315  virtual bool canRoute();
2316 
2321  unsigned int nextid();
2322 
2327  inline unsigned int lastid() const
2328  { return m_nextid; }
2329 
2334  inline int timeout() const
2335  { return m_timeout; }
2336 
2341  inline int routing() const
2342  { return m_routing; }
2343 
2348  inline int routed() const
2349  { return m_routed; }
2350 
2355  inline int total() const
2356  { return m_total; }
2357 
2362  inline int chanCount() const
2363  { return m_chanCount; }
2364 
2369  inline int maxChans() const
2370  { return m_maxchans; }
2371 
2372 protected:
2378  Driver(const char* name, const char* type = 0);
2379 
2383  virtual void initialize();
2384 
2390  void setup(const char* prefix = 0, bool minimal = false);
2391 
2398  virtual bool received(Message &msg, int id);
2399 
2404  virtual void genUpdate(Message& msg);
2405 
2412  virtual bool hasLine(const String& line) const;
2413 
2420  virtual bool msgRoute(Message& msg);
2421 
2428  virtual bool msgExecute(Message& msg, String& dest) = 0;
2429 
2437  virtual bool commandComplete(Message& msg, const String& partLine, const String& partWord);
2438 
2443  virtual void statusModule(String& str);
2444 
2449  virtual void statusParams(String& str);
2450 
2455  virtual void statusDetail(String& str);
2456 
2462  virtual bool setDebug(Message& msg, const String& target);
2463 
2467  virtual void loadLimits();
2468 
2473  virtual bool canStopCall() const
2474  { return false; }
2475 
2480  inline void varchan(bool variable)
2481  { m_varchan = variable; }
2482 
2487  inline void timeout(int tout)
2488  { m_timeout = tout; }
2489 
2494  inline void maxRoute(int ncalls)
2495  { m_maxroute = ncalls; }
2496 
2501  inline void maxChans(int ncalls)
2502  { m_maxchans = ncalls; }
2503 
2508  inline void dtmfDups(bool duplicates)
2509  { m_dtmfDups = duplicates; }
2510 
2511 private:
2512  Driver(); // no default constructor please
2513 };
2514 
2519 class YATE_API Router : public Thread
2520 {
2521  YNOCOPY(Router); // no automatic copies please
2522 private:
2523  Driver* m_driver;
2524  String m_id;
2525  Message* m_msg;
2526 
2527 public:
2534  Router(Driver* driver, const char* id, Message* msg);
2535 
2539  virtual void run();
2540 
2545  virtual bool route();
2546 
2550  virtual void cleanup();
2551 
2552 protected:
2557  const String& id() const
2558  { return m_id; }
2559 };
2560 
2565 class YATE_API CallAccount
2566 {
2568 private:
2569  Mutex* m_mutex;
2570  NamedList m_inbParams;
2571  NamedList m_outParams;
2572  NamedList m_regParams;
2573 
2574 public:
2579  void pickAccountParams(const NamedList& params);
2580 
2581 
2586  void setInboundParams(NamedList& params);
2587 
2592  void setOutboundParams(NamedList& params);
2593 
2598  void setRegisterParams(NamedList& params);
2599 
2604  inline const NamedList& inboundParams() const
2605  { return m_inbParams; }
2606 
2611  inline const NamedList& outboundParams() const
2612  { return m_outParams; }
2613 
2618  inline const NamedList& registerParams() const
2619  { return m_regParams; }
2620 
2621 protected:
2626  inline CallAccount(Mutex* mutex)
2627  : m_mutex(mutex), m_inbParams(""), m_outParams(""), m_regParams("")
2628  { }
2629 };
2630 
2636 YATE_API bool isE164(const char* str);
2637 
2638 }; // namespace TelEngine
2639 
2640 #endif /* __YATEPHONE_H */
2641 
2642 /* vi: set ts=8 sw=4 sts=4 noet: */
CallEndpoint * getPeer() const
Definition: yatephone.h:1172
bool varchan() const
Definition: yatephone.h:2275
bool null() const
Definition: yateclass.h:2265
virtual void setDisconnect(const NamedList *params)
Definition: yatephone.h:1326
static NamedCounter * getCurrentObjCounter(bool always=false)
bool isAnswered() const
Definition: yatephone.h:1926
const FormatInfo * src
Definition: yatephone.h:140
void dtmfDups(bool duplicates)
Definition: yatephone.h:2508
int frameSize
Definition: yatephone.h:73
DataFormat(const String &value)
Definition: yatephone.h:212
int sampleRate
Definition: yatephone.h:83
const DataFormat & getFormat() const
Definition: yatephone.h:344
A class that holds just a block of raw data.
Definition: yateclass.h:4236
static int relayId(const char *name)
Definition: yatephone.h:1474
virtual bool valid() const
Definition: yatephone.h:358
Definition: yateclass.h:1173
A regexp matching class.
Definition: yateclass.h:3270
An unidirectional data translator (codec)
Definition: yatephone.h:838
const String & targetid() const
Definition: yatephone.h:2013
void setMaxcall(const Message &msg, int defTout=-1)
Definition: yatephone.h:1975
ObjList * skipNull() const
virtual void attached(bool added)
Definition: yatephone.h:381
CallEndpoint * getCall() const
Definition: yatephone.h:1078
DataEndpoint * getPeer() const
Definition: yatephone.h:1071
DataConsumer(const char *format="slin")
Definition: yatephone.h:406
static void install(TranslatorFactory *factory)
DataFormat(const DataFormat &value)
Definition: yatephone.h:204
const FormatInfo * dest
Definition: yatephone.h:142
Post-dispatching message hook.
Definition: yatengine.h:683
unsigned long timeStamp() const
Definition: yatephone.h:351
virtual const String & toString() const
Definition: yatephone.h:1158
Definition: yatephone.h:138
void timeout(u_int64_t tout)
Definition: yatephone.h:1953
Thread support class.
Definition: yateclass.h:6004
const String & billid() const
Definition: yatephone.h:2021
void setOutgoing(bool outgoing=true)
Definition: yatephone.h:2182
Mutex * mutex() const
Definition: yatephone.h:1204
constant YSTRING(const char *string)
Data source with own thread.
Definition: yatephone.h:581
const String & id() const
Definition: yatephone.h:2557
const String & type() const
Definition: yatephone.h:1380
static unsigned int updateDelay()
Definition: yatephone.h:1393
int depth
Definition: yatephone.h:53
int height
Definition: yatephone.h:48
TranslatorFactory(const char *name=0)
Definition: yatephone.h:846
bool isIncoming() const
Definition: yatephone.h:1919
Call routing thread.
Definition: yatephone.h:2519
bool disconnect(const char *reason=0, bool notify=true, const NamedList *params=0)
Definition: yatephone.h:1229
int timeout() const
Definition: yatephone.h:2334
int frameSize(int defValue=0) const
Definition: yatephone.h:257
bool filterInstalled() const
Definition: yatephone.h:1407
virtual bool valid() const
Definition: yatephone.h:686
A multiple message receiver.
Definition: yatengine.h:614
CallAccount(Mutex *mutex)
Definition: yatephone.h:2626
Definition: yatephone.h:397
const NamedList & registerParams() const
Definition: yatephone.h:2618
An abstract communication channel.
Definition: yatephone.h:1655
A message handler relay.
Definition: yatengine.h:630
bool isOutgoing() const
Definition: yatephone.h:1912
int numChannels(int defValue=1) const
Definition: yatephone.h:282
virtual bool canStopCall() const
Definition: yatephone.h:2473
NamedList & setParam(NamedString *param)
Definition: yateclass.h:5094
DataSource * getConnSource() const
Definition: yatephone.h:439
const String & getStatus() const
Definition: yatephone.h:2169
NamedList & copyParams(const NamedList &original)
virtual bool setFormat(const DataFormat &format)
Definition: yatephone.h:337
void setChanParams(const NamedList &list, bool in=false)
Definition: yatephone.h:2062
DataFlags
Definition: yatephone.h:306
static unsigned long invalidStamp()
Definition: yatephone.h:373
int lookup(const char *str, const TokenDict *tokens, int defvalue=0, int base=0)
int sampleRate(int defValue=0) const
Definition: yatephone.h:274
int width
Definition: yatephone.h:43
u_int64_t timeout() const
Definition: yatephone.h:1946
FormatInfo()
Definition: yatephone.h:111
int frameTime(int defValue=0) const
Definition: yatephone.h:265
const NamedList & inboundParams() const
Definition: yatephone.h:2604
ObjList & channels()
Definition: yatephone.h:2282
int routing() const
Definition: yatephone.h:2341
const String & id() const
Definition: yatephone.h:1165
An abstract call endpoint.
Definition: yatephone.h:1127
DataSource * getSource() const
Definition: yatephone.h:989
An unidirectional data translator (codec)
Definition: yatephone.h:651
void varchan(bool variable)
Definition: yatephone.h:2480
ObjList * getConsumers() const
Definition: yatephone.h:800
A time holding class.
Definition: yateclass.h:3926
virtual void connected(const char *reason)
Definition: yatephone.h:1313
DataNode(const char *format=0)
Definition: yatephone.h:320
DataFormat(const FormatInfo *format)
Definition: yatephone.h:236
void maxChans(int ncalls)
Definition: yatephone.h:2501
void putStatus(NamedList &list, const char *param="status", bool append=true) const
Definition: yatephone.h:1892
A holder for a debug level.
Definition: yateclass.h:311
Definition: yatephone.h:477
Definition: yatephone.h:59
int numChannels
Definition: yatephone.h:88
A message container class.
Definition: yatengine.h:312
A Data format.
Definition: yatephone.h:182
unsigned long nextStamp() const
Definition: yatephone.h:562
bool converter
Definition: yatephone.h:93
ThreadedSource(const char *format="slin")
Definition: yatephone.h:620
const String & prefix() const
Definition: yatephone.h:2268
u_int64_t maxPDD() const
Definition: yatephone.h:1990
A Channel driver module.
Definition: yatephone.h:2235
DataConsumer * getPeerRecord() const
Definition: yatephone.h:1016
static const String & audioType()
const NamedList & outboundParams() const
Definition: yatephone.h:2611
A named string class.
Definition: yateclass.h:3480
const String & name() const
Definition: yatephone.h:1085
const char * name
Definition: yatephone.h:63
void timeout(int tout)
Definition: yatephone.h:2487
int chanCount() const
Definition: yatephone.h:2362
String & getStatus(String &buf, bool append=true) const
Definition: yatephone.h:1876
const char * c_str(const String *str)
Definition: yateclass.h:3155
NamedList & operator=(const NamedList &value)
DataTranslator * getTranslator() const
Definition: yatephone.h:549
A data transfer endpoint capable of sending and/or receiving data.
Definition: yatephone.h:927
const FormatInfo * getInfo() const
bool relayInstalled(int id) const
Definition: yatephone.h:1505
A named string container class.
Definition: yateclass.h:5015
static void updateDelay(unsigned int delay)
Definition: yatephone.h:1400
void maxcall(u_int64_t tout)
Definition: yatephone.h:1967
Ephemeral mutex or semaphore locking object.
Definition: yateclass.h:5832
DataSource * getOverSource() const
Definition: yatephone.h:446
DataConsumer * getConsumer() const
Definition: yatephone.h:1002
void clearCall(const CallEndpoint *call)
Definition: yatephone.h:1093
int cost
Definition: yatephone.h:144
unsigned int lastid() const
Definition: yatephone.h:2327
virtual void disconnected(bool final, const char *reason)
Definition: yatephone.h:1320
int total() const
Definition: yatephone.h:2355
NamedList & addParam(NamedString *param)
virtual DataSource * getTransSource() const
Definition: yatephone.h:693
virtual DataSource * getTransSource() const
Definition: yatephone.h:453
DataConsumer * getCallRecord() const
Definition: yatephone.h:1030
const NamedList & parameters() const
Definition: yatephone.h:2054
DataSource(const char *format="slin")
Definition: yatephone.h:486
Definition: yateclass.h:219
virtual bool nativeConnect(DataEndpoint *peer)
Definition: yatephone.h:1109
NamedCounter * objectsCounter() const
Definition: yatephone.h:914
A C-style string handling class.
Definition: yateclass.h:2130
void maxRoute(int ncalls)
Definition: yatephone.h:2494
Driver * driver() const
Definition: yatephone.h:1939
const String & address() const
Definition: yatephone.h:1905
DataFormat & operator=(const DataFormat &value)
Definition: yatephone.h:243
Definition: yateclass.h:848
DataFormat(const char *value)
Definition: yatephone.h:196
Priority
Definition: yateclass.h:6014
virtual const char * name() const
Definition: yatephone.h:907
FormatInfo(const char *_name, int fsize=0, int ftime=10000, const char *_type="audio", int srate=8000, int nchan=1, bool convert=false)
Definition: yatephone.h:121
u_int64_t maxcall() const
Definition: yatephone.h:1960
An object list class.
Definition: yateclass.h:1453
Definition: yatephone.h:298
void copyChanParams(NamedList &list) const
Definition: yatephone.h:2076
NamedList & parameters()
Definition: yatephone.h:2219
DataFormat(const NamedList &value)
Definition: yatephone.h:220
void YNOCOPY(class type)
A Plugin that implements a module.
Definition: yatephone.h:1357
void maxPDD(u_int64_t tout)
Definition: yatephone.h:1998
DataFormat()
Definition: yatephone.h:188
static const String & empty()
bool disconnect(const char *reason, const NamedList &params)
Definition: yatephone.h:1238
virtual int costFormat(const DataFormat &format)
Definition: yatephone.h:329
int frameTime
Definition: yatephone.h:78
Settings for an account handling calls.
Definition: yatephone.h:2565
int routed() const
Definition: yatephone.h:2348
Plugin support.
Definition: yatengine.h:1051
NamedList & copySubParams(const NamedList &original, const String &prefix, bool skipPrefix=true, bool replace=false)
Mutex support.
Definition: yateclass.h:5606
virtual bool control(NamedList &params)
Definition: yatephone.h:366
const char * type
Definition: yatephone.h:68
Definition: yateclass.h:1048
int maxChans() const
Definition: yatephone.h:2369
DataFormat(const String *value)
Definition: yatephone.h:228
Definition: yatephone.h:39
Message * message(const char *name, const NamedList &original, const char *params=0, bool minimal=false, bool data=false)
Definition: yatephone.h:1738
A repository for media formats.
Definition: yatephone.h:151
Atomic counter with name.
Definition: yateclass.h:3601
bool isE164(const char *str)