summaryrefslogtreecommitdiffstats
path: root/media/webrtc/signaling/src/sdp/SipccSdpParser.cpp
blob: 04fea305c916e60748ac55aa1c3d971dd9f132c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "signaling/src/sdp/SipccSdpParser.h"
#include "signaling/src/sdp/SipccSdp.h"

#include <utility>
extern "C" {
#include "signaling/src/sdp/sipcc/sdp.h"
}

namespace mozilla
{

extern "C" {

void
sipcc_sdp_parser_error_handler(void *context, uint32_t line,
                               const char *message)
{
  SdpErrorHolder *errorHolder = static_cast<SdpErrorHolder *>(context);
  std::string err(message);
  errorHolder->AddParseError(line, err);
}

} // extern "C"

UniquePtr<Sdp>
SipccSdpParser::Parse(const std::string &sdpText)
{
  ClearParseErrors();

  sdp_conf_options_t *sipcc_config = sdp_init_config();
  if (!sipcc_config) {
    return UniquePtr<Sdp>();
  }

  sdp_nettype_supported(sipcc_config, SDP_NT_INTERNET, true);
  sdp_addrtype_supported(sipcc_config, SDP_AT_IP4, true);
  sdp_addrtype_supported(sipcc_config, SDP_AT_IP6, true);
  sdp_transport_supported(sipcc_config, SDP_TRANSPORT_RTPAVP, true);
  sdp_transport_supported(sipcc_config, SDP_TRANSPORT_RTPAVPF, true);
  sdp_transport_supported(sipcc_config, SDP_TRANSPORT_RTPSAVP, true);
  sdp_transport_supported(sipcc_config, SDP_TRANSPORT_RTPSAVPF, true);
  sdp_transport_supported(sipcc_config, SDP_TRANSPORT_UDPTLSRTPSAVP, true);
  sdp_transport_supported(sipcc_config, SDP_TRANSPORT_UDPTLSRTPSAVPF, true);
  sdp_transport_supported(sipcc_config, SDP_TRANSPORT_TCPTLSRTPSAVP, true);
  sdp_transport_supported(sipcc_config, SDP_TRANSPORT_TCPTLSRTPSAVPF, true);
  sdp_transport_supported(sipcc_config, SDP_TRANSPORT_DTLSSCTP, true);
  sdp_require_session_name(sipcc_config, false);

  sdp_config_set_error_handler(sipcc_config, &sipcc_sdp_parser_error_handler,
                               this);

  // Takes ownership of |sipcc_config| iff it succeeds
  sdp_t *sdp = sdp_init_description(sipcc_config);
  if (!sdp) {
    sdp_free_config(sipcc_config);
    return UniquePtr<Sdp>();
  }

  const char *rawString = sdpText.c_str();
  sdp_result_e sdpres = sdp_parse(sdp, rawString, sdpText.length());
  if (sdpres != SDP_SUCCESS) {
    sdp_free_description(sdp);
    return UniquePtr<Sdp>();
  }

  UniquePtr<SipccSdp> sipccSdp(new SipccSdp);

  bool success = sipccSdp->Load(sdp, *this);
  sdp_free_description(sdp);
  if (!success) {
    return UniquePtr<Sdp>();
  }

  return UniquePtr<Sdp>(Move(sipccSdp));
}

} // namespace mozilla