forked from pion/webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
51 lines (43 loc) · 923 Bytes
/
writer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package rtpdump
import (
"fmt"
"io"
"sync"
)
// Writer writes the RTPDump file format
type Writer struct {
writerMu sync.Mutex
writer io.Writer
}
// NewWriter makes a new Writer and immediately writes the given Header
// to begin the file.
func NewWriter(w io.Writer, hdr Header) (*Writer, error) {
preamble := fmt.Sprintf(
"#!rtpplay1.0 %s/%d\n",
hdr.Source.To4().String(),
hdr.Port)
if _, err := w.Write([]byte(preamble)); err != nil {
return nil, err
}
hData, err := hdr.Marshal()
if err != nil {
return nil, err
}
if _, err := w.Write(hData); err != nil {
return nil, err
}
return &Writer{writer: w}, nil
}
// WritePacket writes a Packet to the output
func (w *Writer) WritePacket(p Packet) error {
w.writerMu.Lock()
defer w.writerMu.Unlock()
data, err := p.Marshal()
if err != nil {
return err
}
if _, err := w.writer.Write(data); err != nil {
return err
}
return nil
}