github.com/pion/webrtc/v3@v3.2.24/pkg/media/rtpdump/writer.go (about) 1 // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly> 2 // SPDX-License-Identifier: MIT 3 4 package rtpdump 5 6 import ( 7 "fmt" 8 "io" 9 "sync" 10 ) 11 12 // Writer writes the RTPDump file format 13 type Writer struct { 14 writerMu sync.Mutex 15 writer io.Writer 16 } 17 18 // NewWriter makes a new Writer and immediately writes the given Header 19 // to begin the file. 20 func NewWriter(w io.Writer, hdr Header) (*Writer, error) { 21 preamble := fmt.Sprintf( 22 "#!rtpplay1.0 %s/%d\n", 23 hdr.Source.To4().String(), 24 hdr.Port) 25 if _, err := w.Write([]byte(preamble)); err != nil { 26 return nil, err 27 } 28 29 hData, err := hdr.Marshal() 30 if err != nil { 31 return nil, err 32 } 33 if _, err := w.Write(hData); err != nil { 34 return nil, err 35 } 36 37 return &Writer{writer: w}, nil 38 } 39 40 // WritePacket writes a Packet to the output 41 func (w *Writer) WritePacket(p Packet) error { 42 w.writerMu.Lock() 43 defer w.writerMu.Unlock() 44 45 data, err := p.Marshal() 46 if err != nil { 47 return err 48 } 49 if _, err := w.writer.Write(data); err != nil { 50 return err 51 } 52 53 return nil 54 }