View Source Membrane.WebRTC.Live.Player (Membrane WebRTC plugin v0.25.3)

LiveView for playing audio and video received via WebRTC from Membrane.WebRTC.Sink.

Note: This module will be available in your code only if you add {:phoenix, "~> 1.7"} and {:phoenix_live_view, "~> 1.0"} to the dependencies of of your root project.

It:

  • renders a single HTMLVideoElement.
  • creates WebRTC PeerConnection on the browser side.
  • forwards signaling messages between the browser and Membrane.WebRTC.Sink via Membrane.WebRTC.Signaling.
  • attaches audio and video from the Elixir to the HTMLVideoElement.

JavaScript Hook

Player live view requires JavaScript hook to be registered under Player name. The hook can be created using createPlayerHook function. For example:

import { createPlayerHook } from "membrane_webrtc_plugin";
let Hooks = {};
const iceServers = [{ urls: "stun:stun.l.google.com:19302" }];
Hooks.Player = createPlayerHook(iceServers);
let liveSocket = new LiveSocket("/live", Socket, {
  // ...
  hooks: Hooks
});

Examples

defmodule StreamerWeb.StreamViewerLive do
  use StreamerWeb, :live_view

  alias Membrane.WebRTC.Live.Player

  @impl true
  def render(assigns) do
  ~H"""
  <Player.live_render socket={@socket} player_id={"player"} />
  """
  end

  @impl true
  def mount(_params, _session, socket) do
    signaling = Membrane.WebRTC.Signaling.new()
    {:ok, _supervisor, _pipelne} = Membrane.Pipeline.start_link(MyPipeline, signaling: signaling)

    socket = socket |> Player.attach(id: "player", signaling: signaling)
    {:ok, socket}
  end
end

Summary

Functions

Attaches required hooks and creates Membrane.WebRTC.Live.Player struct.

Helper function for rendering Player live view.

Types

t()

@type t() :: %Membrane.WebRTC.Live.Player{
  id: String.t(),
  signaling: Membrane.WebRTC.Signaling.t()
}

Functions

attach(socket, opts)

Attaches required hooks and creates Membrane.WebRTC.Live.Player struct.

Created struct is saved in socket's assigns (in socket.assigns[Membrane.WebRTC.Live.Player][id]) and then it is sent by an attached hook to a child live view process.

Options:

  • id - player id. It is used to identify live view and generated HTML video player. It must be unique withing single page.
  • signaling - Membrane.WebRTC.Signaling.t(), that has been passed to Membrane.WebRTC.Sink as well.

get_attached(socket, id)

@spec get_attached(Phoenix.LiveView.Socket.t(), String.t()) :: t()

live_render(assigns)

Helper function for rendering Player live view.

Attributes

  • socket (Phoenix.LiveView.Socket) (required) - Parent live view socket.

  • player_id (:string) (required) - ID of a player previously attached to the socket. It has to be the same as the value passed to :id field Membrane.WebRTC.Live.Player.attach/2.

  • class (:string) - CSS/Tailwind classes for styling. Defaults to nil.