github.com/zartbot/goflow@v0.0.0-20221001080520-456f7dfad288/service/ciscoavc/rtp.go (about)

     1  package ciscoavc
     2  
     3  import (
     4  	"github.com/zartbot/go_utils/map2value"
     5  )
     6  
     7  /*
     8  MOS	Quality	Impairment
     9  5	Excellent	Imperceptible
    10  4	Good	Perceptible but not annoying
    11  3	Fair	Slightly annoying
    12  2	Poor	Annoying
    13  1	Bad    	>Very annoying
    14  
    15  MOS Calculation
    16  
    17    effective_latency = latency +2*jitter + 10.0
    18  
    19  For effective_latency < 160.0 ms:
    20    R = 93.2 - (effective_latency)/40.0
    21  
    22  For effective_latency >= 160.0 ms:
    23  
    24    R = 93.2 - (effective_latency - 120.0)/10.0
    25  
    26  If the effective latency is less than 160.0 ms the overall impact to the voice quality is moderate. For larger values, the voice quality drops more significantly, which is why R is penalized more.
    27  The packet loss (in percentage points) is taken into consideration as follows:
    28  
    29    R = R - 2.5 * packet_loss
    30  
    31  For R < 0:
    32    MOS = 1.0
    33  
    34  For 0 < R < 100.0:
    35    MOS = 1 + 0.035*R + 0.000007*R*(R-60)*(100-R)
    36  
    37  For R >= 100.0:
    38    MOS = 4.5
    39  */
    40  
    41  type RTPStats struct {
    42  	AvgJitter float64
    43  	Loss      float64
    44  	MOS       float64
    45  	Score     float64
    46  }
    47  
    48  func MOS(delay float64, jitter float64, loss float64) (float64, float64) {
    49  	var R, Score, MOS float64
    50  	effectiveLatency := delay + 2*jitter + 10
    51  	if effectiveLatency < 160 {
    52  		R = 93.2 - effectiveLatency/40.0
    53  	} else {
    54  		R = 93.2 - (effectiveLatency-120.0)/10
    55  	}
    56  	R = R - 2.5*loss
    57  	if R < 0 {
    58  		MOS = 1.0
    59  		Score = 0
    60  	} else if R < 100 {
    61  		MOS = 1 + 0.035*R + 0.000007*R*(R-60)*(100-R)
    62  		Score = R
    63  	} else {
    64  		MOS = 4.5
    65  		Score = 100
    66  	}
    67  	return Score * Score * Score / 10000, MOS
    68  }
    69  
    70  func RTPRecordExtend(d map[string]interface{}) *RTPStats {
    71  	r := new(RTPStats)
    72  	_, ErrRTPPayloadType := map2value.MapToUInt8(d, "trans_rtp_payload_type")
    73  	if ErrRTPPayloadType == nil {
    74  		AvgRTPJitter, ErrAvgRTPJitter := map2value.MapToUInt32(d, "trans_rtp_jitter_mean")
    75  		if ErrAvgRTPJitter == nil {
    76  			if AvgRTPJitter > 4294967290 {
    77  				AvgRTPJitter = 0
    78  				d["trans_rtp_jitter_mean"] = 0
    79  			}
    80  		}
    81  		MaxRTPJitter, ErrMaxRTPJitter := map2value.MapToUInt32(d, "trans_rtp_jitter_maximum")
    82  		if ErrMaxRTPJitter == nil {
    83  			if MaxRTPJitter > 4294967290 {
    84  				MaxRTPJitter = 0
    85  				d["trans_rtp_jitter_maximum"] = 0
    86  			}
    87  		}
    88  
    89  		MinRTPJitter, ErrMinRTPJitter := map2value.MapToUInt32(d, "trans_rtp_jitter_minimum")
    90  		if ErrMinRTPJitter == nil {
    91  			if MinRTPJitter > 4294967290 {
    92  				MinRTPJitter = 0
    93  				d["trans_rtp_jitter_minimum"] = 0
    94  			}
    95  		}
    96  
    97  		SumRTPJitter, ErrSumRTPJitter := map2value.MapToUInt64(d, "trans_rtp_jitter_mean_sum")
    98  		if ErrSumRTPJitter == nil {
    99  			if SumRTPJitter > 4294967290 {
   100  				SumRTPJitter = 0
   101  				d["trans_rtp_jitter_mean_sum"] = 0
   102  			}
   103  		}
   104  		var pktLostCnt uint64
   105  		if _v, ok := d["trans_pkts_lost_cnt_long"]; ok {
   106  			if v, valid := _v.(uint32); valid {
   107  				if v > 4294967290 {
   108  					pktLostCnt = uint64(0)
   109  				} else {
   110  					pktLostCnt = uint64(v)
   111  				}
   112  			} else if v, valid := _v.(uint64); valid {
   113  				if v > 4294967290 {
   114  					pktLostCnt = 0
   115  				}
   116  			}
   117  		}
   118  		d["trans_pkts_lost_cnt_long"] = pktLostCnt
   119  		d["total_pkts_loss_sum"] = pktLostCnt
   120  		Totalpkts, ErrTotalPkts := map2value.MapToUInt64(d, "total_pkts")
   121  		if ErrTotalPkts == nil {
   122  			r.Loss = float64(pktLostCnt) / float64(Totalpkts)
   123  			d["rtp_loss_rate"] = float32(r.Loss)
   124  			r.AvgJitter = float64(SumRTPJitter) / float64(Totalpkts)
   125  			d["rtp_jitter_mean"] = r.AvgJitter
   126  			//hardcode for network latency to 20ms
   127  			//since currently ART record does not contain delay
   128  			r.Score, r.MOS = MOS(20, r.AvgJitter, r.Loss)
   129  			d["performance_score"] = r.Score
   130  			d["rtp_mos"] = r.MOS
   131  		}
   132  		FixRTPRecordDirection(d)
   133  	}
   134  	return r
   135  }