github.com/randomizedcoder/goTrackRTP@v0.0.2/cmd/goTrackRTPer/goTrackRTPer.go (about) 1 package main 2 3 import ( 4 "context" 5 "flag" 6 "fmt" 7 "log" 8 "math" 9 "os" 10 "os/signal" 11 "syscall" 12 13 "github.com/randomizedcoder/goTrackRTP" 14 15 _ "unsafe" 16 ) 17 18 const ( 19 loopsCst = math.MaxInt32 20 randnCst = 10 21 22 debugLevelCst = 11 23 24 signalChannelSize = 10 25 26 awCst = 100 27 bwCst = 100 28 abCst = 100 29 bbCst = 100 30 31 // maxRandJumpCst = 10 32 ) 33 34 var ( 35 // Passed by "go build -ldflags" for the show version 36 commit string 37 date string 38 ) 39 40 func main() { 41 42 log.Println("goTrackingRTPer") 43 44 _, cancel := context.WithCancel(context.Background()) 45 defer cancel() 46 47 go initSignalHandler(cancel) 48 49 loops := flag.Int("loops", loopsCst, "loops") 50 randn := flag.Int("randn", randnCst, "randn") 51 52 version := flag.Bool("version", false, "version") 53 54 aw := flag.Int("aw", awCst, "ahead window") 55 bw := flag.Int("bw", bwCst, "behind window") 56 ab := flag.Int("ab", abCst, "ahead buffer") 57 bb := flag.Int("bb", bbCst, "behind buffer") 58 59 dl := flag.Int("dl", debugLevelCst, "nasty debugLevel") 60 61 flag.Parse() 62 63 if *version { 64 fmt.Println("commit:", commit, "\tdate(UTC):", date) 65 os.Exit(0) 66 } 67 68 tr, err := goTrackRTP.New(uint16(*aw), uint16(*bw), uint16(*ab), uint16(*bb), *dl) 69 if err != nil { 70 log.Fatal("goTrackRTP.New:", err) 71 } 72 73 fmt.Println("loops:", loops) 74 75 var s uint16 76 var seq uint16 77 for i := 0; i < *loops; i++ { 78 79 r := uint16(FastRandN(uint32(*randn)) + 1) // FastRandN can return zero (0) 80 p := FastRandN(2) 81 log.Printf("i:%d, s:%d, p:%d", i, s, p) 82 if p == 1 { 83 log.Printf("i:%d, s:%d, seq:%d", i, s, seq) 84 seq = s - r 85 } else { 86 seq = s + r 87 } 88 89 _, err := tr.PacketArrival(seq) 90 if err != nil { 91 log.Fatal("PacketArrival:", err) 92 } 93 94 s++ 95 } 96 97 log.Println("go_generate_rtp: That's all Folks!") 98 } 99 100 // initSignalHandler sets up signal handling for the process, and 101 // will call cancel() when recieved 102 func initSignalHandler(cancel context.CancelFunc) { 103 c := make(chan os.Signal, signalChannelSize) 104 signal.Notify(c, os.Interrupt, syscall.SIGTERM) 105 106 <-c 107 log.Printf("Signal caught, closing application") 108 cancel() 109 os.Exit(0) 110 } 111 112 //go:linkname FastRand runtime.fastrand 113 func FastRand() uint32 114 115 // https://cs.opensource.google/go/go/+/master:src/runtime/stubs.go;l=151?q=FastRandN&ss=go%2Fgo 116 // https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/ 117 118 //go:linkname FastRandN runtime.fastrandn 119 func FastRandN(n uint32) uint32