|
| 1 | +import 'dart:async'; |
| 2 | +import 'dart:convert'; |
| 3 | +import 'package:flutter_webrtc/flutter_webrtc.dart'; |
| 4 | +import 'package:flutter/material.dart'; |
| 5 | +import 'package:websocket/websocket.dart'; |
| 6 | + |
| 7 | +void main() => runApp(MyApp()); |
| 8 | + |
| 9 | +class MyApp extends StatefulWidget { |
| 10 | + @override |
| 11 | + _GetMyAppState createState() => _GetMyAppState(); |
| 12 | +} |
| 13 | + |
| 14 | +class _GetMyAppState extends State<MyApp> { |
| 15 | + // Local media |
| 16 | + final _localRenderer = RTCVideoRenderer(); |
| 17 | + List _remoteRenderers = []; |
| 18 | + |
| 19 | + WebSocket _socket; |
| 20 | + RTCPeerConnection _peerConnection; |
| 21 | + |
| 22 | + @override |
| 23 | + void initState() { |
| 24 | + super.initState(); |
| 25 | + connect(); |
| 26 | + } |
| 27 | + |
| 28 | + Future<void> connect() async { |
| 29 | + _peerConnection = await createPeerConnection({}, {}); |
| 30 | + |
| 31 | + await _localRenderer.initialize(); |
| 32 | + var localStream = await navigator.mediaDevices |
| 33 | + .getUserMedia({'audio': true, 'video': true}); |
| 34 | + _localRenderer.srcObject = localStream; |
| 35 | + |
| 36 | + localStream.getTracks().forEach((track) async { |
| 37 | + await _peerConnection.addTrack(track, localStream); |
| 38 | + }); |
| 39 | + |
| 40 | + _peerConnection.onIceCandidate = (candidate) { |
| 41 | + if (candidate == null) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + _socket.add(JsonEncoder().convert({ |
| 46 | + "event": "candidate", |
| 47 | + "data": JsonEncoder().convert({ |
| 48 | + 'sdpMLineIndex': candidate.sdpMlineIndex, |
| 49 | + 'sdpMid': candidate.sdpMid, |
| 50 | + 'candidate': candidate.candidate, |
| 51 | + }) |
| 52 | + })); |
| 53 | + }; |
| 54 | + |
| 55 | + _peerConnection.onTrack = (event) async { |
| 56 | + if (event.track.kind == 'video' && event.streams.isNotEmpty) { |
| 57 | + var renderer = RTCVideoRenderer(); |
| 58 | + await renderer.initialize(); |
| 59 | + renderer.srcObject = event.streams[0]; |
| 60 | + |
| 61 | + setState(() { _remoteRenderers.add(renderer); }); |
| 62 | + } |
| 63 | + }; |
| 64 | + |
| 65 | + _peerConnection.onRemoveStream = (stream) { |
| 66 | + var rendererToRemove; |
| 67 | + var newRenderList = []; |
| 68 | + |
| 69 | + // Filter existing renderers for the stream that has been stopped |
| 70 | + _remoteRenderers.forEach((r) { |
| 71 | + if (r.srcObject.id == stream.id) { |
| 72 | + rendererToRemove = r; |
| 73 | + } else { |
| 74 | + newRenderList.add(r); |
| 75 | + } |
| 76 | + }); |
| 77 | + |
| 78 | + // Set the new renderer list |
| 79 | + setState(() { _remoteRenderers = newRenderList; }); |
| 80 | + |
| 81 | + // Dispose the renderer we are done with |
| 82 | + if (rendererToRemove != null) { |
| 83 | + rendererToRemove.dispose(); |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + _socket = await WebSocket.connect('ws://localhost:8080/websocket'); |
| 88 | + _socket.stream.listen((raw) async { |
| 89 | + Map<String, dynamic> msg = jsonDecode(raw); |
| 90 | + |
| 91 | + switch (msg['event']) { |
| 92 | + case 'candidate': |
| 93 | + Map<String, dynamic> parsed = jsonDecode(msg['data']); |
| 94 | + _peerConnection |
| 95 | + .addCandidate(RTCIceCandidate(parsed['candidate'], null, 0)); |
| 96 | + return; |
| 97 | + case 'offer': |
| 98 | + Map<String, dynamic> offer = jsonDecode(msg['data']); |
| 99 | + |
| 100 | + // SetRemoteDescription and create answer |
| 101 | + await _peerConnection.setRemoteDescription( |
| 102 | + RTCSessionDescription(offer['sdp'], offer['type'])); |
| 103 | + RTCSessionDescription answer = await _peerConnection.createAnswer({}); |
| 104 | + await _peerConnection.setLocalDescription(answer); |
| 105 | + |
| 106 | + // Send answer over WebSocket |
| 107 | + _socket.add(JsonEncoder().convert({ |
| 108 | + 'event': 'answer', |
| 109 | + 'data': |
| 110 | + JsonEncoder().convert({'type': answer.type, 'sdp': answer.sdp}) |
| 111 | + })); |
| 112 | + return; |
| 113 | + } |
| 114 | + }, onDone: () { |
| 115 | + print('Closed by server!'); |
| 116 | + }); |
| 117 | + } |
| 118 | + |
| 119 | + @override |
| 120 | + Widget build(BuildContext context) { |
| 121 | + return MaterialApp( |
| 122 | + title: 'sfu-ws', |
| 123 | + home: Scaffold( |
| 124 | + appBar: AppBar( |
| 125 | + title: Text('sfu-ws'), |
| 126 | + ), |
| 127 | + body: OrientationBuilder(builder: (context, orientation) { |
| 128 | + return Column( |
| 129 | + children: [ |
| 130 | + Row( |
| 131 | + children: [ |
| 132 | + Text('Local Video', style: TextStyle(fontSize: 50.0)) |
| 133 | + ], |
| 134 | + ), |
| 135 | + Row( |
| 136 | + children: [ |
| 137 | + SizedBox( |
| 138 | + width: 160, |
| 139 | + height: 120, |
| 140 | + child: RTCVideoView(_localRenderer, mirror: true)) |
| 141 | + ], |
| 142 | + ), |
| 143 | + Row( |
| 144 | + children: [ |
| 145 | + Text('Remote Video', style: TextStyle(fontSize: 50.0)) |
| 146 | + ], |
| 147 | + ), |
| 148 | + Row( |
| 149 | + children: [ |
| 150 | + ..._remoteRenderers.map((remoteRenderer) { |
| 151 | + return SizedBox( |
| 152 | + width: 160, |
| 153 | + height: 120, |
| 154 | + child: RTCVideoView(remoteRenderer)); |
| 155 | + }).toList(), |
| 156 | + ], |
| 157 | + ), |
| 158 | + Row( |
| 159 | + children: [ |
| 160 | + Text('Logs Video', style: TextStyle(fontSize: 50.0)) |
| 161 | + ], |
| 162 | + ), |
| 163 | + ], |
| 164 | + ); |
| 165 | + }))); |
| 166 | + } |
| 167 | +} |
0 commit comments