Dynamically add & remove source elements for custom RTC Engine endpoint

Hi, I'm looking for some help with dynamically adding and removing audio sources from a Membrane Bin making sure that all audio from previously added sources is interrupted when a new source is added, in the context of an application built with membrane_rtc_engine,


The users of my application can join a WebRTC room along with a chatbot "peer", represented by the Chatbot module below:

While a user is in the room with the chatbot peer, they can submit an event {:user_event, url} that trigger the Chatbot endpoint to fetch audio from url using membrane_hackney_plugin.

When a user submits such an event, I would like to interrupt whatever audio the chatbot is currently playing and replace it with audio from the new url.

Ultimately I'm trying to determine a way to structure this Bin so that I can implement handle_parent_notification/3

defmodule MediaServer.Endpoint.Speech.Chatbot do
  def handle_parent_notification({:user_event, {:play_audio, url}}, _ctx, state) do
    # 1. interrupt whatever audio is currently playing 
    #   - I think I need to remove every element from :audio_source to before :track_sender with a :remove_child action?
    # 2. Replace it with audio from url
    #   - I think I need to create a new spec for the elements from :audio_source to before :track sender with the new URL with the :spec action?
  end
end

I've tried a few variations of adding / removing children, but keep running into issues -- either with adding duplicate children, or linking pads multiple times. Any advice or examples of how to do this would be appreciated!

10 responses