feat: Add audio track to examples/reflect.
@@ -2,11 +2,12 @@ use anyhow::Result;
use clap::{App, AppSettings, Arg};
use interceptor::registry::Registry;
use rtcp::payload_feedbacks::picture_loss_indication::PictureLossIndication;
+use std::collections::HashMap;
use std::io::Write;
use std::sync::Arc;
use tokio::time::Duration;
use webrtc::api::interceptor_registry::register_default_interceptors;
-use webrtc::api::media_engine::{MediaEngine, MIME_TYPE_VP8};
+use webrtc::api::media_engine::{MediaEngine, MIME_TYPE_OPUS, MIME_TYPE_VP8};
use webrtc::api::APIBuilder;
use webrtc::media::rtp::rtp_codec::{RTCRtpCodecCapability, RTCRtpCodecParameters, RTPCodecType};
use webrtc::media::rtp::rtp_receiver::RTCRtpReceiver;
@@ -69,6 +70,18 @@ async fn main() -> Result<()> {
let mut m = MediaEngine::default();
// Setup the codecs you want to use.
+ m.register_codec(
+ RTCRtpCodecParameters {
+ capability: RTCRtpCodecCapability {
+ mime_type: MIME_TYPE_OPUS.to_owned(),
+ ..Default::default()
+ },
+ payload_type: 120,
+ ..Default::default()
+ },
+ RTPCodecType::Audio,
+ )?;
+
// We'll use a VP8 and Opus but you can also define your own
m.register_codec(
RTCRtpCodecParameters {
@@ -111,14 +124,18 @@ async fn main() -> Result<()> {
// Create a new RTCPeerConnection
let peer_connection = Arc::new(api.new_peer_connection(config).await?);
-
- // Create Track that we send video back to browser on
+ let mut output_tracks = HashMap::new();
+ for s in vec!["audio", "video"] {
let output_track = Arc::new(TrackLocalStaticRTP::new(
RTCRtpCodecCapability {
- mime_type: MIME_TYPE_VP8.to_owned(),
+ mime_type: if s == "video" {
+ MIME_TYPE_VP8.to_owned()
+ } else {
+ MIME_TYPE_OPUS.to_owned()
+ },
..Default::default()
},
- "video".to_owned(),
+ format!("track-{}", s),
"webrtc-rs".to_owned(),
));
@@ -136,6 +153,9 @@ async fn main() -> Result<()> {
Result::<()>::Ok(())
});
+ output_tracks.insert(s.to_owned(), output_track);
+ }
+
// Wait for the offer to be pasted
let line = signal::must_read_stdin()?;
let desc_data = signal::decode(line.as_str())?;
@@ -154,6 +174,8 @@ async fn main() -> Result<()> {
// Send a PLI on an interval so that the publisher is pushing a keyframe every rtcpPLIInterval
// This is a temporary fix until we implement incoming RTCP events, then we would push a PLI only when a viewer requests it
let media_ssrc = track.ssrc();
+
+ if track.kind() == RTPCodecType::Video {
let pc2 = Arc::clone(&pc);
tokio::spawn(async move {
let mut result = Result::<usize>::Ok(0);
@@ -171,6 +193,19 @@ async fn main() -> Result<()> {
};
}
});
+ }
+
+ let kind = if track.kind() == RTPCodecType::Audio {
+ "audio"
+ } else {
+ "video"
+ };
+ let output_track = if let Some(output_track) = output_tracks.get(kind) {
+ Arc::clone(output_track)
+ } else {
+ println!("output_track not found for type = {}", kind);
+ return Box::pin(async {});
+ };
let output_track2 = Arc::clone(&output_track);
tokio::spawn(async move {
