00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208 #ifndef _VXML_H
00209 #define _VXML_H
00210
00211 #ifdef P_USE_PRAGMA
00212 #pragma interface
00213 #endif
00214
00215
00216 #include <ptclib/pxml.h>
00217
00218 #if P_EXPAT
00219
00220 #include <ptlib/pipechan.h>
00221 #include <ptclib/delaychan.h>
00222 #include <ptclib/pwavfile.h>
00223 #include <ptclib/ptts.h>
00224 #include <ptclib/url.h>
00225
00226 #include <queue>
00227
00228
00229 class PVXMLSession;
00230 class PVXMLDialog;
00231 class PVXMLSession;
00232
00233
00234 #define VXML_PCM16 "PCM-16"
00235 #define VXML_G7231 "G.723.1"
00236 #define VXML_G729 "G.729"
00237
00238 #define PVXML_HAS_FACTORY 1
00239
00240 class PVXMLGrammar : public PObject
00241 {
00242 PCLASSINFO(PVXMLGrammar, PObject);
00243 public:
00244 PVXMLGrammar(PXMLElement * field);
00245 virtual BOOL OnUserInput(const char ) { return TRUE; }
00246 virtual void Stop() { }
00247
00248 PString GetValue() const { return value; }
00249 PXMLElement * GetField() { return field; }
00250
00251 enum GrammarState {
00252 FILLED,
00253 NOINPUT,
00254 NOMATCH,
00255 HELP };
00256
00257 GrammarState GetState() const { return state; }
00258
00259 protected:
00260 PXMLElement * field;
00261 PString value;
00262 GrammarState state;
00263 };
00264
00265
00267
00268 class PVXMLMenuGrammar : public PVXMLGrammar
00269 {
00270 PCLASSINFO(PVXMLMenuGrammar, PVXMLGrammar);
00271 public:
00272 PVXMLMenuGrammar(PXMLElement * field);
00273 };
00274
00275
00277
00278 class PVXMLDigitsGrammar : public PVXMLGrammar
00279 {
00280 PCLASSINFO(PVXMLDigitsGrammar, PVXMLGrammar);
00281 public:
00282 PVXMLDigitsGrammar(PXMLElement * field, PINDEX minDigits, PINDEX maxDigits, PString terminators);
00283 BOOL OnUserInput(const char ch);
00284 virtual void Stop();
00285
00286 protected:
00287 PINDEX minDigits;
00288 PINDEX maxDigits;
00289 PString terminators;
00290 };
00291
00292
00294
00295 class PVXMLCache : public PMutex
00296 {
00297 public:
00298 PVXMLCache(const PDirectory & _directory);
00299
00300 PFilePath CreateFilename(const PString & prefix, const PString & key, const PString & fileType);
00301
00302 void Put(const PString & prefix,
00303 const PString & key,
00304 const PString & fileType,
00305 const PString & contentType,
00306 const PFilePath & fn,
00307 PFilePath & dataFn);
00308
00309 BOOL Get(const PString & prefix,
00310 const PString & key,
00311 const PString & fileType,
00312 PString & contentType,
00313 PFilePath & fn);
00314
00315 PFilePath GetCacheDir() const
00316 { return directory; }
00317
00318 PFilePath GetRandomFilename(const PString & prefix, const PString & fileType);
00319
00320 static PVXMLCache & GetResourceCache();
00321
00322 protected:
00323 PDirectory directory;
00324 };
00325
00327
00328 class PVXMLChannel;
00329
00330 class PVXMLChannelInterface {
00331 public:
00332 virtual ~PVXMLChannelInterface() { }
00333 virtual PWAVFile * CreateWAVFile(const PFilePath & fn, PFile::OpenMode mode, int opts, unsigned fmt) = 0;
00334 virtual void RecordEnd() = 0;
00335 virtual void OnEndRecording(const PString & channelName) = 0;
00336 virtual void Trigger() = 0;
00337 };
00338
00340
00341 class PVXMLSession : public PIndirectChannel, public PVXMLChannelInterface
00342 {
00343 PCLASSINFO(PVXMLSession, PIndirectChannel);
00344 public:
00345 PVXMLSession(PTextToSpeech * tts = NULL, BOOL autoDelete = FALSE);
00346 virtual ~PVXMLSession();
00347
00348 void SetFinishWhenEmpty(BOOL v)
00349 { finishWhenEmpty = v; }
00350
00351
00352 PTextToSpeech * SetTextToSpeech(PTextToSpeech * _tts, BOOL autoDelete = FALSE);
00353 PTextToSpeech * SetTextToSpeech(const PString & ttsName);
00354 PTextToSpeech * GetTextToSpeech() { return textToSpeech; }
00355
00356 virtual BOOL Load(const PString & source);
00357 virtual BOOL LoadFile(const PFilePath & file);
00358 virtual BOOL LoadURL(const PURL & url);
00359 virtual BOOL LoadVXML(const PString & xml);
00360 virtual BOOL IsLoaded() const { return loaded; }
00361
00362 virtual BOOL Open(BOOL isPCM);
00363 virtual BOOL Open(const PString & mediaFormat);
00364 virtual BOOL Close();
00365
00366 BOOL Execute();
00367
00368 PVXMLChannel * GetAndLockVXMLChannel()
00369 {
00370 sessionMutex.Wait();
00371 if (vxmlChannel != NULL)
00372 return vxmlChannel;
00373 sessionMutex.Signal();
00374 return NULL;
00375 }
00376 void UnLockVXMLChannel() { sessionMutex.Signal(); }
00377 PMutex & GetSessionMutex() { return sessionMutex; }
00378
00379 BOOL LoadGrammar(PVXMLGrammar * grammar);
00380
00381 virtual BOOL PlayText(const PString & text, PTextToSpeech::TextType type = PTextToSpeech::Default, PINDEX repeat = 1, PINDEX delay = 0);
00382 BOOL ConvertTextToFilenameList(const PString & _text, PTextToSpeech::TextType type, PStringArray & list, BOOL useCacheing);
00383
00384 virtual BOOL PlayFile(const PString & fn, PINDEX repeat = 1, PINDEX delay = 0, BOOL autoDelete = FALSE);
00385 virtual BOOL PlayData(const PBYTEArray & data, PINDEX repeat = 1, PINDEX delay = 0);
00386 virtual BOOL PlayCommand(const PString & data, PINDEX repeat = 1, PINDEX delay = 0);
00387 virtual BOOL PlayResource(const PURL & url, PINDEX repeat = 1, PINDEX delay = 0);
00388 virtual BOOL PlayTone(const PString & toneSpec, PINDEX repeat = 1, PINDEX delay = 0);
00389
00390
00391 virtual BOOL PlaySilence(PINDEX msecs = 0);
00392 virtual BOOL PlaySilence(const PTimeInterval & timeout);
00393
00394 virtual void SetPause(BOOL pause);
00395 virtual void GetBeepData(PBYTEArray & data, unsigned ms);
00396
00397 virtual BOOL StartRecording(const PFilePath & fn, BOOL recordDTMFTerm, const PTimeInterval & recordMaxTime, const PTimeInterval & recordFinalSilence);
00398 virtual BOOL EndRecording();
00399 virtual BOOL IsPlaying() const;
00400 virtual BOOL IsRecording() const;
00401
00402 virtual BOOL OnUserInput(const PString & str);
00403
00404 PString GetXMLError() const;
00405
00406 virtual void OnEndSession() { }
00407
00408 virtual PString GetVar(const PString & str) const;
00409 virtual void SetVar(const PString & ostr, const PString & val);
00410 virtual PString EvaluateExpr(const PString & oexpr);
00411
00412 virtual BOOL RetreiveResource(const PURL & url, PString & contentType, PFilePath & fn, BOOL useCache = TRUE);
00413
00414 PDECLARE_NOTIFIER(PThread, PVXMLSession, VXMLExecute);
00415
00416 void SetCallingToken( PString& token ) { callingCallToken = token; }
00417
00418 PXMLElement * FindHandler(const PString & event);
00419
00420
00421 PWAVFile * CreateWAVFile(const PFilePath & fn, PFile::OpenMode mode, int opts, unsigned fmt);
00422 void OnEndRecording(const PString & channelName);
00423 void RecordEnd();
00424 void Trigger();
00425
00426 PStringToString & GetSessionVars() { return sessionVars; }
00427
00428 protected:
00429 void Initialise();
00430
00431 void AllowClearCall();
00432 void ProcessUserInput();
00433 void ProcessNode();
00434 void ProcessGrammar();
00435
00436 BOOL TraverseAudio();
00437 BOOL TraverseGoto();
00438 BOOL TraverseGrammar();
00439 BOOL TraverseRecord();
00440
00441 BOOL TraverseIf();
00442 BOOL TraverseExit();
00443 BOOL TraverseVar();
00444 BOOL TraverseSubmit();
00445 BOOL TraverseMenu();
00446 BOOL TraverseChoice(const PString & grammarResult);
00447 BOOL TraverseProperty();
00448
00449 void SayAs(const PString & className, const PString & text);
00450 void SayAs(const PString & className, const PString & text, const PString & voice);
00451
00452 static PTimeInterval StringToTime(const PString & str);
00453
00454 PURL NormaliseResourceName(const PString & src);
00455
00456 PXMLElement * FindForm(const PString & id);
00457
00458
00459
00460 PSyncPoint waitForEvent;
00461
00462 PMutex sessionMutex;
00463
00464 PXML xmlFile;
00465
00466 PVXMLGrammar * activeGrammar;
00467 BOOL listening;
00468 int timeout;
00469
00470 PStringToString sessionVars;
00471 PStringToString documentVars;
00472
00473 PMutex userInputMutex;
00474 std::queue<char> userInputQueue;
00475
00476 BOOL recording;
00477 PFilePath recordFn;
00478 BOOL recordDTMFTerm;
00479 PTimeInterval recordMaxTime;
00480 PTimeInterval recordFinalSilence;
00481 PSyncPoint recordSync;
00482
00483 BOOL loaded;
00484 BOOL finishWhenEmpty;
00485 BOOL allowFinish;
00486 PURL rootURL;
00487 BOOL emptyAction;
00488
00489 PThread * vxmlThread;
00490 BOOL threadRunning;
00491 BOOL forceEnd;
00492
00493 PString mediaFormat;
00494 PVXMLChannel * vxmlChannel;
00495
00496 PTextToSpeech * textToSpeech;
00497 BOOL autoDeleteTextToSpeech;
00498
00499 PXMLElement * currentForm;
00500 PXMLElement * currentField;
00501 PXMLObject * currentNode;
00502
00503 private:
00504 void ExecuteDialog();
00505
00506 PString callingCallToken;
00507 PSyncPoint answerSync;
00508 PString grammarResult;
00509 PString eventName;
00510 PINDEX defaultDTMF;
00511 };
00512
00513
00515
00516 class PVXMLRecordable : public PObject
00517 {
00518 PCLASSINFO(PVXMLRecordable, PObject);
00519 public:
00520 PVXMLRecordable()
00521 { consecutiveSilence = 0; finalSilence = 3000; maxDuration = 30000; }
00522
00523 virtual BOOL Open(const PString & _arg) = 0;
00524
00525 virtual void Record(PVXMLChannel & incomingChannel) = 0;
00526
00527 virtual void OnStart() { }
00528
00529 virtual BOOL OnFrame(BOOL ) { return FALSE; }
00530
00531 virtual void OnStop() { }
00532
00533 void SetFinalSilence(unsigned v)
00534 { finalSilence = v; }
00535
00536 unsigned GetFinalSilence()
00537 { return finalSilence; }
00538
00539 void SetMaxDuration(unsigned v)
00540 { maxDuration = v; }
00541
00542 unsigned GetMaxDuration()
00543 { return maxDuration; }
00544
00545 protected:
00546 PTime silenceStart;
00547 PTime recordStart;
00548 unsigned finalSilence;
00549 unsigned maxDuration;
00550 unsigned consecutiveSilence;
00551 };
00552
00554
00555 class PVXMLPlayable : public PObject
00556 {
00557 PCLASSINFO(PVXMLPlayable, PObject);
00558 public:
00559 PVXMLPlayable()
00560 { repeat = 1; delay = 0; sampleFrequency = 8000; autoDelete = FALSE; delayDone = FALSE; }
00561
00562 virtual BOOL Open(PVXMLChannel & , PINDEX _delay, PINDEX _repeat, BOOL _autoDelete)
00563 { delay = _delay; repeat = _repeat; autoDelete = _autoDelete; return TRUE; }
00564
00565 virtual BOOL Open(PVXMLChannel & chan, const PString & _arg, PINDEX _delay, PINDEX _repeat, BOOL v)
00566 { arg = _arg; return Open(chan, _delay, _repeat, v); }
00567
00568 virtual void Play(PVXMLChannel & outgoingChannel) = 0;
00569
00570 virtual void OnRepeat(PVXMLChannel & )
00571 { }
00572
00573 virtual void OnStart() { }
00574
00575 virtual void OnStop() { }
00576
00577 virtual void SetRepeat(PINDEX v)
00578 { repeat = v; }
00579
00580 virtual PINDEX GetRepeat() const
00581 { return repeat; }
00582
00583 virtual PINDEX GetDelay() const
00584 { return delay; }
00585
00586 void SetFormat(const PString & _fmt)
00587 { format = _fmt; }
00588
00589 void SetSampleFrequency(unsigned _rate)
00590 { sampleFrequency = _rate; }
00591
00592 virtual BOOL ReadFrame(PVXMLChannel & channel, void * buf, PINDEX len);
00593
00594 virtual BOOL Rewind(PChannel *)
00595 { return FALSE; }
00596
00597 friend class PVXMLChannel;
00598
00599 protected:
00600 PString arg;
00601 PINDEX repeat;
00602 PINDEX delay;
00603 PString format;
00604 unsigned sampleFrequency;
00605 BOOL autoDelete;
00606 BOOL delayDone;
00607 };
00608
00610
00611 class PVXMLPlayableURL : public PVXMLPlayable
00612 {
00613 PCLASSINFO(PVXMLPlayableURL, PVXMLPlayable);
00614 public:
00615 BOOL Open(PVXMLChannel & chan, const PString & _url, PINDEX _delay, PINDEX _repeat, BOOL v);
00616 void Play(PVXMLChannel & outgoingChannel);
00617 protected:
00618 PURL url;
00619 };
00620
00622
00623 class PVXMLPlayableData : public PVXMLPlayable
00624 {
00625 PCLASSINFO(PVXMLPlayableData, PVXMLPlayable);
00626 public:
00627 BOOL Open(PVXMLChannel & chan, const PString & , PINDEX _delay, PINDEX _repeat, BOOL v);
00628 void SetData(const PBYTEArray & _data);
00629 void Play(PVXMLChannel & outgoingChannel);
00630 BOOL Rewind(PChannel * chan);
00631 protected:
00632 PBYTEArray data;
00633 };
00634
00636
00637 #include <ptclib/dtmf.h>
00638
00639 class PVXMLPlayableTone : public PVXMLPlayableData
00640 {
00641 PCLASSINFO(PVXMLPlayableTone, PVXMLPlayableData);
00642 public:
00643 BOOL Open(PVXMLChannel & chan, const PString & toneSpec, PINDEX _delay, PINDEX _repeat, BOOL v);
00644 protected:
00645 PTones tones;
00646 };
00647
00649
00650 class PVXMLPlayableCommand : public PVXMLPlayable
00651 {
00652 PCLASSINFO(PVXMLPlayableCommand, PVXMLPlayable);
00653 public:
00654 PVXMLPlayableCommand();
00655 void Play(PVXMLChannel & outgoingChannel);
00656 void OnStop();
00657
00658 protected:
00659 PPipeChannel * pipeCmd;
00660 };
00661
00663
00664 class PVXMLPlayableFilename : public PVXMLPlayable
00665 {
00666 PCLASSINFO(PVXMLPlayableFilename, PVXMLPlayable);
00667 public:
00668 BOOL Open(PVXMLChannel & chan, const PString & _fn, PINDEX _delay, PINDEX _repeat, BOOL _autoDelete);
00669 void Play(PVXMLChannel & outgoingChannel);
00670 void OnStop();
00671 virtual BOOL Rewind(PChannel * chan);
00672 protected:
00673 PFilePath fn;
00674 };
00675
00677
00678 class PVXMLPlayableFilenameList : public PVXMLPlayable
00679 {
00680 PCLASSINFO(PVXMLPlayableFilenameList, PVXMLPlayable);
00681 public:
00682 BOOL Open(PVXMLChannel & chan, const PStringArray & _filenames, PINDEX _delay, PINDEX _repeat, BOOL _autoDelete);
00683 void Play(PVXMLChannel & outgoingChannel)
00684 { OnRepeat(outgoingChannel); }
00685 void OnRepeat(PVXMLChannel & outgoingChannel);
00686 void OnStop();
00687 protected:
00688 PINDEX currentIndex;
00689 PStringArray filenames;
00690 };
00691
00693
00694 class PVXMLRecordableFilename : public PVXMLRecordable
00695 {
00696 PCLASSINFO(PVXMLRecordableFilename, PVXMLRecordable);
00697 public:
00698 BOOL Open(const PString & _arg);
00699 void Record(PVXMLChannel & incomingChannel);
00700 BOOL OnFrame(BOOL isSilence);
00701
00702 protected:
00703 PFilePath fn;
00704 };
00705
00707
00708 PQUEUE(PVXMLQueue, PVXMLPlayable);
00709
00711
00712 class PVXMLChannel : public PDelayChannel
00713 {
00714 PCLASSINFO(PVXMLChannel, PDelayChannel);
00715 public:
00716 PVXMLChannel(unsigned frameDelay, PINDEX frameSize);
00717 ~PVXMLChannel();
00718
00719 virtual BOOL Open(PVXMLChannelInterface * _vxml);
00720
00721
00722 virtual BOOL IsOpen() const;
00723 virtual BOOL Close();
00724 virtual BOOL Read(void * buffer, PINDEX amount);
00725 virtual BOOL Write(const void * buf, PINDEX len);
00726
00727
00728 virtual PWAVFile * CreateWAVFile(const PFilePath & fn, BOOL recording = FALSE);
00729
00730 const PString & GetMediaFormat() const { return mediaFormat; }
00731 BOOL IsMediaPCM() const { return mediaFormat == "PCM-16"; }
00732 virtual PString AdjustWavFilename(const PString & fn);
00733
00734
00735 virtual BOOL WriteFrame(const void * buf, PINDEX len) = 0;
00736 virtual BOOL IsSilenceFrame(const void * buf, PINDEX len) const = 0;
00737
00738 virtual BOOL QueueRecordable(PVXMLRecordable * newItem);
00739
00740 BOOL StartRecording(const PFilePath & fn, unsigned finalSilence = 3000, unsigned maxDuration = 30000);
00741 BOOL EndRecording();
00742 BOOL IsRecording() const { return recording; }
00743
00744
00745 virtual BOOL ReadFrame(void * buffer, PINDEX amount) = 0;
00746 virtual PINDEX CreateSilenceFrame(void * buffer, PINDEX amount) = 0;
00747 virtual void GetBeepData(PBYTEArray &, unsigned) { }
00748
00749 virtual BOOL QueueResource(const PURL & url, PINDEX repeat= 1, PINDEX delay = 0);
00750
00751 virtual BOOL QueuePlayable(const PString & type, const PString & str, PINDEX repeat = 1, PINDEX delay = 0, BOOL autoDelete = FALSE);
00752 virtual BOOL QueuePlayable(PVXMLPlayable * newItem);
00753 virtual BOOL QueueData(const PBYTEArray & data, PINDEX repeat = 1, PINDEX delay = 0);
00754
00755 virtual BOOL QueueFile(const PString & fn, PINDEX repeat = 1, PINDEX delay = 0, BOOL autoDelete = FALSE)
00756 { return QueuePlayable("File", fn, repeat, delay, autoDelete); }
00757
00758 virtual BOOL QueueCommand(const PString & cmd, PINDEX repeat = 1, PINDEX delay = 0)
00759 { return QueuePlayable("Command", cmd, repeat, delay, TRUE); }
00760
00761 virtual void FlushQueue();
00762 virtual BOOL IsPlaying() const { return (playQueue.GetSize() > 0) || playing ; }
00763
00764 void SetPause(BOOL _pause) { paused = _pause; }
00765
00766 void SetName(const PString & name) { channelName = name; }
00767
00768 unsigned GetSampleFrequency() const
00769 { return sampleFrequency; }
00770
00771 protected:
00772 PVXMLChannelInterface * vxmlInterface;
00773
00774 unsigned sampleFrequency;
00775 PString mediaFormat;
00776 PString wavFilePrefix;
00777
00778 PMutex channelWriteMutex;
00779 PMutex channelReadMutex;
00780 BOOL closed;
00781
00782
00783 BOOL recording;
00784 PVXMLRecordable * recordable;
00785 unsigned finalSilence;
00786 unsigned silenceRun;
00787
00788
00789 BOOL playing;
00790 PMutex queueMutex;
00791 PVXMLQueue playQueue;
00792 PVXMLPlayable * currentPlayItem;
00793
00794 BOOL paused;
00795 int silentCount;
00796 int totalData;
00797 PTimer delayTimer;
00798
00799
00800
00801 PString channelName;
00802 };
00803
00804
00806
00807 #endif // P_EXPAT
00808 #endif
00809
00810
00811