webrtc icon indicating copy to clipboard operation
webrtc copied to clipboard

Use WebRTC as client for Mediasoup

Open kristian1108 opened this issue 1 year ago • 0 comments

Hi -- I'm trying to use this library as a client for a MediaSoup server, talking to a MediaSoup producer. See https://docs.rs/mediasoup/latest/mediasoup/

I have the DTLS Transport connected, verified with:

 dtls_transport.on_state_change(Box::new(|state| {
        println!("DTLS state changed: {:?}", state);
        Box::pin(async {})
}));

And I have an RTCRtpSender from:

let track_local = Arc::new(TrackLocalStaticRTP::new(
    RTCRtpCodecCapability {
        mime_type: MIME_TYPE_OPUS.to_string(),
        clock_rate: 48000,
        channels: num_channels,
        sdp_fmtp_line: "".to_string(),
        rtcp_feedback: vec![rtcp_feedback],
    },
    String::from("some-track-id"),
    String::from("some-stream-id")
));
let track_local_clone = track_local.clone();

let r = api.new_rtp_sender(Some(track_local), dtls_transport.clone(), interceptor.clone()).await;

And I have some opus chunks being written like this:

let mut sample_chunks = resampled.chunks(samples_per_buffer);
loop {
    interval_timer.tick().await;

    if let Some(chunk) = sample_chunks.next() {
        let opus_bytes = opus_encoder
            .encode_float(&chunk, &mut opus_output)
            .expect("Opus encoding failed");

        let mut packet = Packet::default();
        packet.payload = Bytes::copy_from_slice(&opus_output[..opus_bytes]);
        packet.header.version = 2;
        packet.header.ssrc = ssrc;
        packet.header.sequence_number = seq_number;
        packet.header.timestamp = timestamp;
        packet.header.payload_type = payload_type;

        println!("Sending RTP packet {:?}", packet);

        match track_local_clone.write_rtp(&packet).await {
            Ok(u) => {
                println!("Sent RTP packet with {} bytes to ssrc {}", u, ssrc);
                seq_number = seq_number.wrapping_add(1);
                timestamp += 960;
            }
            Err(err) => {
                eprintln!("Error sending RTP packet: {:?}", err);
            }
        }
    } else {
        sample_chunks = resampled.chunks(samples_per_buffer);
    }
}

But every time this runs, I get u == 0 (no packets written) from the local track. Has anyone ever tried to use this as a client to a MediaSoup server? I'm trying to stream audio off a bunch of raspberry pi's in real time, back to a central server, where it can be processed and forwarded from there.

Thanks for the help!!

kristian1108 avatar Sep 16 '24 21:09 kristian1108