OctopusRage

OctopusRage

hey guys im quite new using membrane, i want to use membrane webrtc to utilize my webrtc call (audio only)

here’s my pipeline

  @impl true
  def handle_init(_ctx, opts) do
    spec =
      child(:webrtc_source, %Membrane.WebRTC.Source{
        signaling: opts[:ingress_signaling]
      })
      |> via_out(:output, options: [kind: :audio])
      |> child(%Membrane.Transcoder{output_stream_format: Membrane.Opus})
      |> child(:audio_realtimer, Membrane.Realtimer)
      |> via_in(:input, options: [kind: :audio])
      |> child(:webrtc_sink, %Membrane.WebRTC.Sink{
        signaling: opts[:egress_signaling]
      })

    {[spec: spec], %{}}
  end

and here’s my channels code

  @impl true
  def join("call:" <> signaling_id, %{"call_id" => call_id, "role" => role} = payload, socket) when role in ["ingress", "egress"] do
    PhoenixSignaling.register_channel(signaling_id)
    signaling = PhoenixSignaling.Registry.get(signaling_id)
    c_pid = CallHandler.new(call_id)
    if role == "egress" do
      CallHandler.add_egress(c_pid, signaling_id, signaling)
    else
      CallHandler.add_ingress(c_pid, signaling_id, signaling)
    end
    socket = assign(socket, :signaling_id, signaling_id)
    {:ok, socket}
  end


  @impl true
  def handle_in(signaling_id, msg, socket) do
    IO.inspect({:receive_frombrows, signaling_id, msg})
    # msg = Jason.decode!(msg)
    PhoenixSignaling.signal(signaling_id, msg)
    {:noreply, socket}
  end

  @impl true
  def handle_info({:membrane_webrtc_signaling, _pid, msg, _metadata}, socket) do
    IO.inspect({:receive_signal, socket.assigns.signaling_id, msg})
    push(socket, socket.assigns.signaling_id, msg)
    {:noreply, socket}
  end

all my peers successfully doing handshake but media wont flow into my peers
no errors in my code either, did i make some mistake on my pipeline?

Showing Posts 1 to 7

varsill

varsill

Membrane Core Team

Hello @OctopusRage !
Could you tell where and how do you spawn your pipeline (i.e. what function do you use to spawn your pipeline) and how does the CallHandler.add_ingress/CallHandler.add_egress functions look like?
I also assume that the browser is sending only audio, isn’t it?

One tiny thing I see is that a more suggested approach would be to create a signalling with given id with PhoenixSignaling.new/1 in the place where you spawn your pipeline instead of fetching it with private PhoenixSignaling.Registry.get/1 function but I don’t think that the root of the problem.

Best wishes!

OctopusRage

OctopusRage OP

hey @varsill
yes browser only send audio

this is callhandler.ex looks like:

defmodule Piqeons.Rtc.CallHandler do
  alias ExWebRTC.SessionDescription
  alias PiqeonsWeb.Endpoint
  alias Piqeons.Rtc.PeerHandler2
  alias Membrane.WebRTC.Signaling
  alias Membrane.WebRTC.PhoenixSignaling
  alias Piqeons.Rtc.CallSpv
  alias Piqeons.Pipelines.RtcPipe
  use GenServer

  def new(call_id) do
    pid = CallSpv.get_call(call_id)

    if pid do
      pid
    else
      {:ok, pid} = CallSpv.spawn_call(call_id)
      pid
    end
  end

  def start_link(call_id, opts) do
    GenServer.start_link(__MODULE__, call_id, opts)
  end

  @impl true
  def init(call_id) do
    {:ok,
     %{
       call_id: call_id,
       ingress: %{
         signaling: nil,
         connected: false
       },
       egress: %{
         signaling: nil,
         connected: false
       },
       started: false
     }}
  end


  def add_egress(pid, peer_id, signaling \\ nil) do
    signaling = if signaling do
      signaling
    else
      # currently unused
      {p_pid, signaling} = PeerHandler2.new(peer_id)
      PeerHandler2.register_call_pid(p_pid, pid)
      signaling
    end

    IO.inspect({:egress, peer_id})
    GenServer.call(pid, {:add_egress, signaling})
  end

  def add_ingress(pid, peer_id, signaling \\ nil) do
    signaling = if signaling do
      signaling
    else
      # currently unused
      {p_pid, signaling} = PeerHandler2.new(peer_id)
      PeerHandler2.register_call_pid(p_pid, pid)
      signaling
    end

    GenServer.call(pid, {:add_ingress, signaling})
  end


  @impl true
  def handle_call({:add_egress, signaling}, _, state) do
    state = %{state | egress: %{signaling: signaling, connected: true}}

    if !state.started and state.ingress.signaling do
      state = start_pipeline(state)
      {:reply, state.egress.signaling, state}
    else
      {:reply, state.egress.signaling, state}
    end
  end

  @impl true
  def handle_call({:add_ingress, signaling}, _, state) do
    state = %{state | ingress: %{signaling: signaling, connected: true}}

    if !state.started and state.egress.signaling do
      state = start_pipeline(state)
      {:reply, state.ingress.signaling, state}
    else
      {:reply, state.ingress.signaling, state}
    end
  end


  @impl true
  def handle_info(msg, state) do
    IO.inspect(msg)
    {:noreply, state}
  end

  defp start_pipeline(state) do
    Task.start(fn ->
      Membrane.Pipeline.start_link(RtcPipe,
        ingress_signaling: state.ingress.signaling,
        egress_signaling: state.egress.signaling
      )
    end)

    %{state | started: true}
  end
end

current state how i tested it is i open 2 different tabs: 1 tab with ingress peer and other tab is egress peer

OctopusRage

OctopusRage OP

hi @varsill
i finally success implementing my code! thx for the insight
the problem lies on my js code.. so my mistake is assuming that ingress peer can do both record and play
and egress peer can also do both

so my other question is, is it possible to send back audio stream to ingress peer? or i have to do it hacky way?

varsill

varsill

Membrane Core Team

Hi once again!

Great to hear that you managed to make it work!

so my other question is, is it possible to send back audio stream to ingress peer?

Though the browser supports using a single PeerConnction to handle both egress and ingress tracks, the Membrane Elements (Membrane.WebRTC.Source and Membrane.WebRTC.Sink) allow for data transfer only in one direction. So to transfer audio back to the browser that generated it you need to create a new signalling and use it with Membrane.WebRTC.Sink just as you would do to transfer data to any other peer.

A side question - are you sure you really need to send audio to the server and then send it the back to the browser? Couldn’t it be handled internally in the browser?

OctopusRage

OctopusRage OP

yes im sure i’ll need it, because i would like to replace the ingress peer with communication to the others server peer instead of browser and it requires media to flow without opening new peer connection

varsill

varsill

Membrane Core Team

I see, so as I mentioned before - each of the Membrane.WebRTC.Source and Membrane.WebRTC.Sink elements supports only either inbound our outbound traffic for a single PeerConnection, but not both at the time.

However it’s possible to write a Membrane component based on ex_webrtc that would handle both inbound and outbound tracks with a single PeerConnection - you can take a look at how they did it here: membrane_rtc_engine/ex_webrtc/lib/ex_webrtc_endpoint.ex at master · fishjam-cloud/membrane_rtc_engine · GitHub .

Unfortunately it’s more complicated than handling just a single type of tracks.

OctopusRage

OctopusRage OP

thx for the insight @varsill , ill take a look

— All posts loaded —

Where Next? Top

Trending in Discussions Top

budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
axelson
Hi there! :wave: @frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
achempion
I’ve been using Emacs as my main code editor for more than a two years. It’s a custom build version although I’ve tried doom emacs and sp...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New
jtormey
Lately I’ve been thinking about how to organize components as a LiveView application grows. One of the pain points I’ve found (for myself...
New
Null-logic-0
What IDE or editor are you using for Elixir development? Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews