You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 9701913
Browse filesBrowse the repository at this point in the historyBrowse files
fix: make track sinks safe to change from any thread and callback (#517)
* fix: register track sinks atomically across threads
AudioTrack and VideoTrack kept their sinks in unguarded maps, so two threads adding the same sink made two native registrations, and an add could overlap dispose(). A shared SinkRegistry now guards the map with a lock that is never held across a native call, and dispose() waits for the native calls still running.
* fix: let sinks add and remove sinks from their callbacks
A sink that removed itself in onVideoFrame() blocked for good: WebRTC holds the broadcaster's lock while it delivers a frame, and RemoveSink() takes it again on the worker thread the caller waits for. The same held for adding a sink, for audio sinks of a remote track or a CustomAudioSource, and for voice activity listeners. A change made within a callback now takes effect for the Java side at once and is made natively on another thread after the callback returned; dispose() there fails with an IllegalStateException.
* feat: let video sinks share read-only frames
VideoTrackSink copied every frame for each sink, so four sinks of a 1080p30 track copied about 356 MiB a second. addSink(sink, VideoFrameAccess.SHARED) hands a sink the pixels the source handed out, without a copy, in read-only byte buffers; frame.copy() gives a writable one. COPY stays the default and keeps sinks from seeing each other's writes.
As with video tracks, sinks may be added and removed from any thread, and also from within `onData`, the sink's own included. Only `dispose()` must not be called from within a callback.
208
+
207
209
## Cleanup
208
210
209
211
When you're done with the custom audio source, make sure to clean up resources:
`addSink` and `removeSink` may be called from any thread, also at the same time, and from within `onVideoFrame`, so a sink can remove itself once it has seen what it wanted:
168
+
169
+
```java
170
+
videoTrack.addSink(newVideoTrackSink() {
171
+
@Override
172
+
publicvoidonVideoFrame(VideoFrameframe) {
173
+
saveSnapshot(frame);
174
+
frame.release();
175
+
176
+
// Gets no further frames.
177
+
videoTrack.removeSink(this);
178
+
}
179
+
});
180
+
```
181
+
182
+
WebRTC holds a lock while it hands a frame to the sinks, and adding or removing a sink takes the same lock. A change made from within a callback therefore takes effect for the sinks at once, while the native sink is added or removed on another thread right after the callback returned. Disposing the track is the one thing a callback cannot do: `dispose()` throws an `IllegalStateException` there, and belongs on another thread.
183
+
184
+
### Sharing Frames Between Sinks
185
+
186
+
By default each sink gets an I420 copy of each frame of its own, which it may write into without the other sinks or the encoder seeing it. That costs a full copy of the pixels per frame and sink: at 1080p and 30 frames a second about 89 MiB a second, for each sink. A sink that only reads the frames can share them instead:
A shared sink gets the pixels the source handed out, without a copy, which the other shared sinks and the encoder use as well. The planes of its `NativeI420Buffer` are read-only, so a write throws a `ReadOnlyBufferException`; a sink that wants to change a frame takes a writable copy with `frame.copy()`. The frames may come from a pool of the decoder or capturer, so a shared sink should not hold on to them for long: keep a copy instead, and release the frame.
195
+
165
196
### Receiving Remote Video Frames
166
197
167
198
To receive frames from a remote peer, you need to add a sink to the remote video track when it's received. This is typically done in the `onTrack` method of your `PeerConnectionObserver`:
0 commit comments