Added iocontext to demuxer options

This commit is contained in:
Quentin Renard
2024-05-07 17:01:37 +02:00
parent ba594675f8
commit 2cfdde8bcf
8 changed files with 43 additions and 44 deletions

4
go.mod
View File

@@ -3,8 +3,8 @@ module github.com/asticode/go-astiencoder
go 1.13
require (
github.com/asticode/go-astiav v0.12.0
github.com/asticode/go-astikit v0.37.0
github.com/asticode/go-astiav v0.14.0
github.com/asticode/go-astikit v0.42.0
github.com/shirou/gopsutil/v3 v3.21.10
github.com/stretchr/testify v1.7.0
)

9
go.sum
View File

@@ -1,10 +1,9 @@
github.com/StackExchange/wmi v1.2.1 h1:VIkavFPXSjcnS+O8yTq7NI32k0R5Aj+v39y29VYDOSA=
github.com/StackExchange/wmi v1.2.1/go.mod h1:rcmrprowKIVzvc+NUiLncP2uuArMWLCbu9SBzvHz7e8=
github.com/asticode/go-astiav v0.12.0 h1:tETfPhVpJrSyh3zvUOmDvebFaCoFpeATSaQAA7B50J8=
github.com/asticode/go-astiav v0.12.0/go.mod h1:phvUnSSlV91S/PELeLkDisYiRLOssxWOsj4oDrqM/54=
github.com/asticode/go-astikit v0.28.2/go.mod h1:h4ly7idim1tNhaVkdVBeXQZEE3L0xblP7fCWbgwipF0=
github.com/asticode/go-astikit v0.37.0 h1:FaMrY4m+xUTHgwLdgNbDuEFG27UVJTSlWdPqbRsmgOM=
github.com/asticode/go-astikit v0.37.0/go.mod h1:h4ly7idim1tNhaVkdVBeXQZEE3L0xblP7fCWbgwipF0=
github.com/asticode/go-astiav v0.14.0 h1:fCP1I2ZI4AM+h0Ic7fx5voy09Y48z2M6rPQi+A3ZIXU=
github.com/asticode/go-astiav v0.14.0/go.mod h1:K7D8UC6GeQt85FUxk2KVwYxHnotrxuEnp5evkkudc2s=
github.com/asticode/go-astikit v0.42.0 h1:pnir/2KLUSr0527Tv908iAH6EGYYrYta132vvjXsH5w=
github.com/asticode/go-astikit v0.42.0/go.mod h1:h4ly7idim1tNhaVkdVBeXQZEE3L0xblP7fCWbgwipF0=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-ole/go-ole v1.2.5/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=

View File

@@ -56,7 +56,7 @@ func (ctx Context) String() string {
} else if ctx.CodecID > 0 {
ss = append(ss, "codec id: "+ctx.CodecID.String())
}
if ctx.TimeBase.ToDouble() > 0 {
if ctx.TimeBase.Float64() > 0 {
ss = append(ss, "timebase: "+ctx.TimeBase.String())
}
@@ -77,10 +77,10 @@ func (ctx Context) String() string {
if ctx.PixelFormat >= 0 {
ss = append(ss, "pixel format: "+ctx.PixelFormat.String())
}
if ctx.SampleAspectRatio.ToDouble() > 0 {
if ctx.SampleAspectRatio.Float64() > 0 {
ss = append(ss, "sample aspect ratio: "+ctx.SampleAspectRatio.String())
}
if ctx.FrameRate.ToDouble() > 0 {
if ctx.FrameRate.Float64() > 0 {
ss = append(ss, "framerate: "+ctx.FrameRate.String())
}
if ctx.GopSize > 0 {

View File

@@ -26,7 +26,7 @@ type Demuxer struct {
er *demuxerEmulateRate
flushOnStart bool
formatContext *astiav.FormatContext
interruptRet *int
interruptCallback astiav.IOInterrupter
l *demuxerLoop
p *pktPool
pb *demuxerProbe
@@ -118,7 +118,7 @@ type demuxerStreamEmulateRate struct {
referenceTS int64
}
func (d *Demuxer) newDemuxerStreamEmulateRate(s *demuxerStream) *demuxerStreamEmulateRate {
func (d *Demuxer) newDemuxerStreamEmulateRate() *demuxerStreamEmulateRate {
return &demuxerStreamEmulateRate{bufferDuration: d.er.bufferDuration}
}
@@ -147,16 +147,13 @@ func (d *Demuxer) newDemuxerStream(s *astiav.Stream) *demuxerStream {
ctx := NewContextFromStream(s)
// Create demuxer stream
ds := &demuxerStream{
return &demuxerStream{
ctx: ctx,
d: ctx.Descriptor(),
er: d.newDemuxerStreamEmulateRate(),
l: newDemuxerStreamLoop(),
s: s,
}
// Create rate emulator
ds.er = d.newDemuxerStreamEmulateRate(ds)
return ds
}
func (d *demuxerStream) stream() *Stream {
@@ -180,6 +177,8 @@ type DemuxerOptions struct {
FlushOnStart bool
// Exact input format
Format *astiav.InputFormat
// IO Context to use
IOContext *astiav.IOContext
// Loop options
Loop DemuxerLoopOptions
// Basic node options
@@ -246,7 +245,7 @@ func NewDemuxer(o DemuxerOptions, eh *astiencoder.EventHandler, c *astikit.Close
d.AddClose(d.formatContext.Free)
// Set interrupt callback
d.interruptRet = d.formatContext.SetInterruptCallback()
d.interruptCallback = d.formatContext.SetInterruptCallback()
// Handle probe cancellation
if o.ProbeCtx != nil {
@@ -254,11 +253,11 @@ func NewDemuxer(o DemuxerOptions, eh *astiencoder.EventHandler, c *astikit.Close
probeCtx, probeCancel := context.WithCancel(o.ProbeCtx)
// Handle interrupt
*d.interruptRet = 0
d.interruptCallback.Resume()
go func() {
<-probeCtx.Done()
if o.ProbeCtx.Err() != nil {
*d.interruptRet = 1
d.interruptCallback.Interrupt()
}
}()
@@ -266,6 +265,11 @@ func NewDemuxer(o DemuxerOptions, eh *astiencoder.EventHandler, c *astikit.Close
defer probeCancel()
}
// No url but an io context, we need to set the pb before opening the input
if o.URL == "" && o.IOContext != nil {
d.formatContext.SetPb(o.IOContext)
}
// Open input
if err = d.formatContext.OpenInput(o.URL, o.Format, dict); err != nil {
err = fmt.Errorf("astilibav: opening input failed: %w", err)
@@ -275,6 +279,16 @@ func NewDemuxer(o DemuxerOptions, eh *astiencoder.EventHandler, c *astikit.Close
// Make sure the input is properly closed
d.AddClose(d.formatContext.CloseInput)
// An url and an io context, we need to set the pb after opening the input
// Make sure the previous pb is closed
if o.URL != "" && o.IOContext != nil {
if pb := d.formatContext.Pb(); pb != nil {
pb.Close()
}
d.formatContext.SetPb(o.IOContext)
d.formatContext.SetFlags(d.formatContext.Flags().Add(astiav.FormatContextFlagCustomIo))
}
// Check whether probe has been cancelled
if o.ProbeCtx != nil && o.ProbeCtx.Err() != nil {
err = fmt.Errorf("astilibav: probing has been cancelled: %w", o.ProbeCtx.Err())
@@ -506,10 +520,10 @@ func (d *Demuxer) DisconnectForStream(h PktHandler, i *Stream) {
func (d *Demuxer) Start(ctx context.Context, t astiencoder.CreateTaskFunc) {
d.BaseNode.Start(ctx, t, func(t *astikit.Task) {
// Handle interrupt callback
*d.interruptRet = 0
d.interruptCallback.Resume()
go func() {
<-d.Context().Done()
*d.interruptRet = 1
d.interruptCallback.Interrupt()
}()
// Update emulate rate time references

View File

@@ -311,7 +311,7 @@ func (e *Encoder) receivePkt(d Descriptor) (stop bool) {
// Set pkt duration based on framerate
if f := e.codecCtx.Framerate(); f.Num() > 0 {
pkt.SetDuration(astiav.RescaleQ(int64(1e9/f.ToDouble()), NanosecondRational, d.TimeBase()))
pkt.SetDuration(astiav.RescaleQ(int64(1e9/f.Float64()), NanosecondRational, d.TimeBase()))
}
// Rescale timestamps

View File

@@ -12,7 +12,6 @@ type EventLog struct {
Format string
Level astiav.LogLevel
Msg string
Parent string
}
type EventHandlerLogAdapterOptions struct {
@@ -27,15 +26,14 @@ func EventHandlerLogAdapter(o EventHandlerLogAdapterOptions) astiencoder.EventHa
// Set log callback
// TODO Process parent and update event's target
astiav.SetLogCallback(func(level astiav.LogLevel, fmt, msg, parent string) {
astiav.SetLogCallback(func(c astiav.Classer, l astiav.LogLevel, fmt, msg string) {
// Emit event
h.Emit(astiencoder.Event{
Name: EventNameLog,
Payload: EventLog{
Format: fmt,
Level: level,
Level: l,
Msg: msg,
Parent: parent,
},
})
})
@@ -63,11 +61,6 @@ func EventHandlerLogAdapter(o EventHandlerLogAdapterOptions) astiencoder.EventHa
format = "astilibav: " + format
msg = "astilibav: " + msg
// Add parent
if strings.Index(v.Parent, "0x") == 0 {
msg += " (" + v.Parent + ")"
}
// Get level
ll, processed, stop := llf(v.Level)
if stop {

View File

@@ -71,22 +71,15 @@ func NewMuxer(o MuxerOptions, eh *astiencoder.EventHandler, c *astikit.Closer, s
// We need to use an io context if this is a file
if !m.formatContext.OutputFormat().Flags().Has(astiav.IOFormatFlagNofile) {
// Create io context
ioContext := astiav.NewIOContext()
// Open
if err = ioContext.Open(o.URL, astiav.NewIOContextFlags(astiav.IOContextFlagWrite)); err != nil {
var ioContext *astiav.IOContext
if ioContext, err = astiav.OpenIOContext(o.URL, astiav.NewIOContextFlags(astiav.IOContextFlagWrite)); err != nil {
err = fmt.Errorf("astilibav: opening io context failed: %w", err)
return
}
// Make sure the io context is properly closed
m.AddCloseWithError(func() error {
if err := ioContext.Closep(); err != nil {
return fmt.Errorf("astilibav: closing io context failed: %w", err)
}
return nil
})
// Make sure the io context is properly freed
m.AddCloseWithError(ioContext.Close)
// Set pb
m.formatContext.SetPb(ioContext)

View File

@@ -65,7 +65,7 @@ func NewRateEnforcer(o RateEnforcerOptions, eh *astiencoder.EventHandler, c *ast
ff: o.FrameFiller,
m: &sync.Mutex{},
outputCtx: o.OutputCtx,
period: time.Duration(float64(1e9) / o.OutputCtx.FrameRate.ToDouble()),
period: time.Duration(float64(1e9) / o.OutputCtx.FrameRate.Float64()),
ptsReference: o.PTSReference,
restamper: o.Restamper,
statFramesDelay: astikit.NewAtomicDuration(0),