protocol 8. (#134)

* protocol 8.

* add FastConnectOptions.

* update.

* fix.

* Deprecated the top connect method.

* Add fast connect button in example.

* Create a Listener before connecting.

* Force protocol-v7 for deprecated APIs.

* remove commented out line.

* relative path import.

* update.
This commit is contained in:
CloudWebRTC
2022-07-11 19:54:08 +08:00
committed by GitHub
parent 66d4052ada
commit 3d8bf96651
8 changed files with 122 additions and 18 deletions
+47 -11
View File
@@ -24,6 +24,7 @@ class _ConnectPageState extends State<ConnectPage> {
static const _storeKeySimulcast = 'simulcast'; static const _storeKeySimulcast = 'simulcast';
static const _storeKeyAdaptiveStream = 'adaptive-stream'; static const _storeKeyAdaptiveStream = 'adaptive-stream';
static const _storeKeyDynacast = 'dynacast'; static const _storeKeyDynacast = 'dynacast';
static const _storeKeyFastConnect = 'fast-connect';
final _uriCtrl = TextEditingController(); final _uriCtrl = TextEditingController();
final _tokenCtrl = TextEditingController(); final _tokenCtrl = TextEditingController();
@@ -31,6 +32,7 @@ class _ConnectPageState extends State<ConnectPage> {
bool _adaptiveStream = true; bool _adaptiveStream = true;
bool _dynacast = true; bool _dynacast = true;
bool _busy = false; bool _busy = false;
bool _fastConnect = false;
@override @override
void initState() { void initState() {
@@ -58,6 +60,7 @@ class _ConnectPageState extends State<ConnectPage> {
_simulcast = prefs.getBool(_storeKeySimulcast) ?? true; _simulcast = prefs.getBool(_storeKeySimulcast) ?? true;
_adaptiveStream = prefs.getBool(_storeKeyAdaptiveStream) ?? true; _adaptiveStream = prefs.getBool(_storeKeyAdaptiveStream) ?? true;
_dynacast = prefs.getBool(_storeKeyDynacast) ?? true; _dynacast = prefs.getBool(_storeKeyDynacast) ?? true;
_fastConnect = prefs.getBool(_storeKeyFastConnect) ?? false;
}); });
} }
@@ -69,6 +72,7 @@ class _ConnectPageState extends State<ConnectPage> {
await prefs.setBool(_storeKeySimulcast, _simulcast); await prefs.setBool(_storeKeySimulcast, _simulcast);
await prefs.setBool(_storeKeyAdaptiveStream, _adaptiveStream); await prefs.setBool(_storeKeyAdaptiveStream, _adaptiveStream);
await prefs.setBool(_storeKeyDynacast, _dynacast); await prefs.setBool(_storeKeyDynacast, _dynacast);
await prefs.setBool(_storeKeyFastConnect, _fastConnect);
} }
Future<void> _connect(BuildContext ctx) async { Future<void> _connect(BuildContext ctx) async {
@@ -84,24 +88,36 @@ class _ConnectPageState extends State<ConnectPage> {
print('Connecting with url: ${_uriCtrl.text}, ' print('Connecting with url: ${_uriCtrl.text}, '
'token: ${_tokenCtrl.text}...'); 'token: ${_tokenCtrl.text}...');
// Try to connect to a room //create new room
final room = Room();
// Create a Listener before connecting
final listener = room.createListener();
// Try to connect to the room
// This will throw an Exception if it fails for any reason. // This will throw an Exception if it fails for any reason.
final room = await LiveKitClient.connect( await room.connect(
_uriCtrl.text, _uriCtrl.text,
_tokenCtrl.text, _tokenCtrl.text,
roomOptions: RoomOptions( roomOptions: RoomOptions(
adaptiveStream: _adaptiveStream, adaptiveStream: _adaptiveStream,
dynacast: _dynacast, dynacast: _dynacast,
defaultVideoPublishOptions: VideoPublishOptions( defaultVideoPublishOptions: VideoPublishOptions(
simulcast: _simulcast, simulcast: _simulcast,
), ),
defaultScreenShareCaptureOptions: const ScreenShareCaptureOptions( defaultScreenShareCaptureOptions:
useiOSBroadcastExtension: true)), const ScreenShareCaptureOptions(useiOSBroadcastExtension: true),
),
fastConnectOptions: _fastConnect
? FastConnectOptions(
microphone: const TrackOption(enabled: true),
camera: const TrackOption(enabled: true),
)
: null,
); );
await Navigator.push<void>( await Navigator.push<void>(
ctx, ctx,
MaterialPageRoute(builder: (_) => RoomPage(room)), MaterialPageRoute(builder: (_) => RoomPage(room, listener)),
); );
} catch (error) { } catch (error) {
print('Could not connect $error'); print('Could not connect $error');
@@ -134,6 +150,13 @@ class _ConnectPageState extends State<ConnectPage> {
}); });
} }
void _setFastConnect(bool? value) async {
if (value == null || _fastConnect == value) return;
setState(() {
_fastConnect = value;
});
}
@override @override
Widget build(BuildContext context) => Scaffold( Widget build(BuildContext context) => Scaffold(
body: Container( body: Container(
@@ -195,6 +218,19 @@ class _ConnectPageState extends State<ConnectPage> {
], ],
), ),
), ),
Padding(
padding: const EdgeInsets.only(bottom: 5),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Text('Fast Connect'),
Switch(
value: _fastConnect,
onChanged: (value) => _setFastConnect(value),
),
],
),
),
Padding( Padding(
padding: const EdgeInsets.only(bottom: 25), padding: const EdgeInsets.only(bottom: 25),
child: Row( child: Row(
+10 -5
View File
@@ -11,9 +11,11 @@ import '../widgets/participant.dart';
class RoomPage extends StatefulWidget { class RoomPage extends StatefulWidget {
// //
final Room room; final Room room;
final EventsListener<RoomEvent> listener;
const RoomPage( const RoomPage(
this.room, { this.room,
this.listener, {
Key? key, Key? key,
}) : super(key: key); }) : super(key: key);
@@ -24,16 +26,19 @@ class RoomPage extends StatefulWidget {
class _RoomPageState extends State<RoomPage> { class _RoomPageState extends State<RoomPage> {
// //
List<Participant> participants = []; List<Participant> participants = [];
late final EventsListener<RoomEvent> _listener = widget.room.createListener(); EventsListener<RoomEvent> get _listener => widget.listener;
bool get fastConnection => widget.room.engine.fastConnectOptions != null;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
widget.room.addListener(_onRoomDidUpdate); widget.room.addListener(_onRoomDidUpdate);
_setUpListeners(); _setUpListeners();
_sortParticipants(); _sortParticipants();
WidgetsBindingCompatible.instance WidgetsBindingCompatible.instance?.addPostFrameCallback((_) {
?.addPostFrameCallback((_) => _askPublish()); if (!fastConnection) {
_askPublish();
}
});
} }
@override @override
+3
View File
@@ -64,6 +64,7 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
ConnectOptions connectOptions; ConnectOptions connectOptions;
RoomOptions roomOptions; RoomOptions roomOptions;
FastConnectOptions? fastConnectOptions;
bool _subscriberPrimary = false; bool _subscriberPrimary = false;
@@ -100,12 +101,14 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
String token, { String token, {
ConnectOptions? connectOptions, ConnectOptions? connectOptions,
RoomOptions? roomOptions, RoomOptions? roomOptions,
FastConnectOptions? fastConnectOptions,
}) async { }) async {
this.url = url; this.url = url;
this.token = token; this.token = token;
// update new options (if exists) // update new options (if exists)
this.connectOptions = connectOptions ?? this.connectOptions; this.connectOptions = connectOptions ?? this.connectOptions;
this.roomOptions = roomOptions ?? this.roomOptions; this.roomOptions = roomOptions ?? this.roomOptions;
this.fastConnectOptions = fastConnectOptions;
_updateConnectionState(ConnectionState.connecting); _updateConnectionState(ConnectionState.connecting);
+30
View File
@@ -14,6 +14,8 @@ import '../participant/remote.dart';
import '../proto/livekit_models.pb.dart' as lk_models; import '../proto/livekit_models.pb.dart' as lk_models;
import '../proto/livekit_rtc.pb.dart' as lk_rtc; import '../proto/livekit_rtc.pb.dart' as lk_rtc;
import '../support/disposable.dart'; import '../support/disposable.dart';
import '../track/local/audio.dart';
import '../track/local/video.dart';
import '../track/track.dart'; import '../track/track.dart';
import '../types/other.dart'; import '../types/other.dart';
import 'engine.dart'; import 'engine.dart';
@@ -117,12 +119,14 @@ class Room extends DisposableChangeNotifier with EventsEmittable<RoomEvent> {
String token, { String token, {
ConnectOptions? connectOptions, ConnectOptions? connectOptions,
RoomOptions? roomOptions, RoomOptions? roomOptions,
FastConnectOptions? fastConnectOptions,
}) => }) =>
engine.connect( engine.connect(
url, url,
token, token,
connectOptions: connectOptions, connectOptions: connectOptions,
roomOptions: roomOptions, roomOptions: roomOptions,
fastConnectOptions: fastConnectOptions,
); );
void _setUpSignalListeners() => _signalListener void _setUpSignalListeners() => _signalListener
@@ -141,6 +145,32 @@ class Room extends DisposableChangeNotifier with EventsEmittable<RoomEvent> {
info: event.response.participant, info: event.response.participant,
); );
if (connectOptions.protocolVersion.index >= ProtocolVersion.v8.index &&
engine.fastConnectOptions != null) {
var options = engine.fastConnectOptions!;
var audio = options.microphone;
if (audio.enabled != null && audio.enabled == true) {
_localParticipant!.setMicrophoneEnabled(true);
} else if (audio.track != null) {
_localParticipant!.publishAudioTrack(audio.track as LocalAudioTrack);
}
var video = options.camera;
if (video.enabled != null && video.enabled == true) {
_localParticipant!.setCameraEnabled(true);
} else if (video.track != null) {
_localParticipant!.publishVideoTrack(video.track as LocalVideoTrack);
}
var screen = options.screen;
if (screen.enabled != null && screen.enabled == true) {
_localParticipant!.setScreenShareEnabled(true);
} else if (screen.track != null) {
_localParticipant!.publishVideoTrack(screen.track as LocalVideoTrack);
}
}
for (final info in event.response.otherParticipants) { for (final info in event.response.otherParticipants) {
logger.fine('Creating RemoteParticipant: ${info.sid}(${info.identity}) ' logger.fine('Creating RemoteParticipant: ${info.sid}(${info.identity}) '
'tracks:${info.tracks.map((e) => e.sid)}'); 'tracks:${info.tracks.map((e) => e.sid)}');
+1
View File
@@ -45,6 +45,7 @@ extension ProtocolVersionExt on ProtocolVersion {
ProtocolVersion.v5: '5', ProtocolVersion.v5: '5',
ProtocolVersion.v6: '6', ProtocolVersion.v6: '6',
ProtocolVersion.v7: '7', ProtocolVersion.v7: '7',
ProtocolVersion.v8: '8',
}[this]!; }[this]!;
} }
+12 -1
View File
@@ -1,5 +1,6 @@
import 'core/room.dart'; import 'core/room.dart';
import 'options.dart'; import 'options.dart';
import 'types/other.dart';
/// Main entry point to connect to a room. /// Main entry point to connect to a room.
/// {@category Room} /// {@category Room}
@@ -9,6 +10,8 @@ class LiveKitClient {
/// Convenience method for connecting to a LiveKit server. /// Convenience method for connecting to a LiveKit server.
/// Returns a [Room] upon a successful connect or throws when it fails. /// Returns a [Room] upon a successful connect or throws when it fails.
/// Alternatively, it is possible to instantiate [Room] and call [Room.connect] directly. /// Alternatively, it is possible to instantiate [Room] and call [Room.connect] directly.
@Deprecated(
'Use `Room.connect` instead, This method is deprecated above Protocol v8.')
static Future<Room> connect( static Future<Room> connect(
String url, String url,
String token, { String token, {
@@ -16,11 +19,19 @@ class LiveKitClient {
RoomOptions? roomOptions, RoomOptions? roomOptions,
}) async { }) async {
final room = Room(); final room = Room();
ConnectOptions copyOptions = ConnectOptions(
autoSubscribe:
connectOptions != null ? connectOptions.autoSubscribe : true,
rtcConfiguration: connectOptions != null
? connectOptions.rtcConfiguration
: const RTCConfiguration(),
protocolVersion: ProtocolVersion.v7,
);
try { try {
await room.connect( await room.connect(
url, url,
token, token,
connectOptions: connectOptions, connectOptions: copyOptions,
roomOptions: roomOptions, roomOptions: roomOptions,
); );
return room; return room;
+18 -1
View File
@@ -8,6 +8,23 @@ import 'types/other.dart';
import 'types/video_encoding.dart'; import 'types/video_encoding.dart';
import 'types/video_parameters.dart'; import 'types/video_parameters.dart';
class TrackOption<E extends Object, T extends Object> {
final E? enabled;
final T? track;
const TrackOption({this.enabled, this.track});
}
class FastConnectOptions {
FastConnectOptions({
this.microphone = const TrackOption(enabled: false),
this.camera = const TrackOption(enabled: false),
this.screen = const TrackOption(enabled: false),
});
final TrackOption<bool, LocalAudioTrack> microphone;
final TrackOption<bool, LocalVideoTrack> camera;
final TrackOption<bool, LocalVideoTrack> screen;
}
/// Options used when connecting to the server. /// Options used when connecting to the server.
class ConnectOptions { class ConnectOptions {
/// Auto-subscribe to existing and new [RemoteTrackPublication]s after /// Auto-subscribe to existing and new [RemoteTrackPublication]s after
@@ -24,7 +41,7 @@ class ConnectOptions {
const ConnectOptions({ const ConnectOptions({
this.autoSubscribe = true, this.autoSubscribe = true,
this.rtcConfiguration = const RTCConfiguration(), this.rtcConfiguration = const RTCConfiguration(),
this.protocolVersion = ProtocolVersion.v7, this.protocolVersion = ProtocolVersion.v8,
}); });
} }
+1
View File
@@ -14,6 +14,7 @@ enum ProtocolVersion {
v5, v5,
v6, // Session migration v6, // Session migration
v7, // Remote unpublish v7, // Remote unpublish
v8,
} }
/// Connection state type used throughout the SDK. /// Connection state type used throughout the SDK.