forked from flutter-webrtc/flutter-webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtc_video_view.dart
178 lines (153 loc) · 4.83 KB
/
rtc_video_view.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
import 'dart:async';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'media_stream.dart';
import 'utils.dart';
import 'enums.dart';
class RTCVideoRenderer {
MethodChannel _channel = WebRTC.methodChannel();
int _textureId;
int _rotation = 0;
double _width = 0.0, _height = 0.0;
bool _mirror = false;
double _aspectRatio = 1.0;
MediaStream _srcObject;
RTCVideoViewObjectFit _objectFit =
RTCVideoViewObjectFit.RTCVideoViewObjectFitContain;
StreamSubscription<dynamic> _eventSubscription;
dynamic onStateChanged;
initialize() async {
final Map<dynamic, dynamic> response =
await _channel.invokeMethod('createVideoRenderer', {});
_textureId = response['textureId'];
_eventSubscription = _eventChannelFor(_textureId)
.receiveBroadcastStream()
.listen(eventListener, onError: errorListener);
}
int get rotation => _rotation;
double get width => _width;
double get height => _height;
int get textureId => _textureId;
double get aspectRatio => (_width == 0 || _height == 0)
? 1.0
: (_rotation == 90 || _rotation == 270)
? _height / _width
: _width / _height;
bool get mirror => _mirror;
set mirror(bool mirror) {
_mirror = mirror;
if (this.onStateChanged != null) {
this.onStateChanged();
}
}
RTCVideoViewObjectFit get objectFit => _objectFit;
set objectFit(RTCVideoViewObjectFit objectFit) {
_objectFit = objectFit;
if (this.onStateChanged != null) {
this.onStateChanged();
}
}
set srcObject(MediaStream stream) {
_srcObject = stream;
_channel.invokeMethod('videoRendererSetSrcObject', <String, dynamic>{
'textureId': _textureId,
'streamId': stream != null ? stream.id : '',
'ownerTag': stream != null ? stream.ownerTag : ''
});
}
Future<Null> dispose() async {
await _eventSubscription?.cancel();
await _channel.invokeMethod(
'videoRendererDispose',
<String, dynamic>{'textureId': _textureId},
);
}
EventChannel _eventChannelFor(int textureId) {
return new EventChannel('FlutterWebRTC/Texture$textureId');
}
void eventListener(dynamic event) {
final Map<dynamic, dynamic> map = event;
switch (map['event']) {
case 'didTextureChangeRotation':
_rotation = map['rotation'];
break;
case 'didTextureChangeVideoSize':
_width = 0.0 + map['width'];
_height = 0.0 + map['height'];
break;
case 'didFirstFrameRendered':
break;
}
if (this.onStateChanged != null) {
this.onStateChanged();
}
}
void errorListener(Object obj) {
final PlatformException e = obj;
throw e;
}
}
class RTCVideoView extends StatefulWidget {
final RTCVideoRenderer _renderer;
RTCVideoView(this._renderer, {Key key}) : super(key: key);
@override
_RTCVideoViewState createState() => new _RTCVideoViewState();
}
class _RTCVideoViewState extends State<RTCVideoView> {
double _aspectRatio;
RTCVideoViewObjectFit _objectFit;
bool _mirror;
@override
void initState() {
super.initState();
_setCallbacks();
_aspectRatio = widget._renderer.aspectRatio;
_mirror = widget._renderer.mirror;
_objectFit = widget._renderer.objectFit;
}
@override
void deactivate() {
super.deactivate();
widget._renderer.onStateChanged = null;
}
void _setCallbacks() {
widget._renderer.onStateChanged = () {
setState(() {
_aspectRatio = widget._renderer.aspectRatio;
_mirror = widget._renderer.mirror;
_objectFit = widget._renderer.objectFit;
});
};
}
Widget _buildVideoView(BoxConstraints constraints) {
return Container(
width: constraints.maxWidth,
height: constraints.maxHeight,
child: FittedBox(
fit:
_objectFit == RTCVideoViewObjectFit.RTCVideoViewObjectFitContain
? BoxFit.contain
: BoxFit.cover,
child: new Center(
child: new SizedBox(
width: constraints.maxHeight * _aspectRatio,
height: constraints.maxHeight,
child: new Transform(
transform: Matrix4.identity()
..rotateY(_mirror ? -pi : 0.0),
alignment: FractionalOffset.center,
child:
new Texture(textureId: widget._renderer._textureId))))));
}
@override
Widget build(BuildContext context) {
bool renderVideo =
(widget._renderer._textureId != null && widget._renderer._srcObject != null);
return new LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
return new Center(
child: renderVideo ? _buildVideoView(constraints) : new Container());
});
}
}