-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cxx
2292 lines (1962 loc) · 68.2 KB
/
main.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* main.cxx
*
* Main source for for OpenAM
*
* A H.323 answering machine application.
*
* Copyright (c) 1993-2001 Equivalence Pty. Ltd.
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is Portable Windows Library.
*
* The Initial Developer of the Original Code is Equivalence Pty. Ltd.
*
* Portions are Copyright (C) 1993 Free Software Foundation, Inc.
* All Rights Reserved.
*
* Contributor(s): ______________________________________.
*
* $Log$
* Revision 1.38 2010/12/08 08:49:20 willamowius
* avoid link error if G7231_File_Capability is not available
*
* Revision 1.37 2010/11/02 11:15:21 willamowius
* use same trace format as GnuGk
*
* Revision 1.36 2010/05/25 20:28:46 willamowius
* fix compile without video
*
* Revision 1.35 2010/01/07 21:54:32 willamowius
* better type checking when setting maxBitRate
*
* Revision 1.34 2010/01/07 21:30:21 willamowius
* fix initialization of videobitrate, use G7231File_Codec only if supplied by PTLib
*
* Revision 1.33 2010/01/07 15:37:57 willamowius
* remove unused code
*
* Revision 1.32 2010/01/01 00:58:36 willamowius
* allow setting of video maxBitRate with --videobitrate <rate>
*
* Revision 1.31 2009/12/20 21:07:21 willamowius
* print error code of executed command
*
* Revision 1.30 2009/08/29 01:53:31 shorne
* Fix compiling on Windows
*
* Revision 1.29 2009/05/14 11:05:09 willamowius
* make sure compilation without video works
*
* Revision 1.28 2009/05/14 10:31:56 willamowius
* disable video codecs when no video ogm is set
*
* Revision 1.27 2009/05/06 18:33:07 willamowius
* generic --no-record switch replaces --no-recordg7231
*
* Revision 1.26 2009/05/06 17:42:07 willamowius
* destructing global objects on shutdown causes an infinite recursion with current PTLibs (eg. 2.4.5), so we don't do that for now
*
* Revision 1.25 2009/05/06 15:22:45 willamowius
* ignore errors on SetFrameRate, fix OGM selection, make error messages seen
*
* Revision 1.24 2009/05/06 13:13:49 willamowius
* fix warnings + cleanup
*
* Revision 1.23 2008/05/23 11:18:10 willamowius
* switch BOOL to PBoolean to be able to compile with Ptlib 2.2.x
*
* Revision 1.22 2008/05/05 15:08:16 willamowius
* add option --displayname <name>
*
* Revision 1.21 2008/04/16 07:55:18 willamowius
* set default message limit to 0 (unlimited)
*
* Revision 1.20 2008/04/14 08:36:02 willamowius
* add switches to set port ranges for H.245, RAS and RTP
*
* Revision 1.19 2008/02/07 08:38:04 shorne
* fix warning on VS 2008
*
* Revision 1.18 2008/01/02 19:12:30 willamowius
* better enforce limit on call duration for video channels
*
* Revision 1.17 2008/01/02 17:12:53 willamowius
* assign different name to each recorded video (same as with audio files)
*
* Revision 1.16 2007/11/22 22:31:13 willamowius
* remove uneccessary restriction to exactly one frame size
*
* Revision 1.15 2007/11/19 23:23:11 willamowius
* trace codec and frame size
*
* Revision 1.14 2007/11/19 00:28:17 willamowius
* limit the video size to size of the provided video OGM
*
* Revision 1.13 2007/11/17 13:52:25 willamowius
* cleanup
*
* Revision 1.12 2007/11/14 15:23:59 willamowius
* declare overloaded methods as virtual
*
* Revision 1.11 2007/11/14 14:16:43 willamowius
* always print message about call start/end to stdout
*
* Revision 1.10 2007/11/14 14:09:32 willamowius
* avoid using NULLOutput device since it doesn't work with 'make optnoshared'
*
* Revision 1.9 2007/11/14 13:12:44 willamowius
* trace message which output device is being created
*
* Revision 1.8 2007/11/14 11:53:47 willamowius
* delete endpoint object on shutdown
*
* Revision 1.7 2007/11/13 14:58:54 willamowius
* reduce read size for very short frame so G7231 OGMs work again
*
* Revision 1.6 2007/11/13 14:37:05 willamowius
* allow operation without audio OGM, only with video OGM
*
* Revision 1.5 2007/11/13 14:03:45 willamowius
* fix message about recording limit
*
* Revision 1.4 2007/11/13 13:51:04 willamowius
* more cleanup
*
* Revision 1.3 2007/11/13 13:18:24 willamowius
* cleanup
*
* Revision 1.2 2007/11/07 05:58:37 shorne
* Fixed Windows compile
*
* Revision 1.1 2007/11/07 03:42:13 willamowius
* port OpenAM to H323Plus
*
* Revision 1.102 2006/04/19 04:11:53 csoutheren
* Fix problem when outputting CIF stream using QCIF source
*
* Revision 1.101 2006/03/12 06:37:53 rjongbloed
* Fixed DevStudio warning
*
* Revision 1.100 2006/03/06 07:05:19 csoutheren
* Preliminary support for capturing video input
*
* Revision 1.99 2006/02/28 12:46:31 csoutheren
* Removed warning on Windows
*
* Revision 1.98 2006/02/28 12:36:31 csoutheren
* Fixed video capabilities
*
* Revision 1.97 2006/02/28 10:12:50 csoutheren
* Added support for NULL receive video
*
* Revision 1.96 2006/02/24 10:16:07 csoutheren
* Fixed for CIF
*
* Revision 1.95 2006/02/21 07:52:18 csoutheren
* Added outgoing video message functions
*
* Revision 1.94 2005/11/25 00:52:49 csoutheren
* Applied patch #1292653
* OpenAM patch to log calling number
*
* Revision 1.93 2005/02/03 01:21:42 csoutheren
* Added called party to call log and command parameters
*
* Revision 1.92 2004/05/26 04:01:57 csoutheren
* Changed to work completely with new plugin codecs
*
* Revision 1.91 2004/05/10 13:07:19 rjongbloed
* Changed G.726 and MS-ADPCM to plug in codecs.
*
* Revision 1.90 2004/05/04 12:21:16 rjongbloed
* Converted LPC-10 codec to plug in.
*
* Revision 1.89 2004/05/03 13:25:46 rjongbloed
* Converted everything to be codec plug in freindly
* Removed GSM and G.729 as now plug ins are "the way"!
*
* Revision 1.88 2004/04/06 11:27:49 rjongbloed
* Changes to support native C++ Run Time Type Information
* Changes for codec plug ins
*
* Revision 1.87 2004/01/31 07:38:21 rjongbloed
* Changed HAS_IXJ usage to if defined not if non-zero
*
* Revision 1.86 2004/01/02 02:52:10 csoutheren
* Thanks to Michal Zygmuntowicz for these changes
* Added support for iLBC codec
* Added ability to specify a gatekeeper password,
* Fixed small bug with Speex capabilities being accidentally included.
*
* Revision 1.85 2003/12/02 09:46:59 csoutheren
* Added --loop option thanks to Jan Willamowius
*
* Revision 1.84 2002/11/13 10:23:12 rogerh
* Enable Speex support by default.
*
* Revision 1.83 2002/11/10 08:12:42 robertj
* Moved constants for "well known" ports to better place (OPAL change).
*
* Revision 1.82 2002/08/21 08:48:09 rogerh
* Fix some problems caused by the user specif OGM changes
*
* Revision 1.81 2002/08/21 06:55:09 rogerh
* Support all 5 Speex bitrates.
*
* Revision 1.80 2002/08/16 02:04:30 craigs
* Ensure compilation without Speex installed
*
* Revision 1.79 2002/08/15 18:52:49 rogerh
* Add support for one of the Speex codecs
*
* Revision 1.78 2002/08/05 10:06:06 robertj
* Changed to use the version of G.7231. file capability/codec as used in
* opalvxml module. Prevents duplicate symbol link errors.
*
* Revision 1.77 2002/06/28 00:30:41 robertj
* Fixed Clone() for G.723 file capability, must clone contents!
*
* Revision 1.76 2002/05/08 03:15:35 robertj
* Removed unecessary initialisation of frameBuffer PBYTEArray
*
* Revision 1.75 2002/04/01 14:45:43 craigs
* Fixed flag to end call after OGM
*
* Revision 1.74 2002/04/01 13:16:09 craigs
* Added ability to kill calls at end of playing OGM
* Added ability to play different OGMs depending on target DN
*
* Revision 1.73 2002/02/21 07:21:52 rogerh
* Change the outgoing audio's FrameDelay() code to use the PAdaptiveDelay
* class which should give clearer audio for the OGMs.
*
* Revision 1.72 2002/02/04 13:23:24 rogerh
* Change the silence frame back to a 4 byte SID. It caused Quicnet cards
* to play a loud noise instead of silence.
*
* Revision 1.71 2002/01/31 16:12:26 rogerh
* Clear the end of the frame buffer if we cannot read a full frame.
*
* Revision 1.70 2002/01/26 09:40:03 rogerh
* Make OpenAM send out G.723.1 OGM audio files correctly.
*
* Revision 1.69 2002/01/25 11:50:10 rogerh
* dd --port option to play and record commands so you can use a
* telephone on the POTS port or use speakers and microphone.
*
* Revision 1.68 2002/01/25 08:58:46 rogerh
* Send out silence using a standard 24 byte frame instead of a 4 byte SID.
* This is a workaround to support an end point which does not understand SID.
*
* Revision 1.67 2002/01/22 04:15:04 craigs
* Updated for move of PWavFile to PTCLib
* Replaced AudioDelay with PAdaptiveDelay
*
* Revision 1.66 2002/01/11 16:07:15 rogerh
* Make PCMExt non static as it is used externally in cmds.cxx
*
* Revision 1.65 2001/11/18 23:07:36 craigs
* Fixed problem whereby recording could start when queue of OGMs was not empty
* Thanks to Frank Derks
*
* Revision 1.64 2001/10/24 10:45:33 rogerh
* replace 4 byte SID frames with 24 byte frames of silence. Windows Media
* Player's codec cannot handle SID frames.
*
* Revision 1.63 2001/10/24 10:28:59 rogerh
* Make -m option find g7231 wav files.
* Fix bug where record file format was set by the outgoing codec type.
* (fixes bug when NetMeeting decides to receive G.723.1 and send MS-GSM)
*
* Revision 1.62 2001/10/24 09:01:07 rogerh
* Fix a mistake in the 24 byte frames used for silence.
*
* Revision 1.61 2001/10/16 12:28:43 rogerh
* Add support for saving g.723.1 audio data into a g.723.1 WAV File
*
* Revision 1.60 2001/10/15 14:55:03 rogerh
* Add error message
*
* Revision 1.59 2001/10/15 14:20:10 rogerh
* Add support for reading G.723.1 WAV files for the G.723.1 codec.
* Recorded messages are still saved out in raw .g723 files.
*
* Revision 1.58 2001/10/15 07:09:38 rogerh
* New sox homepage
*
* Revision 1.57 2001/10/14 08:22:54 rogerh
* Record the silence in G.723.1 connections to the record file.
* This also gives a proper fix for the 100% CPU utilisation bug.
*
* Revision 1.56 2001/10/13 07:13:18 rogerh
* Add a 5ms sleep if the length of the G.723.1 audio data is zero.
* This hack stops the 100% CPU usage seen when recording G.723.1 files.
* Submitted by Maurizio Beni <m.beni@deimositalia.com>
*
* Revision 1.55 2001/10/04 23:55:00 craigs
* Added deletion of file in PCM_RecordFile destructor thanks to Patrick Koorevaar
*
* Revision 1.54 2001/10/02 11:02:31 rogerh
* Use the audio delay class when writing G723.1 audio files.
* Add a quick hack.Set the frameBuffer to 1024 so OpenAM tworks on Unix boxes
*
* Revision 1.53 2001/09/29 07:11:48 rogerh
* Delete ogmChanel in destructor - Patrick Koorevaar <pkoorevaar@hotmail.com>
* Only call conn.ClearCall() once after we pass the record time limit.
* Reported by Patrick.
*
* Revision 1.52 2001/09/28 00:14:30 robertj
* Changed BYTE* to PBYTEArray so get automatic memory management.
* Fixed redundant entries in argument parsing, thanks Patrick Koorevaar
*
* Revision 1.51 2001/09/24 22:39:42 craigs
* Added commands to play and record data files, esp G.723.1
*
* Revision 1.50 2001/08/24 14:04:29 rogerh
* Delete the listener if StartListener() fails.
*
* Revision 1.49 2001/08/13 00:01:15 robertj
* Fixed #ifdef for memory check code to use PMEMORY_CHECK and not _DEBUG
*
* Revision 1.48 2001/07/23 09:17:36 rogerh
* Add the LPC10 codec, a low quality and low bitrate codec.
*
* Revision 1.47 2001/07/23 04:01:10 rogerh
* remove debugging info
*
* Revision 1.46 2001/07/20 03:36:58 robertj
* Minor cosmetic changes to new PWAVFile class.
*
* Revision 1.45 2001/07/19 10:05:27 rogerh
* PWAVFile is now part of the standard PWLib.
*
* Revision 1.44 2001/07/17 14:33:01 rogerh
* Support writing of .wav audio files from PCM codecs (eg G711 and GSM).
* .wav files are written out by default now. If you still want to record
* to raw audio files with a .sw extension, use the --recordraw option.
*
* Revision 1.43 2001/07/17 12:02:37 rogerh
* Change title, OpenAm -> OpenAM
*
* Revision 1.42 2001/07/14 07:44:29 rogerh
* Add .wav file support to the OGM.
* The -m option now looks for .wav and then .sw file extensions.
* .wav file must be 16 bit mono at 8000 Hz
*
* Revision 1.41 2001/07/11 15:47:55 rogerh
* Add G711 A-Law codec, reported by Niels Svennekjær <linux@post.tele.dk>
*
* Revision 1.40 2001/07/01 07:38:57 rogerh
* Add Microsoft GSM codec. Also allocate memory for frameBuffer dynamically
* as different codecs can have different frame sizes.
*
* Revision 1.39 2001/06/29 11:13:15 rogerh
* Add AudioDelay class which removes the jitter in recorded files.
*
* Revision 1.38 2001/06/29 09:01:17 rogerh
* Put back a line accidentally deleted in the last commit
*
* Revision 1.37 2001/06/29 06:34:57 rogerh
* Add mutex locks in PCM_Recordfile. This solves the race condition where
* Close() was called while Write() was still running.
*
* Revision 1.36 2001/04/27 07:08:46 robertj
* Fixed 100% Cip problem, thanks APinaev@microtest.ru
*
* Revision 1.35 2001/03/20 23:42:55 robertj
* Used the new PTrace::Initialise function for starting trace code.
*
* Revision 1.34 2001/01/25 07:27:14 robertj
* Major changes to add more flexible OpalMediaFormat class to normalise
* all information about media types, especially codecs.
*
* Revision 1.33 2000/10/20 23:11:29 robertj
* Fixed incorrect parameter parsing string that stopped -l from working, thanks Bruno BOSQUED
*
* Revision 1.32 2000/10/19 06:55:41 robertj
* Fixed compiler crash by rearranging loop.
*
* Revision 1.31 2000/08/29 23:11:41 robertj
* Fixed MSVC warnings.
*
* Revision 1.30 2000/08/29 12:32:07 craigs
* Fixed problems with recording messages
*
* Revision 1.29 2000/08/28 16:42:59 craigs
* Finally fixed problems with G.723.1. All codecs now working
*
* Revision 1.28 2000/08/28 09:13:54 robertj
* Fixed MSVC compiler warnings.
*
* Revision 1.27 2000/08/28 07:49:26 craigs
* New code to maybe get G.723.1 replaying working
*
* Revision 1.26 2000/08/28 00:38:37 craigs
* Added support for setting listening port number
*
* Revision 1.25 2000/08/27 23:42:24 craigs
* Fixed problem with playback of messages
* Fixed problem with recording messages
*
* Revision 1.24 2000/06/20 02:38:27 robertj
* Changed H323TransportAddress to default to IP.
*
* Revision 1.23 2000/06/17 09:14:52 robertj
* Added setting of closed flag when closing OGM.
*
* Revision 1.22 2000/05/25 13:25:47 robertj
* Fixed incorrect "save" parameter specification.
*
* Revision 1.21 2000/05/25 12:06:17 robertj
* Added PConfigArgs class so can save program arguments to config files.
*
* Revision 1.20 2000/05/11 11:47:11 robertj
* Fixed alpha linux GNU compiler problems.
*
* Revision 1.19 2000/05/10 05:14:25 robertj
* Changed capabilities so has a function to get name of codec, instead of relying on PrintOn.
*
* Revision 1.18 2000/05/09 11:22:15 craigs
* Fixed problems caused by new jitter buffer code
* and fixed OGM problems
*
* Revision 1.17 2000/05/09 02:41:32 craigs
* Added extra debugging, and fixed problems with OGM in non-IVR mode
*
* Revision 1.16 2000/04/26 03:18:38 craigs
* Fixed problem when GSM specified as preferred codec
*
* Revision 1.15 2000/04/25 23:34:22 craigs
* Added lots of new code, including outgoing and incoming
* multiplexors, and the start of an IVR system
*
* Revision 1.14 2000/01/13 04:03:45 robertj
* Added video transmission
*
* Revision 1.13 2000/01/07 08:28:09 robertj
* Additions and changes to line interface device base class.
*
* Revision 1.12 1999/12/10 01:44:46 craigs
* Added ability to set interface
*
* Revision 1.11 1999/12/01 04:38:25 robertj
* Added gatekeeper support to OpenAM
*
* Revision 1.10 1999/11/11 00:27:49 robertj
* Changed OnAnswerCall() call back function to allow for asyncronous response.
*
* Revision 1.9 1999/11/06 13:27:48 craigs
* Added extra output and changed for new library changes
*
* Revision 1.8 1999/10/29 10:57:04 robertj
* Added answering machine project.
*
* Revision 1.7 1999/10/24 12:50:37 craigs
* Fixed G723.1 capability, and added ability for discrete OGMs
*
* Revision 1.6 1999/10/24 08:24:56 craigs
* Added GSM capability back in
*
* Revision 1.5 1999/10/24 08:19:58 craigs
* Fixed problem that caused crash when unknown codecs used
*
* Revision 1.4 1999/10/24 03:29:07 craigs
* Fixed problem with -h parsing
*
* Revision 1.3 1999/10/24 03:08:49 craigs
* Fixed problem with recording zero length messages, and added autodelete of files
*
* Revision 1.2 1999/10/22 09:56:24 craigs
* Fixed various compile warnings
*
* Revision 1.1 1999/10/11 00:15:18 craigs
* Initial version
*
*/
#include <ptlib.h>
#include <ptlib/pipechan.h>
#include <signal.h>
#include "version.h"
#include "opalvxml.h" // for G7231_File_Codec
#include "main.h"
PCREATE_PROCESS(OpenAm);
#define DEFAULT_MSG_LIMIT 0 // unlimited
#define DEFAULT_CALL_LOG "call_log.txt"
#define G7231_SAMPLES_PER_BLOCK 240
#define CHECK_PCM 1
#define CHECK_G7231 2
#define MENU_PREFIX "UserMenu-"
#define DISABLE_COUT 1
static PMutex logMutex;
static PTextFile logFile;
static PFilePath logFilename = DEFAULT_CALL_LOG;
PString G7231Ext(".g723");
PString WAVExt(".wav");
PString PCMExt(".sw");
#if OPENAM_VIDEO
#include <ptclib/pvfiledev.h>
PString VideoGrabberDriverName("YUVFile");
#define DEFAULT_VIDEO_FRAME_RATE 10
#define DEFAULT_VIDEO_SIZE "qcif"
#define DEFAULT_VIDEO_FORMAT "pal"
#define DEFAULT_VIDEO_MODE PVideoInputDevice_YUVFile::Channel_PlayAndClose
// these must be in the same order as the enum in PVideoInputDevice_YUVFile
static const char * VideoModes[PVideoInputDevice_YUVFile::ChannelCount] = {
"Close",
"Repeat",
"KeepLast",
"Black",
};
#endif // OPENAM_VIDEO
///////////////////////////////////////////////////////////////////////////////////////////
static void LogMessage(const PString & str)
{
PTime now;
PString msg = now.AsString("hh:mm:ss dd/MM/yyyy") & str;
logMutex.Wait();
if (!logFile.IsOpen()) {
logFile.Open(logFilename, PFile::ReadWrite);
logFile.SetPosition(0, PFile::End);
}
logFile.WriteLine(msg);
logFile.Close();
logMutex.Signal();
}
static void LogCall(const PFilePath & fn,
const PString & from,
const PString & user,
unsigned len,
const PString & codec,
const PString & product,
const PString & to)
{
PString addr = from;
LogMessage(addr + " \"" + user + "\" " + PString(PString::Unsigned, len) + " " + codec + " \"" + product + "\" \"" + fn + "\" \"" + to + "\"");
}
#ifdef _WIN32
BOOL WINAPI WinCtrlHandlerProc(DWORD dwCtrlType)
{
PString eventName = "CTRL_UNKNOWN_EVENT";
if( dwCtrlType == CTRL_LOGOFF_EVENT ) {
eventName = "CTRL_LOGOFF_EVENT";
LogMessage("OpenAM received " + eventName);
// prevent shut down
return FALSE;
}
if( dwCtrlType == CTRL_C_EVENT )
eventName = "CTRL_C_EVENT";
else if( dwCtrlType == CTRL_BREAK_EVENT )
eventName = "CTRL_BREAK_EVENT";
else if( dwCtrlType == CTRL_CLOSE_EVENT )
eventName = "CTRL_CLOSE_EVENT";
else if( dwCtrlType == CTRL_SHUTDOWN_EVENT )
eventName = "CTRL_SHUTDOWN_EVENT";
LogMessage("OpenAM shutdown due to " + eventName);
OpenAm::Shutdown();
exit(1);
}
#else
void UnixShutdownHandler(int sig)
{
LogMessage(PString("OpenAM received signal ") + PString(sig));
OpenAm::Shutdown();
exit(1);
}
#endif
///////////////////////////////////////////////////////////////
MyH323EndPoint * OpenAm::endpoint = NULL;
OpenAm::OpenAm()
: PProcess("OpenH323 Project", "OpenAM",
MAJOR_VERSION, MINOR_VERSION, BUILD_TYPE, BUILD_NUMBER)
{
}
OpenAm::~OpenAm()
{
}
void OpenAm::Main()
{
cout << GetName()
<< " Version " << GetVersion(TRUE)
<< " by " << GetManufacturer()
<< " on " << GetOSClass() << ' ' << GetOSName()
<< " (" << GetOSVersion() << '-' << GetOSHardware() << ")\n\n";
#ifdef _WIN32
SetConsoleCtrlHandler(WinCtrlHandlerProc, TRUE);
#else
signal(SIGTERM, UnixShutdownHandler);
signal(SIGINT, UnixShutdownHandler);
signal(SIGQUIT, UnixShutdownHandler);
#endif
PConfigArgs args(GetArguments());
args.Parse(
"D-disable:"
"d-directory:"
"g-gatekeeper:" "n-no-gatekeeper."
"-g711-ulaw." "-no-g711-ulaw."
"-g711-alaw." "-no-g711-alaw."
"-g711message:" "-no-g711message."
#if OPENAM_G723
"-g7231." "-no-g7231."
"-g7231message:" "-no-g7231message."
#endif
"-ilbc." "-no-ilbc."
"-ilbcmessage:" "-no-ilbcmessage."
"-gsm." "-no-gsm."
"-gsmmessage:" "-no-gsmmessage."
"h-help."
"H-hangup." "-no-hangup."
"i-interface:" "-no-interface."
"k-kill." "-no-kill."
"l-limit:" "-no-limit."
"-listenport:" "-no-listenport."
"-lpc10message:" "-no-lpc10message."
"-speexmessage:" "-no-speexmessage."
"m-message:" "-no-message."
"-no-record."
"-loop."
#if PTRACING
"o-output:"
#endif
"P-prefer:"
"-pcm." "-no-pcm."
"-pcmmessage:" "-no-pcmmessage."
"-port:"
"r-run:" "-no-run."
"-recordraw."
"-require-gatekeeper." "-no-require-gatekeeper."
"-save."
#if PMEMORY_CHECK
"-setallocationbreakpoint:"
#endif
#if PTRACING
"t-trace."
#endif
"u-username:" "-no-username."
"p-password:"
"-displayname:"
#if OPENAM_VIDEO
"-videomessage:"
"-videorate:"
"-videosize:"
"-videobitrate:"
"-videoformat:"
"-videomode:"
#endif
"-tcp-minport:"
"-tcp-maxport:"
"-udp-minport:"
"-udp-maxport:"
"-rtp-minport:"
"-rtp-maxport:"
, FALSE);
#if PMEMORY_CHECK
if (args.HasOption("setallocationbreakpoint"))
PMemoryHeap::SetAllocationBreakpoint(args.GetOptionString("setallocationbreakpoint").AsInteger());
#endif
#if PTRACING
PTrace::Initialise(args.GetOptionCount('t'),
args.HasOption('o') ? (const char *)args.GetOptionString('o') : NULL,
PTrace::DateAndTime | PTrace::TraceLevel | PTrace::FileAndLine);
#endif
if (args.HasOption('h')) {
cout << "Usage : " << GetName() << " [options]\n"
"Options:\n"
" -d --directory dir : Put recorded mesages into dir\n"
" -l --limit secs : Limit recorded messages to secs duration (default " << DEFAULT_MSG_LIMIT << ")\n"
" --no-record : Don't record a message\n"
" -m --pcmmessage fn : Set outgoing message for PCM derived codecs (G.711/GSM) to fn\n"
#if OPENAM_G723
" --g7231message fn : Set outgoing message for G723.1 codec to fn\n"
" --g711message fn : Set outgoing message for G711 codec to fn\n"
#endif
" --gsmmessage fn : Set outgoing message for GSM codec to fn\n"
" --lpc10message fn : Set outgoing message for LPC10 codec to fn\n"
" --speexmessage fn : Set outgoing message for Speex codec to fn\n"
" --ilbcmessage fn : Set outgoing message for iLBC codec to fn\n"
#if OPENAM_VIDEO
" --videomessage fn : Set outgoing message for video to fn\n"
" --videorate num : Set video frame rate (default is " << DEFAULT_VIDEO_FRAME_RATE << ")\n"
" --videobitrate rate : Set video max bit rate for video (default is unlimited)\n"
" --videosize size : Set video frame size (default is " << DEFAULT_VIDEO_SIZE << ")\n"
" : Must be one of qcif or cif\n"
" --videoformat fmt : Set video frame format (default is " << DEFAULT_VIDEO_FORMAT << ")\n"
" : Must be one of pal or ntsc\n"
" --videomode mode: : Set video play mode. Default is " << VideoModes[DEFAULT_VIDEO_MODE] << "\n"
" : Must be one of Close, Repeat, KeepLast or Black\n"
#endif
" --loop : loop message, no recording\n"
" --recordraw : Record PCM audo in raw files (.sw) instead of .wav\n"
" -r --run cmd : Run this command after each recorded message\n"
" -k --kill : Kill recorded files after user command\n"
" -H --hangup : hangup after playing message\n"
" -u --username str : Set the local endpoint name to str\n"
" -p --password str : Set the gatekeeper password to str\n"
" --displayname str : Set the display name to str\n"
" -i --interface ip : Bind to a specific interface\n"
" --listenport port : Listen on a specific port\n"
" --tcp-minport port : Set min TCP port to use for H.245\n"
" --tcp-maxport port : Set max TCP port to use for H.245\n"
" --udp-minport port : Set min UDP port to use for RAS\n"
" --udp-maxport port : Set max UDP port to use for RAS\n"
" --rtp-minport port : Set min UDP port to use for RTP\n"
" --rtp-maxport port : Set max UDP port to use for RTP\n"
" -g --gatekeeper host: Specify gatekeeper host.\n"
" -n --no-gatekeeper : Disable gatekeeper discovery.\n"
" --require-gatekeeper: Exit if gatekeeper discovery fails.\n"
" -D --disable codec : Disable the specified codec (may be used multiple times)\n"
" -P --prefer codec : Prefer the specified codec (may be used multiple times)\n"
#if PTRACING
" -t --trace : Enable trace, use multiple times for more detail\n"
" -o --output : File for trace output, default is stderr\n"
#endif
" --save : Save arguments in configuration file\n"
" -h --help : Display this help message\n";
return;
}
args.Save("save");
unsigned callLimit = DEFAULT_MSG_LIMIT;
if (args.HasOption('l')) {
callLimit = args.GetOptionString('l').AsInteger();
if (callLimit > 3600) {
cout << "warning: maximum call length " << callLimit << " is out of range. Using " << DEFAULT_MSG_LIMIT << " instead\n";
callLimit = DEFAULT_MSG_LIMIT;
} else if (callLimit == 0)
cout << "warning: recorded message call limit disabled\n";
}
if (!args.HasOption("no-record") && !args.HasOption("loop"))
cout << "Recorded messages limited to " << callLimit << " seconds\n";
else
cout << "Recording disabled\n";
PString runCmd;
if (args.HasOption('r')) {
runCmd = args.GetOptionString('r');
cout << "Executing \"" << runCmd << "\" after each message" << endl;
}
PDirectory dir;
if (args.HasOption('d'))
dir = args.GetOptionString('d');
int flags = 0;
if (args.HasOption('k')) {
cout << "Deleting recorded files after processing" << endl;
if (runCmd.IsEmpty())
cout << "WARNING: recorded files will be deleted even though no run command is present" << endl;
flags |= MyH323EndPoint::DeleteAfterRecord;
}
if (args.HasOption('H'))
flags |= MyH323EndPoint::HangupAfterPlay;
endpoint = new MyH323EndPoint(callLimit, runCmd, dir, flags);
PString userName = "OpenH323 Answering Machine v" + GetVersion();
if (args.HasOption('u'))
userName = args.GetOptionString('u');
endpoint->SetLocalUserName(userName);
if (args.HasOption("displayname"))
endpoint->SetDisplayName(args.GetOptionString("displayname"));
if (args.HasOption('p')) {
const PString password = args.GetOptionString('p');
endpoint->SetGatekeeperPassword(password);
}
// set port ranges
if (args.HasOption("tcp-minport")) {
unsigned tcpMinPort = args.GetOptionString("tcp-minport").AsInteger();
if (tcpMinPort > 65536)
tcpMinPort = 65536;
unsigned tcpMaxPort = 65536;
if (args.HasOption("tcp-maxport")) {
tcpMaxPort = args.GetOptionString("tcp-maxport").AsInteger();
if (tcpMaxPort > 65536)
tcpMaxPort = 65536;
}
endpoint->SetTCPPorts(tcpMinPort, tcpMaxPort);
}
if (args.HasOption("udp-minport")) {
unsigned udpMinPort = args.GetOptionString("udp-minport").AsInteger();
if (udpMinPort > 65536)
udpMinPort = 65536;
unsigned udpMaxPort = 65536;
if (args.HasOption("udp-maxport")) {
udpMaxPort = args.GetOptionString("udp-maxport").AsInteger();
if (udpMaxPort > 65536)
udpMaxPort = 65536;
}
endpoint->SetUDPPorts(udpMinPort, udpMaxPort);
}
if (args.HasOption("rtp-minport")) {
unsigned rtpMinPort = args.GetOptionString("rtp-minport").AsInteger();
if (rtpMinPort > 65536)
rtpMinPort = 65536;
unsigned rtpMaxPort = 65536;
if (args.HasOption("rtp-maxport")) {
rtpMaxPort = args.GetOptionString("rtp-maxport").AsInteger();
if (rtpMaxPort > 65536)
rtpMaxPort = 65536;
}
endpoint->SetRtpIpPorts(rtpMinPort, rtpMaxPort);
}
if (!endpoint->Initialise(args))
return;
// start the H.323 listener
H323ListenerTCP * listener;
PIPSocket::Address interfaceAddress(INADDR_ANY);
WORD listenPort = H323EndPoint::DefaultTcpPort;
if (args.HasOption("listenport"))
listenPort = (WORD)args.GetOptionString("listenport").AsInteger();
if (args.HasOption('i'))
interfaceAddress = PIPSocket::Address(args.GetOptionString('i'));
listener = new H323ListenerTCP(*endpoint, interfaceAddress, listenPort);
if (!endpoint->StartListener(listener)) {
cout << "Could not open H.323 listener port on "
<< listener->GetListenerPort() << endl;
delete listener;
return;
}
if (args.HasOption('g')) {
PString gkName = args.GetOptionString('g');
if (endpoint->SetGatekeeper(gkName, new H323TransportUDP(*endpoint)))
cout << "Gatekeeper set: " << *endpoint->GetGatekeeper() << endl;
else {
cout << "Error registering with gatekeeper at \"" << gkName << '"' << endl;
return;
}
}
else if (!args.HasOption('n')) {
cout << "Searching for gatekeeper..." << flush;
if (endpoint->DiscoverGatekeeper(new H323TransportUDP(*endpoint)))
cout << "\nGatekeeper found: " << *endpoint->GetGatekeeper() << endl;
else {
cout << "\nNo gatekeeper found." << endl;
if (args.HasOption("require-gatekeeper"))
return;
}
}
cout << "Waiting for incoming calls for \"" << endpoint->GetLocalUserName() << '"' << endl;
for (;;)
PThread::Current()->Sleep(5000);
}
void OpenAm::Shutdown()
{
if (endpoint) {
if (endpoint->IsRegisteredWithGatekeeper())
endpoint->RemoveGatekeeper();
delete endpoint;
}
_exit(0); // HACK: avoid destruction of global objects
}
///////////////////////////////////////////////////////////////
MyH323EndPoint::MyH323EndPoint(unsigned _callLimit,
const PString & _runCmd,
const PDirectory & _dir,
int _flags)
: callLimit(_callLimit), runCmd(_runCmd), dir(_dir), flags(_flags),
recordWav(false), recordMessage(false), loopMessage(false)
{
#if OPENAM_VIDEO
videoSize = 0;
videoBitRate = 0;
videoIsPal = TRUE;
frameRate = DEFAULT_VIDEO_FRAME_RATE;
videoChannel = DEFAULT_VIDEO_MODE;
#endif
}
MyH323EndPoint::~MyH323EndPoint()
{
}
PBoolean MyH323EndPoint::OnIncomingCall(H323Connection & _conn,
const H323SignalPDU & setupPDU,
H323SignalPDU &)
{
MyH323Connection & conn = (MyH323Connection &)_conn;
// see if incoming call is to an E.164 number
PString number;
if (setupPDU.GetDestinationE164(number))
conn.SetE164Number(number);
return TRUE;
}
H323Connection * MyH323EndPoint::CreateConnection(unsigned callReference)
{
unsigned options = 0;
options |= H323Connection::H245TunnelingOptionDisable;
// options |= H323Connection::FastStartOptionDisable;
// options |= H323Connection::H245inSetupOptionDisable;
return new MyH323Connection(*this, callReference, options);
}
PBoolean MyH323EndPoint::Initialise(PConfigArgs & args)
{
// format for record files, raw or wav
if (args.HasOption("recordraw"))
SetRecordWav(FALSE);
else
SetRecordWav(TRUE);
#if OPENAM_G723
// get G.723.1 OGM
if (args.HasOption("g7231message"))
g7231Ogm = args.GetOptionString("g7231message");
else if (args.HasOption('m')) {
if (PFile::Exists(args.GetOptionString('m') + "_g7231" + WAVExt)) {
g7231Ogm = args.GetOptionString('m') + "_g7231" + WAVExt;
}
else if (PFile::Exists(args.GetOptionString('m') + PCMExt)) {
g7231Ogm = args.GetOptionString('m') + G7231Ext;
}
}
if (!g7231Ogm.IsEmpty()) {
// check if the file exists. (do not check if filename contains %s)
if ((g7231Ogm.Find("%s") == P_MAX_INDEX) && !PFile::Exists(g7231Ogm)) {
cout << "warning: cannot open G723.1 OGM file \"" << g7231Ogm << "\"" << endl;
g7231Ogm = "";
}
}
if (g7231Ogm.IsEmpty())