From a8a8b96fb89e56a9fa1be02bc80c937b5671aa07 Mon Sep 17 00:00:00 2001 From: abhishesingh-msft <123186776+abhishesingh-msft@users.noreply.github.com> Date: Wed, 18 Oct 2023 23:48:30 +0530 Subject: [PATCH] Implement generci parser for Transcription, Audio data (#39250) --- ...unication.CallAutomation.netstandard2.0.cs | 119 +++++++-------- .../MediaStreamingPackageBase.cs | 12 -- .../MediaStreamingPackageParser.cs | 59 -------- .../Media/AudioData.cs} | 6 +- .../Media/AudioDataInternal.cs} | 4 +- .../Media/AudioMetadata.cs} | 4 +- .../StreamingData.cs} | 4 +- .../Models/Streaming/StreamingDataParser.cs | 86 +++++++++++ .../Transcription/ResultStatus.cs | 2 +- .../Transcription/TextFormat.cs | 2 +- .../Transcription/TranscriptionData.cs | 5 +- .../TranscriptionDataInternal.cs | 2 +- .../Transcription/TranscriptionMetadata.cs | 4 +- .../{ => Streaming}/Transcription/WordData.cs | 2 +- .../TranscriptionPackageParser.cs | 72 --------- .../CallConnections/CallConnectionTests.cs | 1 - .../CallAutomationStreamingParserTests.cs | 126 --------------- .../StreamingDataParserTests.cs} | 143 ++++++++++++++++-- 18 files changed, 286 insertions(+), 367 deletions(-) delete mode 100644 sdk/communication/Azure.Communication.CallAutomation/src/Models/MediaStreaming/MediaStreamingPackageBase.cs delete mode 100644 sdk/communication/Azure.Communication.CallAutomation/src/Models/MediaStreaming/MediaStreamingPackageParser.cs rename sdk/communication/Azure.Communication.CallAutomation/src/Models/{MediaStreaming/MediaStreamingAudioData.cs => Streaming/Media/AudioData.cs} (83%) rename sdk/communication/Azure.Communication.CallAutomation/src/Models/{MediaStreaming/MediaStreamingAudioDataInternal.cs => Streaming/Media/AudioDataInternal.cs} (92%) rename sdk/communication/Azure.Communication.CallAutomation/src/Models/{MediaStreaming/MediaStreamingMetadata.cs => Streaming/Media/AudioMetadata.cs} (89%) rename sdk/communication/Azure.Communication.CallAutomation/src/Models/{Transcription/TranscriptionPackageBase.cs => Streaming/StreamingData.cs} (70%) create mode 100644 sdk/communication/Azure.Communication.CallAutomation/src/Models/Streaming/StreamingDataParser.cs rename sdk/communication/Azure.Communication.CallAutomation/src/Models/{ => Streaming}/Transcription/ResultStatus.cs (86%) rename sdk/communication/Azure.Communication.CallAutomation/src/Models/{ => Streaming}/Transcription/TextFormat.cs (83%) rename sdk/communication/Azure.Communication.CallAutomation/src/Models/{ => Streaming}/Transcription/TranscriptionData.cs (94%) rename sdk/communication/Azure.Communication.CallAutomation/src/Models/{ => Streaming}/Transcription/TranscriptionDataInternal.cs (96%) rename sdk/communication/Azure.Communication.CallAutomation/src/Models/{ => Streaming}/Transcription/TranscriptionMetadata.cs (88%) rename sdk/communication/Azure.Communication.CallAutomation/src/Models/{ => Streaming}/Transcription/WordData.cs (89%) delete mode 100644 sdk/communication/Azure.Communication.CallAutomation/src/Models/Transcription/TranscriptionPackageParser.cs delete mode 100644 sdk/communication/Azure.Communication.CallAutomation/tests/MediaStreaming/CallAutomationStreamingParserTests.cs rename sdk/communication/Azure.Communication.CallAutomation/tests/{Transcription/TranscriptionPackageParserTests.cs => Streaming/StreamingDataParserTests.cs} (52%) diff --git a/sdk/communication/Azure.Communication.CallAutomation/api/Azure.Communication.CallAutomation.netstandard2.0.cs b/sdk/communication/Azure.Communication.CallAutomation/api/Azure.Communication.CallAutomation.netstandard2.0.cs index 7ae2185d60bce..5a7c2313cb326 100644 --- a/sdk/communication/Azure.Communication.CallAutomation/api/Azure.Communication.CallAutomation.netstandard2.0.cs +++ b/sdk/communication/Azure.Communication.CallAutomation/api/Azure.Communication.CallAutomation.netstandard2.0.cs @@ -69,6 +69,28 @@ internal AnswerCallResult() { } public Azure.Communication.CallAutomation.AnswerCallEventResult WaitForEventProcessor(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public System.Threading.Tasks.Task WaitForEventProcessorAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } } + public partial class AudioData : Azure.Communication.CallAutomation.StreamingData + { + internal AudioData() { } + public string Data { get { throw null; } } + public bool IsSilent { get { throw null; } } + public Azure.Communication.CommunicationIdentifier Participant { get { throw null; } } + public System.DateTime Timestamp { get { throw null; } } + } + public partial class AudioMetadata : Azure.Communication.CallAutomation.StreamingData + { + public AudioMetadata() { } + [System.Text.Json.Serialization.JsonPropertyNameAttribute("channels")] + public int Channels { get { throw null; } set { } } + [System.Text.Json.Serialization.JsonPropertyNameAttribute("encoding")] + public string Encoding { get { throw null; } set { } } + [System.Text.Json.Serialization.JsonPropertyNameAttribute("length")] + public int Length { get { throw null; } set { } } + [System.Text.Json.Serialization.JsonPropertyNameAttribute("subscriptionId")] + public string MediaSubscriptionId { get { throw null; } set { } } + [System.Text.Json.Serialization.JsonPropertyNameAttribute("sampleRate")] + public int SampleRate { get { throw null; } set { } } + } public partial class BlobStorage : Azure.Communication.CallAutomation.ExternalStorage { public BlobStorage(System.Uri containerUri) { } @@ -830,14 +852,6 @@ public GroupCallLocator(string id) { } public static bool operator !=(Azure.Communication.CallAutomation.MediaStreamingAudioChannel left, Azure.Communication.CallAutomation.MediaStreamingAudioChannel right) { throw null; } public override string ToString() { throw null; } } - public partial class MediaStreamingAudioData : Azure.Communication.CallAutomation.MediaStreamingPackageBase - { - internal MediaStreamingAudioData() { } - public string Data { get { throw null; } } - public bool IsSilent { get { throw null; } } - public Azure.Communication.CommunicationIdentifier Participant { get { throw null; } } - public System.DateTime Timestamp { get { throw null; } } - } public partial class MediaStreamingConfiguration { public MediaStreamingConfiguration(System.Uri transportUrl, Azure.Communication.CallAutomation.MediaStreamingTransport transportType, Azure.Communication.CallAutomation.MediaStreamingContent contentType, Azure.Communication.CallAutomation.MediaStreamingAudioChannel audioChannelType) { } @@ -863,20 +877,6 @@ public MediaStreamingConfiguration(System.Uri transportUrl, Azure.Communication. public static bool operator !=(Azure.Communication.CallAutomation.MediaStreamingContent left, Azure.Communication.CallAutomation.MediaStreamingContent right) { throw null; } public override string ToString() { throw null; } } - public partial class MediaStreamingMetadata : Azure.Communication.CallAutomation.MediaStreamingPackageBase - { - public MediaStreamingMetadata() { } - [System.Text.Json.Serialization.JsonPropertyNameAttribute("channels")] - public int Channels { get { throw null; } set { } } - [System.Text.Json.Serialization.JsonPropertyNameAttribute("encoding")] - public string Encoding { get { throw null; } set { } } - [System.Text.Json.Serialization.JsonPropertyNameAttribute("length")] - public int Length { get { throw null; } set { } } - [System.Text.Json.Serialization.JsonPropertyNameAttribute("subscriptionId")] - public string MediaSubscriptionId { get { throw null; } set { } } - [System.Text.Json.Serialization.JsonPropertyNameAttribute("sampleRate")] - public int SampleRate { get { throw null; } set { } } - } public partial class MediaStreamingOptions { public MediaStreamingOptions(System.Uri transportUri, Azure.Communication.CallAutomation.MediaStreamingTransport transportType, Azure.Communication.CallAutomation.MediaStreamingContent contentType, Azure.Communication.CallAutomation.MediaStreamingAudioChannel audioChannelType) { } @@ -885,16 +885,6 @@ public MediaStreamingOptions(System.Uri transportUri, Azure.Communication.CallAu public Azure.Communication.CallAutomation.MediaStreamingTransport MediaStreamingTransport { get { throw null; } } public System.Uri TransportUri { get { throw null; } } } - public abstract partial class MediaStreamingPackageBase - { - protected MediaStreamingPackageBase() { } - } - public static partial class MediaStreamingPackageParser - { - public static Azure.Communication.CallAutomation.MediaStreamingPackageBase Parse(System.BinaryData json) { throw null; } - public static Azure.Communication.CallAutomation.MediaStreamingPackageBase Parse(byte[] receivedBytes) { throw null; } - public static Azure.Communication.CallAutomation.MediaStreamingPackageBase Parse(string stringJson) { throw null; } - } [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)] public readonly partial struct MediaStreamingTransport : System.IEquatable { @@ -1230,6 +1220,11 @@ internal ResultInformation() { } public string Message { get { throw null; } } public int? SubCode { get { throw null; } } } + public enum ResultStatus + { + Intermediate = 0, + Final = 1, + } public partial class SendDtmfCompleted : Azure.Communication.CallAutomation.CallAutomationEventBase { internal SendDtmfCompleted() { } @@ -1340,6 +1335,20 @@ public partial class StopTranscriptionOptions public StopTranscriptionOptions() { } public string OperationContext { get { throw null; } set { } } } + public abstract partial class StreamingData + { + protected StreamingData() { } + } + public static partial class StreamingDataParser + { + public static Azure.Communication.CallAutomation.StreamingData Parse(System.BinaryData json) { throw null; } + public static Azure.Communication.CallAutomation.StreamingData Parse(byte[] receivedBytes) { throw null; } + public static Azure.Communication.CallAutomation.StreamingData Parse(string stringJson) { throw null; } + } + public enum TextFormat + { + Display = 0, + } public partial class TextSource : Azure.Communication.CallAutomation.PlaySource { public TextSource(string text) { } @@ -1357,13 +1366,24 @@ internal ToneInfo() { } public int SequenceId { get { throw null; } } public Azure.Communication.CallAutomation.DtmfTone Tone { get { throw null; } } } + public partial class TranscriptionData : Azure.Communication.CallAutomation.StreamingData + { + internal TranscriptionData() { } + public double Confidence { get { throw null; } set { } } + public Azure.Communication.CallAutomation.TextFormat Format { get { throw null; } set { } } + public ulong Offset { get { throw null; } set { } } + public Azure.Communication.CommunicationUserIdentifier Participant { get { throw null; } set { } } + public Azure.Communication.CallAutomation.ResultStatus ResultStatus { get { throw null; } set { } } + public string Text { get { throw null; } set { } } + public System.Collections.Generic.IEnumerable Words { get { throw null; } set { } } + } public partial class TranscriptionFailed : Azure.Communication.CallAutomation.CallAutomationEventBase { internal TranscriptionFailed() { } public Azure.Communication.CallAutomation.TranscriptionUpdate TranscriptionUpdateResult { get { throw null; } } public static Azure.Communication.CallAutomation.TranscriptionFailed Deserialize(string content) { throw null; } } - public partial class TranscriptionMetadata : Azure.Communication.CallAutomation.TranscriptionPackageBase + public partial class TranscriptionMetadata : Azure.Communication.CallAutomation.StreamingData { public TranscriptionMetadata() { } [System.Text.Json.Serialization.JsonPropertyNameAttribute("callConnectionId")] @@ -1383,16 +1403,6 @@ public TranscriptionOptions(System.Uri transportUri, Azure.Communication.CallAut public Azure.Communication.CallAutomation.TranscriptionTransport TranscriptionTransport { get { throw null; } } public System.Uri TransportUrl { get { throw null; } } } - public abstract partial class TranscriptionPackageBase - { - protected TranscriptionPackageBase() { } - } - public static partial class TranscriptionPackageParser - { - public static Azure.Communication.CallAutomation.TranscriptionPackageBase Parse(System.BinaryData json) { throw null; } - public static Azure.Communication.CallAutomation.TranscriptionPackageBase Parse(byte[] receivedBytes) { throw null; } - public static Azure.Communication.CallAutomation.TranscriptionPackageBase Parse(string stringJson) { throw null; } - } public partial class TranscriptionResumed : Azure.Communication.CallAutomation.CallAutomationEventBase { internal TranscriptionResumed() { } @@ -1526,29 +1536,6 @@ public partial class VoipHeader : Azure.Communication.CallAutomation.CustomConte { public VoipHeader(string key, string value) : base (default(string), default(string)) { } } -} -namespace Azure.Communication.CallAutomation.Models.Transcription -{ - public enum ResultStatus - { - Intermediate = 0, - Final = 1, - } - public enum TextFormat - { - Display = 0, - } - public partial class TranscriptionData : Azure.Communication.CallAutomation.TranscriptionPackageBase - { - internal TranscriptionData() { } - public double Confidence { get { throw null; } set { } } - public Azure.Communication.CallAutomation.Models.Transcription.TextFormat Format { get { throw null; } set { } } - public ulong Offset { get { throw null; } set { } } - public Azure.Communication.CommunicationUserIdentifier Participant { get { throw null; } set { } } - public Azure.Communication.CallAutomation.Models.Transcription.ResultStatus ResultStatus { get { throw null; } set { } } - public string Text { get { throw null; } set { } } - public System.Collections.Generic.IEnumerable Words { get { throw null; } set { } } - } public partial class WordData { public WordData() { } diff --git a/sdk/communication/Azure.Communication.CallAutomation/src/Models/MediaStreaming/MediaStreamingPackageBase.cs b/sdk/communication/Azure.Communication.CallAutomation/src/Models/MediaStreaming/MediaStreamingPackageBase.cs deleted file mode 100644 index f2bd0163b37a0..0000000000000 --- a/sdk/communication/Azure.Communication.CallAutomation/src/Models/MediaStreaming/MediaStreamingPackageBase.cs +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -namespace Azure.Communication.CallAutomation -{ - /// - /// Media Streaming Package. - /// - public abstract class MediaStreamingPackageBase - { - } -} diff --git a/sdk/communication/Azure.Communication.CallAutomation/src/Models/MediaStreaming/MediaStreamingPackageParser.cs b/sdk/communication/Azure.Communication.CallAutomation/src/Models/MediaStreaming/MediaStreamingPackageParser.cs deleted file mode 100644 index 86446536215b1..0000000000000 --- a/sdk/communication/Azure.Communication.CallAutomation/src/Models/MediaStreaming/MediaStreamingPackageParser.cs +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Text; -using System.Text.Json; - -namespace Azure.Communication.CallAutomation -{ - /// - /// Parser of the different packages received as part of - /// Media streaming. - /// - public static class MediaStreamingPackageParser - { - /// - /// Parsing a MediaStreaming package from BinaryData. - /// - /// - /// - public static MediaStreamingPackageBase Parse(BinaryData json) - { - return Parse(json.ToString()); - } - - /// - /// Parsing a MediaStreaming package from a byte array. - /// - /// a UTF8 byte array. - /// - public static MediaStreamingPackageBase Parse(byte[] receivedBytes) - { - return Parse(Encoding.UTF8.GetString(receivedBytes)); - } - - /// - /// Parse the incoming package. - /// - /// - /// - /// - public static MediaStreamingPackageBase Parse(string stringJson) - { - JsonElement package = JsonDocument.Parse(stringJson).RootElement; - if (package.GetProperty("kind").ToString() == "AudioMetadata") - { - return JsonSerializer.Deserialize(package.GetProperty("audioMetadata").ToString()); - } - else if (package.GetProperty("kind").ToString() == "AudioData") - { - MediaStreamingAudioDataInternal audioInternal = JsonSerializer.Deserialize(package.GetProperty("audioData").ToString()); - return new MediaStreamingAudioData( - audioInternal.Data, audioInternal.Timestamp, audioInternal.ParticipantRawId, audioInternal.Silent); - } - else - throw new NotSupportedException(stringJson); - } - } -} diff --git a/sdk/communication/Azure.Communication.CallAutomation/src/Models/MediaStreaming/MediaStreamingAudioData.cs b/sdk/communication/Azure.Communication.CallAutomation/src/Models/Streaming/Media/AudioData.cs similarity index 83% rename from sdk/communication/Azure.Communication.CallAutomation/src/Models/MediaStreaming/MediaStreamingAudioData.cs rename to sdk/communication/Azure.Communication.CallAutomation/src/Models/Streaming/Media/AudioData.cs index 4a47ab5fb064c..f1c81b533d53e 100644 --- a/sdk/communication/Azure.Communication.CallAutomation/src/Models/MediaStreaming/MediaStreamingAudioData.cs +++ b/sdk/communication/Azure.Communication.CallAutomation/src/Models/Streaming/Media/AudioData.cs @@ -6,11 +6,11 @@ namespace Azure.Communication.CallAutomation { /// - /// Streaming audio. + /// Streaming Audio. /// - public class MediaStreamingAudioData : MediaStreamingPackageBase + public class AudioData : StreamingData { - internal MediaStreamingAudioData(string data, DateTime timestamp, string participantId, bool silent) + internal AudioData(string data, DateTime timestamp, string participantId, bool silent) { Data = data; Timestamp = timestamp; diff --git a/sdk/communication/Azure.Communication.CallAutomation/src/Models/MediaStreaming/MediaStreamingAudioDataInternal.cs b/sdk/communication/Azure.Communication.CallAutomation/src/Models/Streaming/Media/AudioDataInternal.cs similarity index 92% rename from sdk/communication/Azure.Communication.CallAutomation/src/Models/MediaStreaming/MediaStreamingAudioDataInternal.cs rename to sdk/communication/Azure.Communication.CallAutomation/src/Models/Streaming/Media/AudioDataInternal.cs index 1559575a6a517..f553823460a74 100644 --- a/sdk/communication/Azure.Communication.CallAutomation/src/Models/MediaStreaming/MediaStreamingAudioDataInternal.cs +++ b/sdk/communication/Azure.Communication.CallAutomation/src/Models/Streaming/Media/AudioDataInternal.cs @@ -7,9 +7,9 @@ namespace Azure.Communication.CallAutomation { /// - /// Streaming audio. + /// Streaming Audio. /// - internal class MediaStreamingAudioDataInternal + internal class AudioDataInternal { /// /// The audio data in base64 string. diff --git a/sdk/communication/Azure.Communication.CallAutomation/src/Models/MediaStreaming/MediaStreamingMetadata.cs b/sdk/communication/Azure.Communication.CallAutomation/src/Models/Streaming/Media/AudioMetadata.cs similarity index 89% rename from sdk/communication/Azure.Communication.CallAutomation/src/Models/MediaStreaming/MediaStreamingMetadata.cs rename to sdk/communication/Azure.Communication.CallAutomation/src/Models/Streaming/Media/AudioMetadata.cs index 1c9ac71d9bbd3..015ac85f9632f 100644 --- a/sdk/communication/Azure.Communication.CallAutomation/src/Models/MediaStreaming/MediaStreamingMetadata.cs +++ b/sdk/communication/Azure.Communication.CallAutomation/src/Models/Streaming/Media/AudioMetadata.cs @@ -6,9 +6,9 @@ namespace Azure.Communication.CallAutomation { /// - /// Metadata package for Media Streaming. + /// Metadata for Audio Streaming. /// - public class MediaStreamingMetadata : MediaStreamingPackageBase + public class AudioMetadata : StreamingData { /// /// Subscription Id. diff --git a/sdk/communication/Azure.Communication.CallAutomation/src/Models/Transcription/TranscriptionPackageBase.cs b/sdk/communication/Azure.Communication.CallAutomation/src/Models/Streaming/StreamingData.cs similarity index 70% rename from sdk/communication/Azure.Communication.CallAutomation/src/Models/Transcription/TranscriptionPackageBase.cs rename to sdk/communication/Azure.Communication.CallAutomation/src/Models/Streaming/StreamingData.cs index 5077fa14d6175..63ca3a12d21bd 100644 --- a/sdk/communication/Azure.Communication.CallAutomation/src/Models/Transcription/TranscriptionPackageBase.cs +++ b/sdk/communication/Azure.Communication.CallAutomation/src/Models/Streaming/StreamingData.cs @@ -4,9 +4,9 @@ namespace Azure.Communication.CallAutomation { /// - /// Transcription Package. + /// Base class for Streaming Data /// - public abstract class TranscriptionPackageBase + public abstract class StreamingData { } } diff --git a/sdk/communication/Azure.Communication.CallAutomation/src/Models/Streaming/StreamingDataParser.cs b/sdk/communication/Azure.Communication.CallAutomation/src/Models/Streaming/StreamingDataParser.cs new file mode 100644 index 0000000000000..1af13669b0845 --- /dev/null +++ b/sdk/communication/Azure.Communication.CallAutomation/src/Models/Streaming/StreamingDataParser.cs @@ -0,0 +1,86 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Text; +using System.Text.Json; + +namespace Azure.Communication.CallAutomation +{ + /// + /// A generic parser for different packages, such as Media(Audio) or Transcription, received as + /// part of streaming over websocket + /// + public static class StreamingDataParser + { + /// + /// Parsing a MediaStreaming package from BinaryData. + /// + /// + /// + public static StreamingData Parse(BinaryData json) + { + return Parse(json.ToString()); + } + + /// + /// Parsing a MediaStreaming package from a byte array. + /// + /// a UTF8 byte array. + /// + public static StreamingData Parse(byte[] receivedBytes) + { + return Parse(Encoding.UTF8.GetString(receivedBytes)); + } + + /// + /// Parse the incoming package. + /// + /// + /// + /// + public static StreamingData Parse(string stringJson) + { + JsonElement package = JsonDocument.Parse(stringJson).RootElement; + + string kind = package.GetProperty("kind").ToString(); + + switch (kind) + { + #region Audio + case "AudioMetadata": + return JsonSerializer.Deserialize(package.GetProperty("audioMetadata").ToString()); + + case "AudioData": + AudioDataInternal audioInternal = JsonSerializer.Deserialize(package.GetProperty("audioData").ToString()); + return new AudioData( + audioInternal.Data, audioInternal.Timestamp, audioInternal.ParticipantRawId, audioInternal.Silent); + + #endregion + + #region Transcription + case "TranscriptionMetadata": + return JsonSerializer.Deserialize(package.GetProperty("transcriptionMetadata").ToString()); + + case "TranscriptionData": + TranscriptionDataInternal transcriptionDataInternal = JsonSerializer.Deserialize( + package.GetProperty("transcriptionData").ToString() + ); + return new TranscriptionData( + transcriptionDataInternal.Text, + transcriptionDataInternal.Format, + transcriptionDataInternal.Confidence, + transcriptionDataInternal.Offset, + transcriptionDataInternal.Words, + transcriptionDataInternal.ParticipantRawID, + transcriptionDataInternal.ResultStatus + ); + + #endregion + + default: + throw new NotSupportedException(stringJson); + } + } + } +} diff --git a/sdk/communication/Azure.Communication.CallAutomation/src/Models/Transcription/ResultStatus.cs b/sdk/communication/Azure.Communication.CallAutomation/src/Models/Streaming/Transcription/ResultStatus.cs similarity index 86% rename from sdk/communication/Azure.Communication.CallAutomation/src/Models/Transcription/ResultStatus.cs rename to sdk/communication/Azure.Communication.CallAutomation/src/Models/Streaming/Transcription/ResultStatus.cs index cd66549b8114c..54387c86fef29 100644 --- a/sdk/communication/Azure.Communication.CallAutomation/src/Models/Transcription/ResultStatus.cs +++ b/sdk/communication/Azure.Communication.CallAutomation/src/Models/Streaming/Transcription/ResultStatus.cs @@ -1,7 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -namespace Azure.Communication.CallAutomation.Models.Transcription +namespace Azure.Communication.CallAutomation { /// /// The status of the result of transcription diff --git a/sdk/communication/Azure.Communication.CallAutomation/src/Models/Transcription/TextFormat.cs b/sdk/communication/Azure.Communication.CallAutomation/src/Models/Streaming/Transcription/TextFormat.cs similarity index 83% rename from sdk/communication/Azure.Communication.CallAutomation/src/Models/Transcription/TextFormat.cs rename to sdk/communication/Azure.Communication.CallAutomation/src/Models/Streaming/Transcription/TextFormat.cs index f5464aca03bc2..d04a27a7d2698 100644 --- a/sdk/communication/Azure.Communication.CallAutomation/src/Models/Transcription/TextFormat.cs +++ b/sdk/communication/Azure.Communication.CallAutomation/src/Models/Streaming/Transcription/TextFormat.cs @@ -1,7 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -namespace Azure.Communication.CallAutomation.Models.Transcription +namespace Azure.Communication.CallAutomation { /// /// The format of transcription text diff --git a/sdk/communication/Azure.Communication.CallAutomation/src/Models/Transcription/TranscriptionData.cs b/sdk/communication/Azure.Communication.CallAutomation/src/Models/Streaming/Transcription/TranscriptionData.cs similarity index 94% rename from sdk/communication/Azure.Communication.CallAutomation/src/Models/Transcription/TranscriptionData.cs rename to sdk/communication/Azure.Communication.CallAutomation/src/Models/Streaming/Transcription/TranscriptionData.cs index b1b9a77cb379e..a0a7611cc2927 100644 --- a/sdk/communication/Azure.Communication.CallAutomation/src/Models/Transcription/TranscriptionData.cs +++ b/sdk/communication/Azure.Communication.CallAutomation/src/Models/Streaming/Transcription/TranscriptionData.cs @@ -3,14 +3,13 @@ using System; using System.Collections.Generic; -using System.Text.Json.Serialization; -namespace Azure.Communication.CallAutomation.Models.Transcription +namespace Azure.Communication.CallAutomation { /// /// Streaming Transcription. /// - public class TranscriptionData : TranscriptionPackageBase + public class TranscriptionData : StreamingData { internal TranscriptionData(string text, string format, double confidence, ulong offset, IEnumerable words, string participantRawID, string resultStatus) { diff --git a/sdk/communication/Azure.Communication.CallAutomation/src/Models/Transcription/TranscriptionDataInternal.cs b/sdk/communication/Azure.Communication.CallAutomation/src/Models/Streaming/Transcription/TranscriptionDataInternal.cs similarity index 96% rename from sdk/communication/Azure.Communication.CallAutomation/src/Models/Transcription/TranscriptionDataInternal.cs rename to sdk/communication/Azure.Communication.CallAutomation/src/Models/Streaming/Transcription/TranscriptionDataInternal.cs index 510133f065e06..2ca906d8760d6 100644 --- a/sdk/communication/Azure.Communication.CallAutomation/src/Models/Transcription/TranscriptionDataInternal.cs +++ b/sdk/communication/Azure.Communication.CallAutomation/src/Models/Streaming/Transcription/TranscriptionDataInternal.cs @@ -4,7 +4,7 @@ using System.Collections.Generic; using System.Text.Json.Serialization; -namespace Azure.Communication.CallAutomation.Models.Transcription +namespace Azure.Communication.CallAutomation { /// /// Streaming Transcription. diff --git a/sdk/communication/Azure.Communication.CallAutomation/src/Models/Transcription/TranscriptionMetadata.cs b/sdk/communication/Azure.Communication.CallAutomation/src/Models/Streaming/Transcription/TranscriptionMetadata.cs similarity index 88% rename from sdk/communication/Azure.Communication.CallAutomation/src/Models/Transcription/TranscriptionMetadata.cs rename to sdk/communication/Azure.Communication.CallAutomation/src/Models/Streaming/Transcription/TranscriptionMetadata.cs index 6f6ec6af6632e..0746403b990f1 100644 --- a/sdk/communication/Azure.Communication.CallAutomation/src/Models/Transcription/TranscriptionMetadata.cs +++ b/sdk/communication/Azure.Communication.CallAutomation/src/Models/Streaming/Transcription/TranscriptionMetadata.cs @@ -6,9 +6,9 @@ namespace Azure.Communication.CallAutomation { /// - /// Transcription package for Transcription Streaming. + /// Metadata for Transcription Streaming. /// - public class TranscriptionMetadata : TranscriptionPackageBase + public class TranscriptionMetadata : StreamingData { /// /// Transcription Subscription Id. diff --git a/sdk/communication/Azure.Communication.CallAutomation/src/Models/Transcription/WordData.cs b/sdk/communication/Azure.Communication.CallAutomation/src/Models/Streaming/Transcription/WordData.cs similarity index 89% rename from sdk/communication/Azure.Communication.CallAutomation/src/Models/Transcription/WordData.cs rename to sdk/communication/Azure.Communication.CallAutomation/src/Models/Streaming/Transcription/WordData.cs index 816ea137563a4..a44f851c1f220 100644 --- a/sdk/communication/Azure.Communication.CallAutomation/src/Models/Transcription/WordData.cs +++ b/sdk/communication/Azure.Communication.CallAutomation/src/Models/Streaming/Transcription/WordData.cs @@ -3,7 +3,7 @@ using System.Text.Json.Serialization; -namespace Azure.Communication.CallAutomation.Models.Transcription +namespace Azure.Communication.CallAutomation { /// /// The result for each word of the phrase diff --git a/sdk/communication/Azure.Communication.CallAutomation/src/Models/Transcription/TranscriptionPackageParser.cs b/sdk/communication/Azure.Communication.CallAutomation/src/Models/Transcription/TranscriptionPackageParser.cs deleted file mode 100644 index 4663c7d7c76f1..0000000000000 --- a/sdk/communication/Azure.Communication.CallAutomation/src/Models/Transcription/TranscriptionPackageParser.cs +++ /dev/null @@ -1,72 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Text; -using System.Text.Json; -using Azure.Communication.CallAutomation.Models.Transcription; - -namespace Azure.Communication.CallAutomation -{ - /// - /// Parser of the different packages received as part of - /// Transcription data streaming. - /// - public static class TranscriptionPackageParser - { - /// - /// Parsing a Transcription package from BinaryData. - /// - /// - /// - public static TranscriptionPackageBase Parse(BinaryData json) - { - return Parse(json.ToString()); - } - - /// - /// Parsing a Transcription package from a byte array. - /// - /// a UTF8 byte array. - /// - public static TranscriptionPackageBase Parse(byte[] receivedBytes) - { - return Parse(Encoding.UTF8.GetString(receivedBytes)); - } - - /// - /// Parse the incoming package. - /// - /// - /// - /// - public static TranscriptionPackageBase Parse(string stringJson) - { - JsonElement package = JsonDocument.Parse(stringJson).RootElement; - - string kind = package.GetProperty("kind").ToString(); - - if (kind == "TranscriptionMetadata") - { - return JsonSerializer.Deserialize(package.GetProperty("transcriptionMetadata").ToString()); - } - else if (kind == "TranscriptionData") - { - TranscriptionDataInternal transcriptionDataInternal = JsonSerializer.Deserialize( - package.GetProperty("transcriptionData").ToString() - ); - return new TranscriptionData( - transcriptionDataInternal.Text, - transcriptionDataInternal.Format, - transcriptionDataInternal.Confidence, - transcriptionDataInternal.Offset, - transcriptionDataInternal.Words, - transcriptionDataInternal.ParticipantRawID, - transcriptionDataInternal.ResultStatus - ); - } - else - throw new NotSupportedException(stringJson); - } - } -} diff --git a/sdk/communication/Azure.Communication.CallAutomation/tests/CallConnections/CallConnectionTests.cs b/sdk/communication/Azure.Communication.CallAutomation/tests/CallConnections/CallConnectionTests.cs index df9631b1f37a2..38252281b23a7 100644 --- a/sdk/communication/Azure.Communication.CallAutomation/tests/CallConnections/CallConnectionTests.cs +++ b/sdk/communication/Azure.Communication.CallAutomation/tests/CallConnections/CallConnectionTests.cs @@ -5,7 +5,6 @@ using System.Collections.Generic; using System.Net; using System.Threading.Tasks; -using Azure.Communication.CallAutomation.Models; using Azure.Communication.CallAutomation.Tests.Infrastructure; using NUnit.Framework; diff --git a/sdk/communication/Azure.Communication.CallAutomation/tests/MediaStreaming/CallAutomationStreamingParserTests.cs b/sdk/communication/Azure.Communication.CallAutomation/tests/MediaStreaming/CallAutomationStreamingParserTests.cs deleted file mode 100644 index 48d79f5519af2..0000000000000 --- a/sdk/communication/Azure.Communication.CallAutomation/tests/MediaStreaming/CallAutomationStreamingParserTests.cs +++ /dev/null @@ -1,126 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Text.Json; -using Newtonsoft.Json.Linq; -using NUnit.Framework; - -namespace Azure.Communication.CallAutomation.Tests.MediaStreaming -{ - internal class CallAutomationStreamingParserTests - { - [Test] - public void ParseMetadata_Test() - { - string metadataJson = "{" - + "\"kind\": \"AudioMetadata\"," - + "\"audioMetadata\": {" - + "\"subscriptionId\": \"subscriptionId\"," - + "\"encoding\": \"encodingType\"," - + "\"sampleRate\": 8," - + "\"channels\": 2," - + "\"length\": 640" - + "}" - + "}"; - - MediaStreamingMetadata streamingMetadata = (MediaStreamingMetadata)MediaStreamingPackageParser.Parse(metadataJson); - ValidateMetadata(streamingMetadata); - } - - [Test] - public void ParseAudio_Test() - { - string audioJson = "{" - + "\"kind\": \"AudioData\"," - + "\"audioData\": {" - + "\"data\": \"AQIDBAU=\"," // [1, 2, 3, 4, 5] - + "\"timestamp\": \"2022-08-23T11:48:05Z\"," - + "\"participantRawID\": \"participantId\"," - + "\"silent\": false" - + "}" - + "}"; - - MediaStreamingAudioData streamingAudio = (MediaStreamingAudioData) MediaStreamingPackageParser.Parse(audioJson); - ValidateAudioData(streamingAudio); - } - - [Test] - public void ParseAudio_NoParticipantIdSilent_Test() - { - string audioJson = "{" - + "\"kind\": \"AudioData\"," - + "\"audioData\": {" - + "\"data\": \"AQIDBAU=\"," // [1, 2, 3, 4, 5] - + "\"timestamp\": \"2022-08-23T11:48:05Z\"" - + "}" - + "}"; - - MediaStreamingAudioData streamingAudio = (MediaStreamingAudioData)MediaStreamingPackageParser.Parse(audioJson); - ValidateAudioDataNoParticipant(streamingAudio); - } - - [Test] - public void ParseBinaryData() - { - JObject jsonData = new JObject(); - jsonData["kind"] = "AudioData"; - jsonData["audioData"] = new JObject(); - jsonData["audioData"]["data"] = "AQIDBAU="; - jsonData["audioData"]["timestamp"] = "2022-08-23T11:48:05Z"; - jsonData["audioData"]["participantRawID"] = "participantId"; - jsonData["audioData"]["silent"] = false; - - var binaryData = BinaryData.FromString(jsonData.ToString()); - - MediaStreamingAudioData streamingAudio = (MediaStreamingAudioData)MediaStreamingPackageParser.Parse(binaryData); - ValidateAudioData(streamingAudio); - } - - [Test] - public void ParseAudioEventsWithBynaryArray() - { - JObject jsonData = new JObject(); - jsonData["kind"] = "AudioData"; - jsonData["audioData"] = new JObject(); - jsonData["audioData"]["data"] = "AQIDBAU="; - jsonData["audioData"]["timestamp"] = "2022-08-23T11:48:05Z"; - jsonData["audioData"]["participantRawID"] = "participantId"; - jsonData["audioData"]["silent"] = false; - - byte[] receivedBytes = System.Text.Encoding.UTF8.GetBytes(jsonData.ToString()); - MediaStreamingAudioData parsedPackage = (MediaStreamingAudioData) MediaStreamingPackageParser.Parse(receivedBytes); - - Assert.NotNull(parsedPackage); - ValidateAudioData(parsedPackage); - } - - private static void ValidateMetadata(MediaStreamingMetadata streamingMetadata) - { - Assert.IsNotNull(streamingMetadata); - Assert.AreEqual("subscriptionId", streamingMetadata.MediaSubscriptionId); - Assert.AreEqual("encodingType", streamingMetadata.Encoding); - Assert.AreEqual(8, streamingMetadata.SampleRate); - Assert.AreEqual(2, streamingMetadata.Channels); - Assert.AreEqual(640, streamingMetadata.Length); - } - - private static void ValidateAudioData(MediaStreamingAudioData streamingAudio) - { - Assert.IsNotNull(streamingAudio); - Assert.AreEqual("AQIDBAU=", streamingAudio.Data); - Assert.AreEqual(2022, streamingAudio.Timestamp.Year); - Assert.IsTrue(streamingAudio.Participant is CommunicationIdentifier); - Assert.AreEqual("participantId", streamingAudio.Participant.RawId); - Assert.IsFalse(streamingAudio.IsSilent); - } - private static void ValidateAudioDataNoParticipant(MediaStreamingAudioData streamingAudio) - { - Assert.IsNotNull(streamingAudio); - Assert.AreEqual("AQIDBAU=", streamingAudio.Data); - Assert.AreEqual(2022, streamingAudio.Timestamp.Year); - Assert.IsNull(streamingAudio.Participant); - Assert.IsFalse(streamingAudio.IsSilent); - } - } -} diff --git a/sdk/communication/Azure.Communication.CallAutomation/tests/Transcription/TranscriptionPackageParserTests.cs b/sdk/communication/Azure.Communication.CallAutomation/tests/Streaming/StreamingDataParserTests.cs similarity index 52% rename from sdk/communication/Azure.Communication.CallAutomation/tests/Transcription/TranscriptionPackageParserTests.cs rename to sdk/communication/Azure.Communication.CallAutomation/tests/Streaming/StreamingDataParserTests.cs index c5c541c6e5bf0..ca2ea813d8c0c 100644 --- a/sdk/communication/Azure.Communication.CallAutomation/tests/Transcription/TranscriptionPackageParserTests.cs +++ b/sdk/communication/Azure.Communication.CallAutomation/tests/Streaming/StreamingDataParserTests.cs @@ -4,15 +4,131 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text.Json; -using Azure.Communication.CallAutomation.Models.Transcription; using Newtonsoft.Json.Linq; using NUnit.Framework; -namespace Azure.Communication.CallAutomation.Tests.Trascription +namespace Azure.Communication.CallAutomation.Tests.MediaStreaming { - internal class TranscriptionPackageParserTests + internal class StreamingDataParserTests { + #region Audio + + [Test] + public void ParseAudioMetadata_Test() + { + string metadataJson = "{" + + "\"kind\": \"AudioMetadata\"," + + "\"audioMetadata\": {" + + "\"subscriptionId\": \"subscriptionId\"," + + "\"encoding\": \"encodingType\"," + + "\"sampleRate\": 8," + + "\"channels\": 2," + + "\"length\": 640" + + "}" + + "}"; + + AudioMetadata streamingMetadata = (AudioMetadata)StreamingDataParser.Parse(metadataJson); + ValidateAudioMetadata(streamingMetadata); + } + + [Test] + public void ParseAudioData_Test() + { + string audioJson = "{" + + "\"kind\": \"AudioData\"," + + "\"audioData\": {" + + "\"data\": \"AQIDBAU=\"," // [1, 2, 3, 4, 5] + + "\"timestamp\": \"2022-08-23T11:48:05Z\"," + + "\"participantRawID\": \"participantId\"," + + "\"silent\": false" + + "}" + + "}"; + + AudioData streamingAudio = (AudioData)StreamingDataParser.Parse(audioJson); + ValidateAudioData(streamingAudio); + } + + [Test] + public void ParseAudioData_NoParticipantIdSilent_Test() + { + string audioJson = "{" + + "\"kind\": \"AudioData\"," + + "\"audioData\": {" + + "\"data\": \"AQIDBAU=\"," // [1, 2, 3, 4, 5] + + "\"timestamp\": \"2022-08-23T11:48:05Z\"" + + "}" + + "}"; + + AudioData streamingAudio = (AudioData)StreamingDataParser.Parse(audioJson); + ValidateAudioDataNoParticipant(streamingAudio); + } + + [Test] + public void ParseBinaryAudioData() + { + JObject jsonData = new JObject(); + jsonData["kind"] = "AudioData"; + jsonData["audioData"] = new JObject(); + jsonData["audioData"]["data"] = "AQIDBAU="; + jsonData["audioData"]["timestamp"] = "2022-08-23T11:48:05Z"; + jsonData["audioData"]["participantRawID"] = "participantId"; + jsonData["audioData"]["silent"] = false; + + var binaryData = BinaryData.FromString(jsonData.ToString()); + + AudioData streamingAudio = (AudioData)StreamingDataParser.Parse(binaryData); + ValidateAudioData(streamingAudio); + } + + [Test] + public void ParseAudioDataEventsWithBinaryArray() + { + JObject jsonData = new JObject(); + jsonData["kind"] = "AudioData"; + jsonData["audioData"] = new JObject(); + jsonData["audioData"]["data"] = "AQIDBAU="; + jsonData["audioData"]["timestamp"] = "2022-08-23T11:48:05Z"; + jsonData["audioData"]["participantRawID"] = "participantId"; + jsonData["audioData"]["silent"] = false; + + byte[] receivedBytes = System.Text.Encoding.UTF8.GetBytes(jsonData.ToString()); + AudioData parsedPackage = (AudioData)StreamingDataParser.Parse(receivedBytes); + + Assert.NotNull(parsedPackage); + ValidateAudioData(parsedPackage); + } + + private static void ValidateAudioMetadata(AudioMetadata streamingAudioMetadata) + { + Assert.IsNotNull(streamingAudioMetadata); + Assert.AreEqual("subscriptionId", streamingAudioMetadata.MediaSubscriptionId); + Assert.AreEqual("encodingType", streamingAudioMetadata.Encoding); + Assert.AreEqual(8, streamingAudioMetadata.SampleRate); + Assert.AreEqual(2, streamingAudioMetadata.Channels); + Assert.AreEqual(640, streamingAudioMetadata.Length); + } + + private static void ValidateAudioData(AudioData streamingAudio) + { + Assert.IsNotNull(streamingAudio); + Assert.AreEqual("AQIDBAU=", streamingAudio.Data); + Assert.AreEqual(2022, streamingAudio.Timestamp.Year); + Assert.IsTrue(streamingAudio.Participant is CommunicationIdentifier); + Assert.AreEqual("participantId", streamingAudio.Participant.RawId); + Assert.IsFalse(streamingAudio.IsSilent); + } + private static void ValidateAudioDataNoParticipant(AudioData streamingAudio) + { + Assert.IsNotNull(streamingAudio); + Assert.AreEqual("AQIDBAU=", streamingAudio.Data); + Assert.AreEqual(2022, streamingAudio.Timestamp.Year); + Assert.IsNull(streamingAudio.Participant); + Assert.IsFalse(streamingAudio.IsSilent); + } + #endregion + + #region Transcription + [Test] public void ParseTranscriptionMetadata_Test() { @@ -28,12 +144,12 @@ public void ParseTranscriptionMetadata_Test() "}" + "}"; - TranscriptionMetadata streamingMetadata = (TranscriptionMetadata)TranscriptionPackageParser.Parse(metadataJson); - ValidateMetadata(streamingMetadata); + TranscriptionMetadata streamingMetadata = (TranscriptionMetadata)StreamingDataParser.Parse(metadataJson); + ValidateTranscriptionMetadata(streamingMetadata); } [Test] - public void ParseTranscription_Test() + public void ParseTranscriptionData_Test() { var transcriptionJson = "{" + @@ -60,12 +176,12 @@ public void ParseTranscription_Test() "}" + "}"; - TranscriptionData transcription = (TranscriptionData) TranscriptionPackageParser.Parse(transcriptionJson); + TranscriptionData transcription = (TranscriptionData)StreamingDataParser.Parse(transcriptionJson); ValidateTranscriptionData(transcription); } [Test] - public void ParseBinaryData() + public void ParseTranscriptionBinaryData() { JObject jsonData = new() { @@ -99,12 +215,12 @@ public void ParseBinaryData() var binaryData = BinaryData.FromString(jsonData.ToString()); - TranscriptionData transcription = (TranscriptionData)TranscriptionPackageParser.Parse(binaryData); + TranscriptionData transcription = (TranscriptionData)StreamingDataParser.Parse(binaryData); ValidateTranscriptionData(transcription); } [Test] - public void ParseAudioEventsWithBynaryArray() + public void ParseTranscriptionDataEventsWithBinaryArray() { JObject jsonData = new() { @@ -137,13 +253,13 @@ public void ParseAudioEventsWithBynaryArray() jsonData["transcriptionData"]["resultStatus"] = "final"; byte[] receivedBytes = System.Text.Encoding.UTF8.GetBytes(jsonData.ToString()); - TranscriptionData parsedPackage = (TranscriptionData) TranscriptionPackageParser.Parse(receivedBytes); + TranscriptionData parsedPackage = (TranscriptionData)StreamingDataParser.Parse(receivedBytes); Assert.NotNull(parsedPackage); ValidateTranscriptionData(parsedPackage); } - private static void ValidateMetadata(TranscriptionMetadata transcriptionMetadata) + private static void ValidateTranscriptionMetadata(TranscriptionMetadata transcriptionMetadata) { Assert.IsNotNull(transcriptionMetadata); Assert.AreEqual("subscriptionId", transcriptionMetadata.TranscriptionSubscriptionId); @@ -173,5 +289,6 @@ private static void ValidateTranscriptionData(TranscriptionData transcription) Console.WriteLine(transcription.ResultStatus.ToString()); Assert.AreEqual(ResultStatus.Final, transcription.ResultStatus); } + #endregion } }