github.com/puellanivis/breton@v0.2.16/lib/mpeg/ts/pes/writer.go (about)

     1  package pes
     2  
     3  import (
     4  	"io"
     5  )
     6  
     7  // Writer defines a Writer that will encode an Elementary Stream into a Packetized Elementary Stream.
     8  type Writer struct {
     9  	Stream
    10  
    11  	dst io.Writer
    12  }
    13  
    14  // NewWriter returns a Writer that encodes an Elementary Stream into a Packetized Elementary Stream,
    15  // with the given Stream ID.
    16  // Any furter Header values should be set before any Write is made.
    17  func NewWriter(streamid byte, wr io.Writer) *Writer {
    18  	return &Writer{
    19  		Stream: Stream{
    20  			ID: streamid,
    21  		},
    22  
    23  		dst: wr,
    24  	}
    25  }
    26  
    27  func (w *Writer) Write(b []byte) (n int, err error) {
    28  	pkt := &packet{
    29  		stream:  &w.Stream,
    30  		payload: b,
    31  	}
    32  
    33  	b2, err := pkt.Marshal()
    34  	if err != nil {
    35  		return 0, err
    36  	}
    37  
    38  	n, err = w.dst.Write(b2)
    39  	if err != nil {
    40  		return 0, err
    41  	}
    42  
    43  	if n < len(b2) {
    44  		return 0, io.ErrShortWrite
    45  	}
    46  
    47  	return len(b), nil
    48  }