Skip to content
This repository has been archived by the owner on Sep 19, 2024. It is now read-only.

Commit

Permalink
Improve docs and make API more consistent (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
mickel8 authored Dec 28, 2021
1 parent 5abb75c commit 231a223
Show file tree
Hide file tree
Showing 12 changed files with 455 additions and 304 deletions.
12 changes: 1 addition & 11 deletions assets/js/membraneWebRTC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ export interface Peer {
export interface MembraneWebRTCConfig {
callbacks: Callbacks;
rtcConfig?: RTCConfiguration;
/**
* Determines wheater user want to receive media from other peers.
*/
receiveMedia?: boolean;
}

/**
Expand Down Expand Up @@ -127,8 +123,6 @@ export interface Callbacks {
* Main class that is responsible for connecting to the RTC Engine, sending and receiving media.
*/
export class MembraneWebRTC {
private receiveMedia: boolean;

private localTracksWithStreams: {
track: MediaStreamTrack;
stream: MediaStream;
Expand All @@ -150,10 +144,7 @@ export class MembraneWebRTC {
private readonly callbacks: Callbacks;

constructor(config: MembraneWebRTCConfig) {
const { receiveMedia = true, callbacks, rtcConfig } = config;

this.receiveMedia = receiveMedia;

const { callbacks, rtcConfig } = config;
this.callbacks = callbacks;
this.rtcConfig = rtcConfig || this.rtcConfig;
}
Expand All @@ -175,7 +166,6 @@ export class MembraneWebRTC {
try {
this.localPeer.metadata = peerMetadata;
let mediaEvent = generateMediaEvent("join", {
receiveMedia: this.receiveMedia,
metadata: peerMetadata,
});
this.sendMediaEvent(mediaEvent);
Expand Down
11 changes: 4 additions & 7 deletions lib/membrane_rtc_engine/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ defmodule Membrane.RTC.Engine.Endpoint do

defstruct id: nil, inbound_tracks: %{}

@spec new(id :: id(), inbound_tracks :: [Track.t()]) ::
Endpoint.t()
@spec new(id :: id(), inbound_tracks :: [Track.t()]) :: t()
def new(id, inbound_tracks) do
inbound_tracks = Map.new(inbound_tracks, &{&1.id, &1})
%__MODULE__{id: id, inbound_tracks: inbound_tracks}
Expand All @@ -33,20 +32,18 @@ defmodule Membrane.RTC.Engine.Endpoint do
@spec get_tracks(endpoint :: t()) :: [Track.t()]
def get_tracks(endpoint), do: Map.values(endpoint.inbound_tracks)

@spec update_track_metadata(endpoint :: Endpoint.t(), track_id :: Track.id(), metadata :: any()) ::
Endpoint.t()
@spec update_track_metadata(endpoint :: t(), track_id :: Track.id(), metadata :: any()) :: t()
def update_track_metadata(endpoint, track_id, metadata),
do:
update_in(endpoint, [:inbound_tracks, track_id, :metadata], fn _old_metadata -> metadata end)

@spec get_track_id_to_metadata(endpoint :: Endpoint.t()) :: %{Track.id() => any}
@spec get_track_id_to_metadata(endpoint :: t()) :: %{Track.id() => any}
def get_track_id_to_metadata(%__MODULE__{} = endpoint),
do: Map.values(endpoint.inbound_tracks) |> Map.new(&{&1.id, &1.metadata})

def get_track_id_to_metadata(_endpoint), do: %{}

@spec update_track_encoding(endpoint :: Endpoint.t(), track_id :: Track.id(), encoding :: atom) ::
Endpoint.t()
@spec update_track_encoding(endpoint :: t(), track_id :: Track.id(), encoding :: atom) :: t()
def update_track_encoding(endpoint, track_id, value),
do: update_in(endpoint.inbound_tracks[track_id], &%Track{&1 | encoding: value})
end
7 changes: 3 additions & 4 deletions lib/membrane_rtc_engine/endpoints/hls_endpoint.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
if Enum.all?(
[Membrane.H264.FFmpeg, Membrane.HTTPAdaptiveStream.SinkBin, Membrane.AAC.FDK],
[Membrane.H264.FFmpeg.Parser, Membrane.HTTPAdaptiveStream.SinkBin, Membrane.AAC.FDK.Encoder],
&Code.ensure_loaded?/1
) do
defmodule Membrane.RTC.Engine.Endpoint.HLS do
@moduledoc """
An Endpoint responsible for converting incoming tracks to HLS playlist.
"""
use Membrane.Bin
alias Membrane.RTC.Engine
require Membrane.Logger

def_input_pad :input,
Expand Down Expand Up @@ -40,8 +39,8 @@ if Enum.all?(
Enum.filter(tracks, fn track -> :raw in track.format end)
|> Enum.map(fn track -> {track.id, :raw} end)

actions = Engine.subscribe(subscriptions)
{{:ok, actions}, Map.update!(state, :tracks, &Map.merge(&1, new_tracks))}
{{:ok, notify: {:subscribe, subscriptions}},
Map.update!(state, :tracks, &Map.merge(&1, new_tracks))}
end

@impl true
Expand Down
24 changes: 12 additions & 12 deletions lib/membrane_rtc_engine/endpoints/webrtc_endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ if Code.ensure_loaded?(Membrane.WebRTC.EndpointBin) do
alias Membrane.WebRTC.{SDP, EndpointBin}
alias Membrane.WebRTC
alias Membrane.RTC.Engine
alias ExSDP.Attribute.FMTP
alias ExSDP.Attribute.RTPMapping

@type stun_server_t() :: ExLibnice.stun_server()
@type turn_server_t() :: ExLibnice.relay_info()
Expand Down Expand Up @@ -157,8 +159,9 @@ if Code.ensure_loaded?(Membrane.WebRTC.EndpointBin) do
def handle_notification({:new_tracks, tracks}, _from, _ctx, state) do
tracks = Enum.map(tracks, &to_rtc_track(&1, state.track_id_to_metadata))
inbound_tracks = update_tracks(tracks, state.inbound_tracks)
publish_notification = Engine.publish({:new_tracks, tracks})
{{:ok, publish_notification}, %{state | inbound_tracks: inbound_tracks}}

{{:ok, notify: {:publish, {:new_tracks, tracks}}},
%{state | inbound_tracks: inbound_tracks}}
end

@impl true
Expand All @@ -177,14 +180,13 @@ if Code.ensure_loaded?(Membrane.WebRTC.EndpointBin) do
_ctx,
state
) do
{{:ok, Engine.track_ready(track_id, encoding, depayloading_filter)}, state}
{{:ok, notify: {:track_ready, track_id, encoding, depayloading_filter}}, state}
end

@impl true
def handle_notification({:negotiation_done, new_outbound_tracks}, _from, _ctx, state) do
tracks = Enum.map(new_outbound_tracks, fn track -> {track.id, :RTP} end)
subscriptions = Engine.subscribe(tracks)
{{:ok, subscriptions}, state}
{{:ok, notify: {:subscribe, tracks}}, state}
end

@impl true
Expand All @@ -203,20 +205,18 @@ if Code.ensure_loaded?(Membrane.WebRTC.EndpointBin) do
turns = get_turn_configs(state.ice_name, turns)
enforce_turns? = state.integrated_turn_options[:use_integrated_turn] || false

media_event = {:signal, {:offer_data, media_count, turns, enforce_turns?}}
notification = Engine.custom_event(serialize(media_event))
{{:ok, notification}, state}
media_event_data = {:signal, {:offer_data, media_count, turns, enforce_turns?}}
{{:ok, notify: {:custom_media_event, serialize(media_event_data)}}, state}
end

@impl true
def handle_notification(
{:signal, _notification} = media_event,
{:signal, _notification} = media_event_data,
_element,
_ctx,
state
) do
notification = Engine.custom_event(serialize(media_event))
{{:ok, notification}, state}
{{:ok, notify: {:custom_media_event, serialize(media_event_data)}}, state}
end

@impl true
Expand All @@ -242,7 +242,7 @@ if Code.ensure_loaded?(Membrane.WebRTC.EndpointBin) do
end

@impl true
def handle_other({:custom, event}, ctx, state) do
def handle_other({:custom_media_event, event}, ctx, state) do
{:ok, data} = deserialize(event)
handle_custom_media_event(data, ctx, state)
end
Expand Down
Loading

0 comments on commit 231a223

Please sign in to comment.