10000 Reduce code size · tmthecoder/flutter-webrtc@91f76bf · GitHub
[go: up one dir, main page]

Skip to content

Commit 91f76bf

Browse files
committed
Reduce code size
Remove confusing print Add null check to prevent passing null to native implementations
1 parent 7b6e110 commit 91f76bf

File tree

2 files changed

+93
-104
lines changed

2 files changed

+93
-104
lines changed

lib/src/rtc_video_view.dart

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class RTCVideoValue {
1616
this.rotation = 0,
1717
this.renderVideo = false,
1818
});
19-
static const RTCVideoValue empty = RTCVideoValue();
19+
static const empty = RTCVideoValue();
2020
final double width;
2121
final double height;
2222
final int rotation;
@@ -61,7 +61,7 @@ class RTCVideoRenderer extends ValueNotifier<RTCVideoValue> {
6161
final response = await _channel
6262
.invokeMethod<Map<dynamic, dynamic>>('createVideoRenderer', {});
6363
_textureId = response['textureId'];
64-
_eventSubscription = _eventChannelFor(_textureId)
64+
_eventSubscription = EventChannel('FlutterWebRTC/Texture$textureId')
6565
.receiveBroadcastStream()
6666
.listen(eventListener, onError: errorListener);
6767
}
@@ -71,17 +71,17 @@ class RTCVideoRenderer extends ValueNotifier<RTCVideoValue> {
7171
MediaStream get srcObject => _srcObject;
7272

7373
set srcObject(MediaStream stream) {
74+
if (textureId == null) throw 'Call initialize before setting the stream';
75+
7476
_srcObject = stream;
7577
_channel.invokeMethod('videoRendererSetSrcObject', <String, dynamic>{
76-
'textureId': _textureId,
78+
'textureId': textureId,
7779
'streamId': stream?.id ?? '',
7880
'ownerTag': stream?.ownerTag ?? ''
7981
}).then((_) {
80-
if (stream == null) {
81-
value = RTCVideoValue.empty;
82-
} else {
83-
value = value.copyWith(renderVideo: renderVideo);
84-
}
82+
value = (stream == null)
83+
? RTCVideoValue.empty
84+
: value.copyWith(renderVideo: renderVideo);
8585
});
8686
}
8787

@@ -95,10 +95,6 @@ class RTCVideoRenderer extends ValueNotifier<RTCVideoValue> {
9595
);
9696
}
9797

98-
EventChannel _eventChannelFor(int textureId) {
99-
return EventChannel('FlutterWebRTC/Texture$textureId');
100-
}
101-
10298
void eventListener(dynamic event) {
10399
final Map<dynamic, dynamic> map = event;
104100
switch (map['event']) {
@@ -142,38 +138,37 @@ class RTCVideoView extends StatelessWidget {
142138
@override
143139
Widget build(BuildContext context) {
144140
return LayoutBuilder(
145-
builder: (BuildContext context, BoxConstraints constraints) {
146-
return Center(
147-
child: _buildVideoView(constraints),
148-
);
149-
},
150-
);
141+
builder: (BuildContext context, BoxConstraints constraints) =>
142+
_buildVideoView(constraints));
151143
}
152144

153145
Widget _buildVideoView(BoxConstraints constraints) {
154-
return Container(
155-
width: constraints.maxWidth,
156-
height: constraints.maxHeight,
157-
child: FittedBox(
158-
fit: objectFit == RTCVideoViewObjectFit.RTCVideoViewObjectFitContain
159-
? BoxFit.contain
160-
: BoxFit.cover,
161-
child: Center(
162-
child: ValueListenableBuilder<RTCVideoValue>(
163-
valueListenable: _renderer,
164-
builder: (BuildContext context, RTCVideoValue value, Widget child) {
165-
return SizedBox(
166-
width: constraints.maxHeight * value.aspectRatio,
167-
height: constraints.maxHeight,
168-
child: value.renderVideo ? child : Container(),
169-
);
170-
},
171-
child: Transform(
172-
transform: Matrix4.identity()..rotateY(mirror ? -pi : 0.0),
173-
alignment: FractionalOffset.center,
174-
child: _renderer.textureId != null
175-
? Texture(textureId: _renderer.textureId)
176-
: Container(),
146+
return Center(
147+
child: Container(
148+
width: constraints.maxWidth,
149+
height: constraints.maxHeight,
150+
child: FittedBox(
151+
fit: objectFit == RTCVideoViewObjectFit.RTCVideoViewObjectFitContain
152+
? BoxFit.contain
153+
: BoxFit.cover,
154+
child: Center(
155+
child: ValueListenableBuilder<RTCVideoValue>(
156+
valueListenable: _renderer,
157+
builder:
158+
(BuildContext context, RTCVideoValue value, Widget child) {
159+
return SizedBox(
160+
width: constraints.maxHeight * value.aspectRatio,
161+
height: constraints.maxHeight,
162+
child: value.renderVideo ? child : Container(),
163+
);
164+
},
165+
child: Transform(
166+
transform: Matrix4.identity()..rotateY(mirror ? -pi : 0.0),
167+
alignment: FractionalOffset.center,
168+
child: _renderer.textureId != null
169+
? Texture(textureId: _renderer.textureId)
170+
: Container(),
171+
),
177172
),
178173
),
179174
),

lib/src/web/rtc_video_view.dart

Lines changed: 57 additions & 63 deletions
Original file line numberDiff line number F438 Diff line change
@@ -7,33 +7,34 @@ import '../enums.dart';
77
import './ui_fake.dart' if (dart.library.html) 'dart:ui' as ui;
88
import 'media_stream.dart';
99

10-
typedef VideoRotationChangeCallback = void Function(
11-
int textureId, int rotation);
1210
typedef VideoSizeChangeCallback = void Function(
1311
int textureId, double width, double height);
12+
typedef StateChangeCallback = void Function();
13+
typedef FirstFrameRenderedCallback = void Function();
1414

1515
class RTCVideoRenderer {
1616
RTCVideoRenderer();
17-
double _width = 0.0, _height = 0.0;
17+
var _width = 0.0, _height = 0.0;
18+
var _isFirstFrameRendered = false;
1819
MediaStream _srcObject;
1920
VideoSizeChangeCallback onVideoSizeChanged;
20-
VideoRotationChangeCallback onVideoRotationChanged;
21-
dynamic onFirstFrameRendered;
22-
var isFirstFrameRendered = false;
23-
dynamic onStateChanged;
24-
HtmlElementView htmlElementView;
21+
StateChangeCallback onStateChanged;
22+
FirstFrameRenderedCallback onFirstFrameRendered;
23+
24+
HtmlElementView _htmlElementView;
2525
html.VideoElement _htmlVideoElement;
2626

27-
static final _videoViews = <html.VideoElement>[];
27+
final _videoViews = <html.VideoElement>[];
2828

2929
bool get isMuted => _htmlVideoElement?.muted ?? true;
3030
set isMuted(bool i) => _htmlVideoElement?.muted = i;
3131

32-
static void fixVideoElements() => _videoViews.forEach((v) => v.play());
32+
HtmlElementView get htmlElementView => _htmlElementView;
3333

34-
void initialize() async {
35-
print('You don\'t have to call RTCVideoRenderer.initialize on Flutter Web');
36-
}
34+
void fixVideoElements() => _videoViews.forEach((v) => v.play());
35+
36+
/// You don\'t have to call RTCVideoRenderer.initialize if you use only Flutter web
37+
void initialize() async {}
3738

3839
int get rotation => 0;
3940

@@ -49,14 +50,12 @@ class RTCVideoRenderer {
4950
MediaStream get srcObject => _srcObject;
5051

5152
set srcObject(MediaStream stream) {
52-
_srcObject = stream;
53-
54-
if (_srcObject == null) {
55-
findHtmlView()?.srcObject = null;
53+
if (stream == null) {
5654
return;
5755
}
5856

59-
if (htmlElementView != null) {
57+
_srcObject = stream;
58+
if (_htmlElementView != null) {
6059
findHtmlView()?.srcObject = stream?.jsStream;
6160
}
6261

@@ -70,55 +69,42 @@ class RTCVideoRenderer {
7069
_videoViews.add(x);
7170
return x;
7271
});
73-
htmlElementView = HtmlElementView(viewType: stream.id);
74-
if (onStateChanged != null) {
75-
onStateChanged();
76-
}
72+
73+
_htmlElementView = HtmlElementView(viewType: stream.id);
74+
onStateChanged?.call();
7775
}
7876

7977
void findAndApply(Size size) {
8078
final htmlView = findHtmlView();
81-
if (_srcObject != null && htmlView != null) {
82-
if (htmlView.width == size.width.toInt() &&
83-
htmlView.height == size.height.toInt()) return;
84-
htmlView.srcObject = _srcObject.jsStream;
85-
htmlView.width = size.width.toInt();
86-
htmlView.height = size.height.toInt();
87-
htmlView.onLoadedMetadata.listen((_) {
88-
if (htmlView.videoWidth != 0 &&
89-
htmlView.videoHeight != 0 &a E377 mp;&
90-
(_width != htmlView.videoWidth ||
91-
_height != htmlView.videoHeight)) {
92-
_width = htmlView.videoWidth.toDouble();
93-
_height = htmlView.videoHeight.toDouble();
94-
if (onVideoSizeChanged != null) {
95-
onVideoSizeChanged(0, _width, _height);
96-
}
97-
}
98-
if (!isFirstFrameRendered && onFirstFrameRendered != null) {
99-
onFirstFrameRendered();
100-
isFirstFrameRendered = true;
101-
}
102-
});
103-
htmlView.onResize.listen((_) {
104-
if (htmlView.videoWidth != 0 &&
105-
htmlView.videoHeight != 0 &&
106-
(_width != htmlView.videoWidth ||
107-
_height != htmlView.videoHeight)) {
108-
_width = htmlView.videoWidth.toDouble();
109-
_height = htmlView.videoHeight.toDouble();
110-
if (onVideoSizeChanged != null) {
111-
onVideoSizeChanged(0, _width, _height);
112-
}
113-
}
114-
});
115-
if (htmlView.videoWidth != 0 &&
116-
htmlView.videoHeight != 0 &&
117-
(_width != htmlView.videoWidth || _height != htmlView.videoHeight)) {
118-
_width = htmlView.videoWidth.toDouble();
119-
_height = htmlView.videoHeight.toDouble();
120-
if (onVideoSizeChanged != null) onVideoSizeChanged(0, _width, _height);
79+
if (_srcObject == null || htmlView == null) return;
80+
if (htmlView.width == size.width.toInt() &&
81+
htmlView.height == size.height.toInt()) return;
82+
83+
htmlView.srcObject = _srcObject.jsStream;
84+
htmlView.width = size.width.toInt();
85+
htmlView.height = size.height.toInt();
86+
87+
htmlView.onLoadedMetadata.listen((_) {
88+
_checkVideoSizeChanged(htmlView);
89+
90+
if (!_isFirstFrameRendered) {
91+
onFirstFrameRendered?.call();
92+
_isFirstFrameRendered = true;
12193
}
94+
});
95+
96+
htmlView.onResize.listen((_) => _checkVideoSizeChanged(htmlView));
97+
98+
_checkVideoSizeChanged(htmlView);
99+
}
100+
101+
void _checkVideoSizeChanged(html.VideoElement htmlView) {
102+
if (htmlView.videoWidth != 0 &&
103+
htmlView.videoHeight != 0 &&
104+
(_width != htmlView.videoWidth || _height != htmlView.videoHeight)) {
105+
_width = htmlView.videoWidth.toDouble();
106+
_height = htmlView.videoHeight.toDouble();
107+
onVideoSizeChanged?.call(0, _width, _height);
122108
}
123109
}
124110

@@ -133,7 +119,15 @@ class RTCVideoRenderer {
133119
return lastChild;
134120
}
135121

136-
Future<Null> dispose() async {
122+
///By calling the dispose you are safely disposing the MediaStream
123+
Future<void> dispose() async {
124+
await _srcObject?.dispose();
125+
126+
_srcObject = null;
127+
findHtmlView()?.srcObject = null;
128+
_videoViews.forEach((element) {
129+
element.srcObject = null;
130+
});
137131
// TODO(cloudwebrtc): ???
138132
// https://stackoverflow.com/questions/3258587/how-to-properly-unload-destroy-a-video-element/28060352
139133
}

0 commit comments

Comments
 (0)
0