pion/webrtc · error

file not opened

Error message

file not opened

What it means

errFileNotOpened is returned when a writer constructor or write call receives a nil output target. Despite the name, in this codebase it is shared with ivfwriter, where NewWith returns it when the io.Writer argument is nil. It guards against writing into a nil destination.

Source

Thrown at pkg/media/oggwriter/oggwriter.go:48

	opusGranuleSampleRate              = 48000
	maxOpusPacketSamples               = opusGranuleSampleRate * 120 / 1000
	defaultChannelCount                = 2
	channelMappingFamily0              = 0
	channelMappingFamily1              = 1
	channelMappingFamily2              = 2
	channelMappingFamily255            = 255
	idPageSignature                    = "OpusHead"
	commentPageSignature               = "OpusTags"
	defaultVendor                      = "pion"
	pageHeaderSignature                = "OggS"
	pageHeaderSize                     = 27
	maxOggPageSegments                 = 255
	noGranulePosition                  = ^uint64(0)
	maxUint32Length                    = uint64(1<<32 - 1)
)

var (
	errFileNotOpened        = errors.New("file not opened")
	errOutputNotOpened      = errors.New("output not opened")
	errInvalidNilPacket     = errors.New("invalid nil packet")
	errDuplicateTrackSSRC   = errors.New("duplicate Ogg track SSRC")
	errDuplicateTrackSerial = errors.New("duplicate Ogg track serial")
	errTracksStarted        = errors.New("cannot add Ogg tracks after writing has started")
	errPacketSSRCMismatch   = errors.New("RTP packet SSRC does not match Ogg track SSRC")
	errInvalidOpusPacket    = errors.New("invalid Opus packet")
	errInvalidChannelCount  = errors.New("invalid channel count")
	errInvalidChannelMap    = errors.New("invalid channel mapping")
	errInvalidOpusTags      = errors.New("invalid OpusTags")
)

type pageRewriter interface {
	io.Seeker
	io.WriterAt
}

type writerConfig struct {

View on GitHub (pinned to 8c25dc09fa)

Solutions

  1. Check the error from os.Create/os.Open before passing the file
  2. Pass a non-nil io.Writer (os.File, bytes.Buffer, etc.)
  3. Guard with a nil check before constructing the writer

Example fix

// before
f, _ := os.Create("out.ivf")
w, err := ivfwriter.NewWith(f) // errFileNotOpened if f is nil
// after
f, err := os.Create("out.ivf")
if err != nil {
    return err
}
w, err := ivfwriter.NewWith(f)
Defensive patterns

Strategy: validation

Validate before calling

if out == nil {
    return errors.New("writer output is nil")
}
w, err := ivfwriter.NewWith(out)

Type guard

func hasOutput(w io.Writer) bool { return w != nil }

Try / catch

w, err := ivfwriter.NewWith(out)
if errors.Is(err, ivfwriter.ErrFileNotOpened) {
    return fmt.Errorf("no output provided: %w", err)
}

Prevention

When it happens

Trigger: Calling ivfwriter.NewWith(nil) (ivfwriter.go:84); oggwriter's NewWith path checks the output and returns errFileNotOpened analogously. Referenced in TestIVFWriter_Basic and TestOggWriter_AddPacketAndClose.

Common situations: An os.Create/os.Open error was ignored leaving a nil *os.File, a factory function returns nil writer on failure, refactoring removed the writer initialization.

Related errors


AI-assisted analysis of pion/webrtc@8c25dc09fa (2026-09-03). Data as JSON: /api/errors/ea042512fd75a92f. Report an issue: GitHub.