Finish IVF writer

This commit is contained in:
Sean DuBois
2018-06-10 19:01:53 -07:00
parent fd96da48ea
commit c7a6caa462
4 changed files with 25 additions and 26 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*.ivf

View File

@@ -5,12 +5,15 @@ import (
"fmt"
"os"
"time"
"github.com/pions/webrtc/rtp"
)
type IVFWriter struct {
fd *os.File
time time.Time
count uint64
fd *os.File
time time.Time
count uint64
currentFrame []byte
}
func panicWrite(fd *os.File, data []byte) {
@@ -31,7 +34,7 @@ func NewIVFWriter(fileName string) (*IVFWriter, error) {
panicWrite(f, []byte("VP80")) // FOURCC
panicWrite(f, []byte{128, 2}) // Width (640)
panicWrite(f, []byte{224, 1}) // Height (480)
panicWrite(f, []byte{232, 3, 0, 0}) // Framerate numerator
panicWrite(f, []byte{30, 0, 0, 0}) // Framerate numerator
panicWrite(f, []byte{1, 0, 0, 0}) // Framerate denominator
panicWrite(f, []byte{132, 3, 0, 0}) // Frame count
panicWrite(f, []byte{0, 0, 0, 0}) // Unused
@@ -40,22 +43,28 @@ func NewIVFWriter(fileName string) (*IVFWriter, error) {
return i, nil
}
func (i *IVFWriter) AddBuffer(buffer []byte) {
if len(buffer) == 0 {
func (i *IVFWriter) AddPacket(packet *rtp.Packet) {
i.currentFrame = append(i.currentFrame, packet.Payload[12:]...)
if !packet.Marker {
return
} else if len(i.currentFrame) == 0 {
fmt.Println("skipping")
return
}
bufferLen := make([]byte, 4)
fmt.Println(len(buffer))
binary.LittleEndian.PutUint32(bufferLen, uint32(len(buffer)))
binary.LittleEndian.PutUint32(bufferLen, uint32(len(i.currentFrame)))
pts := make([]byte, 8)
binary.LittleEndian.PutUint64(pts, i.count)
i.count += 33
i.count += 1
panicWrite(i.fd, bufferLen)
panicWrite(i.fd, pts)
panicWrite(i.fd, buffer)
panicWrite(i.fd, i.currentFrame)
i.currentFrame = nil
}
func (i *IVFWriter) Close() error {

View File

@@ -11,6 +11,8 @@ import (
"github.com/pions/webrtc/rtp"
)
var trackCount uint64
func main() {
reader := bufio.NewReader(os.Stdin)
@@ -32,8 +34,8 @@ func main() {
peerConnection := &webrtc.RTCPeerConnection{}
// Set a handler for when a new remote track starts, this handler saves buffers to disk as
// an ivf file, since we could have multiple video tracks we provide a counter
var trackCount uint64
// an ivf file, since we could have multiple video tracks we provide a counter.
// In your application this is where you would handle/process video
peerConnection.Ontrack = func(mediaType webrtc.MediaType, packets chan *rtp.Packet) {
go func() {
track := atomic.AddUint64(&trackCount, 1)
@@ -43,16 +45,8 @@ func main() {
if err != nil {
panic(err)
}
currentFrame := []byte{}
for {
packet := <-packets
currentFrame = append(currentFrame, packet.Payload[4:]...)
if packet.Marker {
i.AddBuffer(currentFrame)
currentFrame = nil
}
i.AddPacket(<-packets)
}
}()
}

View File

@@ -57,11 +57,6 @@ func packetHandler(conn *ipv4.PacketConn, srcString string, remoteKey []byte, tl
fmt.Println("Failed to decrypt packet")
continue
}
// addr := &net.UDPAddr{
// Port: 5000,
// IP: net.ParseIP("127.0.0.1"),
// }
// conn.WriteTo(unencrypted, nil, addr)
packet := &rtp.Packet{}
if err := packet.Unmarshal(unencrypted); err != nil {