-
Notifications
You must be signed in to change notification settings - Fork 198
ai/live: Add segmented MediaWriter / MediaReader #3705
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package media | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
"sync" | ||
) | ||
|
||
type SegmentReaderConfig struct{} | ||
|
||
// NewSegmentWriter makes a ring of n MediaWriters. | ||
func NewSegmentWriter(n int) *SegmentWriter { | ||
size := n + 1 // make space for 'next' | ||
rw := &SegmentWriter{ | ||
writers: make([]*MediaWriter, size), | ||
size: size, | ||
seq: -1, // -1 makes logic simpler in first Next() | ||
} | ||
rw.writers[0] = NewMediaWriter() // precreate first segment | ||
return rw | ||
} | ||
|
||
type SegmentWriter struct { | ||
mu sync.Mutex | ||
writers []*MediaWriter | ||
size int | ||
seq int | ||
closed bool | ||
} | ||
|
||
type writerWrapper struct{ mw *MediaWriter } | ||
|
||
func (w *writerWrapper) Write(p []byte) (int, error) { return w.mw.Write(p) } | ||
func (w *writerWrapper) Close() error { return w.mw.Close() } | ||
|
||
// Return a fresh writer, pre-creating the next writer. | ||
func (rb *SegmentWriter) Next() (io.WriteCloser, error) { | ||
rb.mu.Lock() | ||
defer rb.mu.Unlock() | ||
|
||
if rb.closed { | ||
return nil, io.EOF | ||
} | ||
|
||
rb.seq += 1 | ||
idx := rb.seq % rb.size | ||
mw := rb.writers[idx] | ||
|
||
// close existing writer at next before replacing | ||
nextIdx := (rb.seq + 1) % rb.size | ||
if old := rb.writers[nextIdx]; old != nil { | ||
old.Close() | ||
} | ||
rb.writers[nextIdx] = NewMediaWriter() | ||
return &writerWrapper{mw}, nil | ||
} | ||
|
||
// MakeReader returns a new reader positioned at the currently active segment. | ||
func (rb *SegmentWriter) MakeReader(_ SegmentReaderConfig) *SegmentReader { | ||
rb.mu.Lock() | ||
defer rb.mu.Unlock() | ||
idx := max(rb.seq-1, -1) | ||
return &SegmentReader{ | ||
rb: rb, | ||
seq: idx, | ||
size: rb.size, | ||
} | ||
} | ||
|
||
// Close shuts the SegmentWriter and all its underlying MediaWriters. | ||
// After Close, all future Next() calls on writer or readers will error. | ||
func (rb *SegmentWriter) Close() error { | ||
rb.mu.Lock() | ||
defer rb.mu.Unlock() | ||
|
||
if rb.closed { | ||
return nil | ||
} | ||
// close any open MediaWriters | ||
for _, w := range rb.writers { | ||
if w != nil { | ||
w.Close() | ||
} | ||
} | ||
rb.closed = true | ||
return nil | ||
} | ||
|
||
type SegmentReader struct { | ||
rb *SegmentWriter | ||
seq int | ||
size int | ||
} | ||
|
||
type readerWrapper struct { | ||
cr CloneableReader | ||
seq int | ||
} | ||
|
||
func (r *readerWrapper) Read(p []byte) (int, error) { return r.cr.Read(p) } | ||
func (r *readerWrapper) Close() error { return nil } | ||
func (r *readerWrapper) Seq() int { return r.seq } | ||
|
||
// Return a reader for the next segment (per‐reader cursor). | ||
func (rr *SegmentReader) Next() (*readerWrapper, error) { | ||
rr.rb.mu.Lock() | ||
defer rr.rb.mu.Unlock() | ||
|
||
nextSeq := rr.seq + 1 | ||
// if the writer has been closed, disallow stepping past its last seq | ||
if rr.rb.closed && nextSeq > rr.rb.seq { | ||
return nil, io.EOF | ||
|
||
} | ||
idx := rr.seq + 1 | ||
if idx > (rr.rb.seq + 1) { | ||
return nil, errors.New("segment out of bounds") | ||
} | ||
// +1 to account for the precreate | ||
if idx <= (rr.rb.seq+1)-rr.size { | ||
return nil, errors.New("reader fell behind") | ||
} | ||
rr.seq = idx | ||
idx = idx % rr.size | ||
mw := rr.rb.writers[idx] | ||
if mw == nil { | ||
return nil, errors.New("no writer") | ||
} | ||
return &readerWrapper{cr: mw.MakeReader(), seq: rr.seq}, nil | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lgtm, but just curious do we need this struct? It doesn't have any extra functions on top of
io.WriteCloser
I think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yeah, wanted to add a
Seq()
similar to the reader side but didn't get that in here