|
| 1 | +// Copyright 2024 LiveKit, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package media |
| 16 | + |
| 17 | +import ( |
| 18 | + "bufio" |
| 19 | + "fmt" |
| 20 | + "io" |
| 21 | +) |
| 22 | + |
| 23 | +type BytesFrame interface { |
| 24 | + ~[]byte |
| 25 | + Frame |
| 26 | +} |
| 27 | + |
| 28 | +// BytesWriter is similar to io.WriteCloser, but intentionally breaks API compatibility. |
| 29 | +// |
| 30 | +// This is done to emphasize that BytesWriter implementations are aware of the frame boundaries, |
| 31 | +// and will only use buffer sizes that match the frame size. |
| 32 | +type BytesWriter interface { |
| 33 | + String() string |
| 34 | + WriteRaw(frame []byte) error |
| 35 | + Close() error |
| 36 | +} |
| 37 | + |
| 38 | +// NewBytesWriter creates a BytesWriter that writes to a standard io.WriteCloser. |
| 39 | +// |
| 40 | +// This process will erase the frame boundaries. Implement BytesWriter directly to preserve frame boundaries. |
| 41 | +func NewBytesWriter(w io.WriteCloser) BytesWriter { |
| 42 | + return &fileWriter{ |
| 43 | + w: w, |
| 44 | + bw: bufio.NewWriter(w), |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +type fileWriter struct { |
| 49 | + w io.WriteCloser |
| 50 | + bw *bufio.Writer |
| 51 | +} |
| 52 | + |
| 53 | +func (w *fileWriter) String() string { |
| 54 | + return "FileWriter" |
| 55 | +} |
| 56 | + |
| 57 | +func (w *fileWriter) WriteRaw(data []byte) error { |
| 58 | + _, err := w.bw.Write(data) |
| 59 | + return err |
| 60 | +} |
| 61 | + |
| 62 | +func (w *fileWriter) Close() error { |
| 63 | + if err := w.bw.Flush(); err != nil { |
| 64 | + _ = w.w.Close() |
| 65 | + return err |
| 66 | + } |
| 67 | + return w.w.Close() |
| 68 | +} |
| 69 | + |
| 70 | +// NewFileWriter creates a new frame writer that encodes frame to a binary stream. |
| 71 | +// |
| 72 | +// This process will erase the frame boundaries. Use EncodeBytes to preserve frame boundaries. |
| 73 | +func NewFileWriter[T Frame](w io.WriteCloser, sampleRate int) WriteCloser[T] { |
| 74 | + bw := NewBytesWriter(w) |
| 75 | + return EncodeBytes[T](bw, sampleRate) |
| 76 | +} |
| 77 | + |
| 78 | +// EncodeBytes creates a writer that converts every frame write to a single binary Write call on a standard io.WriteCloser. |
| 79 | +// |
| 80 | +// If preserving frame boundaries is not required, using NewFileWriter would be more efficient. |
| 81 | +func EncodeBytes[S Frame](w BytesWriter, sampleRate int) WriteCloser[S] { |
| 82 | + return &byteEncoder[S]{w: w, sampleRate: sampleRate} |
| 83 | +} |
| 84 | + |
| 85 | +type byteEncoder[S Frame] struct { |
| 86 | + w BytesWriter |
| 87 | + sampleRate int |
| 88 | + buf []byte |
| 89 | +} |
| 90 | + |
| 91 | +func (w *byteEncoder[S]) String() string { |
| 92 | + return fmt.Sprintf("ByteEncoder(%d) -> %s", w.sampleRate, w.w.String()) |
| 93 | +} |
| 94 | + |
| 95 | +func (w *byteEncoder[S]) SampleRate() int { |
| 96 | + return w.sampleRate |
| 97 | +} |
| 98 | + |
| 99 | +func (w *byteEncoder[S]) WriteSample(sample S) error { |
| 100 | + if sz := sample.Size(); cap(w.buf) < sz { |
| 101 | + w.buf = make([]byte, sz) |
| 102 | + } else { |
| 103 | + w.buf = w.buf[:sz] |
| 104 | + } |
| 105 | + n, err := sample.CopyTo(w.buf) |
| 106 | + if err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + return w.w.WriteRaw(w.buf[:n]) |
| 110 | +} |
| 111 | + |
| 112 | +func (w *byteEncoder[T]) Close() error { |
| 113 | + w.buf = nil |
| 114 | + return w.w.Close() |
| 115 | +} |
| 116 | + |
| 117 | +// DecodeBytes creates a writer that converts every binary write from a standard io.WriteCloser to a frame write. |
| 118 | +// |
| 119 | +// Note that directly reading from a file is not possible in this case, as the frame boundaries are unknown. |
| 120 | +func DecodeBytes[S BytesFrame](w WriteCloser[S]) BytesWriter { |
| 121 | + return &byteDecoder[S]{w: w} |
| 122 | +} |
| 123 | + |
| 124 | +type byteDecoder[S BytesFrame] struct { |
| 125 | + w WriteCloser[S] |
| 126 | +} |
| 127 | + |
| 128 | +func (w *byteDecoder[S]) String() string { |
| 129 | + return fmt.Sprintf("ByteDecoder -> %s", w.w.String()) |
| 130 | +} |
| 131 | + |
| 132 | +func (w *byteDecoder[S]) SampleRate() int { |
| 133 | + return w.w.SampleRate() |
| 134 | +} |
| 135 | + |
| 136 | +func (w *byteDecoder[S]) WriteRaw(sample []byte) error { |
| 137 | + return w.w.WriteSample(S(sample)) |
| 138 | +} |
| 139 | + |
| 140 | +func (w *byteDecoder[T]) Close() error { |
| 141 | + return w.w.Close() |
| 142 | +} |
0 commit comments