merge-update-with-lab-changes
haroon amjad 4 years ago
parent e3b7069aa2
commit 39190681c6

@ -168,8 +168,8 @@ class _MyApp extends State<MyApp> {
// ), // ),
// ), // ),
// ), // ),
// initialRoute: SPLASH, initialRoute: SPLASH,
initialRoute: CALL_PAGE, // initialRoute: CALL_PAGE,
// initialRoute: OPENTOK_CALL_PAGE, // initialRoute: OPENTOK_CALL_PAGE,
// initialRoute: PACKAGES_OFFERS, // initialRoute: PACKAGES_OFFERS,
// initialRoute: PACKAGES_ORDER_COMPLETED, // initialRoute: PACKAGES_ORDER_COMPLETED,

@ -1,6 +1,6 @@
import 'dart:convert'; import 'dart:convert';
import 'package:cloud_firestore/cloud_firestore.dart'; // import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:diplomaticquarterapp/pages/webRTC/fcm/FCMSendNotification.dart'; import 'package:diplomaticquarterapp/pages/webRTC/fcm/FCMSendNotification.dart';
import 'package:diplomaticquarterapp/uitl/SignalRUtil.dart'; import 'package:diplomaticquarterapp/uitl/SignalRUtil.dart';
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';

@ -1,6 +1,6 @@
import 'dart:convert'; import 'dart:convert';
import 'package:cloud_firestore/cloud_firestore.dart'; // import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter_webrtc/flutter_webrtc.dart'; import 'package:flutter_webrtc/flutter_webrtc.dart';
typedef void StreamStateCallback(MediaStream stream); typedef void StreamStateCallback(MediaStream stream);
@ -22,8 +22,8 @@ class Signaling {
StreamStateCallback onAddRemoteStream; StreamStateCallback onAddRemoteStream;
Future<String> createRoom(RTCVideoRenderer remoteRenderer) async { Future<String> createRoom(RTCVideoRenderer remoteRenderer) async {
FirebaseFirestore db = FirebaseFirestore.instance; // FirebaseFirestore db = FirebaseFirestore.instance;
DocumentReference roomRef = db.collection('rooms').doc(); // DocumentReference roomRef = db.collection('rooms').doc();
print('Create PeerConnection with configuration: $configuration'); print('Create PeerConnection with configuration: $configuration');
@ -36,11 +36,11 @@ class Signaling {
}); });
// Code for collecting ICE candidates below // Code for collecting ICE candidates below
var callerCandidatesCollection = roomRef.collection('callerCandidates'); // var callerCandidatesCollection = roomRef.collection('callerCandidates');
peerConnection?.onIceCandidate = (RTCIceCandidate candidate) { peerConnection?.onIceCandidate = (RTCIceCandidate candidate) {
print('Got candidate: ${candidate.toMap()}'); print('Got candidate: ${candidate.toMap()}');
callerCandidatesCollection.add(candidate.toMap()); // callerCandidatesCollection.add(candidate.toMap());
}; };
// Finish Code for collecting ICE candidate // Finish Code for collecting ICE candidate
@ -51,8 +51,8 @@ class Signaling {
Map<String, dynamic> roomWithOffer = {'offer': offer.toMap()}; Map<String, dynamic> roomWithOffer = {'offer': offer.toMap()};
await roomRef.set(roomWithOffer); // await roomRef.set(roomWithOffer);
var roomId = roomRef.id; // var roomId = roomRef.id;
print('New room created with SDK offer. Room ID: $roomId'); print('New room created with SDK offer. Room ID: $roomId');
currentRoomText = 'Current room is $roomId - You are the caller!'; currentRoomText = 'Current room is $roomId - You are the caller!';
// Created a Room // Created a Room
@ -67,104 +67,28 @@ class Signaling {
}; };
// Listening for remote session description below // Listening for remote session description below
roomRef.snapshots().listen((snapshot) async { // roomRef.snapshots().listen((snapshot) async {
print('Got updated room: ${snapshot.data()}'); // print('Got updated room: ${snapshot.data()}');
//
Map<String, dynamic> data = snapshot.data() as Map<String, dynamic>; // Map<String, dynamic> data = snapshot.data() as Map<String, dynamic>;
if (peerConnection?.getRemoteDescription() != null && data['answer'] != null) { // if (peerConnection?.getRemoteDescription() != null && data['answer'] != null) {
var answer = RTCSessionDescription( // var answer = RTCSessionDescription(
data['answer']['sdp'], // data['answer']['sdp'],
data['answer']['type'], // data['answer']['type'],
); // );
//
print("Someone tried to connect"); // print("Someone tried to connect");
await peerConnection?.setRemoteDescription(answer); // await peerConnection?.setRemoteDescription(answer);
} // }
}); // });
// Listening for remote session description above // // Listening for remote session description above
//
// Listen for remote Ice candidates below // // Listen for remote Ice candidates below
roomRef.collection('calleeCandidates').snapshots().listen((snapshot) { // roomRef.collection('calleeCandidates').snapshots().listen((snapshot) {
snapshot.docChanges.forEach((change) { // snapshot.docChanges.forEach((change) {
if (change.type == DocumentChangeType.added) { // if (change.type == DocumentChangeType.added) {
Map<String, dynamic> data = change.doc.data() as Map<String, dynamic>; // Map<String, dynamic> data = change.doc.data() as Map<String, dynamic>;
print('Got new remote ICE candidate: ${jsonEncode(data)}'); // print('Got new remote ICE candidate: ${jsonEncode(data)}');
peerConnection.addCandidate(
RTCIceCandidate(
data['candidate'],
data['sdpMid'],
data['sdpMLineIndex'],
),
);
}
});
});
// Listen for remote ICE candidates above
return roomId;
}
Future<void> joinRoom(String roomId, RTCVideoRenderer remoteVideo) async {
FirebaseFirestore db = FirebaseFirestore.instance;
DocumentReference roomRef = db.collection('rooms').doc('$roomId');
var roomSnapshot = await roomRef.get();
print('Got room ${roomSnapshot.exists}');
if (roomSnapshot.exists) {
print('Create PeerConnection with configuration: $configuration');
peerConnection = await createPeerConnection(configuration);
registerPeerConnectionListeners();
localStream.getTracks().forEach((track) {
peerConnection?.addTrack(track, localStream);
});
// Code for collecting ICE candidates below
var calleeCandidatesCollection = roomRef.collection('calleeCandidates');
peerConnection.onIceCandidate = (RTCIceCandidate candidate) {
if (candidate == null) {
print('onIceCandidate: complete!');
return;
}
print('onIceCandidate: ${candidate.toMap()}');
calleeCandidatesCollection.add(candidate.toMap());
};
// Code for collecting ICE candidate above
peerConnection?.onTrack = (RTCTrackEvent event) {
print('Got remote track: ${event.streams[0]}');
event.streams[0].getTracks().forEach((track) {
print('Add a track to the remoteStream: $track');
remoteStream?.addTrack(track);
});
};
// Code for creating SDP answer below
var data = roomSnapshot.data() as Map<String, dynamic>;
print('Got offer $data');
var offer = data['offer'];
await peerConnection?.setRemoteDescription(
RTCSessionDescription(offer['sdp'], offer['type']),
);
var answer = await peerConnection.createAnswer();
print('Created Answer $answer');
await peerConnection.setLocalDescription(answer);
Map<String, dynamic> roomWithAnswer = {
'answer': {'type': answer.type, 'sdp': answer.sdp}
};
await roomRef.update(roomWithAnswer);
// Finished creating SDP answer
// Listening for remote ICE candidates below
// roomRef.collection('callerCandidates').snapshots().listen((snapshot) {
// snapshot.docChanges.forEach((document) {
// var data = document.doc.data() as Map<String, dynamic>;
// print(data);
// print('Got new remote ICE candidate: $data');
// peerConnection.addCandidate( // peerConnection.addCandidate(
// RTCIceCandidate( // RTCIceCandidate(
// data['candidate'], // data['candidate'],
@ -172,9 +96,85 @@ class Signaling {
// data['sdpMLineIndex'], // data['sdpMLineIndex'],
// ), // ),
// ); // );
// }
// }); // });
// }); // });
// Listen for remote ICE candidates above
return roomId;
} }
Future<void> joinRoom(String roomId, RTCVideoRenderer remoteVideo) async {
// FirebaseFirestore db = FirebaseFirestore.instance;
// DocumentReference roomRef = db.collection('rooms').doc('$roomId');
// var roomSnapshot = await roomRef.get();
// print('Got room ${roomSnapshot.exists}');
// if (roomSnapshot.exists) {
// print('Create PeerConnection with configuration: $configuration');
// peerConnection = await createPeerConnection(configuration);
//
// registerPeerConnectionListeners();
//
// localStream.getTracks().forEach((track) {
// peerConnection?.addTrack(track, localStream);
// });
//
// // Code for collecting ICE candidates below
// // var calleeCandidatesCollection = roomRef.collection('calleeCandidates');
// peerConnection.onIceCandidate = (RTCIceCandidate candidate) {
// if (candidate == null) {
// print('onIceCandidate: complete!');
// return;
// }
// print('onIceCandidate: ${candidate.toMap()}');
// // calleeCandidatesCollection.add(candidate.toMap());
// };
// // Code for collecting ICE candidate above
//
// peerConnection?.onTrack = (RTCTrackEvent event) {
// print('Got remote track: ${event.streams[0]}');
// event.streams[0].getTracks().forEach((track) {
// print('Add a track to the remoteStream: $track');
// remoteStream?.addTrack(track);
// });
// };
//
// // Code for creating SDP answer below
// var data = roomSnapshot.data() as Map<String, dynamic>;
// print('Got offer $data');
// var offer = data['offer'];
// await peerConnection?.setRemoteDescription(
// RTCSessionDescription(offer['sdp'], offer['type']),
// );
// var answer = await peerConnection.createAnswer();
// print('Created Answer $answer');
//
// await peerConnection.setLocalDescription(answer);
//
// Map<String, dynamic> roomWithAnswer = {
// 'answer': {'type': answer.type, 'sdp': answer.sdp}
// };
//
// await roomRef.update(roomWithAnswer);
// // Finished creating SDP answer
//
// // Listening for remote ICE candidates below
// // roomRef.collection('callerCandidates').snapshots().listen((snapshot) {
// // snapshot.docChanges.forEach((document) {
// // var data = document.doc.data() as Map<String, dynamic>;
// // print(data);
// // print('Got new remote ICE candidate: $data');
// // peerConnection.addCandidate(
// // RTCIceCandidate(
// // data['candidate'],
// // data['sdpMid'],
// // data['sdpMLineIndex'],
// // ),
// // );
// // });
// // });
// }
} }
Future<void> openUserMedia( Future<void> openUserMedia(
@ -201,15 +201,15 @@ class Signaling {
if (peerConnection != null) peerConnection.close(); if (peerConnection != null) peerConnection.close();
if (roomId != null) { if (roomId != null) {
var db = FirebaseFirestore.instance; // var db = FirebaseFirestore.instance;
var roomRef = db.collection('rooms').doc(roomId); // var roomRef = db.collection('rooms').doc(roomId);
var calleeCandidates = await roomRef.collection('calleeCandidates').get(); // var calleeCandidates = await roomRef.collection('calleeCandidates').get();
calleeCandidates.docs.forEach((document) => document.reference.delete()); // calleeCandidates.docs.forEach((document) => document.reference.delete());
var callerCandidates = await roomRef.collection('callerCandidates').get(); // var callerCandidates = await roomRef.collection('callerCandidates').get();
callerCandidates.docs.forEach((document) => document.reference.delete()); // callerCandidates.docs.forEach((document) => document.reference.delete());
await roomRef.delete(); // await roomRef.delete();
} }
localStream.dispose(); localStream.dispose();

Loading…
Cancel
Save