Skip to content

Commit 6fd73bb

Browse files
authored
feat: Use PlatformView for video rendering on iOS to improve performance and reduce device heat. (#509)
* feat: Use PlatformView for iOS to improve performance and reduce device heat. * Update pubspec.yaml * update. * Update pubspec.yaml * update. * update. * update. * update.
1 parent c1b965d commit 6fd73bb

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
lines changed

example/lib/pages/prejoin.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ class _PreJoinPageState extends State<PreJoinPage> {
271271
color: Colors.black54,
272272
child: _videoTrack != null
273273
? VideoTrackRenderer(
274+
renderMode: VideoRenderMode.auto,
274275
_videoTrack!,
275276
fit: RTCVideoViewObjectFit
276277
.RTCVideoViewObjectFitContain,

example/lib/widgets/participant.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ abstract class _ParticipantWidgetState<T extends ParticipantWidget>
142142
onTap: () => setState(() => _visible = !_visible),
143143
child: activeVideoTrack != null && !activeVideoTrack!.muted
144144
? VideoTrackRenderer(
145+
renderMode: VideoRenderMode.auto,
145146
activeVideoTrack!,
146147
fit: RTCVideoViewObjectFit.RTCVideoViewObjectFitContain,
147148
)

lib/src/widgets/video_track_renderer.dart

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,24 @@ enum VideoViewMirrorMode {
3434
mirror,
3535
}
3636

37+
enum VideoRenderMode {
38+
auto,
39+
texture,
40+
platformView,
41+
}
42+
3743
/// Widget that renders a [VideoTrack].
3844
class VideoTrackRenderer extends StatefulWidget {
3945
final VideoTrack track;
4046
final rtc.RTCVideoViewObjectFit fit;
4147
final VideoViewMirrorMode mirrorMode;
48+
final VideoRenderMode renderMode;
4249

4350
const VideoTrackRenderer(
4451
this.track, {
4552
this.fit = rtc.RTCVideoViewObjectFit.RTCVideoViewObjectFitContain,
4653
this.mirrorMode = VideoViewMirrorMode.auto,
54+
this.renderMode = VideoRenderMode.auto,
4755
Key? key,
4856
}) : super(key: key);
4957

@@ -52,14 +60,17 @@ class VideoTrackRenderer extends StatefulWidget {
5260
}
5361

5462
class _VideoTrackRendererState extends State<VideoTrackRenderer> {
55-
rtc.RTCVideoRenderer? _renderer;
63+
rtc.VideoRenderer? _renderer;
5664
// for flutter web only.
5765
bool _rendererReadyForWeb = false;
5866
EventsListener<TrackEvent>? _listener;
5967
// Used to compute visibility information
6068
late GlobalKey _internalKey;
6169

62-
Future<rtc.RTCVideoRenderer> _initializeRenderer() async {
70+
Future<rtc.VideoRenderer> _initializeRenderer() async {
71+
if (widget.renderMode == VideoRenderMode.platformView) {
72+
return Null as Future<rtc.VideoRenderer>;
73+
}
6374
_renderer ??= rtc.RTCVideoRenderer();
6475
await _renderer!.initialize();
6576
await _attach();
@@ -139,7 +150,7 @@ class _VideoTrackRendererState extends State<VideoTrackRenderer> {
139150
widget.track.onVideoViewBuild?.call(_internalKey);
140151
});
141152
return rtc.RTCVideoView(
142-
_renderer!,
153+
_renderer! as rtc.RTCVideoRenderer,
143154
mirror: _shouldMirror(),
144155
filterQuality: FilterQuality.medium,
145156
objectFit: widget.fit,
@@ -150,7 +161,9 @@ class _VideoTrackRendererState extends State<VideoTrackRenderer> {
150161
Widget _videoViewForNative() => FutureBuilder(
151162
future: _initializeRenderer(),
152163
builder: (context, snapshot) {
153-
if (snapshot.hasData && _renderer != null) {
164+
if ((snapshot.hasData && _renderer != null) ||
165+
[VideoRenderMode.auto, VideoRenderMode.platformView]
166+
.contains(widget.renderMode)) {
154167
return Builder(
155168
key: _internalKey,
156169
builder: (ctx) {
@@ -159,8 +172,21 @@ class _VideoTrackRendererState extends State<VideoTrackRenderer> {
159172
?.addPostFrameCallback((timeStamp) {
160173
widget.track.onVideoViewBuild?.call(_internalKey);
161174
});
175+
if (lkPlatformIs(PlatformType.iOS) &&
176+
[VideoRenderMode.auto, VideoRenderMode.platformView]
177+
.contains(widget.renderMode)) {
178+
return rtc.RTCVideoPlatFormView(
179+
mirror: _shouldMirror(),
180+
objectFit: widget.fit,
181+
onViewReady: (controller) {
182+
_renderer = controller;
183+
_renderer?.srcObject = widget.track.mediaStream;
184+
_attach();
185+
},
186+
);
187+
}
162188
return rtc.RTCVideoView(
163-
_renderer!,
189+
_renderer! as rtc.RTCVideoRenderer,
164190
mirror: _shouldMirror(),
165191
filterQuality: FilterQuality.medium,
166192
objectFit: widget.fit,

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ dependencies:
3737
uuid: '>=3.0.6'
3838
synchronized: ^3.0.0+3
3939
protobuf: ^3.0.0
40-
flutter_webrtc: ^0.11.5
40+
flutter_webrtc: ^0.11.6
4141
device_info_plus: '>=8.0.0'
4242
js: '>=0.6.4'
4343
platform_detect: ^2.0.7

0 commit comments

Comments
 (0)