Skip to content

Commit

Permalink
use mediacommon/pkg/mpegts in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed Jul 30, 2023
1 parent 0a8c761 commit 577d6f3
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 192 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,27 @@ package main

import (
"bufio"
"context"
"log"
"os"
"time"

"github.com/asticode/go-astits"
"github.com/bluenviron/mediacommon/pkg/codecs/h264"
"github.com/bluenviron/mediacommon/pkg/formats/mpegts"
)

func durationGoToMPEGTS(v time.Duration) int64 {
return int64(v.Seconds() * 90000)
}

// mpegtsMuxer allows to save a H264 stream into a MPEG-TS file.
type mpegtsMuxer struct {
sps []byte
pps []byte

f *os.File
b *bufio.Writer
mux *astits.Muxer
w *mpegts.Writer
track *mpegts.Track
dtsExtractor *h264.DTSExtractor
firstIDRReceived bool
startDTS time.Duration
Expand All @@ -32,19 +36,20 @@ func newMPEGTSMuxer(sps []byte, pps []byte) (*mpegtsMuxer, error) {
}
b := bufio.NewWriter(f)

mux := astits.NewMuxer(context.Background(), b)
mux.AddElementaryStream(astits.PMTElementaryStream{
ElementaryPID: 256,
StreamType: astits.StreamTypeH264Video,
})
mux.SetPCRPID(256)
track := &mpegts.Track{
PID: 256,
Codec: &mpegts.CodecH264{},
}

w := mpegts.NewWriter(b, []*mpegts.Track{track})

return &mpegtsMuxer{
sps: sps,
pps: pps,
f: f,
b: b,
mux: mux,
sps: sps,
pps: pps,
f: f,
b: b,
w: w,
track: track,
}, nil
}

Expand Down Expand Up @@ -131,39 +136,8 @@ func (e *mpegtsMuxer) encode(au [][]byte, pts time.Duration) error {
pts -= e.startDTS
}

oh := &astits.PESOptionalHeader{
MarkerBits: 2,
}

if dts == pts {
oh.PTSDTSIndicator = astits.PTSDTSIndicatorOnlyPTS
oh.PTS = &astits.ClockReference{Base: int64(pts.Seconds() * 90000)}
} else {
oh.PTSDTSIndicator = astits.PTSDTSIndicatorBothPresent
oh.DTS = &astits.ClockReference{Base: int64(dts.Seconds() * 90000)}
oh.PTS = &astits.ClockReference{Base: int64(pts.Seconds() * 90000)}
}

// encode into Annex-B
annexb, err := h264.AnnexBMarshal(au)
if err != nil {
return err
}

// write TS packet
_, err = e.mux.WriteData(&astits.MuxerData{
PID: 256,
AdaptationField: &astits.PacketAdaptationField{
RandomAccessIndicator: idrPresent,
},
PES: &astits.PESData{
Header: &astits.PESHeader{
OptionalHeader: oh,
StreamID: 224, // video
},
Data: annexb,
},
})
// encode into MPEG-TS
err := e.w.WriteH26x(e.track, durationGoToMPEGTS(pts), durationGoToMPEGTS(dts), idrPresent, au)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package main

import (
"bufio"
"log"
"os"
"time"

"github.com/bluenviron/mediacommon/pkg/codecs/mpeg4audio"
"github.com/bluenviron/mediacommon/pkg/formats/mpegts"
)

func durationGoToMPEGTS(v time.Duration) int64 {
return int64(v.Seconds() * 90000)
}

// mpegtsMuxer allows to save a MPEG4-audio stream into a MPEG-TS file.
type mpegtsMuxer struct {
config *mpeg4audio.Config
f *os.File
b *bufio.Writer
w *mpegts.Writer
track *mpegts.Track
}

// newMPEGTSMuxer allocates a mpegtsMuxer.
func newMPEGTSMuxer(config *mpeg4audio.Config) (*mpegtsMuxer, error) {
f, err := os.Create("mystream.ts")
if err != nil {
return nil, err
}
b := bufio.NewWriter(f)

track := &mpegts.Track{
PID: 256,
Codec: &mpegts.CodecMPEG4Audio{
Config: *config,
},
}

w := mpegts.NewWriter(b, []*mpegts.Track{track})

return &mpegtsMuxer{
config: config,
f: f,
b: b,
w: w,
track: track,
}, nil
}

// close closes all the mpegtsMuxer resources.
func (e *mpegtsMuxer) close() {
e.b.Flush()
e.f.Close()
}

// encode encodes a MPEG4-audio access unit into MPEG-TS.
func (e *mpegtsMuxer) encode(au []byte, pts time.Duration) error {
// encode into MPEG-TS
err := e.w.WriteMPEG4Audio(e.track, durationGoToMPEGTS(pts), [][]byte{au})
if err != nil {
return err
}

log.Println("wrote TS packet")
return nil
}
91 changes: 0 additions & 91 deletions examples/client-read-format-mpeg4audio-save-to-disk/mpegtsmuxer.go

This file was deleted.

Loading

0 comments on commit 577d6f3

Please sign in to comment.