Documentation
¶
Index ¶
- Constants
- Variables
- func DecodePTSTime(t int64, timeBase time.Time) time.Time
- func DecodeTimeBase(t uint64) time.Time
- func DecodeTimeOffset(t int64) time.Duration
- func EncodePTSTime(t time.Time, timeBase time.Time) int64
- func EncodeTimeBase(t time.Time) uint64
- func EncodeTimeOffset(t time.Duration) int64
- func Extension(fileType FileType) string
- func IsValidCodec(c string) bool
- func IsValidTrackName(name string) bool
- func IsVideoFile(filename string) bool
- func MakeIndexNALU(pts int64, location int64, flags IndexNALUFlags) uint64
- func MakeIndexSentinel(location int64) uint64
- func PreallocateFile(f *os.File, size int64) error
- func SplitIndexNALUEncodedTimeOnly(p uint64) int64
- func SplitIndexNALULocationOnly(p uint64) int64
- func SplitIndexNALUTimeOnly(p uint64) time.Duration
- func TrackFilename(baseFilename string, trackName string, fileType FileType) string
- type File
- type FileType
- type IndexNALUFlags
- type NALU
- type OpenMode
- type PacketReadFlags
- type Track
- func (t *Track) Close() error
- func (t *Track) Count() int
- func (t *Track) CreateTrackFiles(baseFilename string) error
- func (t *Track) Duration() time.Duration
- func (t *Track) Filenames(baseFilename string) (string, string)
- func (t *Track) HasCapacity(nNALU int, maxPTS time.Time, combinedPayloadBytes int) bool
- func (t *Track) IndexFileSize() int64
- func (t *Track) PacketFileSize() int64
- func (t *Track) ReadAtTime(startTime, endTime time.Duration, flags PacketReadFlags) ([]NALU, error)
- func (t *Track) ReadIndex(startIdx, endIdx int) ([]NALU, error)
- func (t *Track) ReadPayload(nalus []NALU) error
- func (t *Track) WriteHeader() error
- func (t *Track) WriteNALUs(nalus []NALU) error
- type TrackType
Constants ¶
const ( MagicAudioTrackBytes = "rf1a" // must be 4 bytes long MagicVideoTrackBytes = "rf1v" // must be 4 bytes long CodecH264 = "h264" // must be 4 bytes long, and present in IsValidCodec() CodecH265 = "h265" // must be 4 bytes long, and present in IsValidCodec() )
const IndexHeaderSize = int(unsafe.Sizeof(C.CommonIndexHeader{}))
const MaxDuration = 1024 * time.Second
const MaxEncodedPTS = 1<<22 - 1
const MaxIndexEntries = 1<<16 - 1
const MaxPacketsFileSize = 1<<30 - 1 // 1 GB
const PrintAggregationStats = false
const TestKeyframeInterval = 30
Number of frames between keyframes
const TestNALUKeyframeInterval = TestKeyframeInterval + 2
Variables ¶
var ErrInvalidCodec = errors.New("invalid codec")
var ErrReadOnly = fmt.Errorf("track is read-only")
Functions ¶
func DecodePTSTime ¶
Decode an int64 time of 1/4096 of a second to a time.Time
func DecodeTimeBase ¶
Decode a microsecond Unix epoch encoding to a time
func DecodeTimeOffset ¶
Decode an int64 time of 1/4096 of a second to a time.Duration
func EncodePTSTime ¶
Encode an offset-based time to units of 1/4096 of a second
func EncodeTimeBase ¶
Encode a time to a microsecond Unix epoch encoding
func EncodeTimeOffset ¶
Encode a time.Duration to units of 1/4096 of a second
func IsValidCodec ¶
func IsValidTrackName ¶
Return true if the given name is a valid track name. Track names become part of filenames, so we impose restrictions on them.
func IsVideoFile ¶
func MakeIndexNALU ¶
func MakeIndexNALU(pts int64, location int64, flags IndexNALUFlags) uint64
Assumes little endian
func MakeIndexSentinel ¶
func PreallocateFile ¶
Pre-allocate space for a file, to avoid fragmentation
func SplitIndexNALUTimeOnly ¶
Types ¶
type File ¶
File is used to read and write rf1 files
func Create ¶
Create a new rf1 file group writer. baseFilename is the base name of the file, eg "/home/user/recording-2024-01-01". tracks may be empty/nil
func Open ¶
Open an existing rf1 file group filename may be either a base filename such as `/foo/bar/myvideo` or a concrete track filename such as `/foo/bar/myvideo_mytrack.rf1i`
type IndexNALUFlags ¶
type IndexNALUFlags uint32
Flags of a NALU in the track index
const ( IndexNALUFlagKeyFrame IndexNALUFlags = 1 // Key frame IndexNALUFlagEssentialMeta IndexNALUFlags = 2 // Essential metadata, required to initialize the decoder (eg SPS+PPS NALUs in h264 / VPS+SPS+PPS NALUs h265) IndexNALUFlagAnnexB IndexNALUFlags = 4 // Packet has Annex-B "emulation prevention bytes" and start codes )
We have 12 bits for flags, so maximum flag value is 1 << 11 = 2048
func CreateTestNALU ¶ added in v1.0.2
func CreateTestNALU(naluIdx int, fps int) (IndexNALUFlags, time.Duration)
Every 32 NALUs is a keyframe. A keyframe is two EssentialMeta NALUs followed by a keyframe. SYNC-TEST-META-COUNT
func SplitIndexNALU ¶
func SplitIndexNALU(p uint64) (pts int64, location int64, flags IndexNALUFlags)
Assumes little endian
func SplitIndexNALUFlagsOnly ¶
func SplitIndexNALUFlagsOnly(p uint64) IndexNALUFlags
type NALU ¶
type NALU struct {
PTS time.Time
Flags IndexNALUFlags
Position int64 // Position in packets file. Only used when reading
Length int64 // Only used when reading (logically this is equal to len(Payload), but Payload might be nil)
Payload []byte
}
func CreateTestNALUs ¶
func CreateTestNALUs(timeBase time.Time, startNALU, endNALU int, fps int, minPacketSize, maxPacketSize int, seed int) []NALU
Generate the range of NALUs [startNALU, endNALU) NALU flags are controlled by TestNALUFlags() seed should be a prime number
func (*NALU) IsKeyFrame ¶
type PacketReadFlags ¶
type PacketReadFlags int
Flags for reading packets
const ( // If the requested time interval does not start on a keyframe, // then seek back to find the first keyframe before the requested start time. PacketReadFlagSeekBackToKeyFrame PacketReadFlags = 1 << iota // Do not read packet data. Only read packet headers. PacketReadFlagHeadersOnly )
type Track ¶
type Track struct {
Type TrackType // Audio or Video
Name string // Name of track - becomes part of filename
TimeBase time.Time // All PTS times are relative to this
Codec string // eg "H264"
Width int // Only applicable to video
Height int // Only applicable to video
// contains filtered or unexported fields
}
Track is one track of audio or video
func MakeVideoTrack ¶
func MakeVideoTrack(name string, timeBase time.Time, codec string, width, height int) (*Track, error)
Create a new track definition, but do not write anything to disk, or associate the track with a file.
func OpenTrack ¶
Open a track for reading/writing If OpenMode is OpenModeReadOnly, then we open the files with O_RDONLY. If OpenMode is OpenModeReadWrite is true, and we can't open the file with O_RDWR, then the function fails.
func (*Track) CreateTrackFiles ¶
Create new track files on disk You will usually not call this function directly. Instead, it is called for you when using File.AddTrack.
func (*Track) HasCapacity ¶
func (*Track) IndexFileSize ¶
Returns the truncated size of the index file
func (*Track) PacketFileSize ¶
Returns the truncated size of the packet file
func (*Track) ReadAtTime ¶
Read NALUs with payload by specifying time instead of packet indices
func (*Track) ReadPayload ¶
Read payloads for the given NALUs