From f8aa535ec7b322359d9642bd4b368fa690189c5e Mon Sep 17 00:00:00 2001 From: Saxon1 Date: Mon, 4 Dec 2017 17:07:39 +1030 Subject: [PATCH 1/9] started writing BitrateCalculatorStruct --- bitrate/BitrateCalculator.go | 37 ++++++++++++++++++++++++++++++++++++ revid/revid.go | 16 ++++------------ 2 files changed, 41 insertions(+), 12 deletions(-) create mode 100644 bitrate/BitrateCalculator.go diff --git a/bitrate/BitrateCalculator.go b/bitrate/BitrateCalculator.go new file mode 100644 index 00000000..00c45231 --- /dev/null +++ b/bitrate/BitrateCalculator.go @@ -0,0 +1,37 @@ +package bitrate + +import ( + "time" + "fmt" +) + +type BitrateCalculator struct { + Delay int + now + prev + isFirstTime bool + elapsedTime time +} + +func (bc *BitrateCalculator) Start() { + if isFirstTime { + if Delay == nil { + Delay = 5 * time.Second + } + now = time.Now() + prev = now + isFirstTime = false + elapsedTime = time.Duration(0) + } + now := time.Now() +} + +func (bc *BitrateCalculator) End(noOfKB int, printOption bool) (bitrate int) { + deltaTime := now.Sub(prevTime) + elapsedTime += deltaTime + if elapsedTime > bitrateOutputDelay*time.Second { + fmt.Printf("Bitrate: %d kbps\n", int64(noOfKB/float64(deltaTime/1e9))) + elapsedTime = time.Duration(0) + } + prevTime = now +} diff --git a/revid/revid.go b/revid/revid.go index 8f5efeb4..d185ef62 100644 --- a/revid/revid.go +++ b/revid/revid.go @@ -298,25 +298,17 @@ func input(input string, output string) { // output handles the writing to specified output func output(output string) { - elapsedTime := time.Duration(0) - now := time.Now() - prevTime := now + bitrateCalc := BitrateCalculator{60} for { if clip, err := ringBuffer.Read(); err == nil { - now := time.Now() + bitrateCalc.Start() err := sendClip(clip, output, conn) for err != nil { outputErrChan <- err err = sendClip(clip, output, conn) } - deltaTime := now.Sub(prevTime) - elapsedTime += deltaTime - if elapsedTime > bitrateOutputDelay*time.Second { - noOfBits := float64(len(clip)*8) / 1024.0 // convert bytes to kilobits - fmt.Printf("Bitrate: %d kbps\n", int64(noOfBits/float64(deltaTime/1e9))) - elapsedTime = time.Duration(0) - } - prevTime = now + noOfKB = float64(len(clip)*8) / 1024.0 // Convert bytes to kilobits + bitrateCalc.End(, true) if err := ringBuffer.DoneReading(); err != nil { outputErrChan <- err } From 429ee0b5b1739a312a2b620503a9eadfbe1c1800 Mon Sep 17 00:00:00 2001 From: Saxon1 Date: Tue, 5 Dec 2017 10:07:28 +1030 Subject: [PATCH 2/9] just testing --- bitrate/BitrateCalculator.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bitrate/BitrateCalculator.go b/bitrate/BitrateCalculator.go index 00c45231..6fb9d74c 100644 --- a/bitrate/BitrateCalculator.go +++ b/bitrate/BitrateCalculator.go @@ -10,9 +10,10 @@ type BitrateCalculator struct { now prev isFirstTime bool - elapsedTime time + elapsedTime time } +// The bitrate calculat func (bc *BitrateCalculator) Start() { if isFirstTime { if Delay == nil { From 4561378a78914c5feabe1914d36ce66ad149e677 Mon Sep 17 00:00:00 2001 From: Saxon Milton Date: Tue, 5 Dec 2017 20:45:36 +1030 Subject: [PATCH 3/9] Heading to bed soon --- bitrate/BitrateCalculator.go | 2 +- bitrate/BitrateCalculator_test.go | 0 revid/revid.go | 16 ++++++++++++---- 3 files changed, 13 insertions(+), 5 deletions(-) create mode 100644 bitrate/BitrateCalculator_test.go diff --git a/bitrate/BitrateCalculator.go b/bitrate/BitrateCalculator.go index 6fb9d74c..320354bc 100644 --- a/bitrate/BitrateCalculator.go +++ b/bitrate/BitrateCalculator.go @@ -27,7 +27,7 @@ func (bc *BitrateCalculator) Start() { now := time.Now() } -func (bc *BitrateCalculator) End(noOfKB int, printOption bool) (bitrate int) { +func (bc *BitrateCalculator) Stop(noOfKB int, printOption bool) (bitrate int) { deltaTime := now.Sub(prevTime) elapsedTime += deltaTime if elapsedTime > bitrateOutputDelay*time.Second { diff --git a/bitrate/BitrateCalculator_test.go b/bitrate/BitrateCalculator_test.go new file mode 100644 index 00000000..e69de29b diff --git a/revid/revid.go b/revid/revid.go index d185ef62..8f5efeb4 100644 --- a/revid/revid.go +++ b/revid/revid.go @@ -298,17 +298,25 @@ func input(input string, output string) { // output handles the writing to specified output func output(output string) { - bitrateCalc := BitrateCalculator{60} + elapsedTime := time.Duration(0) + now := time.Now() + prevTime := now for { if clip, err := ringBuffer.Read(); err == nil { - bitrateCalc.Start() + now := time.Now() err := sendClip(clip, output, conn) for err != nil { outputErrChan <- err err = sendClip(clip, output, conn) } - noOfKB = float64(len(clip)*8) / 1024.0 // Convert bytes to kilobits - bitrateCalc.End(, true) + deltaTime := now.Sub(prevTime) + elapsedTime += deltaTime + if elapsedTime > bitrateOutputDelay*time.Second { + noOfBits := float64(len(clip)*8) / 1024.0 // convert bytes to kilobits + fmt.Printf("Bitrate: %d kbps\n", int64(noOfBits/float64(deltaTime/1e9))) + elapsedTime = time.Duration(0) + } + prevTime = now if err := ringBuffer.DoneReading(); err != nil { outputErrChan <- err } From ca48e4a9d454ee551781cee1b0c00f801174de27 Mon Sep 17 00:00:00 2001 From: Saxon Milton Date: Tue, 5 Dec 2017 20:46:26 +1030 Subject: [PATCH 4/9] Heading top bed soon --- bitrate/BitrateCalculator.go | 7 +++++-- bitrate/BitrateCalculator_test.go | 5 +++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/bitrate/BitrateCalculator.go b/bitrate/BitrateCalculator.go index 320354bc..9f5b86aa 100644 --- a/bitrate/BitrateCalculator.go +++ b/bitrate/BitrateCalculator.go @@ -13,7 +13,7 @@ type BitrateCalculator struct { elapsedTime time } -// The bitrate calculat +// The bitrate calculator func (bc *BitrateCalculator) Start() { if isFirstTime { if Delay == nil { @@ -31,7 +31,10 @@ func (bc *BitrateCalculator) Stop(noOfKB int, printOption bool) (bitrate int) { deltaTime := now.Sub(prevTime) elapsedTime += deltaTime if elapsedTime > bitrateOutputDelay*time.Second { - fmt.Printf("Bitrate: %d kbps\n", int64(noOfKB/float64(deltaTime/1e9))) + bitrate = int64(noOfKB/float64(deltaTime/1e9)) + if printOption { + fmt.Printf("Bitrate: %d kbps\n", bitrate) + } elapsedTime = time.Duration(0) } prevTime = now diff --git a/bitrate/BitrateCalculator_test.go b/bitrate/BitrateCalculator_test.go index e69de29b..617d30b5 100644 --- a/bitrate/BitrateCalculator_test.go +++ b/bitrate/BitrateCalculator_test.go @@ -0,0 +1,5 @@ +package bitrate + +import ( + "testing" +) From f013a3e1f33bd9eb82cbebdd4868b5d7739c6fbe Mon Sep 17 00:00:00 2001 From: Saxon Milton Date: Wed, 6 Dec 2017 08:54:20 +1030 Subject: [PATCH 5/9] Think I've finished the actual code, just need to write some testing utilities --- bitrate/BitrateCalculator.go | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/bitrate/BitrateCalculator.go b/bitrate/BitrateCalculator.go index 9f5b86aa..fea3bc1f 100644 --- a/bitrate/BitrateCalculator.go +++ b/bitrate/BitrateCalculator.go @@ -6,36 +6,33 @@ import ( ) type BitrateCalculator struct { - Delay int + Delay int // sec now prev isFirstTime bool - elapsedTime time + elapsedTime time.Time } // The bitrate calculator func (bc *BitrateCalculator) Start() { - if isFirstTime { + if bc.isFirstTime { if Delay == nil { - Delay = 5 * time.Second + bc.Delay = 5 * time.Second } - now = time.Now() - prev = now - isFirstTime = false - elapsedTime = time.Duration(0) + bc.now = time.Now() + bc.prev = now + bc.isFirstTime = false + bc.elapsedTime = time.Duration(0) } - now := time.Now() + bc.now = time.Now() } -func (bc *BitrateCalculator) Stop(noOfKB int, printOption bool) (bitrate int) { +func (bc *BitrateCalculator) Stop(noOfKB int) (bitrate int) { deltaTime := now.Sub(prevTime) - elapsedTime += deltaTime - if elapsedTime > bitrateOutputDelay*time.Second { - bitrate = int64(noOfKB/float64(deltaTime/1e9)) - if printOption { - fmt.Printf("Bitrate: %d kbps\n", bitrate) - } - elapsedTime = time.Duration(0) + bc.elapsedTime += deltaTime + if bc.elapsedTime > bc.Delay*time.Second { + fmt.Printf("Bitrate: %d kbps\n", int64(noOfKB/float64(deltaTime/1e9))) + bc.elapsedTime = time.Duration(0) } - prevTime = now + bc.prev = now } From 742f92b56b244df903819a4255620026feb2f5e4 Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 6 Dec 2017 12:53:22 +1030 Subject: [PATCH 6/9] testing file in progress --- bitrate/BitrateCalculator.go | 87 +++++++++++++++++++++---------- bitrate/BitrateCalculator_test.go | 69 +++++++++++++++++++++++- 2 files changed, 128 insertions(+), 28 deletions(-) diff --git a/bitrate/BitrateCalculator.go b/bitrate/BitrateCalculator.go index fea3bc1f..2f6fc528 100644 --- a/bitrate/BitrateCalculator.go +++ b/bitrate/BitrateCalculator.go @@ -1,38 +1,71 @@ +/* +NAME + revid - a testbed for re-muxing and re-directing video streams as MPEG-TS over various protocols. + +DESCRIPTION + See Readme.md + +AUTHOR + Alan Noble + +LICENSE + revid is Copyright (C) 2017 Alan Noble. + + It is free software: you can redistribute it and/or modify them + under the terms of the GNU General Public License as published by the + Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + It is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with revid in gpl.txt. If not, see [GNU licenses](http://www.gnu.org/licenses). +*/ + package bitrate import ( - "time" - "fmt" + "fmt" + "time" ) +// bitrateCalculator implements interface BitrateCalculator type BitrateCalculator struct { - Delay int // sec - now - prev - isFirstTime bool - elapsedTime time.Time + outputDelay int // sec + now time.Time + prev time.Time + startedBefore bool + elapsedTime time.Duration + lastDisplayTime time.Time } -// The bitrate calculator -func (bc *BitrateCalculator) Start() { - if bc.isFirstTime { - if Delay == nil { - bc.Delay = 5 * time.Second - } - bc.now = time.Now() - bc.prev = now - bc.isFirstTime = false - bc.elapsedTime = time.Duration(0) +// Place this at the start of the code segment that you would like to time +func (bc *BitrateCalculator) Start(outputDelay int) { + if outputDelay >= 0{ + bc.outputDelay = outputDelay + } else { + bc.outputDelay = 0 } + bc.prev = time.Now() + if !bc.startedBefore { + bc.startedBefore = true + bc.elapsedTime = time.Duration(0) + bc.lastDisplayTime = time.Now() + } +} + +// Place this at the end of the code segment that you would like to time +func (bc *BitrateCalculator) Stop(noOfKB float64) (bitrate int64) { bc.now = time.Now() -} - -func (bc *BitrateCalculator) Stop(noOfKB int) (bitrate int) { - deltaTime := now.Sub(prevTime) - bc.elapsedTime += deltaTime - if bc.elapsedTime > bc.Delay*time.Second { - fmt.Printf("Bitrate: %d kbps\n", int64(noOfKB/float64(deltaTime/1e9))) - bc.elapsedTime = time.Duration(0) - } - bc.prev = now + deltaTime := bc.now.Sub(bc.prev) + if bc.now.Sub(bc.lastDisplayTime) > time.Duration(bc.outputDelay)*time.Second { + bitrate = int64(noOfKB / float64(deltaTime/1e9)) + fmt.Printf("Bitrate: %d kbps\n", bitrate) + bc.elapsedTime = time.Duration(0) + bc.lastDisplayTime = bc.now + } + return } diff --git a/bitrate/BitrateCalculator_test.go b/bitrate/BitrateCalculator_test.go index 617d30b5..506e423e 100644 --- a/bitrate/BitrateCalculator_test.go +++ b/bitrate/BitrateCalculator_test.go @@ -1,5 +1,72 @@ +/* +NAME + revid - a testbed for re-muxing and re-directing video streams as MPEG-TS over various protocols. + +DESCRIPTION + See Readme.md + +AUTHOR + Alan Noble + +LICENSE + revid is Copyright (C) 2017 Alan Noble. + + It is free software: you can redistribute it and/or modify them + under the terms of the GNU General Public License as published by the + Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + It is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with revid in gpl.txt. If not, see [GNU licenses](http://www.gnu.org/licenses). +*/ + package bitrate import ( - "testing" + "testing" + "time" + "fmt" ) + +// Some consts used over duration of testing +const ( + bitrateDelay1 = 0 // s + bitrateDelay2 = 5 // s + amountOfData = 100000.0 + testTime = 2500.0 // ms +) + +// This will be the BitrateCalculator object we use over testing +var bitrateCalc BitrateCalculator + +// Simple test to check that the calculator can calc bitrate over a given +// duration of time +func Test1(t *testing.T) { + bitrateCalc = BitrateCalculator{} + bitrateCalc.Start(bitrateDelay1) + time.Sleep(testTime * time.Millisecond) + currentBitrate := int64(bitrateCalc.Stop(amountOfData)) + actualBitrate := int64(amountOfData / ((testTime * time.Millisecond)/1e9)) + if currentBitrate != actualBitrate { + t.Errorf("Bitrate is wrong! Calculated: %v Actual %v", currentBitrate, actualBitrate) + } +} + +// Now let's check that the output delay feature works +func Test2(t *testing.T){ + bitrateCalc = BitrateCalculator{} + for i := 0; i < 2; i++ { + bitrateCalc.Start(bitrate2) + time.Sleep(testTime*time.Millisecond) + currentBitrate := int64(bitrateCalc.Stop(amountOfData)) + actualBitrate := int64(amountOfData / ((testTime * time.Millisecond)/1e9)) + if i == 0 && currentBitrate != 0 { + t.Errorf("Bitrate is wrong! Calculated: %v Actual %v", currentBitrate, actualBitrate) + } + } +} From 94f8ffb36192dd07c9f7b73bc4f93fa68cad1701 Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 6 Dec 2017 13:37:52 +1030 Subject: [PATCH 7/9] Test file is complete and appears to chose that the library works. --- bitrate/BitrateCalculator_test.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/bitrate/BitrateCalculator_test.go b/bitrate/BitrateCalculator_test.go index 506e423e..e65e6eff 100644 --- a/bitrate/BitrateCalculator_test.go +++ b/bitrate/BitrateCalculator_test.go @@ -30,7 +30,6 @@ package bitrate import ( "testing" "time" - "fmt" ) // Some consts used over duration of testing @@ -60,13 +59,18 @@ func Test1(t *testing.T) { // Now let's check that the output delay feature works func Test2(t *testing.T){ bitrateCalc = BitrateCalculator{} + var currentBitrate int64 for i := 0; i < 2; i++ { - bitrateCalc.Start(bitrate2) + bitrateCalc.Start(bitrateDelay2) time.Sleep(testTime*time.Millisecond) - currentBitrate := int64(bitrateCalc.Stop(amountOfData)) - actualBitrate := int64(amountOfData / ((testTime * time.Millisecond)/1e9)) + currentBitrate = int64(bitrateCalc.Stop(amountOfData)) if i == 0 && currentBitrate != 0 { - t.Errorf("Bitrate is wrong! Calculated: %v Actual %v", currentBitrate, actualBitrate) + t.Errorf("The bitrate calc did not delay outputting!") } + time.Sleep(6000*time.Millisecond) } + actualBitrate := int64(amountOfData / ((testTime * time.Millisecond)/1e9)) + if currentBitrate != actualBitrate { + t.Errorf("Bitrate is wrong! Calculated: %v Actual %v", currentBitrate, actualBitrate) + } } From 93ad5ed2479d7e52c60dfc725688978e95668455 Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 6 Dec 2017 13:41:32 +1030 Subject: [PATCH 8/9] Fixed up comments --- bitrate/BitrateCalculator.go | 37 ++++++++++++++++--------------- bitrate/BitrateCalculator_test.go | 35 +++++++++++++++-------------- 2 files changed, 37 insertions(+), 35 deletions(-) diff --git a/bitrate/BitrateCalculator.go b/bitrate/BitrateCalculator.go index 2f6fc528..752905d6 100644 --- a/bitrate/BitrateCalculator.go +++ b/bitrate/BitrateCalculator.go @@ -1,15 +1,16 @@ /* NAME - revid - a testbed for re-muxing and re-directing video streams as MPEG-TS over various protocols. + BitrateCalculator.go - is a simple struct with methods to allow for easy + calculation of bitrate. DESCRIPTION See Readme.md AUTHOR - Alan Noble + Saxon Nelson-Milton LICENSE - revid is Copyright (C) 2017 Alan Noble. + BitrateCalculator.go is Copyright (C) 2017 the Australian Ocean Lab (AusOcean) It is free software: you can redistribute it and/or modify them under the terms of the GNU General Public License as published by the @@ -34,38 +35,38 @@ import ( // bitrateCalculator implements interface BitrateCalculator type BitrateCalculator struct { - outputDelay int // sec - now time.Time - prev time.Time - startedBefore bool - elapsedTime time.Duration - lastDisplayTime time.Time + outputDelay int // sec + now time.Time + prev time.Time + startedBefore bool + elapsedTime time.Duration + lastDisplayTime time.Time } // Place this at the start of the code segment that you would like to time func (bc *BitrateCalculator) Start(outputDelay int) { - if outputDelay >= 0{ - bc.outputDelay = outputDelay - } else { - bc.outputDelay = 0 - } - bc.prev = time.Now() + if outputDelay >= 0 { + bc.outputDelay = outputDelay + } else { + bc.outputDelay = 0 + } + bc.prev = time.Now() if !bc.startedBefore { bc.startedBefore = true bc.elapsedTime = time.Duration(0) - bc.lastDisplayTime = time.Now() + bc.lastDisplayTime = time.Now() } } // Place this at the end of the code segment that you would like to time func (bc *BitrateCalculator) Stop(noOfKB float64) (bitrate int64) { - bc.now = time.Now() + bc.now = time.Now() deltaTime := bc.now.Sub(bc.prev) if bc.now.Sub(bc.lastDisplayTime) > time.Duration(bc.outputDelay)*time.Second { bitrate = int64(noOfKB / float64(deltaTime/1e9)) fmt.Printf("Bitrate: %d kbps\n", bitrate) bc.elapsedTime = time.Duration(0) - bc.lastDisplayTime = bc.now + bc.lastDisplayTime = bc.now } return } diff --git a/bitrate/BitrateCalculator_test.go b/bitrate/BitrateCalculator_test.go index e65e6eff..f256e1f5 100644 --- a/bitrate/BitrateCalculator_test.go +++ b/bitrate/BitrateCalculator_test.go @@ -1,15 +1,16 @@ /* NAME - revid - a testbed for re-muxing and re-directing video streams as MPEG-TS over various protocols. + BitrateCalculator_test.go - is a file that may be used to test the + BitrateCalculator.go file using the golang testing utilities DESCRIPTION See Readme.md AUTHOR - Alan Noble + Saxon Nelson-Milton LICENSE - revid is Copyright (C) 2017 Alan Noble. + BitrateCalculator_test.go is Copyright (C) 2017 the Australian Ocean Lab (AusOcean) It is free software: you can redistribute it and/or modify them under the terms of the GNU General Public License as published by the @@ -46,30 +47,30 @@ var bitrateCalc BitrateCalculator // Simple test to check that the calculator can calc bitrate over a given // duration of time func Test1(t *testing.T) { - bitrateCalc = BitrateCalculator{} + bitrateCalc = BitrateCalculator{} bitrateCalc.Start(bitrateDelay1) time.Sleep(testTime * time.Millisecond) currentBitrate := int64(bitrateCalc.Stop(amountOfData)) - actualBitrate := int64(amountOfData / ((testTime * time.Millisecond)/1e9)) + actualBitrate := int64(amountOfData / ((testTime * time.Millisecond) / 1e9)) if currentBitrate != actualBitrate { t.Errorf("Bitrate is wrong! Calculated: %v Actual %v", currentBitrate, actualBitrate) } } // Now let's check that the output delay feature works -func Test2(t *testing.T){ - bitrateCalc = BitrateCalculator{} +func Test2(t *testing.T) { + bitrateCalc = BitrateCalculator{} var currentBitrate int64 - for i := 0; i < 2; i++ { - bitrateCalc.Start(bitrateDelay2) - time.Sleep(testTime*time.Millisecond) - currentBitrate = int64(bitrateCalc.Stop(amountOfData)) - if i == 0 && currentBitrate != 0 { - t.Errorf("The bitrate calc did not delay outputting!") - } - time.Sleep(6000*time.Millisecond) - } - actualBitrate := int64(amountOfData / ((testTime * time.Millisecond)/1e9)) + for i := 0; i < 2; i++ { + bitrateCalc.Start(bitrateDelay2) + time.Sleep(testTime * time.Millisecond) + currentBitrate = int64(bitrateCalc.Stop(amountOfData)) + if i == 0 && currentBitrate != 0 { + t.Errorf("The bitrate calc did not delay outputting!") + } + time.Sleep(6000 * time.Millisecond) + } + actualBitrate := int64(amountOfData / ((testTime * time.Millisecond) / 1e9)) if currentBitrate != actualBitrate { t.Errorf("Bitrate is wrong! Calculated: %v Actual %v", currentBitrate, actualBitrate) } From bb6130d5ca325acc994eed282efad5bc2255da3d Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 6 Dec 2017 13:45:39 +1030 Subject: [PATCH 9/9] Just made correction to comment --- bitrate/BitrateCalculator.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bitrate/BitrateCalculator.go b/bitrate/BitrateCalculator.go index 752905d6..f581270d 100644 --- a/bitrate/BitrateCalculator.go +++ b/bitrate/BitrateCalculator.go @@ -33,7 +33,7 @@ import ( "time" ) -// bitrateCalculator implements interface BitrateCalculator +// BitrateCalculator provides fields and methods to allow calculation of bitrate type BitrateCalculator struct { outputDelay int // sec now time.Time