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 5ac2157
Browse filesBrowse the repository at this point in the historyBrowse files
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