1
1
import 'package:flutter/material.dart' ;
2
2
import 'dart:core' ;
3
- import 'settings. dart' ;
3
+ import 'dart:async ' ;
4
4
import 'signaling.dart' ;
5
+ import 'calling_screen.dart' ;
6
+ import 'package:webrtc/webrtc.dart' ;
5
7
6
8
class CallSample extends StatefulWidget {
7
9
static String tag = 'call_sample' ;
@@ -14,20 +16,89 @@ class _CallSampleState extends State<CallSample> {
14
16
Signaling _signaling;
15
17
String _roomId;
16
18
String _displayName = "flutter" ;
19
+ List <dynamic > _peers;
20
+
21
+ final _localRenderer = new RTCVideoRenderer ();
22
+ final _remoteRenderer = new RTCVideoRenderer ();
23
+ bool _inCalling = false ;
24
+ Timer _timer;
25
+
17
26
@override
18
27
initState () {
19
28
super .initState ();
29
+ initRenderers ();
30
+ }
31
+
32
+ initRenderers () async {
33
+ await _localRenderer.initialize ();
34
+ await _remoteRenderer.initialize ();
20
35
}
21
36
22
37
@override
23
38
deactivate () {
24
39
super .deactivate ();
40
+ if (_signaling != null ) _signaling.close ();
41
+ }
42
+
43
+ void _connect () async {
44
+ if (_signaling == null ) {
45
+ _signaling = new Signaling ('ws://192.168.31.152:4442' , _displayName);
46
+ await _signaling.connect ();
47
+
48
+ _signaling.onPeers.listen ((message) {
49
+ Map <String , dynamic > mapData = message;
50
+ List <dynamic > peers = mapData['data' ];
51
+ this .setState (() {
52
+ _peers = peers;
53
+ });
54
+ });
55
+
56
+ _signaling.onLocalStream.listen ((message) {
57
+ Map <String , dynamic > mapData = message;
58
+ _localRenderer.srcObject = mapData['stream' ];
59
+ });
60
+
61
+ _signaling.onRemoteStreamAdd.listen ((message) {
62
+ Map <String , dynamic > mapData = message;
63
+ _remoteRenderer.srcObject = mapData['stream' ];
64
+ });
65
+
66
+ _signaling.onRemoteStreamRemoved.listen ((message) {
67
+ this .setState (() {
68
+ _inCalling = false ;
69
+ _remoteRenderer.srcObject = null ;
70
+ });
71
+ });
72
+ }
73
+ }
74
+
75
+ _invitePeer (context, peerId) {
76
+ this .setState (() {
77
+ _inCalling = true ;
78
+ });
79
+ if (_signaling != null ) {
80
+ _signaling.invite (peerId, 'video' );
81
+ }
25
82
}
26
83
27
- void _connect () {
28
- if (_signaling == null ){
29
- _signaling = new Signaling ('localhost:8088' , _displayName);
30
- }
84
+ _hangUp () {
85
+ this .setState (() {
86
+ _inCalling = false ;
87
+ });
88
+ if (_signaling != null ) {
89
+ _signaling.leave ();
90
+ }
91
+ }
92
+
93
+ _buildRow (context, peer) {
94
+ return ListBody (children: < Widget > [
95
+ ListTile (
96
+ title: Text (peer['name' ]),
97
+ onTap: () => _invitePeer (context, peer['id' ]),
98
+ trailing: Icon (Icons .video_call),
99
+ ),
100
+ Divider ()
101
+ ]);
31
102
}
32
103
33
104
@override
@@ -38,40 +109,61 @@ class _CallSampleState extends State<CallSample> {
38
109
actions: < Widget > [
39
110
IconButton (
40
111
icon: const Icon (Icons .settings),
41
- onPressed: () =>
42
- Navigator .push (context, new MaterialPageRoute (builder: (BuildContext context) => new CallSettings ())),
112
+ onPressed: _connect,
43
113
tooltip: 'setup' ,
44
114
),
45
115
],
46
116
),
47
- body: new OrientationBuilder (
48
- builder: (context, orientation) {
49
- return new Center (
50
- child: new Padding (
51
- padding: new EdgeInsets .only (
52
- top: 100.0 , bottom: 0.0 , left: 20.0 , right: 20.0 ),
53
- child: new Form (
54
- key: _formKey,
55
- child: new Column (children: < Widget > [
56
- new Padding (
57
- padding: new EdgeInsets .only (bottom: 24.0 ),
58
- child: new TextField (
59
- decoration: InputDecoration (
60
- labelText: 'Enter room id' ),
61
- onChanged: (String value) {
62
- _roomId = value;
63
- })),
64
- new Row (children: < Widget > [
65
- new RaisedButton (
66
- child: new Text ('Connect' ),
67
- onPressed: _connect,
68
- )
69
- ])
70
- ])))
71
-
72
- );
73
- },
117
+ floatingActionButton: FloatingActionButton (
118
+ onPressed: _hangUp,
119
+ tooltip: 'Hangup' ,
120
+ child: new Icon (_inCalling ? Icons .call_end : Icons .phone),
74
121
),
122
+ body: _inCalling
123
+ ? new OrientationBuilder (
124
+ builder: (context, orientation) {
125
+ return new Center (
126
+ child: new Container (
127
+ decoration: new BoxDecoration (color: Colors .white),
128
+ child: new Stack (
129
+ children: < Widget > [
130
+ new Align (
131
+ alignment: orientation == Orientation .portrait
132
+ ? const FractionalOffset (0.5 , 0.1 )
133
+ : const FractionalOffset (0.0 , 0.5 ),
134
+ child: new Container (
135
+ width: 320.0 ,
136
+ height: 240.0 ,
137
+ child: new RTCVideoView (_localRenderer),
138
+ decoration:
139
+ new BoxDecoration (color: Colors .black54),
140
+ ),
141
+ ),
142
+ new Align (
143
+ alignment: orientation == Orientation .portrait
144
+ ? const FractionalOffset (0.5 , 0.9 )
145
+ : const FractionalOffset (1.0 , 0.5 ),
146
+ child: new Container (
147
+ width: 320.0 ,
148
+ height: 240.0 ,
149
+ child: new RTCVideoView (_remoteRenderer),
150
+ decoration:
151
+ new BoxDecoration (color: Colors .black54),
152
+ ),
153
+ ),
154
+ ],
155
+ ),
156
+ ),
157
+ );
158
+ },
159
+ )
160
+ : new ListView .builder (
161
+ shrinkWrap: true ,
162
+ padding: const EdgeInsets .all (0.0 ),
163
+ itemCount: (_peers != null ? _peers.length : 0 ),
164
+ itemBuilder: (context, i) {
165
+ return _buildRow (context, _peers[i]);
166
+ }),
75
167
);
76
168
}
77
169
}
0 commit comments