8000 Update P2P Call Sample. · lineCode/flutter-webrtc@d51413e · GitHub
[go: up one dir, main page]

Skip to content

Commit d51413e

Browse files
committed
Update P2P Call Sample.
1 parent 2539409 commit d51413e

File tree

8 files changed

+377
-145
lines changed

8 files changed

+377
-145
lines changed

example/lib/main.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ class MyApp extends StatefulWidget {
1313

1414
final List<RouteItem> items = <RouteItem>[
1515
RouteItem(
16-
title: 'Basic Sample',
17-
subtitle: 'Basic Sample.',
16+
title: 'Basic API Tests',
17+
subtitle: 'Basic API Tests.',
1818
push: (BuildContext context) {
1919
Navigator.push(
2020
context,

example/lib/src/basic_sample/basic_sample.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,23 @@ typedef void RouteCallback(BuildContext context);
99

1010
final List<RouteItem> items = <RouteItem>[
1111
RouteItem(
12-
title: 'GetUserMediaSample',
12+
title: 'GetUserMedia Test',
1313
push: (BuildContext context) {
1414
Navigator.push(
1515
context,
1616
new MaterialPageRoute(
1717
builder: (BuildContext context) => new GetUserMediaSample()));
1818
}),
1919
RouteItem(
20-
title: 'LoopBackSample',
20+
title: 'LoopBack Sample',
2121
push: (BuildContext context) {
2222
Navigator.push(
2323
context,
2424
new MaterialPageRoute(
2525
builder: (BuildContext context) => new LoopBackSample()));
2626
}),
2727
RouteItem(
28-
title: 'DataChannelSample',
28+
title: 'DataChannel Test',
2929
push: (BuildContext context) {
3030
Navigator.push(
3131
context,
@@ -67,7 +67,7 @@ class _BasicSampleState extends State<BasicSample> {
6767
Widget build(BuildContext context) {
6868
return new Scaffold(
6969
appBar: new AppBar(
70-
title: new Text('Basic Sample'),
70+
title: new Text('Basic API Tests'),
7171
),
7272
body: new ListView.builder(
7373
shrinkWrap: true,

example/lib/src/basic_sample/data_channel_sample.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class _DataChannelSampleState extends State<DataChannelSample> {
137137
return
138138
new Scaffold(
139139
appBar: new AppBar(
140-
title: new Text('Data Channel Sample'),
140+
title: new Text('Data Channel Test'),
141141
),
142142
body: new OrientationBuilder(
143143
builder: (context, orientation) {

example/lib/src/basic_sample/get_user_media_sample.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ class _GetUserMediaSampleState extends State<GetUserMediaSample> {
5151
};
5252

5353
try {
54-
_localStream = await navigator.getUserMedia(mediaConstraints);
55-
_localRenderer.srcObject = _localStream;
54+
navigator.getUserMedia(mediaConstraints).then((stream){
55+
_localStream = stream;
56+
_localRenderer.srcObject = _localStream;
57+
});
5658
} catch (e) {
5759
print(e.toString());
5860
}
@@ -79,7 +81,7 @@ class _GetUserMediaSampleState extends State<GetUserMediaSample> {
7981
Widget build(BuildContext context) {
8082
return new Scaffold(
8183
appBar: new AppBar(
82-
title: new Text('getUserMedia example'),
84+
title: new Text('GetUserMedia API Test'),
8385
),
8486
body: new OrientationBuilder(
8587
builder: (context, orientation) {
Lines changed: 126 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import 'package:flutter/material.dart';
22
import 'dart:core';
3-
import 'settings.dart';
3+
import 'dart:async';
44
import 'signaling.dart';
5+
import 'calling_screen.dart';
6+
import 'package:webrtc/webrtc.dart';
57

68
class CallSample extends StatefulWidget {
79
static String tag = 'call_sample';
@@ -14,20 +16,89 @@ class _CallSampleState extends State<CallSample> {
1416
Signaling _signaling;
1517
String _roomId;
1618
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+
1726
@override
1827
initState() {
1928
super.initState();
29+
initRenderers();
30+
}
31+
32+
initRenderers() async {
33+
await _localRenderer.initialize();
34+
await _remoteRenderer.initialize();
2035
}
2136

2237
@override
2338
deactivate() {
2439
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+
}
2582
}
2683

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+
]);
31102
}
32103

33104
@override
@@ -38,40 +109,61 @@ class _CallSampleState extends State<CallSample> {
38109
actions: <Widget>[
39110
IconButton(
40111
icon: const Icon(Icons.settings),
41-
onPressed: () =>
42-
Navigator.push(context, new MaterialPageRoute(builder: (BuildContext context) => new CallSettings())),
112+
onPressed: _connect,
43113
tooltip: 'setup',
44114
),
45115
],
46116
),
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),
74121
),
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+
}),
75167
);
76168
}
77169
}

0 commit comments

Comments
 (0)
0