|
1 |
| -# flutter-webrtc |
| 1 | +# Flutter-WebRTC |
| 2 | +[](https://pub.dartlang.org/packages/flutter_webrtc) |
2 | 3 | Flutter WebRTC plugin for iOS/Android
|
3 | 4 |
|
4 | 5 | ## Usage
|
5 |
| -- `git clone https://github.com/cloudwebrtc/flutter-webrtc` |
6 |
| -- `cd flutter-webrtc/example` |
7 |
| -- `flutter packages get` |
8 |
| -- `flutter run` |
9 |
| -## Note |
10 |
| -- If you want to test `P2P Call Sample`, please use the [webrtc-flutter-server](https://github.com/cloudwebrtc/flutter-webrtc-server), and enter your server address into the example app. |
| 6 | +Add `flutter_webrtc` as a [dependency in your pubspec.yaml file](https://flutter.io/using-packages/). |
| 7 | + |
| 8 | +### iOS |
| 9 | + |
| 10 | +Add the following entry to your _Info.plist_ file, located in `<project root>/ios/Runner/Info.plist`: |
| 11 | + |
| 12 | +```xml |
| 13 | +<key>NSCameraUsageDescription</key> |
| 14 | +<string>$(PRODUCT_NAME) Camera Usage!</string> |
| 15 | +<key>NSMicrophoneUsageDescription</key> |
| 16 | +<string>$(PRODUCT_NAME) Microphone Usage!</string> |
| 17 | +<key>NSPhotoLibraryUsageDescription</key> |
| 18 | +``` |
| 19 | + |
| 20 | +This entry allows your app to access video files by URL. |
| 21 | + |
| 22 | +### Android |
| 23 | + |
| 24 | +Ensure the following permission is present in your Android Manifest file, located in `<project root>/android/app/src/main/AndroidManifest.xml: |
| 25 | + |
| 26 | +```xml |
| 27 | + <uses-feature android:name="android.hardware.camera" /> |
| 28 | + <uses-feature android:name="android.hardware.camera.autofocus" /> |
| 29 | + <uses-permission android:name="android.permission.CAMERA" /> |
| 30 | + <uses-permission android:name="android.permission.RECORD_AUDIO" /> |
| 31 | + <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> |
| 32 | + <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> |
| 33 | + <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> |
| 34 | + |
| 35 | +``` |
| 36 | + |
| 37 | +The Flutter project template adds it, so it may already be there. |
| 38 | + |
| 39 | +### Example |
| 40 | + |
| 41 | +```dart |
| 42 | +import 'package:flutter/material.dart'; |
| 43 | +import 'package:flutter_webrtc/webrtc.dart'; |
| 44 | +import 'dart:core'; |
| 45 | +
|
| 46 | +/** |
| 47 | + * getUserMedia sample |
| 48 | + */ |
| 49 | +class GetUserMediaSample extends StatefulWidget { |
| 50 | + static String tag = 'get_usermedia_sample'; |
| 51 | +
|
| 52 | + @override |
| 53 | + _GetUserMediaSampleState createState() => new _GetUserMediaSampleState(); |
| 54 | +} |
| 55 | +
|
| 56 | +class _GetUserMediaSampleState extends State<GetUserMediaSample> { |
| 57 | + MediaStream _localStream; |
| 58 | + final _localRenderer = new RTCVideoRenderer(); |
| 59 | + bool _inCalling = false; |
| 60 | +
|
| 61 | + @override |
| 62 | + initState() { |
| 63 | + super.initState(); |
| 64 | + initRenderers(); |
| 65 | + } |
| 66 | +
|
| 67 | + @override |
| 68 | + deactivate() { |
| 69 | + super.deactivate(); |
| 70 | + if (_inCalling) { |
| 71 | + _hangUp(); |
| 72 | + } |
| 73 | + } |
| 74 | +
|
| 75 | + initRenderers() async { |
| 76 | + await _localRenderer.initialize(); |
| 77 | + } |
| 78 | +
|
| 79 | + // Platform messages are asynchronous, so we initialize in an async method. |
| 80 | + _makeCall() async { |
| 81 | + final Map<String, dynamic> mediaConstraints = { |
| 82 | + "audio": true, |
| 83 | + "video": { |
| 84 | + "mandatory": { |
| 85 | + "minWidth":'640', // Provide your own width, height and frame rate here |
| 86 | + "minHeight": '480', |
| 87 | + "minFrameRate": '30', |
| 88 | + }, |
| 89 | + "facingMode": "user", |
| 90 | + "optional": [], |
| 91 | + } |
| 92 | + }; |
| 93 | +
|
| 94 | + try { |
| 95 | + navigator.getUserMedia(mediaConstraints).then((stream){ |
| 96 | + _localStream = stream; |
| 97 | + _localRenderer.srcObject = _localStream; |
| 98 | + }); |
| 99 | + } catch (e) { |
| 100 | + print(e.toString()); |
| 101 | + } |
| 102 | + if (!mounted) return; |
| 103 | +
|
| 104 | + setState(() { |
| 105 | + _inCalling = true; |
| 106 | + }); |
| 107 | + } |
| 108 | +
|
| 109 | + _hangUp() async { |
| 110 | + try { |
| 111 | + await _localStream.dispose(); |
| 112 | + _localRenderer.srcObject = null; |
| 113 | + } catch (e) { |
| 114 | + print(e.toString()); |
| 115 | + } |
| 116 | + setState(() { |
| 117 | + _inCalling = false; |
| 118 | + }); |
| 119 | + } |
| 120 | +
|
| 121 | + @override |
| 122 | + Widget build(BuildContext context) { |
| 123 | + return new Scaffold( |
| 124 | + appBar: new AppBar( |
| 125 | + title: new Text('GetUserMedia API Test'), |
| 126 | + ), |
| 127 | + body: new OrientationBuilder( |
| 128 | + builder: (context, orientation) { |
| 129 | + return new Center( |
| 130 | + child: new Container( |
| 131 | + margin: new EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
9D71
|
| 132 | + width: MediaQuery.of(context).size.width, |
| 133 | + height: MediaQuery.of(context).size.height, |
| 134 | + child: RTCVideoView(_localRenderer), |
| 135 | + decoration: new BoxDecoration(color: Colors.black54), |
| 136 | + ), |
| 137 | + ); |
| 138 | + }, |
| 139 | + ), |
| 140 | + floatingActionButton: new FloatingActionButton( |
| 141 | + onPressed: _inCalling ? _hangUp : _makeCall, |
| 142 | + tooltip: _inCalling ? 'Hangup' : 'Call', |
| 143 | + child: new Icon(_inCalling ? Icons.call_end : Icons.phone), |
| 144 | + ), |
| 145 | + ); |
| 146 | + } |
| 147 | +} |
| 148 | +``` |
| 149 | + |
| 150 | +For more examples, please refer to [flutter-webrtc-demo](https://github.com/cloudwebrtc/flutter-webrtc-demo/). |
0 commit comments