How to join to a video room as subscriber
muhammadCodeSh opened this issue · 1 comments
This is not an issue but I tried to jion to a room as subscriber but I could not .
Please send me a example code.
This is the code to jion as publisher please convert it to jion as subscriber.
`subscribeTo(List<Map<String, dynamic>> sources) async {
if (sources.length == 0) return;
var streams = (sources)
.map((e) => PublisherStream(mid: e['mid'], feed: e['feed']))
.toList();
if (remoteHandle != null) {
await remoteHandle?.subscribeToStreams(streams);
return;
}
remoteHandle = await session.attach<JanusVideoRoomPlugin>();
print(sources);
var start = await remoteHandle?.joinSubscriber(myRoom, streams: streams);
remoteHandle?.typedMessages?.listen((event) async {
Object data = event.event.plugindata?.data;
print('listened');
if (data is VideoRoomAttachedEvent) {
print('Attached event');
data.streams?.forEach((element) {
if (element.mid != null && element.feedId != null) {
subStreams[element.mid!]['id'] = element.feedId!;
}
// to avoid duplicate subscriptions
if (subscriptions[element.feedId] == null)
subscriptions[element.feedId] = {};
subscriptions[element.feedId][element.mid] = true;
});
print('substreams');
print(subStreams);
}
if (event.jsep != null) {
await remoteHandle?.handleRemoteJsep(event.jsep);
await start!();
}
}, onError: (error, trace) {
if (error is JanusError) {
print(error.toMap());
}
});
remoteHandle?.remoteTrack?.listen((event) async {
print('listened2');
String mid = event.mid!;
if (subStreams[mid]['id'] != null) {
int feedId = subStreams[mid]['id']!;
if (!remoteStreams.containsKey(feedId)) {
RemoteStream temp = RemoteStream(feedId.toString());
await temp.init();
setState(() {
remoteStreams.putIfAbsent(feedId, () => temp);
});
}
if (event.track != null && event.flowing == true) {
remoteStreams[feedId]?.video.addTrack(event.track!);
remoteStreams[feedId]?.videoRenderer.srcObject =
remoteStreams[feedId]?.video;
if (kIsWeb) {
remoteStreams[feedId]?.videoRenderer.muted = false;
}
}
}
});
return;
}
Future joinRoom() async {
var devices = await wbrtc.navigator.mediaDevices.enumerateDevices();
Map<String, dynamic> constrains = {};
print(devices.first.label);
print(devices.first.groupId);
print("device id"+devices.first.deviceId);
print(devices.first.kind);
devices.map((e) => e.kind.toString()).forEach((element) {
String dat = element.split('input')[0];
dat = dat.split('output')[0];
constrains.putIfAbsent(dat, () => true);
});
myStream =
await plugin.initializeMediaDevices(mediaConstraints: constrains);
RemoteStream mystr = RemoteStream('0');
await mystr.init();
mystr.videoRenderer.srcObject = myStream;
setState(() {
remoteStreams.putIfAbsent(0, () => mystr);
});
await plugin.joinPublisher(myRoom, displayName: displayname);
plugin.typedMessages?.listen((event) async {
Object data = event.event.plugindata?.data;
print('------------------ listen ------------------');
print(feedStreams.entries.map((e) => e.value).toList().length);
print(subscriptions.entries.map((e) => e.value).toList().length);
print(feeds.entries.map((e) => e.value).toList().length);
print(subStreams.entries.map((e) => e.value).toList().length);
for(int i=0;i<subStreams.entries.map((e) => e.value).toList().length;i++){
print(subStreams.entries.map((e) => e.value).toList()[i]);
}
print( mediaStreams.entries.map((e) => e.value).toList().length);
print('------------------------------------');
if (data is VideoRoomJoinedEvent) {
print('------------------1------------------');
(await plugin.publishMedia(bitrate: 3000000));
List<Map<String, dynamic>> publisherStreams = [];
for (Publishers publisher in data.publishers ?? []) {
for (Streams stream in publisher.streams ?? []) {
feedStreams[publisher.id!] = {
"id": publisher.id,
"display": publisher.display,
"streams": publisher.streams,
};
publisherStreams.add({"feed": publisher.id, ...stream.toJson()});
if (publisher.id != null && stream.mid != null) {
subStreams[stream.mid!] = {
"id": publisher.id,
"display": publisher.display,
"streams": publisher.streams,
"name":displayname,
"img":imageAvatar,
"userId":userId
};
print("substreams is:");
print(subStreams);
}
}
}
subscribeTo(publisherStreams);
}
if (data is VideoRoomNewPublisherEvent) {
print('------------------2------------------');
List<Map<String, dynamic>> publisherStreams = [];
for (Publishers publisher in data.publishers ?? []) {
feedStreams[publisher.id!] = {
"id": publisher.id,
"display": publisher.display,
"streams": publisher.streams
};
for (Streams stream in publisher.streams ?? []) {
publisherStreams.add({"feed": publisher.id, ...stream.toJson()});
if (publisher.id != null && stream.mid != null) {
subStreams[stream.mid!] = publisher.id!;
print("substreams is:");
print(subStreams);
}
}
}
print('got new publishers');
print(publisherStreams);
subscribeTo(publisherStreams);
}
if (data is VideoRoomLeavingEvent) {
print('publisher is leaving');
print('------------------3------------------');
print(data.leaving);
unSubscribeStream(data.leaving!);
}
if (data is VideoRoomConfigured) {
print('------------------4------------------');
print('typed event with jsep' + event.jsep.toString());
await plugin.handleRemoteJsep(event.jsep);
}
else{
print('------------------${data.runtimeType}------------------');
print('------------------4------------------');
}
}, onError: (error, trace) {
if (error is JanusError) {
print(error.toMap());
}
});
}
Kindly ask this question in the discussion tab with proper formatting.