aboutsummaryrefslogtreecommitdiffstats
path: root/tests/wpt/web-platform-tests/webrtc/RTCStats-helper.js
blob: e91b40b6854f1cc6c59f7d81bf06a40e6dc0d31f (plain) (blame)
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
'use strict';

// Test is based on the following editor draft:
// webrtc-pc 20171130
// webrtc-stats 20171122

// This file depends on dictionary-helper.js which should
// be loaded from the main HTML file.

/*
  [webrtc-stats]
  6.1.  RTCStatsType enum
    enum RTCStatsType {
      "codec",
      "inbound-rtp",
      "outbound-rtp",
      "remote-inbound-rtp",
      "remote-outbound-rtp",
      "csrc",
      "peer-connection",
      "data-channel",
      "stream",
      "track",
      "transport",
      "candidate-pair",
      "local-candidate",
      "remote-candidate",
      "certificate"
    };
 */
const statsValidatorTable = {
  'codec': validateCodecStats,
  'inbound-rtp': validateInboundRtpStreamStats,
  'outbound-rtp': validateOutboundRtpStreamStats,
  'remote-inbound-rtp': validateRemoteInboundRtpStreamStats,
  'remote-outbound-rtp': validateRemoteOutboundRtpStreamStats,
  'csrc': validateContributingSourceStats,
  'peer-connection': validatePeerConnectionStats,
  'data-channel': validateDataChannelStats,
  'stream': validateMediaStreamStats,
  'track': validateMediaStreamTrackStats,
  'transport': validateTransportStats,
  'candidate-pair': validateIceCandidatePairStats,
  'local-candidate': validateIceCandidateStats,
  'remote-candidate': validateIceCandidateStats,
  'certificate': validateCertificateStats
};

// Validate that the stats objects in a stats report
// follows the respective definitions.
// Stats objects with unknown type are ignored and
// only basic validation is done.
function validateStatsReport(statsReport) {
  for(const [id, stats] of statsReport.entries()) {
    assert_equals(stats.id, id,
      'expect stats.id to be the same as the key in statsReport');

    const validator = statsValidatorTable[stats.type];
    if(validator) {
      validator(statsReport, stats);
    } else {
      validateRtcStats(statsReport, stats);
    }
  }
}

// Assert that the stats report have stats objects of
// given types
function assert_stats_report_has_stats(statsReport, statsTypes) {
  const hasTypes = new Set([...statsReport.values()]
    .map(stats => stats.type));

  for(const type of statsTypes) {
    assert_true(hasTypes.has(type),
      `Expect statsReport to contain stats object of type ${type}`);
  }
}

function findStatsFromReport(statsReport, predicate, message) {
  for (const stats of statsReport.values()) {
    if (predicate(stats)) {
      return stats;
    }
  }

  assert_unreached(message || 'none of stats in statsReport satisfy given condition')
}

// Get stats object of type that is expected to be
// found in the statsReport
function getRequiredStats(statsReport, type) {
  for(const stats of statsReport.values()) {
    if(stats.type === type) {
      return stats;
    }
  }

  assert_unreached(`required stats of type ${type} is not found in stats report`);
}

// Get stats object by the stats ID.
// This is used to retreive other stats objects
// linked to a stats object
function getStatsById(statsReport, statsId) {
  assert_true(statsReport.has(statsId),
    `Expect stats report to have stats object with id ${statsId}`);

  return statsReport.get(statsId);
}

// Validate an ID field in a stats object by making sure
// that the linked stats object is found in the stats report
// and have the type field value same as expected type
// It doesn't validate the other fields of the linked stats
// as validateStatsReport already does all validations
function validateIdField(statsReport, stats, field, type) {
  assert_string_field(stats, field);
  const linkedStats = getStatsById(statsReport, stats[field]);
  assert_equals(linkedStats.type, type,
    `Expect linked stats object to have type ${type}`);
}

function validateOptionalIdField(statsReport, stats, field, type) {
  if(stats[field] !== undefined) {
    validateIdField(statsReport, stats, field, type);
  }
}

/*
  [webrtc-pc]
  8.4.  RTCStats Dictionary
    dictionary RTCStats {
      required  DOMHighResTimeStamp timestamp;
      required  RTCStatsType        type;
      required  DOMString           id;
    };
 */
function validateRtcStats(statsReport, stats) {
  assert_number_field(stats, 'timestamp');
  assert_string_field(stats, 'type');
  assert_string_field(stats, 'id');
}

/*
  [webrtc-stats]
  7.1.  RTCRTPStreamStats dictionary
    dictionary RTCRTPStreamStats : RTCStats {
      unsigned long      ssrc;
      DOMString          mediaType;
      DOMString          trackId;
      DOMString          transportId;
      DOMString          codecId;
      unsigned long      firCount;
      unsigned long      pliCount;
      unsigned long      nackCount;
      unsigned long      sliCount;
      unsigned long long qpSum;
    };

    mediaType of type DOMString
      Either "audio" or "video".

  [webrtc-pc]
  8.6.  Mandatory To Implement Stats
    - RTCRTPStreamStats, with attributes ssrc, mediaType, trackId,
      transportId, codecId, nackCount
 */
function validateRtpStreamStats(statsReport, stats) {
  validateRtcStats(statsReport, stats);

  assert_unsigned_int_field(stats, 'ssrc');
  assert_string_field(stats, 'mediaType');
  assert_enum_field(stats, 'mediaType', ['audio', 'video'])

  validateIdField(statsReport, stats, 'trackId', 'track');
  validateIdField(statsReport, stats, 'transportId', 'transport');
  validateIdField(statsReport, stats, 'codecId', 'codec');

  assert_optional_unsigned_int_field(stats, 'firCount');
  assert_optional_unsigned_int_field(stats, 'pliCount');
  assert_optional_unsigned_int_field(stats, 'nackCount');
  assert_optional_unsigned_int_field(stats, 'sliCount');
  assert_optional_unsigned_int_field(stats, 'qpSum');
}

/*
  [webrtc-stats]
  7.2.  RTCCodecStats dictionary
    dictionary RTCCodecStats : RTCStats {
      unsigned long payloadType;
      RTCCodecType  codecType;
      DOMString     transportId;
      DOMString     mimeType;
      unsigned long clockRate;
      unsigned long channels;
      DOMString     sdpFmtpLine;
      DOMString     implementation;
    };

    enum RTCCodecType {
      "encode",
      "decode",
    };

  [webrtc-pc]
  8.6.  Mandatory To Implement Stats
    - RTCCodecStats, with attributes payloadType, codec, clockRate, channels, sdpFmtpLine
 */

function validateCodecStats(statsReport, stats) {
  validateRtcStats(statsReport, stats);

  assert_unsigned_int_field(stats, 'payloadType');
  assert_optional_enum_field(stats, 'codecType', ['encode', 'decode']);

  validateOptionalIdField(statsReport, stats, 'transportId', 'transport');

  assert_optional_string_field(stats, 'mimeType');
  assert_unsigned_int_field(stats, 'clockRate');
  assert_optional_unsigned_int_field(stats, 'channels');

  assert_optional_string_field(stats, 'sdpFmtpLine');
  assert_optional_string_field(stats, 'implementation');
}

/*
  [webrtc-stats]
  7.3.  RTCReceivedRTPStreamStats dictionary
    dictionary RTCReceivedRTPStreamStats : RTCRTPStreamStats {
        unsigned long      packetsReceived;
        unsigned long long bytesReceived;
        long               packetsLost;
        double             jitter;
        double             fractionLost;
        unsigned long      packetsDiscarded;
        unsigned long      packetsFailedDecryption;
        unsigned long      packetsRepaired;
        unsigned long      burstPacketsLost;
        unsigned long      burstPacketsDiscarded;
        unsigned long      burstLossCount;
        unsigned long      burstDiscardCount;
        double             burstLossRate;
        double             burstDiscardRate;
        double             gapLossRate;
        double             gapDiscardRate;
    };

    [webrtc-pc]
    8.6.  Mandatory To Implement Stats
      - RTCReceivedRTPStreamStats, with all required attributes from its
        inherited dictionaries, and also attributes packetsReceived,
        bytesReceived, packetsLost, jitter, packetsDiscarded
 */
function validateReceivedRtpStreamStats(statsReport, stats) {
  validateRtpStreamStats(statsReport, stats);

  assert_unsigned_int_field(stats, 'packetsReceived');
  assert_unsigned_int_field(stats, 'bytesReceived');
  assert_unsigned_int_field(stats, 'packetsLost');

  assert_number_field(stats, 'jitter');
  assert_optional_number_field(stats, 'fractionLost');

  assert_unsigned_int_field(stats, 'packetsDiscarded');
  assert_optional_unsigned_int_field(stats, 'packetsFailedDecryption');
  assert_optional_unsigned_int_field(stats, 'packetsRepaired');
  assert_optional_unsigned_int_field(stats, 'burstPacketsLost');
  assert_optional_unsigned_int_field(stats, 'burstPacketsDiscarded');
  assert_optional_unsigned_int_field(stats, 'burstLossCount');
  assert_optional_unsigned_int_field(stats, 'burstDiscardCount');

  assert_optional_number_field(stats, 'burstLossRate');
  assert_optional_number_field(stats, 'burstDiscardRate');
  assert_optional_number_field(stats, 'gapLossRate');
  assert_optional_number_field(stats, 'gapDiscardRate');
}

/*
  [webrtc-stats]
  7.4.  RTCInboundRTPStreamStats dictionary
    dictionary RTCInboundRTPStreamStats : RTCReceivedRTPStreamStats {
      DOMString           remoteId;
      unsigned long       framesDecoded;
      DOMHighResTimeStamp lastPacketReceivedTimestamp;
    };

  [webrtc-pc]
  8.6.  Mandatory To Implement Stats
    - RTCInboundRTPStreamStats, with all required attributes from its inherited
      dictionaries, and also attributes remoteId, framesDecoded
 */
function validateInboundRtpStreamStats(statsReport, stats) {
  validateReceivedRtpStreamStats(statsReport, stats);

  validateIdField(statsReport, stats, 'remoteId', 'remote-outbound-rtp');
  assert_unsigned_int_field(stats, 'framesDecoded');
  assert_optional_number_field(stats, 'lastPacketReceivedTimeStamp');
}

/*
  [webrtc-stats]
  7.5.  RTCRemoteInboundRTPStreamStats dictionary
    dictionary RTCRemoteInboundRTPStreamStats : RTCReceivedRTPStreamStats {
        DOMString localId;
        double    roundTripTime;
    };

  [webrtc-pc]
  8.6.  Mandatory To Implement Stats
    - RTCRemoteInboundRTPStreamStats, with all required attributes from its
      inherited dictionaries, and also attributes localId, roundTripTime
 */
function validateRemoteInboundRtpStreamStats(statsReport, stats) {
  validateReceivedRtpStreamStats(statsReport, stats);

  validateIdField(statsReport, stats, 'localId', 'outbound-rtp');
  assert_number_field(stats, 'roundTripTime');
}

/*
  [webrtc-stats]
  7.6.  RTCSentRTPStreamStats dictionary
    dictionary RTCSentRTPStreamStats : RTCRTPStreamStats {
      unsigned long      packetsSent;
      unsigned long      packetsDiscardedOnSend;
      unsigned long long bytesSent;
      unsigned long long bytesDiscardedOnSend;
    };

    [webrtc-pc]
    8.6.  Mandatory To Implement Stats
      - RTCSentRTPStreamStats, with all required attributes from its inherited
        dictionaries, and also attributes packetsSent, bytesSent
 */
function validateSentRtpStreamStats(statsReport, stats) {
  validateRtpStreamStats(statsReport, stats);

  assert_unsigned_int_field(stats, 'packetsSent');
  assert_optional_unsigned_int_field(stats, 'packetsDiscardedOnSend');
  assert_unsigned_int_field(stats, 'bytesSent');
  assert_optional_unsigned_int_field(stats, 'bytesDiscardedOnSend');
}

/*
  [webrtc-stats]
  7.7.  RTCOutboundRTPStreamStats dictionary
    dictionary RTCOutboundRTPStreamStats : RTCSentRTPStreamStats {
      DOMString           remoteId;
      DOMHighResTimeStamp lastPacketSentTimestamp;
      double              targetBitrate;
      unsigned long       framesEncoded;
      double              totalEncodeTime;
      double              averageRTCPInterval;
    };

    [webrtc-pc]
    8.6.  Mandatory To Implement Stats
      - RTCOutboundRTPStreamStats, with all required attributes from its
        inherited dictionaries, and also attributes remoteId, framesEncoded
 */
function validateOutboundRtpStreamStats(statsReport, stats) {
  validateSentRtpStreamStats(statsReport, stats)

  validateIdField(statsReport, stats, 'remoteId', 'remote-inbound-rtp');

  assert_optional_number_field(stats, 'lastPacketSentTimestamp');
  assert_optional_number_field(stats, 'targetBitrate');
  if (stats['mediaType'] == 'video') {
    assert_unsigned_int_field(stats, 'framesEncoded');
  }
  assert_optional_number_field(stats, 'totalEncodeTime');
  assert_optional_number_field(stats, 'averageRTCPInterval');
}

/*
  [webrtc-stats]
  7.8.  RTCRemoteOutboundRTPStreamStats dictionary
    dictionary RTCRemoteOutboundRTPStreamStats : RTCSentRTPStreamStats {
      DOMString           localId;
      DOMHighResTimeStamp remoteTimestamp;
    };

  [webrtc-pc]
  8.6.  Mandatory To Implement Stats
    - RTCRemoteOutboundRTPStreamStats, with all required attributes from its
      inherited dictionaries, and also attributes localId, remoteTimestamp
 */
function validateRemoteOutboundRtpStreamStats(statsReport, stats) {
  validateSentRtpStreamStats(statsReport, stats);

  validateIdField(statsReport, stats, 'localId', 'inbound-rtp');
  assert_number_field(stats, 'remoteTimeStamp');
}

/*
  [webrtc-stats]
  7.9.  RTCRTPContributingSourceStats
    dictionary RTCRTPContributingSourceStats : RTCStats {
      unsigned long contributorSsrc;
      DOMString     inboundRtpStreamId;
      unsigned long packetsContributedTo;
      double        audioLevel;
    };
 */
function validateContributingSourceStats(statsReport, stats) {
  validateRtcStats(statsReport, stats);

  assert_optional_unsigned_int_field(stats, 'contributorSsrc');

  validateOptionalIdField(statsReport, stats, 'inboundRtpStreamId', 'inbound-rtp');
  assert_optional_unsigned_int_field(stats, 'packetsContributedTo');
  assert_optional_number_field(stats, 'audioLevel');
}

/*
  [webrtc-stats]
  7.10. RTCPeerConnectionStats dictionary
    dictionary RTCPeerConnectionStats : RTCStats {
      unsigned long dataChannelsOpened;
      unsigned long dataChannelsClosed;
      unsigned long dataChannelsRequested;
      unsigned long dataChannelsAccepted;
    };

  [webrtc-pc]
  8.6.  Mandatory To Implement Stats
    - RTCPeerConnectionStats, with attributes dataChannelsOpened, dataChannelsClosed
 */
function validatePeerConnectionStats(statsReport, stats) {
  validateRtcStats(statsReport, stats);

  assert_unsigned_int_field(stats, 'dataChannelsOpened');
  assert_unsigned_int_field(stats, 'dataChannelsClosed');
  assert_optional_unsigned_int_field(stats, 'dataChannelsRequested');
  assert_optional_unsigned_int_field(stats, 'dataChannelsAccepted');
}

/*
  [webrtc-stats]
  7.11. RTCMediaStreamStats dictionary
    dictionary RTCMediaStreamStats : RTCStats {
      DOMString           streamIdentifier;
      sequence<DOMString> trackIds;
    };

  [webrtc-pc]
  8.6.  Mandatory To Implement Stats
    - RTCMediaStreamStats, with attributes streamIdentifer, trackIds
 */
function validateMediaStreamStats(statsReport, stats) {
  validateRtcStats(statsReport, stats);

  assert_string_field(stats, 'streamIdentifier');
  assert_array_field(stats, 'trackIds');

  for(const trackId of stats.trackIds) {
    assert_equals(typeof trackId, 'string',
      'Expect trackId elements to be string');

    assert_true(statsReport.has(trackId),
      `Expect stats report to have stats object with id ${trackId}`);

    const trackStats = statsReport.get(trackId);
    assert_equals(trackStats.type, 'track',
      `Expect track stats object to have type 'track'`);
  }
}

/*
  [webrtc-stats]
  7.12. RTCMediaStreamTrackStats dictionary
    dictionary RTCMediaStreamTrackStats : RTCStats {
      DOMString           trackIdentifier;
      boolean             remoteSource;
      boolean             ended;
      boolean             detached;
      DOMString           kind;
      DOMHighResTimeStamp estimatedPlayoutTimestamp;
      unsigned long       frameWidth;
      unsigned long       frameHeight;
      double              framesPerSecond;
      unsigned long       framesCaptured;
      unsigned long       framesSent;
      unsigned long       keyFramesSent;
      unsigned long       framesReceived;
      unsigned long       keyFramesReceived;
      unsigned long       framesDecoded;
      unsigned long       framesDropped;
      unsigned long       framesCorrupted;
      unsigned long       partialFramesLost;
      unsigned long       fullFramesLost;
      double              audioLevel;
      double              totalAudioEnergy;
      boolean             voiceActivityFlag;
      double              echoReturnLoss;
      double              echoReturnLossEnhancement;
      unsigned long long  totalSamplesSent;
      unsigned long long  totalSamplesReceived;
      double              totalSamplesDuration;
      unsigned long long  concealedSamples;
      unsigned long long  concealmentEvents;
      double              jitterBufferDelay;
      RTCPriorityType     priority;
    };

  [webrtc-pc]
  4.9.1.  RTCPriorityType Enum
    enum RTCPriorityType {
      "very-low",
      "low",
      "medium",
      "high"
    };

  8.6.  Mandatory To Implement Stats
    - RTCMediaStreamTrackStats, with attributes trackIdentifier, remoteSource,
      ended, detached, frameWidth, frameHeight, framesPerSecond, framesSent,
      framesReceived, framesDecoded, framesDropped, framesCorrupted, audioLevel
 */

function validateMediaStreamTrackStats(statsReport, stats) {
  validateRtcStats(statsReport, stats);

  assert_string_field(stats, 'trackIdentifier');
  assert_boolean_field(stats, 'remoteSource');
  assert_boolean_field(stats, 'ended');
  assert_boolean_field(stats, 'detached');

  assert_optional_enum_field(stats, 'kind', ['audio', 'video']);
  assert_optional_number_field(stats, 'estimatedPlayoutTimestamp');
  if (stats['kind'] === 'video') {
    assert_unsigned_int_field(stats, 'frameWidth');
    assert_unsigned_int_field(stats, 'frameHeight');
    assert_number_field(stats, 'framesPerSecond');
    if (stats['framesSent']) {
      assert_optional_unsigned_int_field(stats, 'framesCaptured');
      assert_unsigned_int_field(stats, 'framesSent');
      assert_optional_unsigned_int_field(stats, 'keyFramesSent');
    } else {
      assert_unsigned_int_field(stats, 'framesReceived');
      assert_optional_unsigned_int_field(stats, 'keyFramesReceived');
      assert_unsigned_int_field(stats, 'framesDecoded');
      assert_unsigned_int_field(stats, 'framesDropped');
      assert_unsigned_int_field(stats, 'framesCorrupted');
    }

    assert_optional_unsigned_int_field(stats, 'partialFramesLost');
    assert_optional_unsigned_int_field(stats, 'fullFramesLost');
  } else {
    assert_number_field(stats, 'audioLevel');
    assert_optional_number_field(stats, 'totalAudioEnergy');
    assert_optional_boolean_field(stats, 'voiceActivityFlag');
    assert_optional_number_field(stats, 'echoReturnLoss');
    assert_optional_number_field(stats, 'echoReturnLossEnhancement');

    assert_optional_unsigned_int_field(stats, 'totalSamplesSent');
    assert_optional_unsigned_int_field(stats, 'totalSamplesReceived');
    assert_optional_number_field(stats, 'totalSamplesDuration');
    assert_optional_unsigned_int_field(stats, 'concealedSamples');
    assert_optional_unsigned_int_field(stats, 'concealmentEvents');
    assert_optional_number_field(stats, 'jitterBufferDelay');
  }

  assert_optional_enum_field(stats, 'priority',
    ['very-low', 'low', 'medium', 'high']);
}

/*
  [webrtc-stats]
  7.13. RTCDataChannelStats dictionary
    dictionary RTCDataChannelStats : RTCStats {
      DOMString           label;
      DOMString           protocol;
      long                dataChannelIdentifier;
      DOMString           transportId;
      RTCDataChannelState state;
      unsigned long       messagesSent;
      unsigned long long  bytesSent;
      unsigned long       messagesReceived;
      unsigned long long  bytesReceived;
    };

  [webrtc-pc]
  6.2. RTCDataChannel
    enum RTCDataChannelState {
      "connecting",
      "open",
      "closing",
      "closed"
    };

  8.6.  Mandatory To Implement Stats
    - RTCDataChannelStats, with attributes label, protocol, datachannelId, state,
      messagesSent, bytesSent, messagesReceived, bytesReceived
 */
function validateDataChannelStats(statsReport, stats) {
  validateRtcStats(statsReport, stats);

  assert_string_field(stats, 'label');
  assert_string_field(stats, 'protocol');
  assert_int_field(stats, 'dataChannelIdentifier');

  validateOptionalIdField(statsReport, stats, 'transportId', 'transport');

  assert_enum_field(stats, 'state',
    ['connecting', 'open', 'closing', 'closed']);

  assert_unsigned_int_field(stats, 'messagesSent');
  assert_unsigned_int_field(stats, 'bytesSent');
  assert_unsigned_int_field(stats, 'messagesReceived');
  assert_unsigned_int_field(stats, 'bytesReceived');
}

/*
  [webrtc-stats]
  7.14. RTCTransportStats dictionary
    dictionary RTCTransportStats : RTCStats {
      unsigned long         packetsSent;
      unsigned long         packetsReceived;
      unsigned long long    bytesSent;
      unsigned long long    bytesReceived;
      DOMString             rtcpTransportStatsId;
      RTCIceRole            iceRole;
      RTCDtlsTransportState dtlsState;
      DOMString             selectedCandidatePairId;
      DOMString             localCertificateId;
      DOMString             remoteCertificateId;
    };

  [webrtc-pc]
  5.5.  RTCDtlsTransportState Enum
    enum RTCDtlsTransportState {
      "new",
      "connecting",
      "connected",
      "closed",
      "failed"
    };

  5.6.  RTCIceRole Enum
    enum RTCIceRole {
      "controlling",
      "controlled"
    };

  8.6.  Mandatory To Implement Stats
    - RTCTransportStats, with attributes bytesSent, bytesReceived,
      rtcpTransportStatsId, selectedCandidatePairId, localCertificateId,
      remoteCertificateId
 */
function validateTransportStats(statsReport, stats) {
  validateRtcStats(statsReport, stats);

  assert_optional_unsigned_int_field(stats, 'packetsSent');
  assert_optional_unsigned_int_field(stats, 'packetsReceived');
  assert_unsigned_int_field(stats, 'bytesSent');
  assert_unsigned_int_field(stats, 'bytesReceived');

  validateOptionalIdField(statsReport, stats, 'rtcpTransportStatsId',
                          'transport');

  assert_optional_enum_field(stats, 'iceRole',
    ['controlling', 'controlled']);

  assert_optional_enum_field(stats, 'dtlsState',
    ['new', 'connecting', 'connected', 'closed', 'failed']);

  validateIdField(statsReport, stats, 'selectedCandidatePairId', 'candidate-pair');
  validateIdField(statsReport, stats, 'localCertificateId', 'certificate');
  validateIdField(statsReport, stats, 'remoteCertificateId', 'certificate');
}

/*
  [webrtc-stats]
  7.15. RTCIceCandidateStats dictionary
    dictionary RTCIceCandidateStats : RTCStats {
      DOMString           transportId;
      boolean             isRemote;
      RTCNetworkType      networkType;
      DOMString           ip;
      long                port;
      DOMString           protocol;
      RTCIceCandidateType candidateType;
      long                priority;
      DOMString           url;
      DOMString           relayProtocol;
      boolean             deleted = false;
    };

    enum RTCNetworkType {
      "bluetooth",
      "cellular",
      "ethernet",
      "wifi",
      "wimax",
      "vpn",
      "unknown"
    };

  [webrtc-pc]
  4.8.1.3.  RTCIceCandidateType Enum
    enum RTCIceCandidateType {
      "host",
      "srflx",
      "prflx",
      "relay"
    };

  8.6.  Mandatory To Implement Stats
    - RTCIceCandidateStats, with attributes ip, port, protocol, candidateType,
      priority, url
 */
function validateIceCandidateStats(statsReport, stats) {
  validateRtcStats(statsReport, stats);

  validateOptionalIdField(statsReport, stats, 'transportId', 'transport');
  assert_optional_boolean_field(stats, 'isRemote');

  assert_optional_enum_field(stats, 'networkType',
    ['bluetooth', 'cellular', 'ethernet', 'wifi', 'wimax', 'vpn', 'unknown'])

  assert_string_field(stats, 'ip');
  assert_int_field(stats, 'port');
  assert_string_field(stats, 'protocol');

  assert_enum_field(stats, 'candidateType',
    ['host', 'srflx', 'prflx', 'relay']);

  assert_int_field(stats, 'priority');
  assert_optional_string_field(stats, 'url');
  assert_optional_string_field(stats, 'relayProtocol');
  assert_optional_boolean_field(stats, 'deleted');
}

/*
  [webrtc-stats]
  7.16. RTCIceCandidatePairStats dictionary
    dictionary RTCIceCandidatePairStats : RTCStats {
      DOMString                     transportId;
      DOMString                     localCandidateId;
      DOMString                     remoteCandidateId;
      RTCStatsIceCandidatePairState state;
      unsigned long long            priority;
      boolean                       nominated;
      unsigned long                 packetsSent;
      unsigned long                 packetsReceived;
      unsigned long long            bytesSent;
      unsigned long long            bytesReceived;
      DOMHighResTimeStamp           lastPacketSentTimestamp;
      DOMHighResTimeStamp           lastPacketReceivedTimestamp;
      DOMHighResTimeStamp           firstRequestTimestamp;
      DOMHighResTimeStamp           lastRequestTimestamp;
      DOMHighResTimeStamp           lastResponseTimestamp;
      double                        totalRoundTripTime;
      double                        currentRoundTripTime;
      double                        availableOutgoingBitrate;
      double                        availableIncomingBitrate;
      unsigned long                 circuitBreakerTriggerCount;
      unsigned long long            requestsReceived;
      unsigned long long            requestsSent;
      unsigned long long            responsesReceived;
      unsigned long long            responsesSent;
      unsigned long long            retransmissionsReceived;
      unsigned long long            retransmissionsSent;
      unsigned long long            consentRequestsSent;
      DOMHighResTimeStamp           consentExpiredTimestamp;
    };

    enum RTCStatsIceCandidatePairState {
      "frozen",
      "waiting",
      "in-progress",
      "failed",
      "succeeded"
    };

  [webrtc-pc]
  8.6.  Mandatory To Implement Stats
    - RTCIceCandidatePairStats, with attributes transportId, localCandidateId,
      remoteCandidateId, state, priority, nominated, bytesSent, bytesReceived, totalRoundTripTime, currentRoundTripTime
 */
function validateIceCandidatePairStats(statsReport, stats) {
  validateRtcStats(statsReport, stats);

  validateIdField(statsReport, stats, 'transportId', 'transport');
  validateIdField(statsReport, stats, 'localCandidateId', 'local-candidate');
  validateIdField(statsReport, stats, 'remoteCandidateId', 'remote-candidate');

  assert_enum_field(stats, 'state',
    ['frozen', 'waiting', 'in-progress', 'failed', 'succeeded']);

  assert_unsigned_int_field(stats, 'priority');
  assert_boolean_field(stats, 'nominated');
  assert_optional_unsigned_int_field(stats, 'packetsSent');
  assert_optional_unsigned_int_field(stats, 'packetsReceived');
  assert_unsigned_int_field(stats, 'bytesSent');
  assert_unsigned_int_field(stats, 'bytesReceived');

  assert_optional_number_field(stats, 'lastPacketSentTimestamp');
  assert_optional_number_field(stats, 'lastPacketReceivedTimestamp');
  assert_optional_number_field(stats, 'firstRequestTimestamp');
  assert_optional_number_field(stats, 'lastRequestTimestamp');
  assert_optional_number_field(stats, 'lastResponseTimestamp');

  assert_number_field(stats, 'totalRoundTripTime');
  assert_number_field(stats, 'currentRoundTripTime');

  assert_optional_number_field(stats, 'availableOutgoingBitrate');
  assert_optional_number_field(stats, 'availableIncomingBitrate');

  assert_optional_unsigned_int_field(stats, 'circuitBreakerTriggerCount');
  assert_optional_unsigned_int_field(stats, 'requestsReceived');
  assert_optional_unsigned_int_field(stats, 'requestsSent');
  assert_optional_unsigned_int_field(stats, 'responsesReceived');
  assert_optional_unsigned_int_field(stats, 'responsesSent');
  assert_optional_unsigned_int_field(stats, 'retransmissionsReceived');
  assert_optional_unsigned_int_field(stats, 'retransmissionsSent');
  assert_optional_unsigned_int_field(stats, 'consentRequestsSent');
  assert_optional_number_field(stats, 'consentExpiredTimestamp');
}

/*
  [webrtc-stats]
  7.17. RTCCertificateStats dictionary
    dictionary RTCCertificateStats : RTCStats {
      DOMString fingerprint;
      DOMString fingerprintAlgorithm;
      DOMString base64Certificate;
      DOMString issuerCertificateId;
    };

  [webrtc-pc]
  8.6.  Mandatory To Implement Stats
    - RTCCertificateStats, with attributes fingerprint, fingerprintAlgorithm,
      base64Certificate, issuerCertificateId
 */
function validateCertificateStats(statsReport, stats) {
  validateRtcStats(statsReport, stats);

  assert_string_field(stats, 'fingerprint');
  assert_string_field(stats, 'fingerprintAlgorithm');
  assert_string_field(stats, 'base64Certificate');
  assert_optional_string_field(stats, 'issuerCertificateId');
}