RsdparsaSdpParser.cpp (2387B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "sdp/RsdparsaSdpParser.h" 8 9 #include "mozilla/UniquePtr.h" 10 #include "nsError.h" 11 #include "sdp/RsdparsaSdp.h" 12 #include "sdp/RsdparsaSdpGlue.h" 13 #include "sdp/RsdparsaSdpInc.h" 14 #include "sdp/Sdp.h" 15 16 namespace mozilla { 17 18 const std::string& RsdparsaSdpParser::ParserName() { 19 static const std::string& WEBRTC_SDP_NAME = "WEBRTCSDP"; 20 return WEBRTC_SDP_NAME; 21 } 22 23 UniquePtr<SdpParser::Results> RsdparsaSdpParser::Parse( 24 const std::string& aText) { 25 UniquePtr<SdpParser::InternalResults> results( 26 new SdpParser::InternalResults(Name())); 27 RustSdpSession* result = nullptr; 28 RustSdpError* err = nullptr; 29 StringView sdpTextView{aText.c_str(), aText.length()}; 30 nsresult rv = parse_sdp(sdpTextView, false, &result, &err); 31 if (rv != NS_OK) { 32 size_t line = sdp_get_error_line_num(err); 33 char* cString = sdp_get_error_message(err); 34 if (cString) { 35 std::string errMsg(cString); 36 sdp_free_error_message(cString); 37 sdp_free_error(err); 38 results->AddParseError(line, errMsg); 39 } else { 40 results->AddParseError(line, "Unable to retreive parse error."); 41 } 42 return results; 43 } 44 45 if (err) { 46 size_t line = sdp_get_error_line_num(err); 47 char* cString = sdp_get_error_message(err); 48 if (cString) { 49 std::string warningMsg(cString); 50 results->AddParseWarning(line, warningMsg); 51 sdp_free_error_message(cString); 52 sdp_free_error(err); 53 } else { 54 results->AddParseWarning(line, "Unable to retreive parse warning."); 55 } 56 } 57 58 RsdparsaSessionHandle uniqueResult(result); 59 RustSdpOrigin rustOrigin = sdp_get_origin(uniqueResult.get()); 60 auto address = convertExplicitlyTypedAddress(&rustOrigin.addr); 61 SdpOrigin origin(convertStringView(rustOrigin.username), rustOrigin.sessionId, 62 rustOrigin.sessionVersion, address.first, address.second); 63 64 results->SetSdp(MakeUnique<RsdparsaSdp>(std::move(uniqueResult), origin)); 65 return results; 66 } 67 68 bool RsdparsaSdpParser::IsNamed(const std::string& aName) { 69 return aName == ParserName(); 70 } 71 72 } // namespace mozilla