webrtc_sdp (webrtc v0.1.0)
View SourceSDP codec (RFC 8866) with the attributes used by WebRTC.
parse/1 turns a session description into an #sdp{} record (see webrtc_sdp.hrl); encode/1 turns the record back into text. JSEP code works on these records only, never on the text.
{ok, Sdp} = webrtc_sdp:parse(Offer),
[Audio | _] = Sdp#sdp.media,
Codecs = webrtc_sdp_media:codecs(Audio),
Answer = webrtc_sdp:encode(Sdp#sdp{media = [...]}).Parsing:
- Lines may end with CRLF or LF (RFC 8866 §5 allows a lenient parser); the last line may lack its terminator.
- Field order and cardinality follow the RFC 8866 §9 grammar. A
z=line attaches to the precedingt=(accepting both RFC 4566 and RFC 8866 placement). - Errors are
{error, {Line, Reason}}with a 1-based line number. - Attributes listed in
webrtc_sdp_attrare parsed into typed terms, the rest are kept as{Name, Value}binaries in order. A malformed known attribute is an error.
Encoding always writes CRLF and the RFC 8866 field order. Values that cannot be written (for example text containing CR or LF) raise error({invalid_sdp, Detail}). encode(parse(X)) reproduces X byte for byte when X uses CRLF and the canonical spelling of typed attributes (single spaces, upper-case fingerprint hex, lowercase hash names, no leading zeros), which is what browsers emit. fmtp and msid-semantic keep their raw text so they round trip as received.
Summary
Types
-type attribute() :: webrtc_sdp_attr:attribute().
-type media() :: #sdp_media{media :: binary(), port :: inet:port_number(), port_count :: pos_integer() | undefined, proto :: binary(), fmts :: [binary()], information :: binary() | undefined, connections :: [#sdp_connection{nettype :: binary(), addrtype :: binary(), address :: binary()}], bandwidths :: [{binary(), non_neg_integer()}], key :: binary() | undefined, attributes :: [webrtc_sdp:attribute()]}.
-type sdp() :: #sdp{version :: non_neg_integer(), origin :: #sdp_origin{username :: binary(), sess_id :: non_neg_integer(), sess_version :: non_neg_integer(), nettype :: binary(), addrtype :: binary(), address :: binary()}, session_name :: binary(), information :: binary() | undefined, uri :: binary() | undefined, emails :: [binary()], phones :: [binary()], connection :: #sdp_connection{nettype :: binary(), addrtype :: binary(), address :: binary()} | undefined, bandwidths :: [{binary(), non_neg_integer()}], times :: [#sdp_time{start :: non_neg_integer(), stop :: non_neg_integer(), repeats :: [[binary(), ...]], zones :: [{non_neg_integer(), binary()}]}, ...], key :: binary() | undefined, attributes :: [webrtc_sdp:attribute()], media :: [#sdp_media{media :: binary(), port :: inet:port_number(), port_count :: pos_integer() | undefined, proto :: binary(), fmts :: [binary()], information :: binary() | undefined, connections :: [#sdp_connection{nettype :: binary(), addrtype :: binary(), address :: binary()}], bandwidths :: [{binary(), non_neg_integer()}], key :: binary() | undefined, attributes :: [webrtc_sdp:attribute()]}]}.
Functions
Encode a session description with CRLF line endings.
-spec parse(binary()) -> {ok, sdp()} | {error, {pos_integer(), error_reason()}}.
Parse a session description.