Skip to content

Commit

Permalink
IVF records can now be longer than 30s
Browse files Browse the repository at this point in the history
- Added timeout to save-to-disk example
- Writing number of IVF frames when closing the file
- Fixed typos

Relates to #471
  • Loading branch information
Antonito committed Mar 7, 2019
1 parent c4f37ff commit 0c5938b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
3 changes: 2 additions & 1 deletion examples/save-to-disk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ Run `echo $BROWSER_SDP | save-to-disk`
### Input save-to-disk's SessionDescription into your browser
Copy the text that `save-to-disk` just emitted and copy into second text area

### Hit 'Start Session' in jsfiddle, enjoy your video!
### Hit 'Start Session' in jsfiddle, wait, close jsfiddle, enjoy your video!
In the folder you ran `save-to-disk` you should now have a file `output-1.ivf` play with your video player of choice!
> Note: In order to correctly create the files, the remote client (JSFiddle) should be closed. The Go example will automatically close itself.
Congrats, you have used pion-WebRTC! Now start building something cool
39 changes: 25 additions & 14 deletions examples/save-to-disk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"os"
"time"

"github.com/pions/rtcp"
Expand All @@ -21,12 +22,11 @@ func saveToDisk(i media.Writer, track *webrtc.Track) {
}()

for {
packet, err := track.ReadRTP()
rtpPacket, err := track.ReadRTP()
if err != nil {
panic(err)
}

if err := i.AddPacket(packet); err != nil {
if err := i.AddPacket(rtpPacket); err != nil {
panic(err)
}
}
Expand Down Expand Up @@ -61,6 +61,15 @@ func main() {
panic(err)
}

opusFile, err := opuswriter.New("output.opus", 48000, 2)
if err != nil {
panic(err)
}
ivfFile, err := ivfwriter.New("output.ivf")
if err != nil {
panic(err)
}

// 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.
// In your application this is where you would handle/process video
Expand All @@ -78,26 +87,28 @@ func main() {

codec := track.Codec()
if codec.Name == webrtc.Opus {
fmt.Println("Got Opus track, saving to disk as output.opus")
i, err := opuswriter.New("output.opus", codec.ClockRate, codec.Channels)
if err != nil {
panic(err)
}
saveToDisk(i, track)
fmt.Println("Got Opus track, saving to disk as output.opus (48 kHz, 2 channels)")
saveToDisk(opusFile, track)
} else if codec.Name == webrtc.VP8 {
fmt.Println("Got VP8 track, saving to disk as output.ivf")
i, err := ivfwriter.New("output.ivf")
if err != nil {
panic(err)
}
saveToDisk(i, track)
saveToDisk(ivfFile, track)
}
})

// Set the handler for ICE connection state
// This will notify you when the peer has connected/disconnected
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
fmt.Printf("Connection State has changed %s \n", connectionState.String())

if connectionState == webrtc.ICEConnectionStateConnected {
fmt.Println("Ctrl+C the remote client to stop the demo")
} else if connectionState == webrtc.ICEConnectionStateFailed ||
connectionState == webrtc.ICEConnectionStateDisconnected {
opusFile.Close()
ivfFile.Close()
fmt.Println("Done writing media files")
os.Exit(0)
}
})

// Wait for the offer to be pasted
Expand Down
14 changes: 12 additions & 2 deletions pkg/media/ivfwriter/ivfwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (i *IVFWriter) writeHeader() error {
binary.LittleEndian.PutUint16(header[14:], 480) // Header Size
binary.LittleEndian.PutUint32(header[16:], 30) // Framerate numerator
binary.LittleEndian.PutUint32(header[20:], 1) // Framerate Denominator
binary.LittleEndian.PutUint32(header[24:], 900) // Frame count
binary.LittleEndian.PutUint32(header[24:], 900) // Frame count, will be updated on first Close() call
binary.LittleEndian.PutUint32(header[28:], 0) // Unused

_, err := i.stream.Write(header)
Expand Down Expand Up @@ -108,9 +108,19 @@ func (i *IVFWriter) Close() error {
}()

if i.fd == nil {
// Returns no error has it may be convenient to call
// Returns no error as it may be convenient to call
// Close() multiple times
return nil
}
// Update the framecount
if _, err := i.fd.Seek(24, 0); err != nil {
return err
}
buff := make([]byte, 4)
binary.LittleEndian.PutUint32(buff, uint32(i.count))
if _, err := i.fd.Write(buff); err != nil {
return err
}

return i.fd.Close()
}

0 comments on commit 0c5938b

Please sign in to comment.