JLChen
2021-08-19 f798871425ebfbb7dc4fdf17b22943e4ea2a73e2
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
//package com.hdl.hdllinphonesdk.core.linphone;
//
//import android.content.Context;
//import android.content.pm.PackageManager;
//import android.os.Handler;
//import android.os.Looper;
//
//
//import com.hdl.hdllinphonesdk.HDLLinphoneKit;
//import com.hdl.hdllinphonesdk.R;
//
//
//import org.linphone.core.AudioDevice;
//import org.linphone.core.AuthInfo;
//import org.linphone.core.AuthMethod;
//import org.linphone.core.Call;
//import org.linphone.core.CallLog;
//import org.linphone.core.CallStats;
//import org.linphone.core.ChatMessage;
//import org.linphone.core.ChatRoom;
//import org.linphone.core.Conference;
//import org.linphone.core.ConfiguringState;
//import org.linphone.core.Content;
//import org.linphone.core.Core;
//import org.linphone.core.CoreException;
//import org.linphone.core.CoreListener;
//import org.linphone.core.EcCalibratorStatus;
//import org.linphone.core.Event;
//import org.linphone.core.Factory;
//import org.linphone.core.Friend;
//import org.linphone.core.FriendList;
//import org.linphone.core.GlobalState;
//import org.linphone.core.InfoMessage;
//import org.linphone.core.PayloadType;
//import org.linphone.core.PresenceModel;
//import org.linphone.core.ProxyConfig;
//import org.linphone.core.PublishState;
//import org.linphone.core.RegistrationState;
//import org.linphone.core.SubscriptionState;
//import org.linphone.core.ToneID;
//import org.linphone.core.VersionUpdateCheckResult;
//import org.linphone.core.VideoActivationPolicy;
//import org.linphone.core.VideoDefinition;
//import org.linphone.core.tools.Log;
//
//import java.io.File;
//import java.io.IOException;
//import java.nio.ByteBuffer;
//import java.util.Timer;
//import java.util.TimerTask;
//
//import static com.hdl.hdllinphonesdk.HDLLinphoneKit.HDLLinphoneKitNAME;
//
///**
// * Linphone 管理器
// */
//
//public class LinphoneManager implements CoreListener {
//    private static final String TAG = "LinphoneManager";
//    private static LinphoneManager instance;
//    private Context mServiceContext;
//    private Core mLc;
//    private Timer mTimer;
//    private static boolean sExited;
//
//    private String mLPConfigXsd = null;
//    private String mLinphoneFactoryConfigFile = null;
//    public String mLinphoneConfigFile = null;
//    private String mLinphoneRootCaFile = null;
//    private String mRingSoundFile = null;
//    private String mRingBackSoundFile = null;
//    private String mPauseSoundFile = null;
//    private String mChatDatabaseFile = null;
//
//
////    private String mErrorToneFile = null;
//
//    public LinphoneManager(Context serviceContext) {
//        mServiceContext = serviceContext;
//        Factory.instance().setDebugMode(true, HDLLinphoneKitNAME);
//        sExited = false;
//
//        String basePath = mServiceContext.getFilesDir().getAbsolutePath();
//        mLPConfigXsd = basePath + "/lpconfig.xsd";
//        mLinphoneFactoryConfigFile = basePath + "/linphonerc";
//        mLinphoneConfigFile = basePath + "/.linphonerc";
//        mLinphoneRootCaFile = basePath + "/rootca.pem";
//        mRingSoundFile = basePath + "/oldphone_mono.wav";
//        mRingBackSoundFile = basePath + "/ringback.wav";
//        mPauseSoundFile = basePath + "/toy_mono.wav";
//        mChatDatabaseFile = basePath + "/linphone-history.db";
////        mErrorToneFile = basePath + "/error.wav";
//    }
//
//    public synchronized static final LinphoneManager createAndStart(Context context) {
//        if (instance != null) {
//            throw new RuntimeException("Linphone Manager is already initialized");
//        }
//        instance = new LinphoneManager(context);
//        instance.startLibLinphone(context);
//        return instance;
//    }
//
//    public static synchronized Core getLcIfManagerNotDestroyOrNull() {
//        if (sExited || instance == null) {
//            Log.e("Trying to get linphone core while LinphoneManager already destroyed or not created");
//            return null;
//        }
//        return getLc();
//    }
//
//    public static final boolean isInstanceiated() {
//        return instance != null;
//    }
//
//    public static synchronized final Core getLc() {
//        return getInstance().mLc;
//    }
//
//    public static synchronized final LinphoneManager getInstance() {
//        if (instance != null) {
//            return instance;
//        }
//        if (sExited) {
//            throw new RuntimeException("Linphone Manager was already destroyed. "
//                    + "Better use getLcIfManagerNotDestroyed and check returned value");
//        }
//        throw new RuntimeException("Linphone Manager should be created before accessed");
//    }
//
//    private synchronized void startLibLinphone(Context context) {
//        try {
//            copyAssetsFromPackage();
////            mLc = Factory.instance().createCore(this, mLinphoneConfigFile,
////                    mLinphoneFactoryConfigFile, null, context);
//            mLc = Factory.instance().createCore( mLinphoneConfigFile,
//                    mLinphoneFactoryConfigFile,  context);
//            mLc.addListener((CoreListener)context);
//
//            try {
//                initLibLinphone();
//            } catch (CoreException e) {
//                Log.e(e);
//            }
//
//            TimerTask task = new TimerTask() {
//                @Override
//                public void run() {
//                    new Handler(Looper.getMainLooper()).post(new Runnable() {
//                        @Override
//                        public void run() {
//                            if (mLc != null) {
//                                mLc.iterate();
//                            }
//                        }
//                    });
//                }
//            };
//            mTimer = new Timer("Linphone Scheduler");
//            mTimer.schedule(task, 0, 20);
//        } catch (Exception e) {
//            e.printStackTrace();
//            Log.e(TAG, "startLibLinphone: cannot start linphone");
//        }
//    }
//
//    private synchronized void initLibLinphone() throws CoreException {
////        mLc.setContext(mServiceContext);
//        setUserAgent();
//        mLc.setRemoteRingbackTone(mRingSoundFile);
//        mLc.setTone(ToneID.CallWaiting, mRingSoundFile);
//        mLc.setRing(mRingSoundFile);
////        mLc.setRootCA(mLinphoneRootCaFile);
//        mLc.setPlayFile(mPauseSoundFile);
////        mLc.setChatDatabasePath(mChatDatabaseFile);
//        mLc.enableIpv6(true);
//        mLc.enableDnsSrv(true);
////        mLc.setCallErrorTone(Reason.NotFound, mErrorToneFile);//设置呼叫错误播放的铃声
//
//        setBackCamAsDefault();
//
//        int availableCores = Runtime.getRuntime().availableProcessors();
//        Log.w(TAG, "MediaStreamer : " + availableCores + " cores detected and configured");
////        mLc.setCpuCount(availableCores);
//
//        int migrationResult = getLc().migrateToMultiTransport();
//        Log.d(TAG, "Migration to multi transport result = " + migrationResult);
//
//        mLc.setNetworkReachable(true);
//
//        //回声消除
////        boolean isEchoCancellation = (boolean) SPUtils.get(mServiceContext, "echo_cancellation", true);
//        mLc.enableEchoCancellation(true);
//        //自适应码率控制
////        boolean isAdaptiveRateControl = (boolean) SPUtils.get(mServiceContext, "adaptive_rate_control", true);
//        mLc.enableAdaptiveRateControl(true);
//
//        //audio 码率设置
//        LinphoneUtils.getConfig(mServiceContext).setInt("audio", "codec_bitrate_limit", 36);
//
////        mLc.setPreferredVideoSizeByName("qvga");
//
//        VideoDefinition preferredVideoDefinition =
//                Factory.instance().createVideoDefinitionFromName("qvga");
//        getLc().setPreferredVideoDefinition(preferredVideoDefinition);
//
//        mLc.setUploadBandwidth(1536);
//        mLc.setDownloadBandwidth(1536);
//        mLc.setVideoPreset("custom");
//
////        mLc.setVideoPolicy(mLc.getVideoAutoInitiatePolicy(), true);
////        mLc.setVideoPolicy(true, mLc.getVideoAutoAcceptPolicy());
////        mLc.enableVideo(true, true);
////        mLc.enableDownloadOpenH264(true);
//
//        mLc.enableVideoCapture(true);
//        mLc.enableVideoDisplay(true);
//        mLc.getConfig().setBool("app", "open_h264_download_enabled", true);
//        VideoActivationPolicy vap = getLc().getVideoActivationPolicy();
//        vap.setAutomaticallyInitiate(true);
//        mLc.setVideoActivationPolicy(vap);
//
//        // 设置编码格式
//        setCodecMime();
//
////        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
////        filter.addAction(Intent.ACTION_SCREEN_OFF);
////        mServiceContext.registerReceiver(mKeepAliveReceiver, filter);
//    }
//
//    private void setCodecMime() {
////        for (PayloadType payloadType : mLc.getAudioCodecs()) {
////            try {
////                mLc.enablePayloadType(payloadType, true);
////            } catch (CoreException e) {
////                e.printStackTrace();
////            }
//////            android.util.Log.e(TAG, "setCodecMime = " + payloadType.getMime() + " Rate " + payloadType.getRate() + " receviceFmtp " + payloadType.getRecvFmtp());
//////            if (payloadType.getMime().equals("PCMA") && payloadType.getRate() == 8000) {
//////                try {
//////                    android.util.Log.e(TAG, "setCodecMime: " + payloadType.getMime() + " " + payloadType.getRate());
//////                    mLc.enablePayloadType(payloadType, true);
//////                } catch (CoreException e) {
//////                    android.util.Log.e(TAG, "setCodecMime: " + e);
//////                }
//////            } else {
//////                try {
//////                    mLc.enablePayloadType(payloadType, false);
//////                } catch (CoreException e) {
//////                    e.printStackTrace();
//////                }
//////            }
////        }
////        for (PayloadType payloadType : mLc.getVideoCodecs()) {
////            try {
////                android.util.Log.e(TAG, "setCodecMime: mime: " + payloadType.getMime() + " rate: " + payloadType.getRate());
////                mLc.enablePayloadType(payloadType, true);
////            } catch (CoreException e) {
////                e.printStackTrace();
////            }
////        }
//
//        PayloadType[] ptList = mLc.getAudioPayloadTypes();
//        for (PayloadType pt : ptList) {
//
//            org.linphone.mediastream.Log.d("payloadaudio",pt.getMimeType());
//            if (pt.getMimeType().equals("PCMA") && pt.getClockRate() == 8000) {
//                pt.enable(true);
//
//            } else {
//                pt.enable(false);
//            }
//        }
//        mLc.setAudioPayloadTypes(ptList);
//
//        PayloadType[] ptVideoList = mLc.getVideoPayloadTypes();
//        for (PayloadType pt : ptVideoList) {
//            pt.enable(true);
//        }
//        mLc.setVideoPayloadTypes(ptVideoList);
//    }
//
//    private void copyAssetsFromPackage() throws IOException {
//        LinphoneUtils.copyIfNotExist(mServiceContext, R.raw.oldphone_mono, mRingSoundFile);
//        LinphoneUtils.copyIfNotExist(mServiceContext, R.raw.ringback, mRingBackSoundFile);
//        LinphoneUtils.copyIfNotExist(mServiceContext, R.raw.toy_mono, mPauseSoundFile);
//        LinphoneUtils.copyIfNotExist(mServiceContext, R.raw.linphonerc_default, mLinphoneConfigFile);
//        LinphoneUtils.copyIfNotExist(mServiceContext, R.raw.linphonerc_factory, new File(mLinphoneFactoryConfigFile).getName());
//        LinphoneUtils.copyIfNotExist(mServiceContext, R.raw.lpconfig, mLPConfigXsd);
//        LinphoneUtils.copyIfNotExist(mServiceContext, R.raw.rootca, mLinphoneRootCaFile);
//    }
//
//    private void setUserAgent() {
//        try {
//            String versionName = mServiceContext.getPackageManager().getPackageInfo(mServiceContext.getPackageName(),
//                    0).versionName;
//            if (versionName == null) {
//                versionName = String.valueOf(mServiceContext.getPackageManager().getPackageInfo(mServiceContext.getPackageName(), 0).versionCode);
//            }
//            mLc.setUserAgent(HDLLinphoneKitNAME, versionName);
//        } catch (PackageManager.NameNotFoundException e) {
//            e.printStackTrace();
//        }
//    }
//
//    public static synchronized void destroy() {
//        if (instance == null) {
//            return;
//        }
//        sExited = true;
//        instance.doDestroy();
//    }
//
//    private void doDestroy() {
//        try {
//            mTimer.cancel();
//            mLc.stop();
//        } catch (RuntimeException e) {
//            e.printStackTrace();
//        } finally {
//            mLc = null;
//            instance = null;
//        }
//    }
//
//
//    private void setBackCamAsDefault() {
////        int camId = 0;
////        AndroidCameraConfiguration.AndroidCamera[] cameras = AndroidCameraConfiguration.retrieveCameras();
////        for (AndroidCameraConfiguration.AndroidCamera androidCamera :
////                cameras) {
////            if (!androidCamera.frontFacing) {
////                camId = androidCamera.id;
////            }
////        }
////        android.util.Log.e(TAG, "setBackCamAsDefault: cameraId is " + camId);
////        mLc.setVideoDevice(0);
//    }
//
//    @Override
//    public void onQrcodeFound(Core core, String result) {
//
//    }
//
//    @Override
//    public void onInfoReceived(Core core, Call call, InfoMessage message) {
//
//    }
//
//    @Override
//    public void onTransferStateChanged(Core core, Call transfered, Call.State callState) {
//
//    }
//
//    @Override
//    public void onCallEncryptionChanged(Core core, Call call, boolean mediaEncryptionEnabled, String authenticationToken) {
//
//    }
//
//    @Override
//    public void onEcCalibrationAudioInit(Core core) {
//
//    }
//
//    @Override
//    public void onDtmfReceived(Core core, Call call, int dtmf) {
//
//    }
//
//    @Override
//    public void onChatRoomStateChanged(Core core, ChatRoom chatRoom, ChatRoom.State state) {
//
//    }
//
//    @Override
//    public void onLogCollectionUploadStateChanged(Core core, Core.LogCollectionUploadState state, String info) {
//
//    }
//
//    @Override
//    public void onAudioDevicesListUpdated(Core core) {
//
//    }
//
//    @Override
//    public void onCallIdUpdated(Core core, String previousCallId, String currentCallId) {
//
//    }
//
//    @Override
//    public void onMessageSent(Core core, ChatRoom chatRoom, ChatMessage message) {
//
//    }
//
//    @Override
//    public void onFriendListRemoved(Core core, FriendList friendList) {
//
//    }
//
//    @Override
//    public void onSubscribeReceived(Core core, Event linphoneEvent, String subscribeEvent, Content body) {
//
//    }
//
//    @Override
//    public void onAuthenticationRequested(Core core, AuthInfo authInfo, AuthMethod method) {
//
//    }
//
//    @Override
//    public void onEcCalibrationAudioUninit(Core core) {
//
//    }
//
//    @Override
//    public void onLogCollectionUploadProgressIndication(Core core, int offset, int total) {
//
//    }
//
//    @Override
//    public void onImeeUserRegistration(Core core, boolean status, String userId, String info) {
//
//    }
//
//    @Override
//    public void onRegistrationStateChanged(Core core, ProxyConfig proxyConfig, RegistrationState state, String message) {
//
//    }
//
//    @Override
//    public void onCallStateChanged(Core core, Call call, Call.State state, String message) {
//
//    }
//
//    @Override
//    public void onConfiguringStatus(Core core, ConfiguringState status, String message) {
//
//    }
//
//    @Override
//    public void onGlobalStateChanged(Core core, GlobalState state, String message) {
//
//    }
//
//    @Override
//    public void onChatRoomEphemeralMessageDeleted(Core core, ChatRoom chatRoom) {
//
//    }
//
//    @Override
//    public void onFriendListCreated(Core core, FriendList friendList) {
//
//    }
//
//    @Override
//    public void onBuddyInfoUpdated(Core core, Friend linphoneFriend) {
//
//    }
//
//    @Override
//    public void onChatRoomSubjectChanged(Core core, ChatRoom chatRoom) {
//
//    }
//
//    @Override
//    public void onNotifyReceived(Core core, Event linphoneEvent, String notifiedEvent, Content body) {
//
//    }
//
//    @Override
//    public void onLastCallEnded(Core core) {
//
//    }
//
//    @Override
//    public void onChatRoomRead(Core core, ChatRoom chatRoom) {
//
//    }
//
//    @Override
//    public void onNotifyPresenceReceived(Core core, Friend linphoneFriend) {
//
//    }
//
//    @Override
//    public void onConferenceStateChanged(Core core, Conference conference, Conference.State state) {
//
//    }
//
//    @Override
//    public void onVersionUpdateCheckResultReceived(Core core, VersionUpdateCheckResult result, String version, String url) {
//
//    }
//
//    @Override
//    public void onMessageReceived(Core core, ChatRoom chatRoom, ChatMessage message) {
//
//    }
//
//    @Override
//    public void onCallStatsUpdated(Core core, Call call, CallStats callStats) {
//
//    }
//
//    @Override
//    public void onFirstCallStarted(Core core) {
//
//    }
//
//    @Override
//    public void onSubscriptionStateChanged(Core core, Event linphoneEvent, SubscriptionState state) {
//
//    }
//
//    @Override
//    public void onMessageReceivedUnableDecrypt(Core core, ChatRoom chatRoom, ChatMessage message) {
//
//    }
//
//    @Override
//    public void onEcCalibrationResult(Core core, EcCalibratorStatus status, int delayMs) {
//
//    }
//
//    @Override
//    public void onIsComposingReceived(Core core, ChatRoom chatRoom) {
//
//    }
//
//    @Override
//    public void onReferReceived(Core core, String referTo) {
//
//    }
//
//    @Override
//    public void onNetworkReachable(Core core, boolean reachable) {
//
//    }
//
//    @Override
//    public void onPublishStateChanged(Core core, Event linphoneEvent, PublishState state) {
//
//    }
//
//    @Override
//    public void onAudioDeviceChanged(Core core, AudioDevice audioDevice) {
//
//    }
//
//    @Override
//    public void onCallLogUpdated(Core core, CallLog callLog) {
//
//    }
//
//    @Override
//    public void onCallCreated(Core core, Call call) {
//
//    }
//
//    @Override
//    public void onNotifyPresenceReceivedForUriOrTel(Core core, Friend linphoneFriend, String uriOrTel, PresenceModel presenceModel) {
//
//    }
//
//    @Override
//    public void onNewSubscriptionRequested(Core core, Friend linphoneFriend, String url) {
//
//    }
//
////    public void resetCameraFromPreferences(boolean useFrontCam) {
////        Core core = getCore();
////        if (core == null) return;
////
////
////        String firstDevice = null;
////        for (String camera : core.getVideoDevicesList()) {
////            if (firstDevice == null) {
////                firstDevice = camera;
////            }
////
////            if (useFrontCam) {
////                if (camera.contains("Front")) {
////                    Log.i("[Manager] Found front facing camera: " + camera);
////                    core.setVideoDevice(camera);
////                    return;
////                }
////            }
////        }
////
////        Log.i("[Manager] Using first camera available: " + firstDevice);
////        core.setVideoDevice(firstDevice);
////    }
//}