Skip to content

Commit

Permalink
utils: allow subscribing or ignore specific stream ids
Browse files Browse the repository at this point in the history
  • Loading branch information
ferjm committed Nov 14, 2021
1 parent bbdd40e commit 09ac4d8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions examples/src/bin/basic_video_chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@ async fn main() -> anyhow::Result<()> {
}));

let credentials_ = credentials.clone();
std::thread::spawn(move || {
Subscriber::new(credentials_, duration).run().unwrap();
let on_stream_created = Box::new(move |_: &Publisher, stream_id: String| {
let credentials = credentials_.clone();
std::thread::spawn(move || {
Subscriber::new(credentials, duration, None, Some(vec![stream_id]))
.run()
.unwrap();
});
});

Publisher::new(credentials, None, duration).run()?;
Publisher::new(credentials, Some(on_stream_created), duration).run()?;

Ok(opentok::deinit()?)
}
2 changes: 1 addition & 1 deletion examples/src/bin/subscriber_only.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async fn main() -> anyhow::Result<()> {
println!("{:?}", msg);
}));

Subscriber::new(credentials, duration).run()?;
Subscriber::new(credentials, duration, None, None).run()?;

Ok(opentok::deinit()?)
}
26 changes: 25 additions & 1 deletion utils/src/subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,23 @@ pub struct Subscriber {
credentials: Credentials,
main_loop: glib::MainLoop,
duration: Option<u64>,
stream_id: Arc<Mutex<Option<String>>>,
ignored_stream_ids: Arc<Mutex<Vec<String>>>,
}

impl Subscriber {
pub fn new(credentials: Credentials, duration: Option<u64>) -> Self {
pub fn new(
credentials: Credentials,
duration: Option<u64>,
stream_id: Option<String>,
ignored_stream_ids: Option<Vec<String>>,
) -> Self {
Self {
credentials,
main_loop: glib::MainLoop::new(None, false),
duration,
stream_id: Arc::new(Mutex::new(stream_id)),
ignored_stream_ids: Arc::new(Mutex::new(ignored_stream_ids.unwrap_or_default())),
}
}

Expand Down Expand Up @@ -85,6 +94,8 @@ impl Subscriber {

let subscriber = Arc::new(OpenTokSubscriber::new(subscriber_callbacks));

let stream_id = self.stream_id.clone();
let ignored_stream_ids = self.ignored_stream_ids.clone();
let session_callbacks = SessionCallbacks::builder()
.on_stream_received(move |session, stream| {
*renderer__.lock().unwrap() = Some(renderer::Renderer::new().unwrap());
Expand All @@ -93,6 +104,19 @@ impl Subscriber {
stream.get_video_width(),
stream.get_video_height()
);

if let Some(ref stream_id) = *stream_id.lock().unwrap() {
if stream.id() != *stream_id {
println!("{} is not the stream we want to susbscribe to", stream_id);
return;
}
}

if ignored_stream_ids.lock().unwrap().contains(&stream.id()) {
println!("Ignoring stream {}", stream.id());
return;
}

if subscriber.set_stream(stream).is_ok() {
if let Err(e) = session.subscribe(&subscriber) {
eprintln!("Could not subscribe to session {:?}", e);
Expand Down

0 comments on commit 09ac4d8

Please sign in to comment.