8000 set the specified MediaStream trackId (#1110) · nader-nagy/flutter-webrtc@98bf328 · GitHub
[go: up one dir, main page]

Skip to content

Commit 98bf328

Browse files
Beoyan-1佟博研
andauthored
set the specified MediaStream trackId (flutter-webrtc#1110)
* Set the specified MediaStream trackId * 回退代码并添加web端设置trackId Co-authored-by: 佟博研 <tongby@haoxin.cn>
1 parent 640dd79 commit 98bf328

File tree

5 files changed

+103
-1
lines changed

5 files changed

+103
-1
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,35 @@ public void setStream(MediaStream mediaStream) {
154154

155155
setVideoTrack(videoTrack);
156156
}
157+
/**
158+
* Sets the {@code MediaStream} to be rendered by this {@code FlutterRTCVideoRenderer}.
159+
* The implementation renders the first {@link VideoTrack}, if any, of the
160+
* specified trackId
161+
*
162+
* @param mediaStream The {@code MediaStream} to be rendered by this
163+
* {@code FlutterRTCVideoRenderer} or {@code null}.
164+
* @param trackId The {@code trackId} to be rendered by this
165+
* {@code FlutterRTCVideoRenderer} or {@code null}.
166+
*/
167+
public void setStream(MediaStream mediaStream,String trackId) {
168+
VideoTrack videoTrack;
169+
this.mediaStream = mediaStream;
170+
if (mediaStream == null) {
171+
videoTrack = null;
172+
} else {
173+
List<VideoTrack> videoTracks = mediaStream.videoTracks;
174+
175+
videoTrack = videoTracks.isEmpty() ? null : videoTracks.get(0);
176+
177+
for (VideoTrack track : videoTracks){
178+
if (track.id().equals(trackId)){
179+
videoTrack = track;
180+
}
181+
}
182+
}
183+
184+
setVideoTrack(videoTrack);
185+
}
157186

158187
/**
159188
* Sets the {@code VideoTrack} to be rendered by this {@code FlutterRTCVideoRenderer}.

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ public void onMethodCall(MethodCall call, @NonNull Result notSafeResult) {
434434
int textureId = call.argument("textureId");
435435
String streamId = call.argument("streamId");
436436
String ownerTag = call.argument("ownerTag");
437+
String trackId = call.argument("trackId");
437438
FlutterRTCVideoRenderer render = renders.get(textureId);
438439
if (render == null) {
439440
resultError("videoRendererSetSrcObject", "render [" + textureId + "] not found !", result);
@@ -445,7 +446,11 @@ public void onMethodCall(MethodCall call, @NonNull Result notSafeResult) {
445446
} else {
446447
stream = getStreamForId(streamId, ownerTag);
447448
}
448-
render.setStream(stream);
449+
if (!trackId.equals("0")){
450+
render.setStream(stream,trackId);
451+
}else {
452+
render.setStream(stream);
453+
}
449454
result.success(null);
450455
break;
451456
}

common/darwin/Classes/FlutterWebRTCPlugin.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult) result
562562
FlutterRTCVideoRenderer *render = self.renders[textureId];
563563
NSString *streamId = argsMap[@"streamId"];
564564
NSString *ownerTag = argsMap[@"ownerTag"];
565+
NSString *trackId = argsMap[@"trackId"];
565566
if(!render) {
566567
result([FlutterError errorWithCode:@"videoRendererSetSrcObject: render is nil" message:nil details:nil]);
567568
return;
@@ -577,6 +578,11 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult) result
577578
if(stream){
578579
NSArray *videoTracks = stream ? stream.videoTracks : nil;
579580
videoTrack = videoTracks && videoTracks.count ? videoTracks[0] : nil;
581+
for ( RTCVideoTrack * track in videoTracks) {
582+
if([track.trackId isEqualToString:trackId]){
583+
videoTrack = track;
584+
}
585+
}
580586
if (!videoTrack) {
581587
NSLog(@"Not found video track for RTCMediaStream: %@", streamId);
582588
}

lib/src/native/rtc_video_renderer_impl.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,22 @@ class RTCVideoRenderer extends ValueNotifier<RTCVideoValue>
6161
});
6262
}
6363

64< 8000 /td>+
setSrcObject({MediaStream? stream, String? trackId}) {
65+
if (textureId == null) throw 'Call initialize before setting the stream';
66+
67+
_srcObject = stream;
68+
WebRTC.invokeMethod('videoRendererSetSrcObject', <String, dynamic>{
69+
'textureId': textureId,
70+
'streamId': stream?.id ?? '',
71+
'ownerTag': stream?.ownerTag ?? '',
72+
'trackId': trackId ?? '0'
73+
}).then((_) {
74+
value = (stream == null)
75+
? RTCVideoValue.empty
76+
: value.copyWith(renderVideo: renderVideo);
77+
});
78+
}
79+
6480
@override
6581
Future<void> dispose() async {
6682
await _eventSubscription?.cancel();

lib/src/web/rtc_video_renderer_impl.dart

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,52 @@ class RTCVideoRenderer extends ValueNotifier<RTCVideoValue>
144144
value = value.copyWith(renderVideo: renderVideo);
145145
}
146146

147+
setSrcObject({MediaStream? stream, String? trackId}) {
148+
if (stream == null) {
149+
findHtmlView()?.srcObject = null;
150+
_audioElement?.srcObject = null;
151+
_srcObject = null;
152+
return;
153+
}
154+
155+
_srcObject = stream as MediaStreamWeb;
156+
157+
if (null != _srcObject) {
158+
if (stream.getVideoTracks().isNotEmpty) {
159+
_videoStream = html.MediaStream();
160+
for (final track in _srcObject!.jsStream.getVideoTracks()) {
161+
if (track.id == trackId) {
162+
_videoStream!.addTrack(track);
163+
}
164+
}
165+
}
166+
if (stream.getAudioTracks().isNotEmpty) {
167+
_audioStream = html.MediaStream();
168+
for (final track in _srcObject!.jsStream.getAudioTracks()) {
169+
_audioStream!.addTrack(track);
170+
}
171+
}
172+
} else {
173+
_videoStream = null;
174+
_audioStream = null;
175+
}
176+
177+
if (null != _audioStream) {
178+
if (null == _audioElement) {
179+
_audioElement = html.AudioElement()
180+
..id = _elementIdForAudio
181+
..muted = stream.ownerTag == 'local'
182+
..autoplay = true;
183+
_ensureAudioManagerDiv().append(_audioElement!);
184+
}
185+
_audioElement?.srcObject = _audioStream;
186+
}
187+
188+
findHtmlView()?.srcObject = _videoSt 5E1A ream;
189+
190+
value = value.copyWith(renderVideo: renderVideo);
191+
}
192+
147193
html.DivElement _ensureAudioManagerDiv() {
148194
var div = html.document.getElementById(_elementIdForAudioManager);
149195
if (null != div) return div as html.DivElement;

0 commit comments

Comments
 (0)
0