Encoded packet data is now available.

This commit is contained in:
zergon321
2021-12-24 01:05:47 +03:00
parent 8f03055a3a
commit ca72290eb0
2 changed files with 49 additions and 5 deletions

View File

@@ -179,7 +179,7 @@ func (media *Media) ReadPacket() (*Packet, bool, error) {
return nil, false, nil
}
return &Packet{media: media}, true, nil
return newPacket(media, media.packet), true, nil
}
// CloseDecode closes the media container for decoding.

View File

@@ -6,24 +6,68 @@ package reisen
// #include <libavutil/avconfig.h>
// #include <libswscale/swscale.h>
import "C"
import "unsafe"
// Packet is a piece of data
// Packet is a piece of encoded data
// acquired from the media container.
//
// It can be either a video frame or
// an audio frame.
type Packet struct {
media *Media
media *Media
streamIndex int
data []byte
pts int64
dts int64
pos int64
duration int64
size int
flags int
}
// StreamIndex returns the index of the
// stream the packet belongs to.
func (pkt *Packet) StreamIndex() int {
return int(pkt.media.packet.stream_index)
return pkt.streamIndex
}
// Type returns the type of the packet
// (video or audio).
func (pkt *Packet) Type() StreamType {
return pkt.media.Streams()[pkt.StreamIndex()].Type()
return pkt.media.Streams()[pkt.streamIndex].Type()
}
// Data returns the data
// encoded in the packet.
func (pkt *Packet) Data() []byte {
buf := make([]byte, pkt.size)
copy(buf, pkt.data)
return buf
}
// Returns the size of the
// packet data.
func (pkt *Packet) Size() int {
return pkt.size
}
// newPacket creates a
// new packet info object.
func newPacket(media *Media, cPkt *C.AVPacket) *Packet {
pkt := &Packet{
media: media,
streamIndex: int(cPkt.stream_index),
data: C.GoBytes(unsafe.Pointer(
cPkt.data), cPkt.size),
pts: int64(cPkt.pts),
dts: int64(cPkt.dts),
pos: int64(cPkt.pos),
duration: int64(cPkt.duration),
size: int(cPkt.size),
flags: int(cPkt.flags),
}
return pkt
}