-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathAampProfiler.cpp
More file actions
669 lines (598 loc) · 26.1 KB
/
AampProfiler.cpp
File metadata and controls
669 lines (598 loc) · 26.1 KB
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
/*
* If not stated otherwise in this file or this component's license file the
* following copyright and licenses apply:
*
* Copyright 2020 RDK Management
*
* Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file AampProfiler.cpp
* @brief ProfileEventAAMP class impl
*/
#include "AampProfiler.h"
#include "AampConstants.h"
#include "AampUtils.h"
#include "AampConfig.h"
#ifdef AAMP_TELEMETRY_SUPPORT
#include "AampTelemetry2.hpp"
#endif //AAMP_TELEMETRY_SUPPORT
#include <algorithm>
#include <iomanip>
#define MAX std::max
/**
* @brief ProfileEventAAMP Constructor
*/
ProfileEventAAMP::ProfileEventAAMP():
tuneStartMonotonicBase(0), tuneStartBaseUTCMS(0), bandwidthBitsPerSecondVideo(0),
bandwidthBitsPerSecondAudio(0), buckets(), drmErrorCode(0), enabled(false), xreTimeBuckets(), tuneEventList(),
tuneEventListMtx(), mTuneFailBucketType(PROFILE_BUCKET_MANIFEST), mTuneFailErrorCode(0), rateCorrection(0), bitrateChange(0), bufferChange(0), telemetryParam(NULL), mLldLowBuffObject(NULL),discontinuityParamMutex(), mStopDurationMs(0)
{
}
std::string ProfileEventAAMP::GetTuneTimeMetricAsJson(TuneEndMetrics tuneMetricsData, const char *tuneTimeStrPrefix,
unsigned int licenseAcqNWTime, bool playerPreBuffered,
unsigned int durationSeconds, bool interfaceWifi, std::string failureReason, std::string appName)
{
//Convert to JSON format
std::string metrics = "";
cJSON *item = nullptr;
item = cJSON_CreateObject();
if( nullptr == item )
{
return metrics;
}
cJSON_AddStringToObject(item, "pre", tuneTimeStrPrefix);
cJSON_AddNumberToObject(item, "ver", AAMP_TUNETIME_VERSION);
cJSON_AddStringToObject(item, "bld", AAMP_VERSION);
cJSON_AddNumberToObject(item, "tbu", tuneStartBaseUTCMS);
cJSON_AddNumberToObject(item, "mms", buckets[PROFILE_BUCKET_MANIFEST].tStart);
cJSON_AddNumberToObject(item, "mmt", bucketDuration(PROFILE_BUCKET_MANIFEST));
cJSON_AddNumberToObject(item, "mme", buckets[PROFILE_BUCKET_MANIFEST].errorCount);
cJSON_AddNumberToObject(item, "vps", buckets[PROFILE_BUCKET_PLAYLIST_VIDEO].tStart);
cJSON_AddNumberToObject(item, "vpt", bucketDuration(PROFILE_BUCKET_PLAYLIST_VIDEO));
cJSON_AddNumberToObject(item, "vpe", buckets[PROFILE_BUCKET_PLAYLIST_VIDEO].errorCount);
cJSON_AddNumberToObject(item, "aps", buckets[PROFILE_BUCKET_PLAYLIST_AUDIO].tStart);
cJSON_AddNumberToObject(item, "apt", bucketDuration(PROFILE_BUCKET_PLAYLIST_AUDIO));
cJSON_AddNumberToObject(item, "ape", buckets[PROFILE_BUCKET_PLAYLIST_AUDIO].errorCount);
cJSON_AddNumberToObject(item, "vis", buckets[PROFILE_BUCKET_INIT_VIDEO].tStart);
cJSON_AddNumberToObject(item, "vit", bucketDuration(PROFILE_BUCKET_INIT_VIDEO));
cJSON_AddNumberToObject(item, "vie", buckets[PROFILE_BUCKET_INIT_VIDEO].errorCount);
cJSON_AddNumberToObject(item, "ais", buckets[PROFILE_BUCKET_INIT_AUDIO].tStart);
cJSON_AddNumberToObject(item, "ait", bucketDuration(PROFILE_BUCKET_INIT_AUDIO));
cJSON_AddNumberToObject(item, "aie", buckets[PROFILE_BUCKET_INIT_AUDIO].errorCount);
cJSON_AddNumberToObject(item, "vfs", buckets[PROFILE_BUCKET_FRAGMENT_VIDEO].tStart);
cJSON_AddNumberToObject(item, "vft", bucketDuration(PROFILE_BUCKET_FRAGMENT_VIDEO));
cJSON_AddNumberToObject(item, "vfe", buckets[PROFILE_BUCKET_FRAGMENT_VIDEO].errorCount);
cJSON_AddNumberToObject(item, "vfb", bandwidthBitsPerSecondVideo);
cJSON_AddNumberToObject(item, "afs", buckets[PROFILE_BUCKET_FRAGMENT_AUDIO].tStart);
cJSON_AddNumberToObject(item, "aft", bucketDuration(PROFILE_BUCKET_FRAGMENT_AUDIO));
cJSON_AddNumberToObject(item, "afe", buckets[PROFILE_BUCKET_FRAGMENT_AUDIO].errorCount);
cJSON_AddNumberToObject(item, "afb", bandwidthBitsPerSecondAudio);
cJSON_AddNumberToObject(item, "las", buckets[PROFILE_BUCKET_LA_TOTAL].tStart);
cJSON_AddNumberToObject(item, "lat", bucketDuration(PROFILE_BUCKET_LA_TOTAL));
cJSON_AddNumberToObject(item, "dfe", drmErrorCode);
cJSON_AddNumberToObject(item, "lpr", bucketDuration(PROFILE_BUCKET_LA_PREPROC));
cJSON_AddNumberToObject(item, "lnw", licenseAcqNWTime);
cJSON_AddNumberToObject(item, "lps", bucketDuration(PROFILE_BUCKET_LA_POSTPROC));
cJSON_AddNumberToObject(item, "vdd", bucketDuration(PROFILE_BUCKET_DECRYPT_VIDEO));
cJSON_AddNumberToObject(item, "add", bucketDuration(PROFILE_BUCKET_DECRYPT_AUDIO));
cJSON_AddNumberToObject(item, "gps", (playerPreBuffered && tuneMetricsData.success > 0) ? buckets[PROFILE_BUCKET_FIRST_BUFFER].tStart - buckets[PROFILE_BUCKET_PLAYER_PRE_BUFFERED].tStart : buckets[PROFILE_BUCKET_FIRST_BUFFER].tStart);
cJSON_AddNumberToObject(item, "gff", (playerPreBuffered && tuneMetricsData.success > 0) ? buckets[PROFILE_BUCKET_FIRST_FRAME].tStart - buckets[PROFILE_BUCKET_PLAYER_PRE_BUFFERED].tStart : buckets[PROFILE_BUCKET_FIRST_FRAME].tStart);
if (buckets[PROFILE_BUCKET_FIRST_FRAME].tStart > 0)
{
cJSON_AddNumberToObject(item, "gdt", (buckets[PROFILE_BUCKET_FIRST_FRAME].tStart - (buckets[PROFILE_BUCKET_DECRYPT_VIDEO].tFinish ? buckets[PROFILE_BUCKET_DECRYPT_VIDEO].tFinish : buckets[PROFILE_BUCKET_FIRST_BUFFER].tStart)));
}
else
{
cJSON_AddNumberToObject(item, "gdt", buckets[PROFILE_BUCKET_FIRST_FRAME].tStart);
}
cJSON_AddNumberToObject(item, "cnt", tuneMetricsData.contentType);
cJSON_AddNumberToObject(item, "stt", tuneMetricsData.streamType);
cJSON_AddBoolToObject(item, "ftt", tuneMetricsData.mFirstTune);
cJSON_AddNumberToObject(item, "pbm", playerPreBuffered);
cJSON_AddNumberToObject(item, "tpb", playerPreBuffered ? buckets[PROFILE_BUCKET_PLAYER_PRE_BUFFERED].tStart : 0);
cJSON_AddNumberToObject(item, "dus", durationSeconds);
cJSON_AddNumberToObject(item, "ifw", interfaceWifi);
cJSON_AddNumberToObject(item, "tat", tuneMetricsData.mTuneAttempts);
cJSON_AddNumberToObject(item, "tst", tuneMetricsData.success);
cJSON_AddStringToObject(item, "frs", failureReason.c_str());
cJSON_AddStringToObject(item, "app", appName.c_str());
cJSON_AddNumberToObject(item, "tsb", tuneMetricsData.mFogTSBEnabled);
cJSON_AddNumberToObject(item, "tot", tuneMetricsData.mTotalTime);
cJSON_AddNumberToObject(item, "pst", mStopDurationMs);
//lets use cJSON_PrintUnformatted , cJSON_Print is formated adds whitespace n hence takes more memory also eats up more logs if logged.
char *jsonStr = cJSON_PrintUnformatted(item);
if (jsonStr)
{
metrics.assign(jsonStr);
#ifdef AAMP_TELEMETRY_SUPPORT
AAMPTelemetry2 at2;
at2.send("VideoStartTime", jsonStr);
#endif // AAMP_TELEMETRY_SUPPORT
cJSON_free(jsonStr);
}
cJSON_Delete(item);
return metrics;
}
/**
* @brief Get tune time events in JSON format
*/
void ProfileEventAAMP::getTuneEventsJSON(std::string &outStr, const std::string &streamType, const char *url, bool success)
{
bool siblingEvent = false;
unsigned int tEndTime = (unsigned int)NOW_STEADY_TS_MS;
unsigned int td = (unsigned int)(tEndTime - tuneStartMonotonicBase);
size_t end = 0;
std::string tempUrl = url;
end = tempUrl.find("?");
if (end != std::string::npos)
{
tempUrl = tempUrl.substr(0, end);
}
char outPtr[512];
memset(outPtr, '\0', 512);
snprintf(outPtr, 512, "{\"s\":%lld,\"td\":%d,\"st\":\"%s\",\"u\":\"%s\",\"tf\":{\"i\":%d,\"er\":%d},\"r\":%d,\"v\":[",tuneStartBaseUTCMS, td, streamType.c_str(), tempUrl.c_str(), mTuneFailBucketType, mTuneFailErrorCode, (success ? 1 : 0));
outStr.append(outPtr);
std::lock_guard<std::mutex> lock(tuneEventListMtx);
for(auto &te:tuneEventList)
{
if(siblingEvent)
{
outStr.append(",");
}
char eventPtr[256];
memset(eventPtr, '\0', 256);
snprintf(eventPtr, 256, "{\"i\":%d,\"b\":%d,\"d\":%d,\"o\":%d}", te.id, te.start, te.duration, te.result);
outStr.append(eventPtr);
siblingEvent = true;
}
outStr.append("]}");
tuneEventList.clear();
mTuneFailErrorCode = 0;
mTuneFailBucketType = PROFILE_BUCKET_MANIFEST;
}
/**
* @brief Profiler method to perform tune begin related operations.
*/
void ProfileEventAAMP::TuneBegin(void)
{ // start tune
memset(buckets, 0, sizeof(buckets));
tuneStartBaseUTCMS = NOW_SYSTEM_TS_MS;
tuneStartMonotonicBase = NOW_STEADY_TS_MS;
bandwidthBitsPerSecondVideo = 0;
bandwidthBitsPerSecondAudio = 0;
drmErrorCode = 0;
enabled = true;
mTuneFailBucketType = PROFILE_BUCKET_MANIFEST;
mTuneFailErrorCode = 0;
tuneEventList.clear();
rateCorrection = 0;
bitrateChange = 0;
bufferChange = 0;
cJSON *newParam = cJSON_CreateObject();
if (!newParam)
{
AAMPLOG_ERR("TuneBegin: cJSON_CreateObject() failed (OOM); retaining existing telemetry state");
}
else
{
cJSON *oldParam;
{
std::lock_guard<std::mutex> lock(discontinuityParamMutex);
mLldLowBuffObject = nullptr;
// mLldLowBuffObject is a child of telemetryParam, so it's automatically deleted below
oldParam = telemetryParam;
telemetryParam = newParam;
}
cJSON_Delete(oldParam); // delete outside the lock to keep the critical section small
}
}
/**
* @brief Logging performance metrics after tune completion.
*
* @details
* Metric prefix: IP_AAMP_TUNETIME
*
* All time fields are offsets in milliseconds from tune start unless
* otherwise stated.
*
* Index mapping (profileData[index]):
* [0] version Protocol version (AAMP_TUNETIME_VERSION)
* [1] build Player build/version string (AAMP_VERSION)
* [2] tunestartUtcMs Absolute UTC time (ms) at tune start
* [3] manifestStart Main manifest download start offset
* [4] manifestTotal Main manifest download duration
* [5] manifestFailCount Main manifest download failure count
* [6] videoPlaylistStart Video playlist download start offset
* [7] videoPlaylistTotal Video playlist download duration
* [8] videoPlaylistFailCount Video playlist download failure count
* [9] audioPlaylistStart Audio playlist download start offset
* [10] audioPlaylistTotal Audio playlist download duration
* [11] audioPlaylistFailCount Audio playlist download failure count
* [12] videoInitStart Video init-segment download start offset
* [13] videoInitTotal Video init-segment download duration
* [14] videoInitFailCount Video init-segment download failure count
* [15] audioInitStart Audio init-segment download start offset
* [16] audioInitTotal Audio init-segment download duration
* [17] audioInitFailCount Audio init-segment download failure count
* [18] videoFragStart Video fragment download start offset
* [19] videoFragTotal Video fragment download duration
* [20] videoFragFailCount Video fragment download failure count
* [21] videoFragBandwidth Video fragment bandwidth (bps)
* [22] audioFragStart Audio fragment download start offset
* [23] audioFragTotal Audio fragment download duration
* [24] audioFragFailCount Audio fragment download failure count
* [25] audioFragBandwidth Audio fragment bandwidth (bps)
* [26] drmLicenseStart DRM license acquisition start offset
* [27] drmLicenseTotal DRM license acquisition duration
* [28] drmFailErrorCode DRM failure error code (0 if success)
* [29] laPreProcDuration License acquisition pre-processing (ms)
* [30] laNetworkDuration License acquisition network duration (ms)
* [31] laPostProcDuration License acquisition post-processing (ms)
* [32] videoDecryptDuration Video decrypt duration (ms)
* [33] audioDecryptDuration Audio decrypt duration (ms)
* [34] gstStart Offset (ms) from tune start when pipeline first fed data
* [35] gstFirstFrame Offset (ms) from tune start when first video frame is decoded/presented
* [36] contentType Playback mode (e.g., LINEAR, VOD, etc)
* [37] streamType Stream type (e.g., HLS, DASH, etc)
* [38] firstTune First tune after reboot/crash (bool)
* [39] prebuffered Player was in prebuffer (BG) mode (bool)
* [40] prebufferedTime Time spent in prebuffer (BG) mode (ms)
* [41] durationSeconds Asset duration (seconds)
* [42] interfaceWifi Connection type: wifi(1) ethernet(0)
* [43] tuneAttempts Number of tune attempts
* [44] success Tune status: success(1) failure(0)
* [45] failureReason Failure reason string
* [46] appName Application name string
* [47] timedMetadataCount Timed metadata count
* [48] timedMetadataStart Timed metadata start offset (clamped >= 0)
* [49] timedMetadataTotal Timed metadata total duration (ms)
* [50] tsbEnabled TSB enabled (1) / disabled (0)
* [51] totalTime Total tune time (ms)
* [52] stopDuration Stop duration (ms)
* [53] gstDecodeTime Decode time to first frame excluding decryption
*
* Note: [34] and [35] are adjusted when prebuffered && success by subtracting
* the prebuffer start offset.
*/
void ProfileEventAAMP::TuneEnd(TuneEndMetrics &mTuneEndMetrics,std::string appName, std::string playerActiveMode, int playerId, bool playerPreBuffered, unsigned int durationSeconds, bool interfaceWifi, std::string failureReason, std::string *tuneMetricData)
{
if(!enabled )
{
return;
}
enabled = false;
unsigned int licenseAcqNWTime = bucketDuration(PROFILE_BUCKET_LA_NETWORK);
char tuneTimeStrPrefix[128];
memset(tuneTimeStrPrefix, '\0', sizeof(tuneTimeStrPrefix));
int totalTuneTime = 0;
int mTimedMetadataStartTime = static_cast<int> (mTuneEndMetrics.mTimedMetadataStartTime - tuneStartMonotonicBase);
auto tFirstFrameStart = buckets[PROFILE_BUCKET_FIRST_FRAME].tStart;
auto tDecryptVideoFinish = buckets[PROFILE_BUCKET_DECRYPT_VIDEO].tFinish;
auto tFirstBufferStart = buckets[PROFILE_BUCKET_FIRST_BUFFER].tStart;
auto tPreBufferStart = buckets[PROFILE_BUCKET_PLAYER_PRE_BUFFERED].tStart;
// compute gstreamer decode time, excluding decryption. For clear streams, measure from first buffer start time
auto tDecode = tFirstFrameStart;
if (tDecode > 0)
{
tDecode -= (tDecryptVideoFinish?tDecryptVideoFinish:tFirstBufferStart);
}
if (mTuneEndMetrics.success > 0)
{
totalTuneTime = playerPreBuffered ? tFirstFrameStart - tPreBufferStart : tFirstFrameStart;
}
else
{
totalTuneTime = static_cast<int> (mTuneEndMetrics.mTotalTime - tuneStartMonotonicBase);
}
// Store the computed total tune time back in mTuneEndMetrics so
// TuneTimeMetricsData sees the updated value from this metrics struct.
mTuneEndMetrics.mTotalTime = totalTuneTime;
if (!appName.empty())
{
if (gpGlobalConfig && gpGlobalConfig->IsConfigSet(eAAMPConfig_UseFireboltSDK))
{
appName += "_VIPA";
}
snprintf(tuneTimeStrPrefix, sizeof(tuneTimeStrPrefix), "%s PLAYER[%d] APP: %s IP_AAMP_TUNETIME", playerActiveMode.c_str(),playerId,appName.c_str());
}
else
{
snprintf(tuneTimeStrPrefix, sizeof(tuneTimeStrPrefix), "%s PLAYER[%d] IP_AAMP_TUNETIME", playerActiveMode.c_str(),playerId);
}
AAMPLOG_MIL("%s:%d,%s,%lld," // prefix, version, build, tuneStartBaseUTCMS
"%d,%d,%d," // main manifest (start,total,err)
"%d,%d,%d," // video playlist (start,total,err)
"%d,%d,%d," // audio playlist (start,total,err)
"%d,%d,%d," // video init-segment (start,total,err)
"%d,%d,%d," // audio init-segment (start,total,err)
"%d,%d,%d,%" BITSPERSECOND_FORMAT "," // video fragment (start,total,err, bitrate)
"%d,%d,%d,%" BITSPERSECOND_FORMAT "," // audio fragment (start,total,err, bitrate)
"%d,%d,%d," // licenseAcqStart, licenseAcqTotal, drmFailErrorCode
"%d,%d,%d," // LAPreProcDuration, LANetworkDuration, LAPostProcDuration
"%d,%d," // VideoDecryptDuration, AudioDecryptDuration
"%d,%d," // gstPlayStartTime, gstFirstFrameTime
"%d,%d,%d," // contentType, streamType, firstTune
"%d,%d," // If Player was in prebuffered mode, time spent in prebuffered(BG) mode
"%d,%d," // Asset duration in seconds, Connection is wifi or not - wifi(1) ethernet(0)
"%d,%d,%s,%s," // TuneAttempts ,Tunestatus -success(1) failure (0) ,Failure Reason, AppName
"%d,%d,%d,%d,%d," // TimedMetadata (count,start,total) ,TSBEnabled or not - enabled(1) not enabled(0)
// TotalTime -for failure and interrupt tune -it is time at which failure /interrupt reported
// TODO: settop type, flags, isFOGEnabled, isDDPlus, isDemuxed, assetDurationMs
"%u,%d", // Stop Duration, gstDecodeTime;
tuneTimeStrPrefix,
AAMP_TUNETIME_VERSION, // version for this protocol, initially zero
AAMP_VERSION, // build - incremented when there are significant player changes/optimizations
tuneStartBaseUTCMS, // when tune logically started from AAMP perspective
buckets[PROFILE_BUCKET_MANIFEST].tStart, bucketDuration(PROFILE_BUCKET_MANIFEST), buckets[PROFILE_BUCKET_MANIFEST].errorCount,
buckets[PROFILE_BUCKET_PLAYLIST_VIDEO].tStart, bucketDuration(PROFILE_BUCKET_PLAYLIST_VIDEO), buckets[PROFILE_BUCKET_PLAYLIST_VIDEO].errorCount,
buckets[PROFILE_BUCKET_PLAYLIST_AUDIO].tStart, bucketDuration(PROFILE_BUCKET_PLAYLIST_AUDIO), buckets[PROFILE_BUCKET_PLAYLIST_AUDIO].errorCount,
buckets[PROFILE_BUCKET_INIT_VIDEO].tStart, bucketDuration(PROFILE_BUCKET_INIT_VIDEO), buckets[PROFILE_BUCKET_INIT_VIDEO].errorCount,
buckets[PROFILE_BUCKET_INIT_AUDIO].tStart, bucketDuration(PROFILE_BUCKET_INIT_AUDIO), buckets[PROFILE_BUCKET_INIT_AUDIO].errorCount,
buckets[PROFILE_BUCKET_FRAGMENT_VIDEO].tStart, bucketDuration(PROFILE_BUCKET_FRAGMENT_VIDEO), buckets[PROFILE_BUCKET_FRAGMENT_VIDEO].errorCount,bandwidthBitsPerSecondVideo,
buckets[PROFILE_BUCKET_FRAGMENT_AUDIO].tStart, bucketDuration(PROFILE_BUCKET_FRAGMENT_AUDIO), buckets[PROFILE_BUCKET_FRAGMENT_AUDIO].errorCount,bandwidthBitsPerSecondAudio,
buckets[PROFILE_BUCKET_LA_TOTAL].tStart, bucketDuration(PROFILE_BUCKET_LA_TOTAL), drmErrorCode,
bucketDuration(PROFILE_BUCKET_LA_PREPROC), licenseAcqNWTime, bucketDuration(PROFILE_BUCKET_LA_POSTPROC),
bucketDuration(PROFILE_BUCKET_DECRYPT_VIDEO),bucketDuration(PROFILE_BUCKET_DECRYPT_AUDIO),
(playerPreBuffered && mTuneEndMetrics.success > 0) ? tFirstBufferStart - tPreBufferStart : tFirstBufferStart, // gstPlaying: offset in ms from tunestart when pipeline first fed data
(playerPreBuffered && mTuneEndMetrics.success > 0) ? tFirstFrameStart - tPreBufferStart : tFirstFrameStart, // gstFirstFrame: offset in ms from tunestart when first frame of video is decoded/presented
mTuneEndMetrics.contentType, mTuneEndMetrics.streamType, mTuneEndMetrics.mFirstTune,
playerPreBuffered,
playerPreBuffered ? tPreBufferStart : 0,
durationSeconds,interfaceWifi,
mTuneEndMetrics.mTuneAttempts, mTuneEndMetrics.success, failureReason.c_str(), appName.c_str(),
mTuneEndMetrics.mTimedMetadata,
mTimedMetadataStartTime < 0 ? 0 : mTimedMetadataStartTime,
mTuneEndMetrics.mTimedMetadataDuration, mTuneEndMetrics.mFogTSBEnabled, totalTuneTime, mStopDurationMs,
tDecode // gstDecode: time taken to decode first frame, excluding decryption time. For clear streams, this will be the overall time spent in pipeline
);
// Telemetry is generated in GetTuneTimeMetricAsJson hence calling always,
std::string metricsDataJson = GetTuneTimeMetricAsJson(mTuneEndMetrics, tuneTimeStrPrefix, licenseAcqNWTime, playerPreBuffered, durationSeconds, interfaceWifi, std::move(failureReason), std::move(appName));
// tuneMetricData could be NULL if application has not registered for tuneMetrics event,
if( NULL != tuneMetricData)
{
//provided the time tune metric data as an json format to application
*tuneMetricData = std::move(metricsDataJson);
}
}
/**
* @brief Marking the beginning of a bucket
*/
void ProfileEventAAMP::ProfileBegin(ProfilerBucketType type)
{
struct ProfilerBucket *bucket = &buckets[type];
if (!bucket->complete && (0==bucket->tStart)) //No other Begin should record before the End
{
bucket->tStart = (unsigned int)(NOW_STEADY_TS_MS - tuneStartMonotonicBase);
bucket->tFinish = bucket->tStart;
bucket->profileStarted = true;
}
}
/**
* @brief Marking error while executing a bucket
*/
void ProfileEventAAMP::ProfileError(ProfilerBucketType type, int result)
{
struct ProfilerBucket *bucket = &buckets[type];
if (!bucket->complete && bucket->profileStarted)
{
SetTuneFailCode(result, type);
bucket->errorCount++;
}
}
/**
* @brief Marking the end of a bucket
*/
void ProfileEventAAMP::ProfileEnd(ProfilerBucketType type)
{
struct ProfilerBucket *bucket = &buckets[type];
if (!bucket->complete && bucket->profileStarted)
{
bucket->tFinish = (unsigned int)(NOW_STEADY_TS_MS - tuneStartMonotonicBase);
bucket->complete = true;
}
}
/**
* @brief Resetting the buckets
*/
void ProfileEventAAMP::ProfileReset(ProfilerBucketType type)
{
struct ProfilerBucket *bucket = &buckets[type];
bucket->complete = false;
bucket->profileStarted = false;
bucket->tFinish = 0;
bucket->tStart = 0;
}
/**
* @brief Method to mark the end of a bucket, for which beginning is not marked
*/
void ProfileEventAAMP::ProfilePerformed(ProfilerBucketType type)
{
ProfileBegin(type);
buckets[type].complete = true;
}
/**
* @brief Method to set Failure code and Bucket Type used for microevents
*/
void ProfileEventAAMP::SetTuneFailCode(int tuneFailCode, ProfilerBucketType failBucketType)
{
if(!mTuneFailErrorCode)
{
AAMPLOG_INFO("Tune Fail: ProfilerBucketType: %d, tuneFailCode: %d", failBucketType, tuneFailCode);
mTuneFailErrorCode = tuneFailCode;
mTuneFailBucketType = failBucketType;
}
}
/**
* @brief to mark the discontinuity switch and save the parameters
*/
void ProfileEventAAMP::SetDiscontinuityParam()
{
ProfileEnd(PROFILE_BUCKET_DISCO_FIRST_FRAME);
ProfileEnd(PROFILE_BUCKET_DISCO_TOTAL);
std::lock_guard<std::mutex> lock(discontinuityParamMutex);
if(telemetryParam)
{
cJSON* discontinuity = cJSON_AddArrayToObject(telemetryParam, "disc");
if(discontinuity)
{
cJSON* item;
unsigned int total = bucketDuration(PROFILE_BUCKET_DISCO_TOTAL);
unsigned int flush = bucketDuration(PROFILE_BUCKET_DISCO_FLUSH);
unsigned int fframe = bucketDuration(PROFILE_BUCKET_DISCO_FIRST_FRAME);
unsigned int diff = total - (flush + fframe);
cJSON_AddItemToArray(discontinuity, item = cJSON_CreateObject());
cJSON_AddNumberToObject(item,"tt",total);
cJSON_AddNumberToObject(item,"ft",flush);
cJSON_AddNumberToObject(item,"fft",fframe);
cJSON_AddNumberToObject(item,"d",diff);
ProfileReset(PROFILE_BUCKET_DISCO_TOTAL);
ProfileReset(PROFILE_BUCKET_DISCO_FLUSH);
ProfileReset(PROFILE_BUCKET_DISCO_FIRST_FRAME);
}
}
}
/**
* @brief API to add LLD specific Low buffer object
*/
void ProfileEventAAMP::AddLLDLowBufferObject()
{
if(telemetryParam)
{
if (mLldLowBuffObject == NULL)
{
mLldLowBuffObject = cJSON_AddArrayToObject(telemetryParam, "lldlb");
}
}
}
/**
* @brief API to add double with pre defined precision
*/
void ProfileEventAAMP::AddWithPrecisionNumber(cJSON* item, const char* label, double num)
{
if (item)
{
std::ostringstream streamObj;
streamObj << std::fixed << std::setprecision(2) << num;/**< Set precision to 2 digits */
std::string str = streamObj.str(); /**< Converts double to string */
AAMPLOG_TRACE("[DEBUG]string num = %s double num : %lf", str.c_str(), std::stod(str));
//cJSON_AddStringToObject(item, label, str.c_str());
cJSON_AddNumberToObject(item, label, std::stod(str));
}
}
/**
* @brief to mark the discontinuity switch and save the parameters
*/
void ProfileEventAAMP::SetLLDLowBufferParam(double latency, double buff, double rate, double bw, double buffLowCount)
{
std::lock_guard<std::mutex> lock(discontinuityParamMutex);
if(telemetryParam)
{
AddLLDLowBufferObject();
if(mLldLowBuffObject)
{
cJSON* item;
cJSON_AddItemToArray(mLldLowBuffObject, item = cJSON_CreateObject());
AddWithPrecisionNumber(item,"lt",latency);
AddWithPrecisionNumber(item,"buf",buff);
AddWithPrecisionNumber(item,"pbr",rate);
cJSON_AddNumberToObject(item,"bw",bw);
cJSON_AddNumberToObject(item,"lbc",buffLowCount);
}
else
{
AAMPLOG_WARN("LLD Low buffer Object is not initialized !!");
}
}
}
/**
* @brief to mark the latency parameters
*/
void ProfileEventAAMP::SetLatencyParam(double latency, double buffer, double playbackRate, double bw)
{
std::lock_guard<std::mutex> lock(discontinuityParamMutex);
if(latency >= 0)
{
AddWithPrecisionNumber(telemetryParam,"lt",latency);
}
if(buffer >= 0)
{
AddWithPrecisionNumber(telemetryParam,"buf",buffer);
}
AddWithPrecisionNumber(telemetryParam,"pbr", playbackRate);
if(bw > 0)
{
cJSON_AddNumberToObject(telemetryParam,"bw", bw);
}
if(rateCorrection != 0)
{
cJSON_AddNumberToObject(telemetryParam,"rtc",rateCorrection);
rateCorrection = 0;
}
if(bitrateChange != 0)
{
cJSON_AddNumberToObject(telemetryParam,"btc",bitrateChange);
bitrateChange = 0;
}
if(bufferChange != 0)
{
cJSON_AddNumberToObject(telemetryParam,"bfc",bufferChange);
bufferChange = 0;
}
}
/**
* @brief IncrementChangeCount - to increment the changes in buffer, ratecorrection and bitrate
*/
void ProfileEventAAMP::IncrementChangeCount(CountType type)
{
if(type == Count_RateCorrection)
{
rateCorrection++;
}
else if(type == Count_BitrateChange)
{
bitrateChange++;
}
else if(type == Count_BufferChange)
{
bufferChange++;
}
}
/**
* @brief to log the telemetry parameters
*/
void ProfileEventAAMP::GetTelemetryParam()
{
cJSON *newParam = cJSON_CreateObject();
if (!newParam)
{
AAMPLOG_ERR("GetTelemetryParam: cJSON_CreateObject() failed (OOM); retaining existing telemetry state");
return;
}
cJSON *oldParam;
{
std::lock_guard<std::mutex> lock(discontinuityParamMutex);
mLldLowBuffObject = nullptr;
// mLldLowBuffObject is a child of telemetryParam, so it's automatically deleted below
oldParam = telemetryParam;
telemetryParam = newParam;
}
// Log and delete the old tree outside the lock to keep the critical section small
if (oldParam != nullptr)
{
char *jsonStr = cJSON_PrintUnformatted(oldParam);
AAMPLOG_MIL("Telemetry values %s", jsonStr);
cJSON_free(jsonStr);
cJSON_Delete(oldParam);
}
}