如何在Flutter中从mediamtx监听webrtc流?

问题描述 投票:0回答:1

我无法找到有关如何在 flutter 移动应用程序和 mediamtx 服务器之间正确建立连接的详细信息,从文档来看我应该使用 WHIP,但除了一般文档页面之外没有给出更多详细信息,这也不是很清楚.

我尽我所能搜索并进行了尝试,但无法找到任何明确的答案。 我应该与哪些端点进行通信,或者我在哪里可以找到更多相关信息?

android flutter webrtc video-streaming
1个回答
0
投票

您需要使用内容类型 application/sdp 和本地 SDP 向 HTTP:///stream/whep 发送 http post 请求。作为回应,您将收到远程答复。以下代码可能会有所帮助

Future<void> createOffer() async {
    
    // create an offer 
    RTCSessionDescription offer = await _peerConnection.createOffer({
      'mandatory': {
        'OfferToReceiveAudio': false,
        'OfferToReceiveVideo': true
      }
    });
    // set the local Description to offer; 
    await _peerConnection.setLocalDescription(offer);




    // Send the offer to the MediaSoup server via HTTP and handle the SDP answer
    String basicAuth = 'Basic ' + base64Encode(utf8.encode($username:$password));
   
    var response = await http.post(
      Uri.parse('$serverUrl'),
      headers: {
        'Content-Type': 'application/sdp',
        'Authorization': basicAuth
      },
      body:  offer.sdp,
    );

    if (response.statusCode == 201) {
      final location = response.headers['location'];
      print('location :' + location.toString());
      if (location == null || location.isEmpty) {
          throw Exception('Failed to send offer: No location header');
        }
      var remoteDescription = RTCSessionDescription(response.body.toString(),'answer');
      await _peerConnection.setRemoteDescription(remoteDescription);
    } else {
      throw Exception('Failed to post offer');
    }
  }

© www.soinside.com 2019 - 2024. All rights reserved.