summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/sdp/RsdparsaSdpParser.cpp
blob: b955bcd46b57fad1051634e03b81c43f510e32b1 (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
/* -*- 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 "nsError.h"

#include "mozilla/UniquePtr.h"

#include "sdp/Sdp.h"
#include "sdp/RsdparsaSdp.h"
#include "sdp/RsdparsaSdpParser.h"
#include "sdp/RsdparsaSdpInc.h"
#include "sdp/RsdparsaSdpGlue.h"

namespace mozilla {

const std::string& RsdparsaSdpParser::ParserName() {
  static const std::string& WEBRTC_SDP_NAME = "WEBRTCSDP";
  return WEBRTC_SDP_NAME;
}

UniquePtr<SdpParser::Results> RsdparsaSdpParser::Parse(
    const std::string& aText) {
  UniquePtr<SdpParser::InternalResults> results(
      new SdpParser::InternalResults(Name()));
  RustSdpSession* result = nullptr;
  RustSdpError* err = nullptr;
  StringView sdpTextView{aText.c_str(), aText.length()};
  nsresult rv = parse_sdp(sdpTextView, false, &result, &err);
  if (rv != NS_OK) {
    size_t line = sdp_get_error_line_num(err);
    char* cString = sdp_get_error_message(err);
    if (cString) {
      std::string errMsg(cString);
      sdp_free_error_message(cString);
      sdp_free_error(err);
      results->AddParseError(line, errMsg);
    } else {
      results->AddParseError(line, "Unable to retreive parse error.");
    }
    return results;
  }

  if (err) {
    size_t line = sdp_get_error_line_num(err);
    char* cString = sdp_get_error_message(err);
    if (cString) {
      std::string warningMsg(cString);
      results->AddParseWarning(line, warningMsg);
      sdp_free_error_message(cString);
      sdp_free_error(err);
    } else {
      results->AddParseWarning(line, "Unable to retreive parse warning.");
    }
  }

  RsdparsaSessionHandle uniqueResult(result);
  RustSdpOrigin rustOrigin = sdp_get_origin(uniqueResult.get());
  auto address = convertExplicitlyTypedAddress(&rustOrigin.addr);
  SdpOrigin origin(convertStringView(rustOrigin.username), rustOrigin.sessionId,
                   rustOrigin.sessionVersion, address.first, address.second);

  results->SetSdp(MakeUnique<RsdparsaSdp>(std::move(uniqueResult), origin));
  return results;
}

bool RsdparsaSdpParser::IsNamed(const std::string& aName) {
  return aName == ParserName();
}

}  // namespace mozilla