8000 Merge pull request #2 from rostopira/android_update_webrtc · flutter-webrtc/flutter-webrtc@a5b28f1 · GitHub
[go: up one dir, main page]

Skip to content

Commit a5b28f1

Browse files
authored
Merge pull request #2 from rostopira/android_update_webrtc
Android update webrtc
2 parents 63d378f + d2245d0 commit a5b28f1

11 files changed

+182
-647
lines changed

android/build.gradle

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ buildscript {
88
}
99

1010
dependencies {
11-
classpath 'com.android.tools.build:gradle:3.0.1'
11+
classpath 'com.android.tools.build:gradle:3.2.1'
1212
}
1313
}
1414

@@ -22,23 +22,24 @@ rootProject.allprojects {
2222
apply plugin: 'com.android.library'
2323

2424
android {
25-
compileSdkVersion 23
25+
compileSdkVersion 28
2626

2727
defaultConfig {
28-
minSdkVersion 21
28+
minSdkVersion 17
2929
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
30-
ndk {
31-
abiFilters "armeabi-v7a", "x86"
32-
}
3330
}
31+
3432
lintOptions {
3533
disable 'InvalidPackage'
3634
}
35+
36+
compileOptions {
37+
sourceCompatibility JavaVersion.VERSION_1_8
38+
targetCompatibility JavaVersion.VERSION_1_8
39+
}
3740
}
3841

3942
dependencies {
40-
implementation fileTree(dir: 'libs', include: ['*.jar'])
41-
implementation "com.android.support:appcompat-v7:23.0.1"
42-
implementation "com.android.support:support-v4:23.0.1"
43+
api 'org.webrtc:google-webrtc:1.0.25821'
4344
implementation "com.android.support:support-annotations:22.0.0"
4445
}
-15.6 MB
Binary file not shown.

android/libs/libwebrtc.jar

-730 KB
Binary file not shown.

android/src/main/java/com/cloudwebrtc/webrtc/FlutterRTCVideoRenderer.java

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,14 @@
1212
import org.webrtc.EglBase;
1313
import org.webrtc.MediaStream;
1414
import org.webrtc.RendererCommon.RendererEvents;
15-
import org.webrtc.VideoRenderer;
1615
import org.webrtc.VideoTrack;
1716

1817
import io.flutter.plugin.common.EventChannel;
1918

20-
public class FlutterRTCVideoRenderer implements EventChannel.StreamHandler {
19+
public class FlutterRTCVideoRenderer implements EventChannel.StreamHandler {
2120

2221
private static final String TAG = FlutterWebRTCPlugin.TAG;
2322
private final SurfaceTexture texture;
24-
private final Context context;
2523
private int id = -1;
2624

2725
public void Dispose(){
@@ -90,12 +88,6 @@ public void onFrameResolutionChanged(
9088

9189
private SurfaceTextureRenderer surfaceTextureRenderer;
9290

93-
/**
94-
* The {@code VideoRenderer}, if any, which renders {@link #videoTrack} on
95-
* this {@code View}.
96-
*/
97-
private VideoRenderer videoRenderer;
98-
9991
/**
10092
* The {@code VideoTrack}, if any, rendered by this {@code FlutterRTCVideoRenderer}.
10193
*/
@@ -104,10 +96,12 @@ public void onFrameResolutionChanged(
10496
EventChannel eventChannel;
10597
EventChannel.EventSink eventSink;
10698

107-
public FlutterRTCVideoRenderer(SurfaceTexture texture, Context context) {
108-
this.surfaceTextureRenderer = new SurfaceTextureRenderer(context, texture);
99+
public FlutterRTCVideoRenderer(SurfaceTexture texture) {
100+
this.surfaceTextureRenderer = new SurfaceTextureRenderer("");
101+
surfaceTextureRenderer.init(EglUtils.getRootEglBaseContext(), rendererEvents);
102+
surfaceTextureRenderer.surfaceCreated(texture);
103+
109104
this.texture = texture;
110-
this.context = context;
111105
this.eventSink = null;
112106
}
113107

@@ -134,11 +128,7 @@ public void onCancel(Object o) {
134128
* resources (if rendering is in progress).
135129
*/
136130
private void removeRendererFromVideoTrack() {
137-
if (videoRenderer != null) {
138-
videoTrack.removeRenderer(videoRenderer);
139-
videoRenderer.dispose();
140-
videoRenderer = null;
141-
}
131+
videoTrack.removeSink(surfaceTextureRenderer);
142132
}
143133

144134
/**
@@ -181,8 +171,8 @@ private void setVideoTrack(VideoTrack videoTrack) {
181171

182172
if (videoTrack != null) {
183173
tryAddRendererToVideoTrack();
184-
}else{
185-
174+
} else {
175+
Log.w(TAG, "VideoTrack is null");
186176
}
187177
}
188178
}
@@ -192,8 +182,7 @@ private void setVideoTrack(VideoTrack videoTrack) {
192182
* all preconditions for the start of rendering are met.
193183
*/
194184
private void tryAddRendererToVideoTrack() {
195-
if (videoRenderer == null
196-
&& videoTrack != null) {
185+
if (videoTrack != null) {
197186
EglBase.Context sharedContext = EglUtils.getRootEglBaseContext();
198187

199188
if (sharedContext == null) {
@@ -204,11 +193,11 @@ private void tryAddRendererToVideoTrack() {
204193
}
205194

206195
surfaceTextureRenderer.release();
207-
surfaceTextureRenderer = new SurfaceTextureRenderer(context, texture);
208196
surfaceTextureRenderer.init(sharedContext, rendererEvents);
197+
surfaceTextureRenderer.surfaceCreated(texture);
209198

210-
videoRenderer = new VideoRenderer(surfaceTextureRenderer);
211-
videoTrack.addRenderer(videoRenderer);
199+
videoTrack.addSink(surfaceTextureRenderer);
212200
}
213201
}
202+
214203
}

android/src/main/java/com/cloudwebrtc/webrtc/FlutterWebRTCPlugin.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import java.util.*;
1616

1717
import org.webrtc.AudioTrack;
18+
import org.webrtc.DefaultVideoDecoderFactory;
19+
import org.webrtc.DefaultVideoEncoderFactory;
1820
import org.webrtc.EglBase;
1921
import org.webrtc.IceCandidate;
2022
import org.webrtc.Logging;
@@ -26,6 +28,8 @@
2628
import org.webrtc.SdpObserver;
2729
import org.webrtc.SessionDescription;
2830
import org.webrtc.VideoTrack;
31+
import org.webrtc.audio.AudioDeviceModule;
32+
import org.webrtc.audio.JavaAudioDeviceModule;
2933

3034
import io.flutter.plugin.common.EventChannel;
3135
import io.flutter.plugin.common.MethodChannel;
@@ -90,16 +94,24 @@ private FlutterWebRTCPlugin(Registrar registrar, MethodChannel channel) {
9094

9195
PeerConnectionFactory.initialize(
9296
PeerConnectionFactory.InitializationOptions.builder(registrar.context())
93-
.setEnableInternalTracer(false)
94-
.setEnableVideoHwAcceleration(true)
97+
.setEnableInternalTracer(true)
9598
.createInitializationOptions());
9699

97-
mFactory = new PeerConnectionFactory(null);
100+
final AudioDeviceModule audioDeviceModule = JavaAudioDeviceModule.builder(registrar.context())
101+
.setUseHardwareAcousticEchoCanceler(true)
102+
.setUseHardwareNoiseSuppressor(true)
103+
.createAudioDeviceModule();
104+
98105
// Initialize EGL contexts required for HW acceleration.
99106
EglBase.Context eglContext = EglUtils.getRootEglBaseContext();
100-
if (eglContext != null) {
101-
mFactory.setVideoHwAccelerationOptions(eglContext, eglContext);
102-
}
107+
108+
mFactory = PeerConnectionFactory.builder()
109+
.setOptions(new PeerConnectionFactory.Options())
110+
.setVideoEncoderFactory(new DefaultVideoEncoderFactory(eglContext, true, true))
111+
.setVideoDecoderFactory(new DefaultVideoDecoderFactory(eglContext))
112+
.setAudioDeviceModule(audioDeviceModule)
113+
.createPeerConnectionFactory();
114 F438 +
103115
getUserMediaImpl = new GetUserMediaImpl(this, registrar.context());
104116
}
105117

@@ -219,7 +231,7 @@ public void onMethodCall(MethodCall call, Result result) {
219231
} else if (call.method.equals("createVideoRenderer")) {
220232
TextureRegistry.SurfaceTextureEntry entry = textures.createSurfaceTexture();
221233
SurfaceTexture surfaceTexture = entry.surfaceTexture();
222-
FlutterRTCVideoRenderer render = new FlutterRTCVideoRenderer(surfaceTexture, getContext());
234+
FlutterRTCVideoRenderer render = new FlutterRTCVideoRenderer(surfaceTexture);
223235
renders.put(entry.id(), render);
224236

225237
EventChannel eventChannel =
@@ -1042,4 +1054,5 @@ public void dataChannelClose(String peerConnectionId, int dataChannelId) {
10421054
pco.dataChannelClose(dataChannelId);
10431055
}
10441056
}
1057+
10451058
}

android/src/main/java/com/cloudwebrtc/webrtc/GetUserMediaImpl.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.cloudwebrtc.webrtc.utils.Callback;
2121
import com.cloudwebrtc.webrtc.utils.ConstraintsArray;
2222
import com.cloudwebrtc.webrtc.utils.ConstraintsMap;
23+
import com.cloudwebrtc.webrtc.utils.EglUtils;
2324
import com.cloudwebrtc.webrtc.utils.ObjectType;
2425
import com.cloudwebrtc.webrtc.utils.PermissionUtils;
2526

@@ -365,7 +366,7 @@ public void onStop() {
365366
if (videoCapturer != null) {
366367

367368
PeerConnectionFactory pcFactory = plugin.mFactory;
368-
VideoSource videoSource = pcFactory.createVideoSource< 10000 /span>(videoCapturer);
369+
VideoSource videoSource = pcFactory.createVideoSource(true);
369370

370371
// Fall back to defaults if keys are missing.
371372
int width
@@ -426,7 +427,7 @@ public void onStop() {
426427
}
427428
}
428429

429-
String streamId = mediaStream.label();
430+
String streamId = mediaStream.getId();
430431

431432
Log.d(TAG, "MediaStream id: " + streamId);
432433
plugin.localStreams.put(streamId, mediaStream);
@@ -580,7 +581,7 @@ private void getUserMedia(
580581
}
581582
}
582583

583-
String streamId = mediaStream.label();
584+
String streamId = mediaStream.getId();
584585

585586
Log.d(TAG, "MediaStream id: " + streamId);
586587
plugin.localStreams.put(streamId, mediaStream);
@@ -636,7 +637,10 @@ private VideoTrack getUserVideo(ConstraintsMap constraints) {
636637
}
637638

638639
PeerConnectionFactory pcFactory = plugin.mFactory;
639-
VideoSource videoSource = pcFactory.createVideoSource(videoCapturer);
640+
VideoSource videoSource = pcFactory.createVideoSource(false);
641+
String threadName = Thread.currentThread().getName();
642+
SurfaceTextureHelper surfaceTextureHelper = SurfaceTextureHelper.create(threadName, EglUtils.getRootEglBaseContext());
643+
videoCapturer.initialize(surfaceTextureHelper, context, videoSource.getCapturerObserver());
640644

641645
// Fall back to defaults if keys are missing.
642646
int width

android/src/main/java/com/cloudwebrtc/webrtc/PeerConnectionObserver.java

Lines changed: 30 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import java.io.UnsupportedEncodingException;
44
import java.lang.ref.SoftReference;
55
import java.nio.ByteBuffer;
6+
import java.util.ArrayList;
67
import java.util.HashMap;
78
import java.util.Iterator;
9+
import java.util.List;
810
import java.util.Map;
911

1012
import android.util.Base64;
@@ -276,7 +278,7 @@ private String getUIDForStream(MediaStream mediaStream) {
276278
@Override
277279
public void onAddStream(MediaStream mediaStream) {
278280
String streamUID = null;
279-
String streamId = mediaStream.label();
281+
String streamId = mediaStream.getId();
280282
// The native WebRTC implementation has a special concept of a default
281283
// MediaStream instance with the label default that the implementation
282284
// reuses.
@@ -347,7 +349,7 @@ void sendEvent(ConstraintsMap event) {
347349
@Override
348350
public void onRemoveStream(MediaStream mediaStream) {
349351

350-
String streamId = mediaStream.label();
352+
String streamId = mediaStream.getId();
351353

352354
for (VideoTrack track : mediaStream.videoTracks) {
353355
this.remoteTracks.remove(track.id());
@@ -364,55 +366,36 @@ public void onRemoveStream(MediaStream mediaStream) {
364366
}
365367

366368
@Override
367-
public void onAddTrack(MediaStream mediaStream,MediaStreamTrack track){
369+
public void onAddTrack(RtpReceiver receiver, MediaStream[] mediaStreams){
368370
Log.d(TAG, "onAddTrack");
369371

370-
String streamId = mediaStream.label();
371-
372-
ConstraintsMap params = new ConstraintsMap();
373-
params.putString("event", "onAddTrack");
374-
params.putString("streamId", streamId);
375-
params.putString("trackId", track.id());
376-
377-
String trackId = track.id();
378-
ConstraintsMap trackInfo = new ConstraintsMap();
379-
trackInfo.putString("id", trackId);
380-
trackInfo.putString("label", track.kind());
381-
trackInfo.putString("kind", track.kind());
382-
trackInfo.putBoolean("enabled", track.enabled());
383-
trackInfo.putString("readyState", track.state().toString());
384-
trackInfo.putBoolean("remote", true);
385-
386-
params.putMap("track", trackInfo.toMap());
387-
388-
sendEvent(params);
389-
}
390-
391-
@Override
392-
public void onRemoveTrack(MediaStream mediaStream,MediaStreamTrack track){
393-
Log.d(TAG, "onRemoveTrack");
394-
String streamId = mediaStream.label();
395-
396-
ConstraintsMap params = new ConstraintsMap();
397-
params.putString("event", "onRemoveTrack");
398-
params.putString("streamId", streamId);
399-
params.putString("trackId", track.id());
400-
401-
String trackId = track.id();
402-
ConstraintsMap trackInfo = new ConstraintsMap();
403-
trackInfo.putString("id", trackId);
404-
trackInfo.putString("label", track.kind());
405-
trackInfo.putString("kind", track.kind());
406-
trackInfo.putBoolean("enabled", track.enabled());
407-
trackInfo.putString("readyState", track.state().toString());
408-
trackInfo.putBoolean("remote", true);
409-
410-
params.putMap("track", trackInfo.toMap());
411-
412-
sendEvent(params);
372+
for (MediaStream stream : mediaStreams) {
373+
String streamId = stream.getId();
374+
List<MediaStreamTrack> tracks = new ArrayList<>(stream.audioTracks);
375+
tracks.addAll(stream.videoTracks);
376+
377+
for (MediaStreamTrack track : tracks) {
378+
ConstraintsMap params = new ConstraintsMap();
379+
params.putString("event", "onAddTrack");
380+
params.putString("streamId", streamId);
381+
params.putString("trackId", track.id());
382+
383+
String trackId = track.id();
384+
ConstraintsMap trackInfo = new ConstraintsMap();
385+
trackInfo.putString("id", trackId);
386+
trackInfo.putString("label", track.kind());
387+
trackInfo.putString("kind", track.kind());
388+
trackInfo.putBoolean("enabled", track.enabled());
389+
trackInfo.putString("readyState", track.state().toString());
390+
trackInfo.putBoolean("remote", true);
391+
392+
params.putMap("track", trackInfo.toMap());
393+
394+
sendEvent(params);
395+
}
396+
}
413397
}
414398

415-
416399
9BCB @Override
417400
public void onDataChannel(DataChannel dataChannel) {
418401
// XXX Unfortunately, the Java WebRTC API doesn't expose the id
@@ -472,11 +455,6 @@ public void onSignalingChange(PeerConnection.SignalingState signalingState) {
472455
sendEvent(params);
473456
}
474457

475-
@Override
476-
public void onAddRtpReceiver(final RtpReceiver receiver, final MediaStream[] mediaStreams) {
477-
Log.d(TAG, "onAddRtpReceiver");
478-
}
479-
480458
@Nullable
481459
private String iceConnectionStateString(PeerConnection.IceConnectionState iceConnectionState) {
482460
switch (iceConnectionState) {

0 commit comments

Comments
 (0)
0