-
Notifications
You must be signed in to change notification settings - Fork 23
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
[web-wasm] Handle EOS and inputs with different framerates #878
Merged
Merged
Changes from all commits
Commits
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 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 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,81 @@ | ||
import type { Frame } from '@live-compositor/browser-render'; | ||
import { FrameFormat } from '@live-compositor/browser-render'; | ||
import type { FrameWithPts } from './decoder/h264Decoder'; | ||
import { assert } from '../utils'; | ||
|
||
/** | ||
* Represents frame produced by decoder. | ||
* Memory has to be manually managed by incrementing reference count on `FrameRef` copy and decrementing it once it's no longer used | ||
* `Input` manages memory in `getFrameRef()` | ||
* `Queue` on tick pulls `FrameRef` for each input and once render finishes, decrements the ref count | ||
*/ | ||
export class FrameRef { | ||
private frame: FrameWithPts; | ||
private refCount: number; | ||
private downloadedFrame?: Frame; | ||
|
||
public constructor(frame: FrameWithPts) { | ||
this.frame = frame; | ||
this.refCount = 1; | ||
} | ||
|
||
/** | ||
* Increments reference count. Should be called every time the reference is copied. | ||
*/ | ||
public incrementRefCount(): void { | ||
assert(this.refCount > 0); | ||
this.refCount++; | ||
} | ||
|
||
/** | ||
* Decrements reference count. If reference count reaches 0, `FrameWithPts` is freed from the memory. | ||
* It's unsafe to use the returned frame after `decrementRefCount()` call. | ||
* Should be used after we're sure we no longer need the frame. | ||
*/ | ||
public decrementRefCount(): void { | ||
assert(this.refCount > 0); | ||
|
||
this.refCount--; | ||
if (this.refCount === 0) { | ||
this.frame.frame.close(); | ||
} | ||
} | ||
|
||
/** | ||
* Returns underlying frame. Fails if frame was freed from memory. | ||
*/ | ||
public async getFrame(): Promise<Frame> { | ||
assert(this.refCount > 0); | ||
|
||
if (!this.downloadedFrame) { | ||
this.downloadedFrame = await downloadFrame(this.frame); | ||
} | ||
return this.downloadedFrame; | ||
} | ||
|
||
public getPtsMs(): number { | ||
return this.frame.ptsMs; | ||
} | ||
} | ||
|
||
async function downloadFrame(frameWithPts: FrameWithPts): Promise<Frame> { | ||
// Safari does not support conversion to RGBA | ||
// Chrome does not support conversion to YUV | ||
const isSafari = !!(window as any).safari; | ||
const options = { | ||
format: isSafari ? 'I420' : 'RGBA', | ||
}; | ||
|
||
const frame = frameWithPts.frame; | ||
const buffer = new Uint8ClampedArray(frame.allocationSize(options as VideoFrameCopyToOptions)); | ||
await frame.copyTo(buffer, options as VideoFrameCopyToOptions); | ||
|
||
return { | ||
resolution: { | ||
width: frame.displayWidth, | ||
height: frame.displayHeight, | ||
}, | ||
format: isSafari ? FrameFormat.YUV_BYTES : FrameFormat.RGBA_BYTES, | ||
data: buffer, | ||
}; | ||
} |
This file contains 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
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.
misleading name, I'm not sure what this function does, but it is definitely a lot more than geting a value
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.
Discussed offline