From b1e5b4341ffc8e4010bef4a4a73a985873c5ac80 Mon Sep 17 00:00:00 2001 From: Trek H Date: Wed, 24 Apr 2019 16:39:18 +0930 Subject: [PATCH] revid: pid for audio being written to mts packets --- cmd/revid-cli/main.go | 4 ++-- container/mts/audio_test.go | 12 ++++++------ container/mts/encoder.go | 17 ++++++++++------- revid/revid.go | 14 ++++++++++---- 4 files changed, 28 insertions(+), 19 deletions(-) diff --git a/cmd/revid-cli/main.go b/cmd/revid-cli/main.go index 97cc8ecc..b4abf0ef 100644 --- a/cmd/revid-cli/main.go +++ b/cmd/revid-cli/main.go @@ -62,7 +62,7 @@ const ( defaultLogVerbosity = logger.Info defaultSleepTime = 60 // Seconds sampleSize = 2 // Bytes - blockSize = 16000 // Bytes + chunkSize = 16000 // Bytes ) // canProfile is set to false with revid-cli is built with "-tags profile". @@ -195,7 +195,7 @@ func handleFlags() revid.Config { switch *inputPtr { case "Audio": - cfg.WriteRate = float64(*sampleRatePtr*sampleSize) / float64(blockSize) + cfg.WriteRate = float64(*sampleRatePtr*sampleSize) / float64(chunkSize) default: cfg.WriteRate = float64(*frameRatePtr) } diff --git a/container/mts/audio_test.go b/container/mts/audio_test.go index 23ba16e6..0674df19 100644 --- a/container/mts/audio_test.go +++ b/container/mts/audio_test.go @@ -43,8 +43,8 @@ func TestEncodePcm(t *testing.T) { var buf bytes.Buffer sampleRate := 48000 sampleSize := 2 - blockSize := 16000 - writeFreq := float64(sampleRate*sampleSize) / float64(blockSize) + chunkSize := 16000 + writeFreq := float64(sampleRate*sampleSize) / float64(chunkSize) e := NewEncoder(&buf, writeFreq, Audio) inPath := "../../../test/test-data/av/input/sweep_400Hz_20000Hz_-3dBFS_5s_48khz.pcm" @@ -54,15 +54,15 @@ func TestEncodePcm(t *testing.T) { } // Break pcm into blocks and encode to mts and get the resulting bytes. - for i := 0; i < len(inPcm); i += blockSize { - if len(inPcm)-i < blockSize { + for i := 0; i < len(inPcm); i += chunkSize { + if len(inPcm)-i < chunkSize { block := inPcm[i:] _, err = e.Write(block) if err != nil { t.Errorf("unable to write block: %v", err) } } else { - block := inPcm[i : i+blockSize] + block := inPcm[i : i+chunkSize] _, err = e.Write(block) if err != nil { t.Errorf("unable to write block: %v", err) @@ -73,7 +73,7 @@ func TestEncodePcm(t *testing.T) { // Get the first MTS packet to check var pkt packet.Packet - pesPacket := make([]byte, 0, blockSize) + pesPacket := make([]byte, 0, chunkSize) got := make([]byte, 0, len(inPcm)) i := 0 if i+PacketSize <= len(clip) { diff --git a/container/mts/encoder.go b/container/mts/encoder.go index 2d7ce656..b27a100d 100644 --- a/container/mts/encoder.go +++ b/container/mts/encoder.go @@ -99,11 +99,12 @@ var ( ) const ( - sdtPid = 17 - patPid = 0 - pmtPid = 4096 - videoPid = 256 - audioPid = 210 + sdtPid = 17 + patPid = 0 + pmtPid = 4096 + videoPid = 256 + // AudioPid is the Id for packets containing audio data + AudioPid = 210 videoStreamID = 0xe0 // First video stream ID. audioStreamID = 0xc0 // First audio stream ID. ) @@ -154,7 +155,7 @@ func NewEncoder(dst io.Writer, rate float64, mediaType int) *Encoder { var sid byte switch mediaType { case Audio: - mPid = audioPid + mPid = AudioPid sid = audioStreamID case Video: mPid = videoPid @@ -248,7 +249,9 @@ func (e *Encoder) Write(data []byte) (int, error) { pkt.PCR = e.pcr() pusi = false } - _, err := e.dst.Write(pkt.Bytes(e.tsSpace[:PacketSize])) + bytes := pkt.Bytes(e.tsSpace[:PacketSize]) + fmt.Printf("Packet: %v", bytes) + _, err := e.dst.Write(bytes) if err != nil { return len(data), err } diff --git a/revid/revid.go b/revid/revid.go index 8b0ea88b..6c21762f 100644 --- a/revid/revid.go +++ b/revid/revid.go @@ -187,7 +187,7 @@ func (r *Revid) setConfig(config Config) error { // mtsEnc and flvEnc will be called to obtain an mts encoder and flv encoder // respectively. multiWriter will be used to create an ioext.multiWriteCloser // so that encoders can write to multiple senders. -func (r *Revid) setupPipeline(mtsEnc, flvEnc func(dst io.Writer, rate int) (io.Writer, error), multiWriter func(...io.WriteCloser) io.WriteCloser) error { +func (r *Revid) setupPipeline(mtsEnc func(dst io.Writer, rate, mediaType int) (io.Writer, error), flvEnc func(dst io.Writer, rate int) (io.Writer, error), multiWriter func(...io.WriteCloser) io.WriteCloser) error { r.buffer = (*buffer)(ring.NewBuffer(ringBufferSize, ringBufferElementSize, writeTimeout)) r.encoder = r.encoder[:0] @@ -231,7 +231,13 @@ func (r *Revid) setupPipeline(mtsEnc, flvEnc func(dst io.Writer, rate int) (io.W // as a destination. if len(mtsSenders) != 0 { mw := multiWriter(mtsSenders...) - e, _ := mtsEnc(mw, int(r.config.FrameRate)) + var mediaType int + if r.config.Input == Audio { + mediaType = mts.Audio + } else { + mediaType = mts.Video + } + e, _ := mtsEnc(mw, int(r.config.WriteRate), mediaType) r.encoder = append(r.encoder, e) } @@ -275,8 +281,8 @@ func (r *Revid) setupPipeline(mtsEnc, flvEnc func(dst io.Writer, rate int) (io.W return nil } -func newMtsEncoder(dst io.Writer, fps int) (io.Writer, error) { - e := mts.NewEncoder(dst, float64(fps), mts.Video) +func newMtsEncoder(dst io.Writer, writeRate, mediaType int) (io.Writer, error) { + e := mts.NewEncoder(dst, float64(writeRate), mediaType) return e, nil }