Skip to content

Commit

Permalink
Make RtpSender.getCapabilities static (#154)
Browse files Browse the repository at this point in the history
Co-authored-by: evdokimovs <evdokimovs@protonmail.ch>
  • Loading branch information
logist322 and evdokimovs committed Apr 17, 2024
1 parent 20b07e0 commit c742876
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 25 deletions.
33 changes: 22 additions & 11 deletions example/integration_test/webrtc_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,7 @@ void main() {

testWidgets('Get capabilities', (WidgetTester tester) async {
if (!Platform.isAndroid && !Platform.isIOS) {
var pc = await PeerConnection.create(IceTransportType.all, []);
var t1 = await pc.addTransceiver(
MediaKind.video, RtpTransceiverInit(TransceiverDirection.sendRecv));

var capabilities = await t1.sender.getCapabilities(MediaKind.video);
var capabilities = await RtpSender.getCapabilities(MediaKind.video);

expect(
capabilities.codecs
Expand All @@ -267,28 +263,43 @@ void main() {
});

testWidgets('SetCodecPreferences', (WidgetTester tester) async {
var pc = await PeerConnection.create(IceTransportType.all, []);
var vtrans = await pc.addTransceiver(
var pc1 = await PeerConnection.create(IceTransportType.all, []);
var pc2 = await PeerConnection.create(IceTransportType.all, []);

var vtrans = await pc1.addTransceiver(
MediaKind.video, RtpTransceiverInit(TransceiverDirection.sendRecv));

var capabilities = await vtrans.sender.getCapabilities(MediaKind.video);
var capabilities = await RtpSender.getCapabilities(MediaKind.video);

var names = capabilities.codecs.map((c) => c.name).toList();
expect(names.contains("VP9"), isTrue);
expect(names.contains("VP8"), isTrue);

var h264Preferences = capabilities.codecs.where((element) {
var vp8Preferences = capabilities.codecs.where((element) {
return element.name == 'VP8';
}).toList();

await vtrans.setCodecPreferences(h264Preferences);
await vtrans.setCodecPreferences(vp8Preferences);

var offer = await pc.createOffer();
var offer = await pc1.createOffer();

expect(offer.description.contains("VP8"), isTrue);
expect(offer.description.contains("H264"), isFalse);
expect(offer.description.contains("VP9"), isFalse);
expect(offer.description.contains("AV1"), isFalse);

await pc2.setRemoteDescription(offer);

var answer = await pc2.createAnswer();

expect(answer.description.contains("VP8"), isTrue);
expect(answer.description.contains("H264"), isFalse);
expect(answer.description.contains("VP9"), isFalse);
expect(answer.description.contains("AV1"), isFalse);

await pc1.close();
await pc2.close();
await vtrans.dispose();
});

testWidgets('Get transceivers', (WidgetTester tester) async {
Expand Down
1 change: 1 addition & 0 deletions lib/medea_flutter_webrtc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export 'src/api/transceiver.dart' if (dart.library.html) 'none.dart';
export 'src/api/parameters.dart' if (dart.library.html) 'none.dart';
export 'src/api/send_encoding_parameters.dart'
if (dart.library.html) 'none.dart';
export 'src/model/capability.dart' if (dart.library.html) 'none.dart';
export 'src/model/constraints.dart' if (dart.library.html) 'none.dart';
export 'src/model/constraints.dart' show FacingMode;
export 'src/model/device.dart' if (dart.library.html) 'none.dart';
Expand Down
34 changes: 20 additions & 14 deletions lib/src/api/sender.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,24 @@ abstract class RtpSender {
Future<void> setParameters(RtpParameters parameters);

/// [RtpCapabilities] of an RTP sender of the specified [MediaKind].
Future<RtpCapabilities> getCapabilities(MediaKind kind);
static Future<RtpCapabilities> getCapabilities(MediaKind kind) {
if (isDesktop) {
return _RtpSenderFFI.getCapabilities(kind);
} else {
return _RtpSenderChannel.getCapabilities(kind);
}
}
}

/// [MethodChannel]-based implementation of a [RtpSender].
class _RtpSenderChannel extends RtpSender {
/// [RtpCapabilities] of an RTP sender of the specified [MediaKind].
static Future<RtpCapabilities> getCapabilities(MediaKind kind) async {
var map = await _peerConnectionFactoryMethodChannel
.invokeMethod('getRtpSenderCapabilities', {'kind': kind.index});
return RtpCapabilities.fromMap(map);
}

/// Creates an [RtpSender] basing on the [Map] received from the native side.
_RtpSenderChannel.fromMap(dynamic map) {
_chan = methodChannel('RtpSender', map['channelId']);
Expand Down Expand Up @@ -81,17 +94,16 @@ class _RtpSenderChannel extends RtpSender {
Future<void> setParameters(RtpParameters parameters) async {
await _chan.invokeListMethod('setParameters', parameters.toMap());
}

@override
Future<RtpCapabilities> getCapabilities(MediaKind kind) async {
var map = await _peerConnectionFactoryMethodChannel
.invokeMethod('getRtpSenderCapabilities', {'kind': kind.index});
return RtpCapabilities.fromMap(map);
}
}

/// FFI-based implementation of a [RtpSender].
class _RtpSenderFFI extends RtpSender {
/// [RtpCapabilities] of an RTP sender of the specified [MediaKind].
static Future<RtpCapabilities> getCapabilities(MediaKind kind) async {
return RtpCapabilities.fromFFI(await api!
.getRtpSenderCapabilities(kind: ffi.MediaType.values[kind.index]));
}

/// Native side peer connection.
final ffi.ArcPeerConnection _peer;

Expand Down Expand Up @@ -122,10 +134,4 @@ class _RtpSenderFFI extends RtpSender {
await api!.senderSetParameters(
transceiver: _transceiver, params: parameters.toFFI());
}

@override
Future<RtpCapabilities> getCapabilities(MediaKind kind) async {
return RtpCapabilities.fromFFI(await api!
.getRtpSenderCapabilities(kind: ffi.MediaType.values[kind.index]));
}
}

0 comments on commit c742876

Please sign in to comment.