-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpjsua2-memstream.patch
1197 lines (1135 loc) · 35 KB
/
pjsua2-memstream.patch
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
diff --git a/.gitignore b/.gitignore
index a3589c66f..05230fa1a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,78 +1,78 @@
-pjlib/include/pj/config_site.h
-lib/
-bin/
-output/
-
-# VS stuff
-*.ncb
-*.suo
-*.cache
-*.resw
-.vs/
-.vscode/
-**/build/**/*.vcproj.*.user
-**/build/**/*.vcxproj.*.user
-**/build/**/*.vcproj.user
-**/build/**/*.vcxproj.user
-**/Win32/Debug
-**/x64/Debug
-**/x86/Debug
-UpgradeLog*.htm
-# VS 2015 stuff
-*.pdb
-*.db
-*.opendb
-samples.log
-
-# GNU stuff
-config.log
-config.status
-build.mak
-user.mak
-cc-auto.mak
-os-auto.mak
-m_auto.h
-os_auto.h
-config_auto.h
-sip_autoconf.h
-*.depend
-
-# Apple stuff
-*.DS_Store
-autom4te.cache
-xcuserdata
-pjsip-apps/src/pjsua/ios*/lib*.a
-pjsip-apps/src/pjsua/ios/ipjsua.xcodeproj/project.xcworkspace/
-pjsip-apps/src/pjsua/ios/ipjsua.xcworkspace/xcshareddata
-pjsip-apps/src/pjsua/ios-swift/Libraries/
-pjsip-apps/src/pjsua2/ios-swift-pjsua2/Libraries/
-
-# SWIG Java/Android stuff
-pjsip-apps/src/swig/java/android/app/src/main/java/org/pjsip/pjsua2/*.java
-pjsip-apps/src/swig/java/android/app/src/main/java/org/pjsip/PjCamera*.java
-pjsip-apps/src/swig/java/android/app/src/main/jniLibs/
-
-# SWIG CSharp/Xamarin stuff
-pjsip-apps/src/swig/csharp/pjsua2xamarin/
-
-# SWIG Python stuff
-pjsip-apps/src/swig/python/build/
-pjsip-apps/src/swig/python/pjsua2.py
-pjsip-apps/src/swig/python/pjsua2_wrap.*
-
-# unit tests files
-tests/pjsua/*.pyc
-tests/pjsua/scripts-*/*.pyc
-tests/pjsua/*.log
-tests/pjsua/wavs/tmp*.wav
-tests/pjsua/tools/cmp_wav
-tests/pjsua/tools/cmp_wav.dSYM
-tests/pjsua/logs
-# benchmark output
-*bench*.htm
-
-# Doxygen stuff
-*/docs/html/
-*/docs/xml/
-*/docs/latex/
-*/docs/*.tag
+pjlib/include/pj/config_site.h
+lib/
+bin/
+output/
+
+# VS stuff
+*.ncb
+*.suo
+*.cache
+*.resw
+.vs/
+.vscode/
+**/build/**/*.vcproj.*.user
+**/build/**/*.vcxproj.*.user
+**/build/**/*.vcproj.user
+**/build/**/*.vcxproj.user
+**/Win32/Debug
+**/x64/Debug
+**/x86/Debug
+UpgradeLog*.htm
+# VS 2015 stuff
+*.pdb
+*.db
+*.opendb
+samples.log
+
+# GNU stuff
+config.log
+config.status
+build.mak
+user.mak
+cc-auto.mak
+os-auto.mak
+m_auto.h
+os_auto.h
+config_auto.h
+sip_autoconf.h
+*.depend
+
+# Apple stuff
+*.DS_Store
+autom4te.cache
+xcuserdata
+pjsip-apps/src/pjsua/ios*/lib*.a
+pjsip-apps/src/pjsua/ios/ipjsua.xcodeproj/project.xcworkspace/
+pjsip-apps/src/pjsua/ios/ipjsua.xcworkspace/xcshareddata
+pjsip-apps/src/pjsua/ios-swift/Libraries/
+pjsip-apps/src/pjsua2/ios-swift-pjsua2/Libraries/
+
+# SWIG Java/Android stuff
+pjsip-apps/src/swig/java/android/app/src/main/java/org/pjsip/pjsua2/*.java
+pjsip-apps/src/swig/java/android/app/src/main/java/org/pjsip/PjCamera*.java
+pjsip-apps/src/swig/java/android/app/src/main/jniLibs/
+
+# SWIG CSharp/Xamarin stuff
+pjsip-apps/src/swig/csharp/pjsua2xamarin/
+
+# SWIG Python stuff
+pjsip-apps/src/swig/python/build/
+pjsip-apps/src/swig/python/pjsua2.py
+pjsip-apps/src/swig/python/pjsua2_wrap.*
+
+# unit tests files
+tests/pjsua/*.pyc
+tests/pjsua/scripts-*/*.pyc
+tests/pjsua/*.log
+tests/pjsua/wavs/tmp*.wav
+tests/pjsua/tools/cmp_wav
+tests/pjsua/tools/cmp_wav.dSYM
+tests/pjsua/logs
+# benchmark output
+*bench*.htm
+
+# Doxygen stuff
+*/docs/html/
+*/docs/xml/
+*/docs/latex/
+*/docs/*.tag
diff --git a/README.md b/README.md
new file mode 100644
index 000000000..65bd38bf2
--- /dev/null
+++ b/README.md
@@ -0,0 +1,38 @@
+# PJSIP with call audio capturing and streaming features
+PJSIP library is modified to capture PCM frames from the call and stream PCM frames to the calls.
+
+## Core classes
+Added two classes to `media.hpp` and `media.cpp` files:
+ - `AudioMediaCapture`: contains list to accumulate incoming frames, frames can be extracted with `getFrames/getFramesAsString` methods.
+ - `AudioMediaStream`: contains list for outgoing frames, frames can be populated with `putFrame/putFrameAsString` methods.
+
+## PJSUA2/SWIG
+Need some changes to SWIG to access those features from the Python:
+- Uncomment `USE_THREADS = -threads -DSWIG_NO_EXPORT_ITERATOR_METHODS` in `swig/python/Makefile`
+- Add typemaps to `swig/pjsua2.i` to send/receive `bytes` from Python:
+```
+#ifdef SWIGPYTHON
+ %typemap(in) (char *data, size_t datasize) {
+ Py_ssize_t len;
+ PyBytes_AsStringAndSize($input, &$1, &len);
+ $2 = (size_t)len;
+ }
+
+ %typemap(in, numinputs=0) (char **data, size_t *datasize)(char *temp, size_t tempsize) {
+ $1 = &temp;
+ $2 = &tempsize;
+ }
+
+ %typemap(argout) (char **data, size_t *datasize) {
+ if(*$1) {
+ $result = PyBytes_FromStringAndSize(*$1, *$2);
+ free(*$1);
+ }
+ }
+#endif
+```
+
+## Run Demo
+ 1. `docker-compose up`
+ 2. `docker-compose exec pjsip python demo.py`
+Demo script dumps frames to from the call to `pjsip/pjsuademo/output.lpcm` and streams `hw.raw` to the call. The recording of the call with both parties will be in `asterisk_files/recordings` directory.
diff --git a/pjsip-apps/src/swig/pjsua2.i b/pjsip-apps/src/swig/pjsua2.i
index a3416a91a..db35bd87f 100644
--- a/pjsip-apps/src/swig/pjsua2.i
+++ b/pjsip-apps/src/swig/pjsua2.i
@@ -1,5 +1,7 @@
%module(directors="1") pjsua2
+%apply (char *STRING, size_t LENGTH) { (char *data, size_t datasize) }
+
//
// Suppress few warnings
//
@@ -28,9 +30,9 @@ using namespace pj;
#ifdef SWIGCSHARP
%typemap(throws, canthrow=1) pj::Error {
- SWIG_CSharpSetPendingException(SWIG_CSharpApplicationException,
+ SWIG_CSharpSetPendingException(SWIG_CSharpApplicationException,
(std::string("C++ pj::Error:\n") + $1.info(true).c_str()).c_str());
-
+
return $null;
}
#endif
@@ -53,7 +55,7 @@ using namespace pj;
public String getMessage() {
return getTitle();
}
-
+
// Disable serialization (check ticket #1868)
private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException {
throw new java.io.NotSerializableException("Check ticket #1868!");
@@ -71,24 +73,6 @@ using namespace pj;
#ifdef SWIGJAVA
%include "enumtypeunsafe.swg"
%javaconst(1);
-
-%pragma(java) jniclasscode=%{
- static {
- try {
- System.loadLibrary("openh264");
- } catch (UnsatisfiedLinkError e) {
- System.err.println("Failed to load native library openh264\n" + e);
- System.out.println("This could be safely ignored if you " +
- "don't use OpenH264 video codec.");
- }
- try {
- System.loadLibrary("pjsua2");
- } catch (UnsatisfiedLinkError e) {
- System.err.println("Failed to load native library pjsua2\n" + e);
- }
- }
-%}
-
#endif
%include "symbols.i"
@@ -98,12 +82,13 @@ using namespace pj;
// Classes that can be extended in the target language
//
%feature("director") LogWriter;
-%feature("director") Endpoint;
+%feature("director") Endpoint;
%feature("director") Account;
%feature("director") Call;
%feature("director") Buddy;
%feature("director") FindBuddyMatch;
%feature("director") AudioMediaPlayer;
+%feature("director") AudioMediaCapture;
//
// STL stuff.
@@ -159,7 +144,7 @@ using namespace pj;
%template(CodecInfoVector2) std::vector<pj::CodecInfo>;
%template(VideoDevInfoVector) std::vector<pj::VideoDevInfo*>;
%template(VideoDevInfoVector2) std::vector<pj::VideoDevInfo>;
-%template(CodecFmtpVector) std::vector<pj::CodecFmtp>;
+%template(CodecFmtpVector) std::vector<pj::CodecFmtp>;
%template(MediaFormatAudioVector) std::vector<pj::MediaFormatAudio>;
%template(MediaFormatVideoVector) std::vector<pj::MediaFormatVideo>;
%template(CallMediaInfoVector) std::vector<pj::CallMediaInfo>;
@@ -190,6 +175,27 @@ using namespace pj;
}
#endif
+
+#ifdef SWIGPYTHON
+ %typemap(in) (char *data, size_t datasize) {
+ Py_ssize_t len;
+ PyBytes_AsStringAndSize($input, &$1, &len);
+ $2 = (size_t)len;
+ }
+
+ %typemap(in, numinputs=0) (char **data, size_t *datasize)(char *temp, size_t tempsize) {
+ $1 = &temp;
+ $2 = &tempsize;
+ }
+
+ %typemap(argout) (char **data, size_t *datasize) {
+ if(*$1) {
+ $result = PyBytes_FromStringAndSize(*$1, *$2);
+ free(*$1);
+ }
+ }
+#endif
+
%include "pjsua2/media.hpp"
%include "pjsua2/presence.hpp"
%include "pjsua2/account.hpp"
@@ -215,6 +221,22 @@ using namespace pj;
libDestroy_();
}
%}
+%typemap(throws, throws="java.lang.Exception") pj::Error {
+ jclass excep = jenv->FindClass("java/lang/Exception");
+ if (excep)
+ jenv->ThrowNew(excep, $1.info(true).c_str());
+ return $null;
+}
+
+ // Force the Error Java class to extend java.lang.Exception
+ %typemap(javabase) pj::Error "java.lang.Exception";
+
+ // Override getMessage()
+ %typemap(javacode) pj::Error %{
+ public String getMessage() {
+ return getTitle();
+ }
+%}
#endif
%include "pjsua2/endpoint.hpp"
diff --git a/pjsip-apps/src/swig/python/Makefile b/pjsip-apps/src/swig/python/Makefile
index ddc6fcb43..49fe20644 100644
--- a/pjsip-apps/src/swig/python/Makefile
+++ b/pjsip-apps/src/swig/python/Makefile
@@ -8,7 +8,7 @@ else
PYTHON_EXE=python2
endif
-#PYTHON_SETUP_FLAGS = --inplace
+#PYTHON_SETUP_FLAGS = --inplace
ifeq ($(OS),Windows_NT)
PYTHON_PKG_DIR := $(shell $(PYTHON_EXE) -m site --user-site | sed 's/\\/\//g')
PYTHON_SETUP_FLAGS += --compiler=mingw32
@@ -22,11 +22,11 @@ SWIG_FLAGS=-I../../../../pjlib/include \
-I../../../../pjlib-util/include \
-I../../../../pjmedia/include \
-I../../../../pjsip/include \
- -I../../../../pjnath/include -c++
+ -I../../../../pjnath/include -c++
SRC_DIR=../../../../pjsip/include
SRCS=$(SRC_DIR)/pjsua2/endpoint.hpp $(SRC_DIR)/pjsua2/types.hpp
-#USE_THREADS = -threads -DSWIG_NO_EXPORT_ITERATOR_METHODS
+USE_THREADS = -threads -DSWIG_NO_EXPORT_ITERATOR_METHODS
SWIG_FLAGS += -w312 $(USE_THREADS)
.PHONY: all install uninstall
@@ -53,4 +53,3 @@ install:
uninstall:
rm -f $(PYTHON_PKG_DIR)/pjsua2*
rm -f $(PYTHON_PKG_DIR)/_pjsua2*
-
diff --git a/pjsip/include/pjsua2/media.hpp b/pjsip/include/pjsua2/media.hpp
index 3282bf7ef..d8be7129e 100644
--- a/pjsip/include/pjsua2/media.hpp
+++ b/pjsip/include/pjsua2/media.hpp
@@ -25,6 +25,8 @@
*/
#include <pjsua-lib/pjsua.h>
#include <pjsua2/types.hpp>
+#include <mutex>
+#include <list>
/** PJSUA2 API is inside pj namespace */
namespace pj
@@ -37,7 +39,7 @@ namespace pj
*/
using std::string;
-using std::vector;
+// using std::list;
/**
* This structure contains all the information needed to completely describe
@@ -121,7 +123,7 @@ typedef std::vector<MediaFormatVideo> MediaFormatVideoVector;
/**
* This structure descibes information about a particular media port that
- * has been registered into the conference bridge.
+ * has been registered into the conference bridge.
*/
struct ConfPortInfo
{
@@ -223,7 +225,7 @@ public:
/**
* Audio Media. This is a lite wrapper class for audio conference bridge port,
- * i.e: this class only maintains one data member, conference slot ID, and
+ * i.e: this class only maintains one data member, conference slot ID, and
* the methods are simply proxies for conference bridge operations.
*
* Application can create a derived class and use registerMediaPort2()/
@@ -415,7 +417,7 @@ private:
pj_pool_t *mediaPool;
};
-/**
+/**
* Warning: deprecated, use AudioMediaVector2 instead.
*
* Array of Audio Media.
@@ -466,13 +468,13 @@ public:
class AudioMediaPlayer : public AudioMedia
{
public:
- /**
+ /**
* Constructor.
*/
AudioMediaPlayer();
/**
- * Create a file player, and automatically add this
+ * Create a file player, and automatically add this
* player to the conference bridge.
*
* @param file_name The filename to be played. Currently only
@@ -654,6 +656,48 @@ private:
int recorderId;
};
+// pj_status_t incomingFrameHandler(pjmedia_port *, void *);
+
+class AudioMediaCapture : public AudioMedia {
+public:
+ AudioMediaCapture();
+ ~AudioMediaCapture();
+ void createMediaCapture();
+ void getFrames(char **data, size_t *datasize);
+ string getFramesAsString();
+ // static pj_status_t processFrames(pjmedia_port *, void *);
+ static void processFrames(pjmedia_port *, void *);
+ virtual void onFrame(char *data, size_t datasize) {}
+ void disableReceivingFrames();
+ void enableReceivingFrames();
+private:
+ bool receive_frames;
+ pj_pool_t *pool;
+ void *frame_buffer;
+ uint frame_size;
+ pjmedia_port *capture_port;
+ std::list<string> frames;
+ std::mutex frames_mtx;
+};
+
+class AudioMediaStream : public AudioMedia {
+public:
+ AudioMediaStream();
+ void createMediaStream();
+ virtual ~AudioMediaStream();
+// static pj_status_t processFrames(pjmedia_port *, void *);
+ static void processFrames(pjmedia_port *, void *);
+ void putFrame(char *data, size_t datasize);
+ void putFrameAsString(string);
+private:
+ pj_pool_t *pool;
+ pjmedia_port *stream_port;
+ void *frame_buffer;
+ uint frame_size;
+ std::list<string> frames;
+ std::mutex frames_mtx;
+};
+
/**
* Tone descriptor (abstraction for pjmedia_tone_desc)
*/
@@ -860,7 +904,7 @@ struct AudioDevInfo
~AudioDevInfo();
};
-/**
+/**
* Warning: deprecated, use AudioDevInfoVector2 instead.
*
* Array of audio device info.
@@ -908,7 +952,7 @@ public:
/**
* Select or change capture sound device. Application may call this
- * function at any time to replace current sound device. Calling this
+ * function at any time to replace current sound device. Calling this
* method will not change the state of the sound device (opened/closed).
*
* @param capture_dev Device ID of the capture device.
@@ -917,7 +961,7 @@ public:
/**
* Select or change playback sound device. Application may call this
- * function at any time to replace current sound device. Calling this
+ * function at any time to replace current sound device. Calling this
* method will not change the state of the sound device (opened/closed).
*
* @param playback_dev Device ID of the playback device.
@@ -1510,7 +1554,7 @@ private:
/**
* Extra audio device. This class allows application to have multiple
* sound device instances active concurrently.
-
+
* Application may also use this class to improve media clock. Normally
* media clock is driven by sound device in master port, but unfortunately
* some sound devices may produce jittery clock. To improve media clock,
@@ -1590,7 +1634,7 @@ struct MediaSize
/**
* This structure descibes information about a particular media port that
- * has been registered into the conference bridge.
+ * has been registered into the conference bridge.
*/
struct VidConfPortInfo
{
@@ -1825,7 +1869,7 @@ public:
* @return Video media of this renderer window.
*/
VideoMedia getVideoMedia() PJSUA2_THROW(Error);
-
+
/**
* Show or hide window. This operation is not valid for native windows
* (VideoWindowInfo.isNative=true), on which native windowing API
@@ -1836,7 +1880,7 @@ public:
*
*/
void Show(bool show) PJSUA2_THROW(Error);
-
+
/**
* Set video window position. This operation is not valid for native windows
* (VideoWindowInfo.isNative=true), on which native windowing API
@@ -1846,7 +1890,7 @@ public:
*
*/
void setPos(const MediaCoordinate &pos) PJSUA2_THROW(Error);
-
+
/**
* Resize window. This operation is not valid for native windows
* (VideoWindowInfo.isNative=true), on which native windowing API
@@ -1856,7 +1900,7 @@ public:
*
*/
void setSize(const MediaSize &size) PJSUA2_THROW(Error);
-
+
/**
* Rotate the video window. This function will change the video orientation
* and also possibly the video window size (width and height get swapped).
@@ -1952,7 +1996,7 @@ public:
*/
VideoPreviewOpParam();
- /**
+ /**
* Convert from pjsip
*/
void fromPj(const pjsua_vid_preview_param &prm);
@@ -2009,8 +2053,6 @@ public:
private:
pjmedia_vid_dev_index devId;
- pjsua_vid_win_id winId;
- void updateDevId();
};
/**
@@ -2039,8 +2081,8 @@ struct VideoDevInfo
*/
pjmedia_dir dir;
- /**
- * Device capabilities, as bitmask combination of #pjmedia_vid_dev_cap
+ /**
+ * Device capabilities, as bitmask combination of #pjmedia_vid_dev_cap
*/
unsigned caps;
@@ -2070,7 +2112,7 @@ public:
~VideoDevInfo();
};
-/**
+/**
* Warning: deprecated, use VideoDevInfoVector2 instead.
*
* Array of video device info.
@@ -2091,7 +2133,7 @@ struct VideoSwitchParam
*/
pjmedia_vid_dev_index target_id;
};
-
+
/**
* Video device manager.
*/
@@ -2128,7 +2170,7 @@ public:
unsigned getDevCount();
/**
- * Retrieve the video device info for the specified device index.
+ * Retrieve the video device info for the specified device index.
*
* @param dev_id The video device id
*
@@ -2177,9 +2219,9 @@ public:
string capName(pjmedia_vid_dev_cap cap) const;
/**
- * This will configure video format capability to the video device.
- * If video device is currently active, the method will forward the setting
- * to the video device instance to be applied immediately, if it
+ * This will configure video format capability to the video device.
+ * If video device is currently active, the method will forward the setting
+ * to the video device instance to be applied immediately, if it
* supports it.
*
* This method is only valid if the device has
@@ -2202,9 +2244,9 @@ public:
/**
* Get the video format capability to the video device.
* If video device is currently active, the method will forward the request
- * to the video device. If video device is currently inactive, and if
- * application had previously set the setting and mark the setting as kept,
- * then that setting will be returned. Otherwise, this method will
+ * to the video device. If video device is currently inactive, and if
+ * application had previously set the setting and mark the setting as kept,
+ * then that setting will be returned. Otherwise, this method will
* raise error.
*
* This method is only valid if the device has
@@ -2262,10 +2304,10 @@ public:
* to the video device instance to be applied immediately, if it
* supports it.
*
- * This method is only valid if the device has
- * PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW_FLAGS capability in VideoDevInfo.caps
+ * This method is only valid if the device has
+ * PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW_FLAGS capability in VideoDevInfo.caps
* flags, otherwise Error will be thrown.
- *
+ *
* Note that in case the setting is kept for future use, it will be applied
* to any devices, even when application has changed the video device to be
* used.
@@ -2287,7 +2329,7 @@ public:
* raise error.
*
* This method is only valid if the device has
- * PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW_FLAGS capability in VideoDevInfo.caps
+ * PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW_FLAGS capability in VideoDevInfo.caps
* flags, otherwise Error will be thrown.
*
* @param dev_id The video device id.
@@ -2314,7 +2356,7 @@ public:
/**
* Check whether the video capture device is currently active, i.e. if
* a video preview has been started or there is a video call using
- * the device.
+ * the device.
*
* @param dev_id The video device id
*
@@ -2395,7 +2437,7 @@ struct CodecInfo
void fromPj(const pjsua_codec_info &codec_info);
};
-/**
+/**
* Warning: deprecated, use CodecInfoVector2 instead.
*
* Array of codec info.
@@ -2503,7 +2545,7 @@ struct CodecOpusConfig
/**
* Detailed codec attributes used in configuring a video codec and in querying
- * the capability of video codec factories.
+ * the capability of video codec factories.
*
* Please note that codec parameter also contains SDP specific setting,
* #decFmtp and #encFmtp, which may need to be set appropriately based on
@@ -2603,7 +2645,7 @@ struct MediaEvent
* will be specific to the event type being reported.
*/
MediaEventData data;
-
+
/**
* Pointer to original pjmedia_event. Only valid when the struct
* is converted from PJSIP's pjmedia_event.
diff --git a/pjsip/src/pjsua2/media.cpp b/pjsip/src/pjsua2/media.cpp
index 8c958cec1..be921c73f 100644
--- a/pjsip/src/pjsua2/media.cpp
+++ b/pjsip/src/pjsua2/media.cpp
@@ -123,7 +123,7 @@ AudioMediaTransmitParam::AudioMediaTransmitParam()
{
}
-AudioMedia::AudioMedia()
+AudioMedia::AudioMedia()
: Media(PJMEDIA_TYPE_AUDIO), id(PJSUA_INVALID_ID), mediaPool(NULL)
{
@@ -190,7 +190,7 @@ void AudioMedia::unregisterMediaPort()
Endpoint::instance().mediaRemove(*this);
}
-AudioMedia::~AudioMedia()
+AudioMedia::~AudioMedia()
{
}
@@ -224,7 +224,7 @@ void AudioMedia::startTransmit2(const AudioMedia &sink,
PJSUA2_THROW(Error)
{
pjsua_conf_connect_param pj_param;
-
+
pjsua_conf_connect_param_default(&pj_param);
pj_param.level = param.level;
PJSUA2_CHECK_EXPR( pjsua_conf_connect2(id, sink.id, &pj_param) );
@@ -404,7 +404,7 @@ void AudioMediaPlayer::eof_cb(pjmedia_port *port,
{
PJ_UNUSED_ARG(port);
AudioMediaPlayer *player = (AudioMediaPlayer*)usr_data;
-
+
player->onEof2();
}
@@ -458,6 +458,177 @@ AudioMediaRecorder* AudioMediaRecorder::typecastFromAudioMedia(
///////////////////////////////////////////////////////////////////////////////
+// pj_status_t incomingFrameHandler(pjmedia_port *port, void *usr_data);
+
+AudioMediaCapture::AudioMediaCapture()
+{
+ pool = pjsua_pool_create("capture_pool", 2000, 2000);
+ frame_buffer = NULL;
+ capture_port = NULL;
+ receive_frames = true;
+}
+
+AudioMediaCapture::~AudioMediaCapture()
+{
+ if(capture_port) {
+ unregisterMediaPort();
+ pjmedia_port_destroy(capture_port);
+ capture_port = NULL;
+ frame_buffer = NULL;
+ }
+ if(pool) {
+ pj_pool_safe_release(&pool);
+ }
+}
+
+void AudioMediaCapture::createMediaCapture()
+{
+ int sample_rate = 8000 * 6;
+ int bits_per_sample = 16;
+ int samples_per_frame = 160 * 6;
+ int channels_count = 1;
+
+ frame_size = bits_per_sample*samples_per_frame*channels_count/8;
+ frame_buffer = pj_pool_zalloc(pool, frame_size);
+
+ pj_status_t status;
+
+ status = pjmedia_mem_capture_create( pool, //Pool
+ frame_buffer, //Buffer
+ frame_size, //Buffer Size
+ sample_rate,
+ channels_count,
+ samples_per_frame,
+ bits_per_sample,
+ 0, //Options
+ &capture_port); //The return port}
+ if (status != PJ_SUCCESS) {
+ PJSUA2_RAISE_ERROR(status);
+ }
+
+ status = pjmedia_mem_capture_set_eof_cb2(capture_port, this, AudioMediaCapture::processFrames);
+ if (status != PJ_SUCCESS) {
+ PJSUA2_RAISE_ERROR(status);
+ }
+
+ registerMediaPort2(capture_port, pool);
+}
+
+void AudioMediaCapture::processFrames(pjmedia_port *port, void *usr_data) {
+ AudioMediaCapture *capture = static_cast<AudioMediaCapture *>(usr_data);
+// const std::lock_guard<std::mutex> lock(capture->frames_mtx);
+// capture->frames.push_back(string((char *)capture->frame_buffer, capture->frame_size));
+
+ if (!capture->receive_frames) {
+ return;
+ }
+
+ capture->onFrame((char *)capture->frame_buffer, capture->frame_size);
+}
+
+string AudioMediaCapture::getFramesAsString()
+{
+ const std::lock_guard<std::mutex> lock(frames_mtx);
+ std::string s="";
+ for (const auto &piece : frames) s += piece;
+ frames.clear();
+ return s;
+}
+
+void AudioMediaCapture::getFrames(char **data, size_t *datasize)
+{
+ std::string s = getFramesAsString();
+ size_t size = s.length();
+ char *c = new char[size];
+ s.copy(c, size, 0);
+ *datasize = size;
+ *data = c;
+}
+
+void AudioMediaCapture::disableReceivingFrames() {receive_frames = false;}
+void AudioMediaCapture::enableReceivingFrames() {receive_frames = true;}
+
+AudioMediaStream::AudioMediaStream() {
+ pool = pjsua_pool_create("stream_pool", 2000, 2000);
+ frame_buffer = NULL;
+ stream_port = NULL;
+}
+
+AudioMediaStream::~AudioMediaStream() {
+ if(stream_port) {
+ unregisterMediaPort();
+ pjmedia_port_destroy(stream_port);
+ stream_port = NULL;
+ frame_buffer = NULL;
+ }
+ if(pool) {
+ pj_pool_safe_release(&pool);
+ }
+}
+
+void AudioMediaStream::putFrameAsString(string data) {
+ const std::lock_guard<std::mutex> lock(frames_mtx);
+ frames.push_front(data);
+}
+
+void AudioMediaStream::putFrame(char *data, size_t datasize) {
+ const std::lock_guard<std::mutex> lock(frames_mtx);
+ frames.push_front(std::string(data, datasize));
+ //frames.push_front(frame);
+}
+
+//pj_status_t AudioMediaStream::processFrames(pjmedia_port *port, void *usr_data) {
+void AudioMediaStream::processFrames(pjmedia_port *port, void *usr_data) {
+ AudioMediaStream *stream = static_cast<AudioMediaStream *>(usr_data);
+ if(stream->frames.size() > 0) {
+ string f = stream->frames.back();
+ uint buf_size = f.length();
+ if(buf_size > stream->frame_size) buf_size = stream->frame_size;
+ f.copy((char*)stream->frame_buffer, buf_size, 0);
+ const std::lock_guard<std::mutex> lock(stream->frames_mtx);
+ stream->frames.pop_back();
+ } else {
+ memset((char*)stream->frame_buffer, 0, stream->frame_size);
+ }
+}
+
+void AudioMediaStream::createMediaStream()
+{
+ int sample_rate = 8000 * 6;
+ int bits_per_sample = 16;
+ int samples_per_frame = 160 * 6;
+ int channels_count = 1;
+
+ frame_size = bits_per_sample*samples_per_frame*channels_count/8;
+ frame_buffer = pj_pool_zalloc(pool, frame_size);
+
+ pj_status_t status;
+
+ status = pjmedia_mem_player_create( pool, //Pool
+ frame_buffer, //Buffer
+ frame_size, //Buffer Size
+ sample_rate,
+ channels_count,
+ samples_per_frame,
+ bits_per_sample,
+ 0, //Options
+ // PJMEDIA_MEM_NO_LOOP,
+ &stream_port); //The return port}
+ if (status != PJ_SUCCESS) {
+ PJSUA2_RAISE_ERROR(status);
+ }
+
+ // pjmedia_mem_player_set_eof_cb(stream_port, this, AudioMediaStream::processFrames);
+ status = pjmedia_mem_player_set_eof_cb2(stream_port, this, AudioMediaStream::processFrames);
+ if (status != PJ_SUCCESS) {
+ PJSUA2_RAISE_ERROR(status);
+ }
+
+ registerMediaPort2(stream_port, pool);
+}
+
+///////////////////////////////////////////////////////////////////////////////
+
ToneGenerator::ToneGenerator()
: pool(NULL), tonegen(NULL)
{
@@ -682,7 +853,7 @@ AudDevManager::~AudDevManager()
// At this point, devMedia should have been cleaned up by Endpoint,
// as AudDevManager destructor is called after Endpoint destructor.
//delete devMedia;
-
+
clearAudioDevList();
}
@@ -711,12 +882,14 @@ AudioMedia &AudDevManager::getPlaybackDevMedia() PJSUA2_THROW(Error)
}
void AudDevManager::setCaptureDev(int capture_dev) const PJSUA2_THROW(Error)
-{
+{
pjsua_snd_dev_param param;
+ pjsua_snd_dev_param_default(¶m);
PJSUA2_CHECK_EXPR(pjsua_get_snd_dev2(¶m));
param.capture_dev = capture_dev;
-
+ param.playback_dev = getPlaybackDev();
+
/* Normalize invalid ID or null device to default device */
if (param.playback_dev == PJMEDIA_AUD_INVALID_DEV ||
param.playback_dev == PJSUA_SND_NULL_DEV)
@@ -724,7 +897,7 @@ void AudDevManager::setCaptureDev(int capture_dev) const PJSUA2_THROW(Error)
param.playback_dev = PJMEDIA_AUD_DEFAULT_PLAYBACK_DEV;
}
- param.mode |= PJSUA_SND_DEV_NO_IMMEDIATE_OPEN;
+ param.mode = PJSUA_SND_DEV_NO_IMMEDIATE_OPEN;
PJSUA2_CHECK_EXPR( pjsua_set_snd_dev2(¶m) );
}
@@ -732,6 +905,7 @@ void AudDevManager::setCaptureDev(int capture_dev) const PJSUA2_THROW(Error)
void AudDevManager::setPlaybackDev(int playback_dev) const PJSUA2_THROW(Error)
{
pjsua_snd_dev_param param;
+ pjsua_snd_dev_param_default(¶m);
PJSUA2_CHECK_EXPR(pjsua_get_snd_dev2(¶m));
param.playback_dev = playback_dev;
@@ -745,7 +919,7 @@ void AudDevManager::setPlaybackDev(int playback_dev) const PJSUA2_THROW(Error)
param.mode |= PJSUA_SND_DEV_NO_IMMEDIATE_OPEN;
- PJSUA2_CHECK_EXPR( pjsua_set_snd_dev2(¶m) );
+ PJSUA2_CHECK_EXPR( pjsua_set_snd_dev2(¶m) );
}
#if !DEPRECATED_FOR_TICKET_2232
@@ -796,10 +970,16 @@ MediaPort *AudDevManager::setNoDev()
}
void AudDevManager::setSndDevMode(unsigned mode) const PJSUA2_THROW(Error)
-{
+{
+ int capture_dev = 0, playback_dev = 0;
pjsua_snd_dev_param param;
-
- PJSUA2_CHECK_EXPR(pjsua_get_snd_dev2(¶m));
+ pj_status_t status = pjsua_get_snd_dev(&capture_dev, &playback_dev);
+ if (status != PJ_SUCCESS) {
+ PJSUA2_RAISE_ERROR2(status, "AudDevManager::setSndDevMode()");
+ }
+ pjsua_snd_dev_param_default(¶m);
+ param.capture_dev = capture_dev;
+ param.playback_dev = playback_dev;
param.mode = mode;
PJSUA2_CHECK_EXPR( pjsua_set_snd_dev2(¶m) );
}
@@ -1099,7 +1279,7 @@ int AudDevManager::getActiveDev(bool is_capture) const PJSUA2_THROW(Error)
///////////////////////////////////////////////////////////////////////////////
ExtraAudioDevice::ExtraAudioDevice (int playdev, int recdev) :
playDev(playdev), recDev(recdev), ext_snd_dev(NULL)