Skip to content

Commit ccbc15b

Browse files
committed
fix: Get resource URL(Location) from the response headers.
1 parent b04fc67 commit ccbc15b

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

lib/src/whip.dart

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class WHIP {
2525
RTCPeerConnection? pc;
2626
late WhipMode mode;
2727
final String url;
28+
String? resourceURL;
2829
Map<String, String>? headers = {};
2930
String videoCodec = 'vp8';
3031
WHIP({required this.url, this.headers});
@@ -61,11 +62,8 @@ class WHIP {
6162
try {
6263
setState(WhipState.kConnecting);
6364
var desc = await pc!.createOffer();
64-
6565
setPreferredCodec(desc, videoCodec: videoCodec);
66-
6766
await pc!.setLocalDescription(desc);
68-
6967
var offer = await pc!.getLocalDescription();
7068
log.debug('Sending offer: $offer');
7169
var respose = await httpPost(Uri.parse(url),
@@ -74,6 +72,17 @@ class WHIP {
7472
if (headers != null) ...headers!
7573
},
7674
body: offer!.sdp);
75+
76+
if (respose.statusCode != 200 && respose.statusCode != 201) {
77+
throw Exception('Failed to send offer: ${respose.statusCode}');
78+
}
79+
80+
resourceURL = respose.headers['location'];
81+
if (resourceURL == null) {
82+
throw 'Resource url not found!';
83+
}
84+
85+
log.debug('Resource URL: $resourceURL');
7786
final answer = RTCSessionDescription(respose.body, 'answer');
7887
log.debug('Received answer: ${answer.sdp}');
7988
await pc!.setRemoteDescription(answer);
@@ -91,9 +100,11 @@ class WHIP {
91100
}
92101
log.debug('Closing whip connection');
93102
await pc?.close();
94-
95103
try {
96-
await httpDelete(Uri.parse(url));
104+
if (resourceURL == null) {
105+
throw 'Resource url not found!';
106+
}
107+
await httpDelete(Uri.parse(resourceURL ?? url));
97108
} catch (e) {
98109
log.error('connect error: $e');
99110
setState(WhipState.kFailure);
@@ -104,12 +115,15 @@ class WHIP {
104115
}
105116

106117
void onicecandidate(RTCIceCandidate? candidate) async {
118+
if (resourceURL == null) {
119+
throw 'Resource url not found!';
120+
}
107121
if (candidate == null) {
108122
return;
109123
}
110124
log.debug('Sending candidate: ${candidate.toMap().toString()}');
111125
try {
112-
var respose = await httpPatch(Uri.parse(url),
126+
var respose = await httpPatch(Uri.parse(resourceURL!),
113127
headers: {
114128
'Content-Type': 'application/trickle-ice-sdpfrag',
115129
if (headers != null) ...headers!

0 commit comments

Comments
 (0)