Skip to content

Commit

Permalink
Merge pull request #48 from ant-media/fix/issue-6205
Browse files Browse the repository at this point in the history
Fix conference issues
  • Loading branch information
mustafaboleken committed Apr 21, 2024
2 parents 638e10f + 30c6eb5 commit a630619
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 62 deletions.
45 changes: 15 additions & 30 deletions example/SampleProject/lib/conference.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ class _ConferenceState extends State<Conference> {
}),

//onAddRemoteStream
((stream) {}),
((stream) {
setState(() {});
}),

// onDataChannel
(dc) {},
Expand All @@ -125,38 +127,21 @@ class _ConferenceState extends State<Conference> {

//onUpdateConferenceUser
(streams) async {
print("onUpdateConferenceUser: ${streams.length}");
List<Widget> widgetlist = [];
Map<String, MediaStream> mediaStreams = {};
for (final track in streams[(streams.length) - 1].getTracks()) {
var incomingTrackID = track.id?.substring("ARDAMSx".length);
if (incomingTrackID == widget.roomId ||
incomingTrackID == widget.id) {
continue;
}
print("incomingTrackID: $incomingTrackID");
if (mediaStreams.containsKey(incomingTrackID)) {
mediaStreams[incomingTrackID]?.addTrack(track);
} else {
MediaStream newStream =
await createLocalMediaStream(incomingTrackID!);
newStream.addTrack(track);
mediaStreams[incomingTrackID] = newStream;
}
}

for (MapEntry<String, MediaStream> mediaStream
in mediaStreams.entries) {
SizedBox widget = SizedBox(
child: PlayWidget(
roomMediaStream: mediaStream.value,
roomId: this.widget.roomId),
);
widgetlist.add(widget);
}
streams.getVideoTracks().forEach((element) async {
MediaStream localStream =
await createLocalMediaStream("${element.hashCode}");
localStream.addTrack(element);

setState(() {
widgets = widgetlist;
widgetlist.add(PlayWidget(
roomMediaStream: localStream,
roomId: widget.roomId,
));

setState(() {
widgets = widgetlist;
});
});
},

Expand Down
24 changes: 20 additions & 4 deletions example/SampleProject/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ packages:
dependency: transitive
description:
name: dart_webrtc
sha256: "5897a3bdd6c7fded07e80e250260ca4c9cd61f9080911aa308b516e1206745a9"
sha256: "5cbc40bd9b33d0c9b8004cff52e9883c71f0f54799afc8faca77535eeb9ef857"
url: "https://pub.dev"
source: hosted
version: "1.1.3"
version: "1.2.1"
fake_async:
dependency: transitive
description:
Expand Down Expand Up @@ -123,10 +123,10 @@ packages:
dependency: "direct main"
description:
name: flutter_webrtc
sha256: "577216727181cb13776a65d3e7cb33e783e740c5496335011aed4a038b28c3fe"
sha256: "2f17fb96e0c9c6ff75f6b1c36d94755461fc7f36a5c28386f5ee5a18b98688c8"
url: "https://pub.dev"
source: hosted
version: "0.9.47"
version: "0.9.48+hotfix.1"
get:
dependency: "direct main"
description:
Expand Down Expand Up @@ -191,6 +191,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.1"
logging:
dependency: transitive
description:
name: logging
sha256: "623a88c9594aa774443aa3eb2d41807a48486b5613e67599fb4c41c0ad47c340"
url: "https://pub.dev"
source: hosted
version: "1.2.0"
matcher:
dependency: transitive
description:
Expand Down Expand Up @@ -460,6 +468,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.2.0"
synchronized:
dependency: transitive
description:
name: synchronized
sha256: "539ef412b170d65ecdafd780f924e5be3f60032a1128df156adad6c5b373d558"
url: "https://pub.dev"
source: hosted
version: "3.1.0+1"
term_glyph:
dependency: transitive
description:
Expand Down
76 changes: 49 additions & 27 deletions lib/src/helpers/helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import '../utils/websocket.dart'
// AntHelper is interface to the Flutter SDK of Ant Media Server
class AntHelper extends Object {
MediaStream? _localStream;
List<RTCRtpSender> _senders = <RTCRtpSender>[];
List<MediaStream> _remoteStreams = [];
HelperStateCallback onStateChange;
StreamStateCallback onLocalStream;
Expand Down Expand Up @@ -55,7 +56,7 @@ class AntHelper extends Object {
this.iceServers,
this.callbacks) {
final Map<String, dynamic> config = {
"sdpSemantics": "plan-b",
"sdpSemantics": "unified-plan",
'iceServers': iceServers,
'mandatory': {},
'optional': [
Expand All @@ -72,7 +73,7 @@ class AntHelper extends Object {
var _peerConnections = new Map<String, RTCPeerConnection>();
RTCDataChannel? _dataChannel;
var _remoteCandidates = [];
var _currentStreams = [];
Map<String, MediaStream> mediaStreamList = {};

final Map<String, dynamic> _constraints = {
'mandatory': {
Expand Down Expand Up @@ -288,8 +289,36 @@ class AntHelper extends Object {

case 'error':
{
print(mapData['definition']);
onStateChange(HelperState.ConnectionError);
if (mapData['definition'] == 'no_stream_exist') {
if (_type == AntMediaType.Conference) {
Timer(Duration(seconds: 5), () {
play(
_roomId,
"",
_roomId,
[],
"",
"",
"");
});
} else if (_type == AntMediaType.Play) {
Timer(Duration(seconds: 5), () {
play(
_streamId,
"",
_roomId,
[],
"",
"",
"");
}
);
}
return;
} else {
print(mapData['definition']);
onStateChange(HelperState.ConnectionError);
}
}
break;

Expand All @@ -299,7 +328,9 @@ class AntHelper extends Object {

if (mapData['definition'] == 'play_finished' &&
_type == AntMediaType.Conference) {
play(_roomId, _token, _roomId, [], "", "", "");
Timer(Duration(seconds: 5), () {
play(_roomId, "", _roomId, [], "", "", "");
});
return;
}

Expand Down Expand Up @@ -455,7 +486,11 @@ class AntHelper extends Object {
_type == AntMediaType.Default ||
_type == AntMediaType.Conference &&
_type != AntMediaType.DataChannelOnly) {
if (media != 'data' && _localStream != null) pc.addStream(_localStream!);
if (media != 'data' && _localStream != null) {
_localStream!.getTracks().forEach((track) async {
_senders.add(await pc.addTrack(track, _localStream!));
});
} //pc.addStream(_localStream!);
}

pc.onIceCandidate = (candidate) {
Expand All @@ -478,29 +513,13 @@ class AntHelper extends Object {
}
};

pc.onTrack = (event) {
this.onupdateConferencePerson(_currentStreams);
pc.onTrack = (event) async {
this.onupdateConferencePerson(event.streams[0]);
this.onAddRemoteStream(event.streams[0]);
};

pc.onRemoveTrack = (stream, track) {
this.onupdateConferencePerson(_currentStreams);
};

pc.onAddStream = (stream) {
if (_type == AntMediaType.Conference) {
_currentStreams.add(stream);
this.onupdateConferencePerson(_currentStreams);
}
this.onAddRemoteStream(stream);
_remoteStreams.add(stream);
};

pc.onRemoveStream = (stream) {
this.onRemoveRemoteStream(stream);
this.onupdateConferencePerson(_currentStreams);
_remoteStreams.removeWhere((it) {
return (it.id == stream.id);
});
this.onupdateConferencePerson(stream);
};

pc.onDataChannel = (channel) {
Expand All @@ -511,7 +530,9 @@ class AntHelper extends Object {
_type == AntMediaType.Peer ||
_type == AntMediaType.Conference &&
_type != AntMediaType.DataChannelOnly) {
pc.addStream(_localStream!);
_localStream!.getTracks().forEach((track) {
pc.addTrack(track, _localStream!);
});
}

return pc;
Expand Down Expand Up @@ -589,6 +610,7 @@ class AntHelper extends Object {
if (dc != null) {
dc.close();
}
_senders.clear();
this.onStateChange(HelperState.CallStateBye);
}

Expand Down
8 changes: 8 additions & 0 deletions lib/src/utils/conference_widget/playwidget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class _PlayWidgetState extends State<PlayWidget> {
print('initRenderer');
print(widget.roomMediaStream);
_remoteRenderer.srcObject = widget.roomMediaStream;
setState(() {});
}

@override
Expand All @@ -54,6 +55,13 @@ class _PlayWidgetState extends State<PlayWidget> {
child: RTCVideoView(
_remoteRenderer,
objectFit: RTCVideoViewObjectFit.RTCVideoViewObjectFitCover,
placeholderBuilder: (BuildContext context) {
return Container(
child: Center(
child: CircularProgressIndicator(),
),
);
}
),
));
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ dependencies:

get: ^4.6.6
collection: ^1.18.0
flutter_webrtc: ^0.9.47
flutter_webrtc: ^0.9.48+hotfix.1
permission_handler: ^11.1.0
shared_preferences: ^2.2.2
http: ^1.1.2
Expand Down

0 comments on commit a630619

Please sign in to comment.