JLChen
2020-12-10 a8c5f79b0d93adfa7f23601dd0fee30edc14f0d4
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
/*
 * Copyright (c) 2010-2019 Belledonne Communications SARL.
 *
 * This file is part of linphone-android
 * (see https://www.linphone.org).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.linphone.call;
 
import android.content.Context;
import android.text.Html;
import android.view.View;
import android.widget.TextView;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;
import org.linphone.LinphoneManager;
import org.linphone.R;
import org.linphone.core.AddressFamily;
import org.linphone.core.Call;
import org.linphone.core.CallListenerStub;
import org.linphone.core.CallParams;
import org.linphone.core.CallStats;
import org.linphone.core.PayloadType;
import org.linphone.core.StreamType;
import org.linphone.utils.LinphoneUtils;
 
public class CallStatsChildViewHolder {
    private Timer mTimer;
    private Call mCall;
    private CallListenerStub mListener;
    private HashMap<String, String> mEncoderTexts;
    private HashMap<String, String> mDecoderTexts;
    private Context mContext;
 
    private TextView mTitleAudio;
    private TextView mTitleVideo;
    private TextView mCodecAudio;
    private TextView mCodecVideo;
    private TextView mEncoderAudio;
    private TextView mDecoderAudio;
    private TextView mEncoderVideo;
    private TextView mDecoderVideo;
    private TextView mAudioCaptureFilter;
    private TextView mAudioPlayerFilter;
    private TextView mVideoCaptureFilter;
    private TextView mVideoDisplayFilter;
    private TextView mDlAudio;
    private TextView mUlAudio;
    private TextView mDlVideo;
    private TextView mUlVideo;
    private TextView mEdlVideo;
    private TextView mIceAudio;
    private TextView mIceVideo;
    private TextView mVideoResolutionSent;
    private TextView mVideoResolutionReceived;
    private TextView mVideoFpsSent;
    private TextView mVideoFpsReceived;
    private TextView mSenderLossRateAudio;
    private TextView mReceiverLossRateAudio;
    private TextView mSenderLossRateVideo;
    private TextView mReceiverLossRateVideo;
    private TextView mIpAudio;
    private TextView mIpVideo;
    private TextView mJitterBufferAudio;
    private View mVideoLayout;
    private View mAudioLayout;
 
    public CallStatsChildViewHolder(View view, Context context) {
        mContext = context;
 
        mEncoderTexts = new HashMap<>();
        mDecoderTexts = new HashMap<>();
 
        mTitleAudio = view.findViewById(R.id.call_stats_audio);
        mTitleVideo = view.findViewById(R.id.call_stats_video);
        mCodecAudio = view.findViewById(R.id.codec_audio);
        mCodecVideo = view.findViewById(R.id.codec_video);
        mEncoderAudio = view.findViewById(R.id.encoder_audio);
        mDecoderAudio = view.findViewById(R.id.decoder_audio);
        mEncoderVideo = view.findViewById(R.id.encoder_video);
        mDecoderVideo = view.findViewById(R.id.decoder_video);
        mAudioCaptureFilter = view.findViewById(R.id.audio_capture_filter);
        mAudioPlayerFilter = view.findViewById(R.id.audio_player_filter);
        mVideoCaptureFilter = view.findViewById(R.id.video_capture_device);
        mVideoDisplayFilter = view.findViewById(R.id.display_filter);
        mDlAudio = view.findViewById(R.id.downloadBandwith_audio);
        mUlAudio = view.findViewById(R.id.uploadBandwith_audio);
        mDlVideo = view.findViewById(R.id.downloadBandwith_video);
        mUlVideo = view.findViewById(R.id.uploadBandwith_video);
        mEdlVideo = view.findViewById(R.id.estimatedDownloadBandwidth_video);
        mIceAudio = view.findViewById(R.id.ice_audio);
        mIceVideo = view.findViewById(R.id.ice_video);
        mVideoResolutionSent = view.findViewById(R.id.video_resolution_sent);
        mVideoResolutionReceived = view.findViewById(R.id.video_resolution_received);
        mVideoFpsSent = view.findViewById(R.id.video_fps_sent);
        mVideoFpsReceived = view.findViewById(R.id.video_fps_received);
        mSenderLossRateAudio = view.findViewById(R.id.senderLossRateAudio);
        mReceiverLossRateAudio = view.findViewById(R.id.receiverLossRateAudio);
        mSenderLossRateVideo = view.findViewById(R.id.senderLossRateVideo);
        mReceiverLossRateVideo = view.findViewById(R.id.receiverLossRateVideo);
        mIpAudio = view.findViewById(R.id.ip_audio);
        mIpVideo = view.findViewById(R.id.ip_video);
        mJitterBufferAudio = view.findViewById(R.id.jitterBufferAudio);
        mVideoLayout = view.findViewById(R.id.callStatsVideo);
        mAudioLayout = view.findViewById(R.id.callStatsAudio);
 
        mListener =
                new CallListenerStub() {
                    public void onStateChanged(Call call, Call.State cstate, String message) {
                        if (cstate == Call.State.End || cstate == Call.State.Error) {
                            if (mTimer != null) {
                                org.linphone.core.tools.Log.i(
                                        "[Call Stats] Call is terminated, stopping mCountDownTimer in charge of stats refreshing.");
                                mTimer.cancel();
                            }
                        }
                    }
                };
    }
 
    public void setCall(Call call) {
        if (mCall != null) {
            mCall.removeListener(mListener);
        }
        mCall = call;
        mCall.addListener(mListener);
 
        init();
    }
 
    private void init() {
        TimerTask mTask;
 
        mTimer = new Timer();
        mTask =
                new TimerTask() {
                    @Override
                    public void run() {
                        if (mCall == null) {
                            mTimer.cancel();
                            return;
                        }
 
                        if (mTitleAudio == null
                                || mCodecAudio == null
                                || mDlVideo == null
                                || mEdlVideo == null
                                || mIceAudio == null
                                || mVideoResolutionSent == null
                                || mVideoLayout == null
                                || mTitleVideo == null
                                || mIpVideo == null
                                || mIpAudio == null
                                || mCodecVideo == null
                                || mDlAudio == null
                                || mUlAudio == null
                                || mUlVideo == null
                                || mIceVideo == null
                                || mVideoResolutionReceived == null) {
                            mTimer.cancel();
                            return;
                        }
 
                        LinphoneUtils.dispatchOnUIThread(
                                new Runnable() {
                                    @Override
                                    public void run() {
                                        if (mCall == null) {
                                            return;
                                        }
 
                                        if (mCall.getState() != Call.State.Released) {
                                            CallParams params = mCall.getCurrentParams();
                                            if (params != null) {
                                                CallStats audioStats =
                                                        mCall.getStats(StreamType.Audio);
                                                CallStats videoStats = null;
 
                                                if (params.videoEnabled())
                                                    videoStats = mCall.getStats(StreamType.Video);
 
                                                PayloadType payloadAudio =
                                                        params.getUsedAudioPayloadType();
                                                PayloadType payloadVideo =
                                                        params.getUsedVideoPayloadType();
 
                                                formatText(
                                                        mAudioPlayerFilter,
                                                        mContext.getString(
                                                                R.string.call_stats_player_filter),
                                                        mCall.getCore().getPlaybackDevice());
 
                                                formatText(
                                                        mAudioCaptureFilter,
                                                        mContext.getString(
                                                                R.string.call_stats_capture_filter),
                                                        mCall.getCore().getCaptureDevice());
 
                                                formatText(
                                                        mVideoDisplayFilter,
                                                        mContext.getString(
                                                                R.string.call_stats_display_filter),
                                                        mCall.getCore().getVideoDisplayFilter());
 
                                                formatText(
                                                        mVideoCaptureFilter,
                                                        mContext.getString(
                                                                R.string.call_stats_capture_filter),
                                                        mCall.getCore().getVideoDevice());
 
                                                displayMediaStats(
                                                        params,
                                                        audioStats,
                                                        payloadAudio,
                                                        mAudioLayout,
                                                        mTitleAudio,
                                                        mCodecAudio,
                                                        mDlAudio,
                                                        mUlAudio,
                                                        null,
                                                        mIceAudio,
                                                        mIpAudio,
                                                        mSenderLossRateAudio,
                                                        mReceiverLossRateAudio,
                                                        mEncoderAudio,
                                                        mDecoderAudio,
                                                        null,
                                                        null,
                                                        null,
                                                        null,
                                                        false,
                                                        mJitterBufferAudio);
 
                                                displayMediaStats(
                                                        params,
                                                        videoStats,
                                                        payloadVideo,
                                                        mVideoLayout,
                                                        mTitleVideo,
                                                        mCodecVideo,
                                                        mDlVideo,
                                                        mUlVideo,
                                                        mEdlVideo,
                                                        mIceVideo,
                                                        mIpVideo,
                                                        mSenderLossRateVideo,
                                                        mReceiverLossRateVideo,
                                                        mEncoderVideo,
                                                        mDecoderVideo,
                                                        mVideoResolutionSent,
                                                        mVideoResolutionReceived,
                                                        mVideoFpsSent,
                                                        mVideoFpsReceived,
                                                        true,
                                                        null);
                                            }
                                        }
                                    }
                                });
                    }
                };
        mTimer.scheduleAtFixedRate(mTask, 0, 1000);
    }
 
    private void displayMediaStats(
            CallParams params,
            CallStats stats,
            PayloadType media,
            View layout,
            TextView title,
            TextView codec,
            TextView dl,
            TextView ul,
            TextView edl,
            TextView ice,
            TextView ip,
            TextView senderLossRate,
            TextView receiverLossRate,
            TextView enc,
            TextView dec,
            TextView videoResolutionSent,
            TextView videoResolutionReceived,
            TextView videoFpsSent,
            TextView videoFpsReceived,
            boolean isVideo,
            TextView jitterBuffer) {
 
        if (stats != null) {
            String mime = null;
 
            layout.setVisibility(View.VISIBLE);
            title.setVisibility(TextView.VISIBLE);
            if (media != null) {
                mime = media.getMimeType();
                formatText(
                        codec,
                        mContext.getString(R.string.call_stats_codec),
                        mime + " / " + (media.getClockRate() / 1000) + "kHz");
            }
            if (mime != null) {
 
                formatText(
                        enc,
                        mContext.getString(R.string.call_stats_encoder_name),
                        getEncoderText(mime));
                formatText(
                        dec,
                        mContext.getString(R.string.call_stats_decoder_name),
                        getDecoderText(mime));
            }
            formatText(
                    dl,
                    mContext.getString(R.string.call_stats_download),
                    (int) stats.getDownloadBandwidth() + " kbits/s");
            formatText(
                    ul,
                    mContext.getString(R.string.call_stats_upload),
                    (int) stats.getUploadBandwidth() + " kbits/s");
            if (isVideo) {
                formatText(
                        edl,
                        mContext.getString(R.string.call_stats_estimated_download),
                        stats.getEstimatedDownloadBandwidth() + " kbits/s");
            }
            formatText(
                    ice,
                    mContext.getString(R.string.call_stats_ice),
                    stats.getIceState().toString());
            formatText(
                    ip,
                    mContext.getString(R.string.call_stats_ip),
                    (stats.getIpFamilyOfRemote() == AddressFamily.Inet6)
                            ? "IpV6"
                            : (stats.getIpFamilyOfRemote() == AddressFamily.Inet)
                                    ? "IpV4"
                                    : "Unknown");
 
            formatText(
                    senderLossRate,
                    mContext.getString(R.string.call_stats_sender_loss_rate),
                    new DecimalFormat("##.##").format(stats.getSenderLossRate()) + "%");
            formatText(
                    receiverLossRate,
                    mContext.getString(R.string.call_stats_receiver_loss_rate),
                    new DecimalFormat("##.##").format(stats.getReceiverLossRate()) + "%");
 
            if (isVideo) {
                formatText(
                        videoResolutionSent,
                        mContext.getString(R.string.call_stats_video_resolution_sent),
                        "\u2191 " + params.getSentVideoDefinition() != null
                                ? params.getSentVideoDefinition().getName()
                                : "");
                formatText(
                        videoResolutionReceived,
                        mContext.getString(R.string.call_stats_video_resolution_received),
                        "\u2193 " + params.getReceivedVideoDefinition() != null
                                ? params.getReceivedVideoDefinition().getName()
                                : "");
                formatText(
                        videoFpsSent,
                        mContext.getString(R.string.call_stats_video_fps_sent),
                        "\u2191 " + params.getSentFramerate());
                formatText(
                        videoFpsReceived,
                        mContext.getString(R.string.call_stats_video_fps_received),
                        "\u2193 " + params.getReceivedFramerate());
            } else {
                formatText(
                        jitterBuffer,
                        mContext.getString(R.string.call_stats_jitter_buffer),
                        new DecimalFormat("##.##").format(stats.getJitterBufferSizeMs()) + " ms");
            }
        } else {
            layout.setVisibility(View.GONE);
            title.setVisibility(TextView.GONE);
        }
    }
 
    private String getEncoderText(String mime) {
        String ret = mEncoderTexts.get(mime);
        if (ret == null) {
            org.linphone.mediastream.Factory msfactory =
                    LinphoneManager.getCore().getMediastreamerFactory();
            ret = msfactory.getEncoderText(mime);
            mEncoderTexts.put(mime, ret);
        }
        return ret;
    }
 
    private String getDecoderText(String mime) {
        String ret = mDecoderTexts.get(mime);
        if (ret == null) {
            org.linphone.mediastream.Factory msfactory =
                    LinphoneManager.getCore().getMediastreamerFactory();
            ret = msfactory.getDecoderText(mime);
            mDecoderTexts.put(mime, ret);
        }
        return ret;
    }
 
    private void formatText(TextView tv, String name, String value) {
        tv.setText(Html.fromHtml("<b>" + name + " </b>" + value));
    }
}