| 1 | /* |
| 2 | * Copyright 2025 LiveKit, Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "livekit/rtc_error.h" |
| 18 | |
| 19 | #include <iomanip> |
| 20 | #include <sstream> |
| 21 | #include <string> |
| 22 | |
| 23 | namespace livekit_ffi { |
| 24 | |
| 25 | RtcError to_error(const webrtc::RTCError& error) { |
| 26 | RtcError lk_error; |
| 27 | lk_error.error_detail = static_cast<RtcErrorDetailType>(error.error_detail()); |
| 28 | lk_error.error_type = static_cast<RtcErrorType>(error.type()); |
| 29 | lk_error.has_sctp_cause_code = error.sctp_cause_code().has_value(); |
| 30 | lk_error.sctp_cause_code = error.sctp_cause_code().value_or(0); |
| 31 | lk_error.message = error.message(); |
| 32 | return lk_error; |
| 33 | } |
| 34 | |
| 35 | std::string serialize_error(const RtcError& error) { |
| 36 | std::stringstream ss; |
| 37 | ss << std::hex << std::setfill('0'); |
| 38 | ss << std::setw(8) << (uint32_t)error.error_type; |
| 39 | ss << std::setw(8) << (uint32_t)error.error_detail; |
| 40 | ss << std::setw(2) << (uint16_t)error.has_sctp_cause_code; |
| 41 | ss << std::setw(4) << (uint16_t)error.sctp_cause_code; |
| 42 | ss << std::dec << std::setw(1) << std::string(error.message); |
| 43 | return ss.str(); |
| 44 | } |
| 45 | |
| 46 | #ifdef LIVEKIT_TEST |
| 47 | rust::String serialize_deserialize() { |
| 48 | RtcError lk_error; |
| 49 | lk_error.error_type = RtcErrorType::InternalError; |
| 50 | lk_error.error_detail = RtcErrorDetailType::DataChannelFailure; |
| 51 | lk_error.has_sctp_cause_code = true; |
| 52 | lk_error.sctp_cause_code = 24; |
| 53 | lk_error.message = "this is not a test, I repeat, this is not a test"; |
| 54 | return serialize_error(lk_error); |
| 55 | } |
| 56 | #endif |
| 57 | |
| 58 | } // namespace livekit_ffi |