-
Notifications
You must be signed in to change notification settings - Fork 36
/
main.dart
350 lines (312 loc) · 9.61 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import 'package:apivideo_live_stream/apivideo_live_stream.dart';
import 'package:apivideo_live_stream_example/settings_screen.dart';
import 'package:apivideo_live_stream_example/types/params.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:wakelock_plus/wakelock_plus.dart';
import 'constants.dart';
MaterialColor apiVideoOrange = const MaterialColor(0xFFFA5B30, const {
50: const Color(0xFFFBDDD4),
100: const Color(0xFFFFD6CB),
200: const Color(0xFFFFD1C5),
300: const Color(0xFFFFB39E),
400: const Color(0xFFFA5B30),
500: const Color(0xFFF8572A),
600: const Color(0xFFF64819),
700: const Color(0xFFEE4316),
800: const Color(0xFFEC3809),
900: const Color(0xFFE53101)
});
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: LiveViewPage(),
theme: ThemeData(
primarySwatch: apiVideoOrange,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
);
}
}
class LiveViewPage extends StatefulWidget {
const LiveViewPage({Key? key}) : super(key: key);
@override
_LiveViewPageState createState() => new _LiveViewPageState();
}
class _LiveViewPageState extends State<LiveViewPage>
with WidgetsBindingObserver {
final ButtonStyle buttonStyle =
ElevatedButton.styleFrom(textStyle: const TextStyle(fontSize: 20));
Params config = Params();
late final ApiVideoLiveStreamController _controller;
bool _isStreaming = false;
@override
void initState() {
WidgetsBinding.instance.addObserver(this);
_controller = createLiveStreamController();
_controller.initialize().catchError((e) {
showInSnackBar(e.toString());
});
super.initState();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
// App state changed before we got the chance to initialize.
if (!_controller.isInitialized) {
return;
}
if (state == AppLifecycleState.inactive) {
_controller.stop();
} else if (state == AppLifecycleState.resumed) {
_controller.startPreview();
}
}
ApiVideoLiveStreamController createLiveStreamController() {
return ApiVideoLiveStreamController(
initialAudioConfig: config.audio,
initialVideoConfig: config.video,
onConnectionSuccess: () {
print('Connection succeeded');
},
onConnectionFailed: (error) {
print('Connection failed: $error');
_showDialog(context, 'Connection failed', '$error');
if (mounted) {
setIsStreaming(false);
}
},
onDisconnection: () {
showInSnackBar('Disconnected');
if (mounted) {
setIsStreaming(false);
}
},
onError: (error) {
// Get error such as missing permission,...
_showDialog(context, 'Error', '$error');
if (mounted) {
setIsStreaming(false);
}
});
}
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
appBar: AppBar(
title: const Text('Live Stream Example'),
actions: <Widget>[
PopupMenuButton<String>(
onSelected: (choice) => _onMenuSelected(choice, context),
itemBuilder: (BuildContext context) {
return Constants.choices.map((String choice) {
return PopupMenuItem<String>(
value: choice,
child: Text(choice),
);
}).toList();
},
)
],
),
body: SafeArea(
child: Center(
child: Column(
children: <Widget>[
Expanded(
child: Container(
child: Padding(
padding: const EdgeInsets.all(1.0),
child: Center(
child: ApiVideoCameraPreview(controller: _controller),
),
),
),
),
_controlRowWidget()
],
),
),
));
}
void _onMenuSelected(String choice, BuildContext context) {
if (choice == Constants.Settings) {
_awaitResultFromSettingsFinal(context);
}
}
void _awaitResultFromSettingsFinal(BuildContext context) async {
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SettingsScreen(params: config)));
_controller.setVideoConfig(config.video);
_controller.setAudioConfig(config.audio);
}
/// Display the control bar with buttons to take pictures and record videos.
Widget _controlRowWidget() {
final ApiVideoLiveStreamController? liveStreamController = _controller;
return Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
IconButton(
icon: const Icon(Icons.cameraswitch),
color: apiVideoOrange,
onPressed:
liveStreamController != null ? onSwitchCameraButtonPressed : null,
),
IconButton(
icon: const Icon(Icons.mic_off),
color: apiVideoOrange,
onPressed: liveStreamController != null
? onToggleMicrophoneButtonPressed
: null,
),
IconButton(
icon: const Icon(Icons.fiber_manual_record),
color: Colors.red,
onPressed: liveStreamController != null && !_isStreaming
? onStartStreamingButtonPressed
: null,
),
IconButton(
icon: const Icon(Icons.stop),
color: Colors.red,
onPressed: liveStreamController != null && _isStreaming
? onStopStreamingButtonPressed
: null),
],
);
}
void showInSnackBar(String message) {
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text(message)));
}
Future<void> switchCamera() async {
final ApiVideoLiveStreamController? liveStreamController = _controller;
if (liveStreamController == null) {
showInSnackBar('Error: create a camera controller first.');
return;
}
return await liveStreamController.switchCamera();
}
Future<void> toggleMicrophone() async {
final ApiVideoLiveStreamController? liveStreamController = _controller;
if (liveStreamController == null) {
showInSnackBar('Error: create a camera controller first.');
return;
}
return await liveStreamController.toggleMute();
}
Future<void> startStreaming() async {
final ApiVideoLiveStreamController? controller = _controller;
if (controller == null) {
print('Error: create a camera controller first.');
return;
}
return await controller.startStreaming(
streamKey: config.streamKey, url: config.rtmpUrl);
}
Future<void> stopStreaming() async {
final ApiVideoLiveStreamController? controller = _controller;
if (controller == null) {
print('Error: create a camera controller first.');
return;
}
return await controller.stopStreaming();
}
void onSwitchCameraButtonPressed() {
switchCamera().then((_) {
if (mounted) {
setState(() {});
}
}).catchError((error) {
if (error is PlatformException) {
_showDialog(
context, "Error", "Failed to switch camera: ${error.message}");
} else {
_showDialog(context, "Error", "Failed to switch camera: $error");
}
});
}
void onToggleMicrophoneButtonPressed() {
toggleMicrophone().then((_) {
if (mounted) {
setState(() {});
}
}).catchError((error) {
if (error is PlatformException) {
_showDialog(
context, "Error", "Failed to toggle mute: ${error.message}");
} else {
_showDialog(context, "Error", "Failed to toggle mute: $error");
}
});
}
void onStartStreamingButtonPressed() {
startStreaming().then((_) {
if (mounted) {
setIsStreaming(true);
}
}).catchError((error) {
if (error is PlatformException) {
_showDialog(
context, "Error", "Failed to start stream: ${error.message}");
} else {
_showDialog(context, "Error", "Failed to start stream: $error");
}
});
}
void onStopStreamingButtonPressed() {
stopStreaming().then((_) {
if (mounted) {
setIsStreaming(false);
}
}).catchError((error) {
if (error is PlatformException) {
_showDialog(
context, "Error", "Failed to stop stream: ${error.message}");
} else {
_showDialog(context, "Error", "Failed to stop stream: $error");
}
});
}
void setIsStreaming(bool isStreaming) {
setState(() {
if (isStreaming) {
WakelockPlus.enable();
} else {
WakelockPlus.disable();
}
_isStreaming = isStreaming;
});
}
}
Future<void> _showDialog(
BuildContext context, String title, String description) async {
return showDialog<void>(
context: context,
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
title: Text(title),
content: SingleChildScrollView(
child: Text(description),
),
actions: <Widget>[
TextButton(
child: const Text('Dismiss'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}