Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null safety support #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions lib/flutter_live.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,17 @@ class RealtimePlayer {
/// streamUrl: "webrtc://d.ossrs.net:11985/live/livestream"
class WebRTCUri {
/// The api server url for WebRTC streaming.
String api;
String? api;
/// The stream url to play or publish.
String streamUrl;
String? streamUrl;

/// Parse the url to WebRTC uri.
static WebRTCUri parse(String url) {
Uri uri = Uri.parse(url);

var schema = 'https'; // For native, default to HTTPS
if (uri.queryParameters.containsKey('schema')) {
schema = uri.queryParameters['schema'];
schema = uri.queryParameters['schema']!;
} else {
schema = 'https';
}
Expand All @@ -134,7 +134,7 @@ class WebRTCUri {

var api = '/rtc/v1/play/';
if (uri.queryParameters.containsKey('play')) {
api = uri.queryParameters['play'];
api = uri.queryParameters['play']!;
}

var apiParams = [];
Expand All @@ -159,8 +159,8 @@ class WebRTCUri {

/// A WebRTC player, using [flutter_webrtc](https://pub.dev/packages/flutter_webrtc)
class WebRTCPlayer {
webrtc.AddStreamCallback _onRemoteStream;
webrtc.RTCPeerConnection _pc;
webrtc.AddStreamCallback? _onRemoteStream;
webrtc.RTCPeerConnection? _pc;

/// When got a remote stream.
set onRemoteStream(webrtc.AddStreamCallback v) {
Expand All @@ -175,7 +175,7 @@ class WebRTCPlayer {
/// [url] must a path parsed by [WebRTCUri.parse] in https://github.com/rtcdn/rtcdn-draft
Future<void> play(String url) async {
if (_pc != null) {
await _pc.close();
await _pc!.close();
}

// Create the peer connection.
Expand All @@ -187,36 +187,36 @@ class WebRTCPlayer {
print('WebRTC: createPeerConnection done');

// Setup the peer connection.
_pc.onAddStream = (webrtc.MediaStream stream) {
_pc!.onAddStream = (webrtc.MediaStream stream) {
print('WebRTC: got stream ${stream.id}');
if (_onRemoteStream == null) {
print('Warning: Stream ${stream.id} is leak');
return;
}
_onRemoteStream(stream);
_onRemoteStream!(stream);
};

_pc.addTransceiver(
_pc!.addTransceiver(
kind: webrtc.RTCRtpMediaType.RTCRtpMediaTypeAudio,
init: webrtc.RTCRtpTransceiverInit(direction: webrtc.TransceiverDirection.RecvOnly),
);
_pc.addTransceiver(
_pc!.addTransceiver(
kind: webrtc.RTCRtpMediaType.RTCRtpMediaTypeVideo,
init: webrtc.RTCRtpTransceiverInit(direction: webrtc.TransceiverDirection.RecvOnly),
);
print('WebRTC: Setup PC done, A|V RecvOnly');

// Start SDP handshake.
webrtc.RTCSessionDescription offer = await _pc.createOffer({
webrtc.RTCSessionDescription offer = await _pc!.createOffer({
'mandatory': {'OfferToReceiveAudio': true, 'OfferToReceiveVideo': true},
});
await _pc.setLocalDescription(offer);
print('WebRTC: createOffer, ${offer.type} is ${offer.sdp.replaceAll('\n', '\\n').replaceAll('\r', '\\r')}');
await _pc!.setLocalDescription(offer);
print('WebRTC: createOffer, ${offer.type} is ${offer.sdp!.replaceAll('\n', '\\n').replaceAll('\r', '\\r')}');

webrtc.RTCSessionDescription answer = await _handshake(url, offer.sdp);
print('WebRTC: got ${answer.type} is ${answer.sdp.replaceAll('\n', '\\n').replaceAll('\r', '\\r')}');
webrtc.RTCSessionDescription answer = await _handshake(url, offer.sdp!);
print('WebRTC: got ${answer.type} is ${answer.sdp!.replaceAll('\n', '\\n').replaceAll('\r', '\\r')}');

await _pc.setRemoteDescription(answer);
await _pc!.setRemoteDescription(answer);
}

/// Handshake to exchange SDP, send offer and got answer.
Expand All @@ -238,7 +238,7 @@ class WebRTCPlayer {
// {api: "xxx", sdp: "offer", streamurl: "webrtc://d.ossrs.net:11985/live/livestream"}
// Response:
// {code: 0, sdp: "answer", sessionid: "007r51l7:X2Lv"}
HttpClientRequest req = await client.postUrl(Uri.parse(uri.api));
HttpClientRequest req = await client.postUrl(Uri.parse(uri.api!));
req.headers.set('Content-Type', 'application/json');
req.add(utf8.encode(json.encode({'api': uri.api, 'streamurl': uri.streamUrl, 'sdp': offer})));
print('WebRTC request: ${uri.api} offer=${offer.length}B');
Expand All @@ -261,7 +261,7 @@ class WebRTCPlayer {
/// Dispose the player.
void dispose() {
if (_pc != null) {
_pc.close();
_pc!.close();
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ description: Live streaming player, iOS+Android, RTMP/HTTP-FLV/HLS/WebRTC, by Fl
homepage: https://github.com/ossrs/flutter_live

environment:
sdk: ">=2.7.0 <3.0.0"
sdk: ">=2.12.0 <3.0.0"
flutter: ">=1.20.0 <2.0.0"

dependencies:
flutter:
sdk: flutter
fijkplayer: ^0.8.8
flutter_webrtc: ^0.6.3
camera_with_rtmp: ^0.3.2
camera_with_rtmp:
git:
url: https://github.com/babulpatel1309/flutter_rtmppublisher.git
ref: 963c0b82cd94f09f1e4387d33a89e3b067957c19


dev_dependencies:
flutter_test:
Expand Down