Skip to content

Commit

Permalink
Fix/some bugs reported by users (#441)
Browse files Browse the repository at this point in the history
* Set different rendering methods for web and native.

* Revert a modification that issued an exception.

* Update video_track_renderer.dart

* dart format.

* update.
  • Loading branch information
cloudwebrtc authored Dec 28, 2023
1 parent 42ad127 commit 1015b57
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 16 deletions.
3 changes: 0 additions & 3 deletions lib/src/core/engine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -830,11 +830,8 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
}) async {
final previousAnswer =
(await subscriber?.pc.getLocalDescription())?.toPBType();
final previousOffer =
(await publisher?.pc.getLocalDescription())?.toPBType();
signalClient.sendSyncState(
answer: previousAnswer,
offer: previousOffer,
subscription: subscription,
publishTracks: publishTracks,
dataChannelInfo: dataChannelInfo(),
Expand Down
2 changes: 0 additions & 2 deletions lib/src/core/signal_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -523,15 +523,13 @@ extension SignalClientRequests on SignalClient {
@internal
void sendSyncState({
required lk_rtc.SessionDescription? answer,
required lk_rtc.SessionDescription? offer,
required lk_rtc.UpdateSubscription subscription,
required Iterable<lk_rtc.TrackPublishedResponse>? publishTracks,
required Iterable<lk_rtc.DataChannelInfo>? dataChannelInfo,
}) =>
_sendRequest(lk_rtc.SignalRequest(
syncState: lk_rtc.SyncState(
answer: answer,
offer: offer,
subscription: subscription,
publishTracks: publishTracks,
dataChannels: dataChannelInfo,
Expand Down
61 changes: 50 additions & 11 deletions lib/src/widgets/video_track_renderer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import 'package:flutter_webrtc/flutter_webrtc.dart' as rtc;
Expand Down Expand Up @@ -52,10 +53,19 @@ class VideoTrackRenderer extends StatefulWidget {

class _VideoTrackRendererState extends State<VideoTrackRenderer> {
rtc.RTCVideoRenderer? _renderer;
// for flutter web only.
bool _rendererReadyForWeb = false;
EventsListener<TrackEvent>? _listener;
// Used to compute visibility information
late GlobalKey _internalKey;

Future<rtc.RTCVideoRenderer> _initializeRenderer() async {
_renderer ??= rtc.RTCVideoRenderer();
await _renderer!.initialize();
await _attach();
return _renderer!;
}

void disposeRenderer() {
try {
_renderer?.srcObject = null;
Expand All @@ -70,12 +80,12 @@ class _VideoTrackRendererState extends State<VideoTrackRenderer> {
void initState() {
super.initState();
_internalKey = widget.track.addViewKey();
() async {
_renderer ??= rtc.RTCVideoRenderer();
await _renderer!.initialize();
await _attach();
setState(() {});
}();
if (kIsWeb) {
() async {
await _initializeRenderer();
setState(() => _rendererReadyForWeb = true);
}();
}
}

@override
Expand Down Expand Up @@ -118,9 +128,9 @@ class _VideoTrackRendererState extends State<VideoTrackRenderer> {
}
}

@override
Widget build(BuildContext context) => _renderer != null
? Builder(
Widget _videoViewForWeb() => !_rendererReadyForWeb
? Container()
: Builder(
key: _internalKey,
builder: (ctx) {
// let it render before notifying build
Expand All @@ -135,8 +145,37 @@ class _VideoTrackRendererState extends State<VideoTrackRenderer> {
objectFit: widget.fit,
);
},
)
: Container();
);

Widget _videoViewForNative() => FutureBuilder(
future: _initializeRenderer(),
builder: (context, snapshot) {
if (snapshot.hasData && _renderer != null) {
return Builder(
key: _internalKey,
builder: (ctx) {
// let it render before notifying build
WidgetsBindingCompatible.instance
?.addPostFrameCallback((timeStamp) {
widget.track.onVideoViewBuild?.call(_internalKey);
});
return rtc.RTCVideoView(
_renderer!,
mirror: _shouldMirror(),
filterQuality: FilterQuality.medium,
objectFit: widget.fit,
);
},
);
}
return Container();
});

// FutureBuilder will cause flickering for flutter web. so using
// different rendering methods for web and native.
@override
Widget build(BuildContext context) =>
kIsWeb ? _videoViewForWeb() : _videoViewForNative();

bool _shouldMirror() {
// off for screen share
Expand Down

0 comments on commit 1015b57

Please sign in to comment.