diff --git a/app/src/CallManager.js b/app/src/CallManager.js new file mode 100644 index 0000000..32cc494 --- /dev/null +++ b/app/src/CallManager.js @@ -0,0 +1,67 @@ +/* global webkitRTCPeerConnection */ +const emptyListener = (e) => console.log('EMPTY LISTENER FOR PHONE MANAGER', e); + +const CONFIG = { + iceServers: [ + { + urls: [ + 'stun:stun.l.google.com:19302', + 'stun:stun1.l.google.com:19302', + 'stun:stun2.l.google.com:19302', + 'stun:stun3.l.google.com:19302', + 'stun:stun4.l.google.com:19302', + ], + }, + ], + iceCandidatePoolSize: 10, +}; + +export default class PhoneManager { + constructor(listener = emptyListener) { + this.mediaStream = null; + this.listener = listener; + /* B I N D S */ + this.joinCall = this.joinCall.bind(this); + this.getMediaStream = this.getMediaStream.bind(this); + } + async getMediaStream() { + return new Promise((resolve, reject) => { + if (this.mediaStream !== null && this.mediaStream !== undefined) resolve(true); + navigator.mediaDevices + .getUserMedia({ + video: false, + audio: true, + }) + .then((mediaStream) => { + this.mediaStream = mediaStream; + resolve(true); + }) + .catch((error) => reject(error)); + }); + } + async joinCall() { + await this.getMediaStream(); + + var peerConnection = new webkitRTCPeerConnection(CONFIG); + console.log('Created Peer Connection', peerConnection); + + peerConnection.addTrack(this.mediaStream.getAudioTracks()[0], this.mediaStream); + peerConnection.onicecandidate = (ev) => console.log('Got ICE', ev); + + var offer = peerConnection.createOffer(); + console.log('Created Offer', offer); + + var answer = peerConnection.createAnswer(); + console.log('Created Answer', answer); + } + async leaveCall() { + if (this.mediaStream !== null && this.mediaStream !== undefined) { + this.mediaStream.getAudioTracks().forEach((track) => track.stop()); + } + } + toggleMute() { + if (this.mediaStream !== null && this.mediaStream !== undefined) { + this.mediaStream.getAudioTracks().forEach((track) => (track.enabled = !track.enabled)); + } + } +} diff --git a/app/src/Chat.js b/app/src/Chat.js index e1892d9..34a4e15 100644 --- a/app/src/Chat.js +++ b/app/src/Chat.js @@ -31,6 +31,7 @@ import { import { goTo } from 'route-lite'; import Create from './Create'; import VideoListener from './VideoListener'; +import CallManager from './CallManager'; const giphyFetch = new GiphyFetch('ojJbIMUN6XRMn4mHiFFav3rMCpXUg8sq'); const alertMessage = { @@ -63,6 +64,7 @@ export default class Chat extends Component { this.sendMessageInvoker = this.sendMessageInvoker.bind(this); this.videoEventListener = this.videoEventListener.bind(this); this.mVideoListener = new VideoListener(this.videoEventListener); + this.mCallManager = new CallManager(); } componentDidMount() { // EVENT CAPTURE FOR ENTER TO SEND @@ -213,23 +215,10 @@ export default class Chat extends Component { ? giphyFetch.trending({ offset, limit: 1 }) : giphyFetch.search(this.state.newMessage, { offset, limit: 1 }); } - handlePhoneCall() { - if (!this.state.onCall) { - navigator.mediaDevices - .getUserMedia({ - video: false, - audio: true, - }) - .then((mediaStream) => { - this.setState({ onCall: true, muted: false }); - console.log('GOT DEVICE'); - }) - .catch((err) => { - console.error(err); - }); - } else { - this.setState({ onCall: false }); - } + async handlePhoneCall() { + if (!this.state.onCall) this.mCallManager.joinCall(); + else this.mCallManager.leaveCall(); + this.setState({ onCall: !this.state.onCall }); } render() { const { admin, name, messages, newMessage, gif, onCall, muted } = this.state; @@ -264,6 +253,20 @@ export default class Chat extends Component {
+ {newMessage ? null : ( + + {onCall ? : } + + )} + {!newMessage && onCall && !muted ? ( + this.mCallManager.toggleMute()} color="secondary"> + + + ) : !newMessage && onCall && muted ? ( + this.mCallManager.toggleMute()} color="primary"> + + + ) : null} - - {onCall ? : } - - {onCall && !muted ? ( - - - - ) : onCall && muted ? ( - - - - ) : null}
{gif ? ( div { border-radius: 26px; }