github.com/pion/webrtc/v4@v4.0.1/stats_go.go (about)

     1  // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
     2  // SPDX-License-Identifier: MIT
     3  
     4  //go:build !js
     5  // +build !js
     6  
     7  package webrtc
     8  
     9  // GetConnectionStats is a helper method to return the associated stats for a given PeerConnection
    10  func (r StatsReport) GetConnectionStats(conn *PeerConnection) (PeerConnectionStats, bool) {
    11  	statsID := conn.getStatsID()
    12  	stats, ok := r[statsID]
    13  	if !ok {
    14  		return PeerConnectionStats{}, false
    15  	}
    16  
    17  	pcStats, ok := stats.(PeerConnectionStats)
    18  	if !ok {
    19  		return PeerConnectionStats{}, false
    20  	}
    21  	return pcStats, true
    22  }
    23  
    24  // GetDataChannelStats is a helper method to return the associated stats for a given DataChannel
    25  func (r StatsReport) GetDataChannelStats(dc *DataChannel) (DataChannelStats, bool) {
    26  	statsID := dc.getStatsID()
    27  	stats, ok := r[statsID]
    28  	if !ok {
    29  		return DataChannelStats{}, false
    30  	}
    31  
    32  	dcStats, ok := stats.(DataChannelStats)
    33  	if !ok {
    34  		return DataChannelStats{}, false
    35  	}
    36  	return dcStats, true
    37  }
    38  
    39  // GetICECandidateStats is a helper method to return the associated stats for a given ICECandidate
    40  func (r StatsReport) GetICECandidateStats(c *ICECandidate) (ICECandidateStats, bool) {
    41  	statsID := c.statsID
    42  	stats, ok := r[statsID]
    43  	if !ok {
    44  		return ICECandidateStats{}, false
    45  	}
    46  
    47  	candidateStats, ok := stats.(ICECandidateStats)
    48  	if !ok {
    49  		return ICECandidateStats{}, false
    50  	}
    51  	return candidateStats, true
    52  }
    53  
    54  // GetICECandidatePairStats is a helper method to return the associated stats for a given ICECandidatePair
    55  func (r StatsReport) GetICECandidatePairStats(c *ICECandidatePair) (ICECandidatePairStats, bool) {
    56  	statsID := c.statsID
    57  	stats, ok := r[statsID]
    58  	if !ok {
    59  		return ICECandidatePairStats{}, false
    60  	}
    61  
    62  	candidateStats, ok := stats.(ICECandidatePairStats)
    63  	if !ok {
    64  		return ICECandidatePairStats{}, false
    65  	}
    66  	return candidateStats, true
    67  }
    68  
    69  // GetCertificateStats is a helper method to return the associated stats for a given Certificate
    70  func (r StatsReport) GetCertificateStats(c *Certificate) (CertificateStats, bool) {
    71  	statsID := c.statsID
    72  	stats, ok := r[statsID]
    73  	if !ok {
    74  		return CertificateStats{}, false
    75  	}
    76  
    77  	certificateStats, ok := stats.(CertificateStats)
    78  	if !ok {
    79  		return CertificateStats{}, false
    80  	}
    81  	return certificateStats, true
    82  }
    83  
    84  // GetCodecStats is a helper method to return the associated stats for a given Codec
    85  func (r StatsReport) GetCodecStats(c *RTPCodecParameters) (CodecStats, bool) {
    86  	statsID := c.statsID
    87  	stats, ok := r[statsID]
    88  	if !ok {
    89  		return CodecStats{}, false
    90  	}
    91  
    92  	codecStats, ok := stats.(CodecStats)
    93  	if !ok {
    94  		return CodecStats{}, false
    95  	}
    96  	return codecStats, true
    97  }