forked from livekit/react-native-webrtc
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVideoTrackAdapter.java
More file actions
218 lines (180 loc) · 7.33 KB
/
VideoTrackAdapter.java
File metadata and controls
218 lines (180 loc) · 7.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package com.oney.WebRTCModule;
import android.util.*;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.WritableMap;
import org.webrtc.VideoFrame;
import org.webrtc.VideoSink;
import org.webrtc.VideoTrack;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Implements mute/unmute events for remote video tracks.
* Mute event is fired when there are no frames to be render for 1500ms
* initially and 500ms after the first frame was received.
*/
public class VideoTrackAdapter {
static final String TAG = VideoTrackAdapter.class.getCanonicalName();
static final long INITIAL_MUTE_DELAY = 3000;
static final long MUTE_DELAY = 1500;
private Map<String, TrackMuteUnmuteImpl> muteImplMap = new HashMap<>();
private Map<String, VideoDimensionDetectorImpl> dimensionDetectorMap = new HashMap<>();
private Timer timer = new Timer("VideoTrackMutedTimer");
private final int peerConnectionId;
private final WebRTCModule webRTCModule;
public VideoTrackAdapter(WebRTCModule webRTCModule, int peerConnectionId) {
this.peerConnectionId = peerConnectionId;
this.webRTCModule = webRTCModule;
}
public void addAdapter(VideoTrack videoTrack) {
String trackId = videoTrack.id();
if (muteImplMap.containsKey(trackId)) {
Log.w(TAG, "Attempted to add adapter twice for track ID: " + trackId);
return;
}
TrackMuteUnmuteImpl onMuteImpl = new TrackMuteUnmuteImpl(trackId);
Log.d(TAG, "Created adapter for " + trackId);
muteImplMap.put(trackId, onMuteImpl);
videoTrack.addSink(onMuteImpl);
onMuteImpl.start();
}
public void removeAdapter(VideoTrack videoTrack) {
String trackId = videoTrack.id();
TrackMuteUnmuteImpl onMuteImpl = muteImplMap.remove(trackId);
if (onMuteImpl == null) {
Log.w(TAG, "removeAdapter - no adapter for " + trackId);
return;
}
videoTrack.removeSink(onMuteImpl);
onMuteImpl.dispose();
Log.d(TAG, "Deleted adapter for " + trackId);
}
public void addDimensionDetector(VideoTrack videoTrack) {
String trackId = videoTrack.id();
if (dimensionDetectorMap.containsKey(trackId)) {
Log.w(TAG, "Attempted to add dimension detector twice for track ID: " + trackId);
return;
}
VideoDimensionDetectorImpl dimensionDetector = new VideoDimensionDetectorImpl(trackId);
Log.d(TAG, "Created dimension detector for " + trackId);
dimensionDetectorMap.put(trackId, dimensionDetector);
videoTrack.addSink(dimensionDetector);
}
public void removeDimensionDetector(VideoTrack videoTrack) {
String trackId = videoTrack.id();
VideoDimensionDetectorImpl dimensionDetector = dimensionDetectorMap.remove(trackId);
if (dimensionDetector == null) {
Log.w(TAG, "removeDimensionDetector - no detector for " + trackId);
return;
}
videoTrack.removeSink(dimensionDetector);
dimensionDetector.dispose();
Log.d(TAG, "Deleted dimension detector for " + trackId);
}
/**
* Implements 'mute'/'unmute' events for remote video tracks through
* the {@link VideoSink} interface.
*/
private class TrackMuteUnmuteImpl implements VideoSink {
private TimerTask emitMuteTask;
private volatile boolean disposed;
private AtomicInteger frameCounter;
private boolean mutedState;
private final String trackId;
TrackMuteUnmuteImpl(String trackId) {
this.trackId = trackId;
this.frameCounter = new AtomicInteger();
}
@Override
public void onFrame(VideoFrame frame) {
frameCounter.addAndGet(1);
}
private void start() {
if (disposed) {
return;
}
synchronized (this) {
if (emitMuteTask != null) {
emitMuteTask.cancel();
}
emitMuteTask = new TimerTask() {
private int lastFrameNumber = frameCounter.get();
@Override
public void run() {
if (disposed) {
return;
}
boolean isMuted = lastFrameNumber == frameCounter.get();
if (isMuted != mutedState) {
mutedState = isMuted;
emitMuteEvent(isMuted);
}
lastFrameNumber = frameCounter.get();
}
};
timer.schedule(emitMuteTask, INITIAL_MUTE_DELAY, MUTE_DELAY);
}
}
private void emitMuteEvent(boolean muted) {
WritableMap params = Arguments.createMap();
params.putInt("pcId", peerConnectionId);
params.putString("trackId", trackId);
params.putBoolean("muted", muted);
Log.d(TAG, (muted ? "Mute" : "Unmute") + " event pcId: " + peerConnectionId + " trackId: " + trackId);
VideoTrackAdapter.this.webRTCModule.sendEvent("mediaStreamTrackMuteChanged", params);
}
void dispose() {
disposed = true;
synchronized (this) {
if (emitMuteTask != null) {
emitMuteTask.cancel();
emitMuteTask = null;
}
}
}
}
/**
* Implements dimension change events for remote video tracks through
* the {@link VideoSink} interface.
*/
private class VideoDimensionDetectorImpl implements VideoSink {
private volatile boolean disposed;
private int currentWidth = 0;
private int currentHeight = 0;
private boolean hasInitialSize = false;
private final String trackId;
VideoDimensionDetectorImpl(String trackId) {
this.trackId = trackId;
}
@Override
public void onFrame(VideoFrame frame) {
if (disposed) {
return;
}
int width = frame.getBuffer().getWidth();
int height = frame.getBuffer().getHeight();
// Check if this is a meaningful size change
if (!hasInitialSize) {
currentWidth = width;
currentHeight = height;
hasInitialSize = true;
emitDimensionChangeEvent(width, height);
} else if (currentWidth != width || currentHeight != height) {
currentWidth = width;
currentHeight = height;
emitDimensionChangeEvent(width, height);
}
}
private void emitDimensionChangeEvent(int width, int height) {
WritableMap params = Arguments.createMap();
params.putInt("pcId", peerConnectionId);
params.putString("trackId", trackId);
params.putInt("width", width);
params.putInt("height", height);
Log.d(TAG, "Dimension change event pcId: " + peerConnectionId + " trackId: " + trackId + " dimensions: " + width + "x" + height);
VideoTrackAdapter.this.webRTCModule.sendEvent("videoTrackDimensionChanged", params);
}
void dispose() {
disposed = true;
}
}
}