-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic Jason integration with
flutter_webrtc
library (#10)
- add bindings for `PeerConnection`, `MediaStreamTrack`, `IceCandidate` and `Transceiver` - add ability to return `ForeignValue` from Dart without memory leaking - add support for Dart callbacks with two arguments - adapt internal Jason implementations for working with async `flutter_webrtc` methods Additionally: - use `instrumentisto/flutter-webrtc` fork instead of upstream
- Loading branch information
1 parent
2ffa445
commit 23ca7a5
Showing
38 changed files
with
2,953 additions
and
453 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,18 @@ | ||
import 'dart:ffi'; | ||
|
||
import 'object.dart' as object; | ||
import 'media_track.dart' as media_track; | ||
import 'peer_connection.dart' as peer_connection; | ||
import 'transceiver.dart' as transceiver; | ||
import 'ice_servers.dart' as ice_servers; | ||
import 'ice_candidate.dart' as ice_candidate; | ||
|
||
/// Registers functions needed for platform utils working. | ||
void registerFunctions(DynamicLibrary dl) { | ||
object.registerFunctions(dl); | ||
media_track.registerFunctions(dl); | ||
peer_connection.registerFunctions(dl); | ||
transceiver.registerFunctions(dl); | ||
ice_servers.registerFunctions(dl); | ||
ice_candidate.registerFunctions(dl); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import 'dart:ffi'; | ||
|
||
import 'package:flutter_webrtc/flutter_webrtc.dart'; | ||
import 'package:medea_jason/src/native/ffi/foreign_value.dart'; | ||
|
||
/// Registers functions allowing Rust to create Dart [RTCIceCandidate]s. | ||
void registerFunctions(DynamicLibrary dl) { | ||
dl.lookupFunction<Void Function(Pointer), void Function(Pointer)>( | ||
'register_IceCandidate__new')( | ||
Pointer.fromFunction< | ||
Handle Function( | ||
ForeignValue, ForeignValue, ForeignValue)>(newRtcIceCandidate)); | ||
dl.lookupFunction<Void Function(Pointer), void Function(Pointer)>( | ||
'register_IceCandidate__candidate')( | ||
Pointer.fromFunction<Pointer Function(Handle)>(candidate)); | ||
dl.lookupFunction<Void Function(Pointer), void Function(Pointer)>( | ||
'register_IceCandidate__sdp_m_line_index')( | ||
Pointer.fromFunction<Pointer Function(Handle)>(sdpMLineIndex)); | ||
dl.lookupFunction<Void Function(Pointer), void Function(Pointer)>( | ||
'register_IceCandidate__sdp_mid')( | ||
Pointer.fromFunction<Pointer Function(Handle)>(sdpMid)); | ||
} | ||
|
||
/// Returns the provided [RTCIceCandidate] [String]. | ||
Pointer candidate(RTCIceCandidate iceCandidate) { | ||
if (iceCandidate.candidate != null) { | ||
return ForeignValue.fromString(iceCandidate.candidate!).intoRustOwned(); | ||
} else { | ||
return ForeignValue.none().intoRustOwned(); | ||
} | ||
} | ||
|
||
/// Returns SDP M line index of the provided [RTCIceCandidate]. | ||
Pointer sdpMLineIndex(RTCIceCandidate iceCandidate) { | ||
if (iceCandidate.sdpMlineIndex != null) { | ||
return ForeignValue.fromInt(iceCandidate.sdpMlineIndex!).intoRustOwned(); | ||
} else { | ||
return ForeignValue.none().intoRustOwned(); | ||
} | ||
} | ||
|
||
/// Returns SDP MID of the provided [RTCIceCandidate]. | ||
Pointer sdpMid(RTCIceCandidate iceCandidate) { | ||
if (iceCandidate.sdpMid != null) { | ||
return ForeignValue.fromString(iceCandidate.sdpMid!).intoRustOwned(); | ||
} else { | ||
return ForeignValue.none().intoRustOwned(); | ||
} | ||
} | ||
|
||
/// Creates a new [RTCIceCandidate] with the provided values. | ||
Object newRtcIceCandidate( | ||
ForeignValue candidate, ForeignValue sdpMid, ForeignValue sdpMlineIndex) { | ||
var candidateArg = candidate.toDart(); | ||
var sdpMidArg = sdpMid.toDart(); | ||
var sdpMLineIndexArg = sdpMlineIndex.toDart(); | ||
return RTCIceCandidate(candidateArg, sdpMidArg, sdpMLineIndexArg); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import 'dart:ffi'; | ||
import 'package:ffi/ffi.dart'; | ||
import 'package:flutter_webrtc/flutter_webrtc.dart'; | ||
import 'package:medea_jason/src/native/ffi/foreign_value.dart'; | ||
|
||
/// Registers [RTCPeerConnection] ICE servers related functions in Rust. | ||
void registerFunctions(DynamicLibrary dl) { | ||
dl.lookupFunction<Void Function(Pointer), void Function(Pointer)>( | ||
'register_IceServers__new')( | ||
Pointer.fromFunction<Handle Function()>(newIceServers)); | ||
dl.lookupFunction<Void Function(Pointer), void Function(Pointer)>( | ||
'register_IceServers__add')(Pointer.fromFunction< | ||
Void Function(Handle, Pointer<Utf8>, ForeignValue, ForeignValue)>( | ||
addIceServer)); | ||
} | ||
|
||
/// Returns a new empty `IceServer`s [List]. | ||
Object newIceServers() { | ||
return List.empty(growable: true); | ||
} | ||
|
||
/// Adds an `IceServer` with the provided data to the provided [List]. | ||
void addIceServer(Object servers, Pointer<Utf8> url, ForeignValue username, | ||
ForeignValue credentials) { | ||
servers as List; | ||
var iceServer = {'url': url.toDartString()}; | ||
username = username.toDart(); | ||
if (username is String) { | ||
iceServer['username'] = username as String; | ||
} | ||
credentials = credentials.toDart(); | ||
if (credentials is String) { | ||
iceServer['credentials'] = credentials as String; | ||
} | ||
servers.add(iceServer); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import 'package:flutter_webrtc/flutter_webrtc.dart'; | ||
import 'dart:ffi'; | ||
import 'package:ffi/ffi.dart'; | ||
|
||
/// Registers [MediaStreamTrack] related functions in Rust. | ||
void registerFunctions(DynamicLibrary dl) { | ||
dl.lookupFunction<Void Function(Pointer), void Function(Pointer)>( | ||
'register_MediaStreamTrack__id')( | ||
Pointer.fromFunction<Pointer<Utf8> Function(Handle)>(id)); | ||
dl.lookupFunction<Void Function(Pointer), void Function(Pointer)>( | ||
'register_MediaStreamTrack__device_id')( | ||
Pointer.fromFunction<Pointer<Utf8> Function(Handle)>(deviceId)); | ||
dl.lookupFunction<Void Function(Pointer), void Function(Pointer)>( | ||
'register_MediaStreamTrack__facing_mode')( | ||
Pointer.fromFunction<Int64 Function(Handle)>(facingMode, 0)); | ||
dl.lookupFunction<Void Function(Pointer), void Function(Pointer)>( | ||
'register_MediaStreamTrack__kind')( | ||
Pointer.fromFunction<Int64 Function(Handle)>(kind, 0)); | ||
dl.lookupFunction<Void Function(Pointer), void Function(Pointer)>( | ||
'register_MediaStreamTrack__height')( | ||
Pointer.fromFunction<Int64 Function(Handle)>(height, 0)); | ||
dl.lookupFunction<Void Function(Pointer), void Function(Pointer)>( | ||
'register_MediaStreamTrack__width')( | ||
Pointer.fromFunction<Int64 Function(Handle)>(width, 0)); | ||
dl.lookupFunction<Void Function(Pointer), void Function(Pointer)>( | ||
'register_MediaStreamTrack__set_enabled')( | ||
Pointer.fromFunction<Void Function(Handle, Int8)>(setEnabled)); | ||
dl.lookupFunction<Void Function(Pointer), void Function(Pointer)>( | ||
'register_MediaStreamTrack__enabled')( | ||
Pointer.fromFunction<Int8 Function(Handle)>(enabled, 0)); | ||
dl.lookupFunction<Void Function(Pointer), void Function(Pointer)>( | ||
'register_MediaStreamTrack__stop')( | ||
Pointer.fromFunction<Void Function(Handle)>(stop)); | ||
dl.lookupFunction<Void Function(Pointer), void Function(Pointer)>( | ||
'register_MediaStreamTrack__on_ended')( | ||
Pointer.fromFunction<Void Function(Handle, Handle)>(onEnded)); | ||
} | ||
|
||
/// Returns ID of the provided [MediaStreamTrack]. | ||
Pointer<Utf8> id(MediaStreamTrack track) { | ||
return track.id!.toNativeUtf8(); | ||
} | ||
|
||
/// Returns kind of the provided [MediaStreamTrack]. | ||
int kind(MediaStreamTrack track) { | ||
if (track.kind == 'audio') { | ||
return 0; | ||
} else { | ||
return 1; | ||
} | ||
} | ||
|
||
/// Subscribes on the [MediaStreamTrack.onEnded] of the provided | ||
/// [MediaStreamTrack]. | ||
void onEnded(MediaStreamTrack track, Function f) { | ||
track.onEnded = () { | ||
f(); | ||
}; | ||
} | ||
|
||
/// Returns device ID of the provided [MediaStreamTrack]. | ||
Pointer<Utf8> deviceId(MediaStreamTrack track) { | ||
// TODO: Correct implementation requires flutter_webrtc-side fixes. | ||
return id(track); | ||
} | ||
|
||
/// Returns facingMode of the provided [MediaStreamTrack]. | ||
int facingMode(MediaStreamTrack track) { | ||
// TODO: Correct implementation requires flutter_webrtc-side fixes. | ||
return 0; | ||
} | ||
|
||
/// Returns height of the video of the provided [MediaStreamTrack]. | ||
int height(MediaStreamTrack track) { | ||
// TODO: Correct implementation requires flutter_webrtc-side fixes. | ||
return 1600; | ||
} | ||
|
||
/// Returns width of the video of the provided [MediaStreamTrack]. | ||
int width(MediaStreamTrack track) { | ||
// TODO: Correct implementation requires flutter_webrtc-side fixes. | ||
return 1300; | ||
} | ||
|
||
/// Sets [MediaStreamTrack.enabled] state of the provided [MediaStreamTrack]. | ||
void setEnabled(MediaStreamTrack track, int enabled) { | ||
track.enabled = enabled == 1; | ||
} | ||
|
||
/// Stops provided [MediaStreamTrack]. | ||
void stop(MediaStreamTrack track) { | ||
track.stop(); | ||
} | ||
|
||
/// Returns `1` if the provided [MediaStreamTrack] is enabled and `0` otherwise. | ||
int enabled(MediaStreamTrack track) { | ||
return track.enabled ? 1 : 0; | ||
} |
Oops, something went wrong.