File: writer.go

package info (click to toggle)
golang-github-pion-webrtc.v3 3.1.56-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,392 kB
  • sloc: javascript: 595; sh: 28; makefile: 5
file content (51 lines) | stat: -rw-r--r-- 923 bytes parent folder | download | duplicates (2)
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
}