From 4bbead1880f0ff6ebc65b5a4fb9ed25df2b6ee22 Mon Sep 17 00:00:00 2001 From: Robert Adams Date: Sun, 12 Jan 2025 15:55:53 +0000 Subject: [PATCH] Update JSON to OSD and value extraction to be smarter about how numbers are stored (OSD doesn't have OSDLong and uses binary arrays). Bump version. --- Janus/JanusMessages.cs | 53 ++++++++++++++++--- Janus/WebRtcJanusService.csproj | 6 +-- VERSION | 2 +- WebRtcVoice/WebRtcVoice.csproj | 6 +-- .../WebRtcVoiceRegionModule.csproj | 6 +-- .../WebRtcVoiceServiceModule.csproj | 6 +-- 6 files changed, 60 insertions(+), 19 deletions(-) diff --git a/Janus/JanusMessages.cs b/Janus/JanusMessages.cs index c2fbd8c..ca20194 100644 --- a/Janus/JanusMessages.cs +++ b/Janus/JanusMessages.cs @@ -80,7 +80,7 @@ namespace WebRtcVoice } // Note that the session_id is a long number in the JSON so we convert the string. public string sessionId { - get { return m_message.ContainsKey("session_id") ? m_message["session_id"].AsLong().ToString() : String.Empty; } + get { return m_message.ContainsKey("session_id") ? OSDToLong(m_message["session_id"]).ToString() : String.Empty; } set { m_message["session_id"] = long.Parse(value); } } public bool hasSessionId { get { return m_message.ContainsKey("session_id"); } } @@ -109,6 +109,47 @@ namespace WebRtcVoice { return m_message.ToString(); } + // Utility function to convert an OSD object to an long. The OSD object can be an OSDInteger + // or an OSDArray of 4 or 8 integers. + // This exists because the JSON to OSD parser can return an OSDArray for a long number + // since there is not an OSDLong type. + // The design of the OSD conversion functions kinda needs one to know how the number + // is stored in order to extract it. Like, if it's stored as a long value (8 bytes) + // and one fetches it with .AsInteger(), it will return the first 4 bytes as an integer + // and not the long value. So this function looks at the type of the OSD object and + // extracts the number appropriately. + public long OSDToLong(OSD pIn) + { + long ret = 0; + switch (pIn.Type) + { + case OSDType.Integer: + ret = (long)(pIn as OSDInteger).AsInteger(); + break; + case OSDType.Binary: + byte[] value = (pIn as OSDBinary).value; + if (value.Length == 4) + { + ret = (long)(pIn as OSDBinary).AsInteger(); + } + if (value.Length == 8) + { + ret = (pIn as OSDBinary).AsLong(); + } + break; + case OSDType.Array: + if ((pIn as OSDArray).Count == 4) + { + ret = (long)pIn.AsInteger(); + } + if ((pIn as OSDArray).Count == 8) + { + ret = pIn.AsLong(); + } + break; + } + return ret; + } } // ============================================================== // A Janus request message is a basic Janus message with an API token @@ -210,7 +251,7 @@ namespace WebRtcVoice { var err = m_message["error"]; if (err is OSDMap) - ret = (int)(err as OSDMap)["code"].AsLong(); + ret = (int)OSDToLong((err as OSDMap)["code"]); } return ret; }} @@ -245,7 +286,7 @@ namespace WebRtcVoice // and the ODMap conversion interprets it as a long (OSDLong). // If one just does a "ToString()" on the OSD object, you // get an interpretation of the binary value. - return dataSection.ContainsKey("id") ? dataSection["id"].AsLong().ToString() : String.Empty; + return dataSection.ContainsKey("id") ? OSDToLong(dataSection["id"]).ToString() : String.Empty; }} } // ============================================================== @@ -290,7 +331,7 @@ namespace WebRtcVoice public AttachPluginResp(JanusMessageResp pResp) : base(pResp.RawBody) { } public string pluginId { get { - return dataSection.ContainsKey("id") ? dataSection["id"].AsLong().ToString() : String.Empty; + return dataSection.ContainsKey("id") ? OSDToLong(dataSection["id"]).ToString() : String.Empty; }} } // ============================================================== @@ -378,7 +419,7 @@ namespace WebRtcVoice { // Move the plugin data up into the m_data var so it is easier to get to m_pluginData = m_message["plugindata"] as OSDMap; - if (m_pluginData.ContainsKey("data")) + if (m_pluginData is not null && m_pluginData.ContainsKey("data")) { m_data = m_pluginData["data"] as OSDMap; // m_log.DebugFormat("{0} AudioBridgeResp. Found both plugindata and data: data={1}", LogHeader, m_data.ToString()); @@ -393,7 +434,7 @@ namespace WebRtcVoice { if (m_data is null) return 0; - return m_data.ContainsKey(pKey) ? (int)m_data[pKey].AsLong() : 0; + return m_data.ContainsKey(pKey) ? (int)OSDToLong(m_data[pKey]) : 0; } // Get a string value for a key in the response data or empty string if not there public string PluginRespDataString(string pKey) diff --git a/Janus/WebRtcJanusService.csproj b/Janus/WebRtcJanusService.csproj index 20a07b4..2490a4f 100644 --- a/Janus/WebRtcJanusService.csproj +++ b/Janus/WebRtcJanusService.csproj @@ -2,7 +2,7 @@ net8.0 -1.1.3 +1.1.4 false Library false @@ -28,7 +28,7 @@ false True False -1.1.3 +1.1.4 False ../../../bin/ False @@ -52,7 +52,7 @@ false True False -1.1.3 +1.1.4 False ../../../bin/ False diff --git a/VERSION b/VERSION index 781dcb0..65087b4 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.1.3 +1.1.4 diff --git a/WebRtcVoice/WebRtcVoice.csproj b/WebRtcVoice/WebRtcVoice.csproj index b6808bc..c1cc3ae 100644 --- a/WebRtcVoice/WebRtcVoice.csproj +++ b/WebRtcVoice/WebRtcVoice.csproj @@ -2,7 +2,7 @@ net8.0 -1.1.3 +1.1.4 false Library false @@ -28,7 +28,7 @@ false True False -1.1.3 +1.1.4 False ../../../bin/ False @@ -52,7 +52,7 @@ false True False -1.1.3 +1.1.4 False ../../../bin/ False diff --git a/WebRtcVoiceRegionModule/WebRtcVoiceRegionModule.csproj b/WebRtcVoiceRegionModule/WebRtcVoiceRegionModule.csproj index 9287ae4..b82e628 100644 --- a/WebRtcVoiceRegionModule/WebRtcVoiceRegionModule.csproj +++ b/WebRtcVoiceRegionModule/WebRtcVoiceRegionModule.csproj @@ -2,7 +2,7 @@ net8.0 -1.1.3 +1.1.4 false Library false @@ -28,7 +28,7 @@ false True False -1.1.3 +1.1.4 False ../../../bin/ False @@ -52,7 +52,7 @@ false True False -1.1.3 +1.1.4 False ../../../bin/ False diff --git a/WebRtcVoiceServiceModule/WebRtcVoiceServiceModule.csproj b/WebRtcVoiceServiceModule/WebRtcVoiceServiceModule.csproj index 0f81210..81144fb 100644 --- a/WebRtcVoiceServiceModule/WebRtcVoiceServiceModule.csproj +++ b/WebRtcVoiceServiceModule/WebRtcVoiceServiceModule.csproj @@ -2,7 +2,7 @@ net8.0 -1.1.3 +1.1.4 false Library false @@ -28,7 +28,7 @@ false True False -1.1.3 +1.1.4 False ../../../bin/ False @@ -52,7 +52,7 @@ false True False -1.1.3 +1.1.4 False ../../../bin/ False