diff --git a/Deepgram/Abstractions/v2/AbstractRestClient.cs b/Deepgram/Abstractions/v2/AbstractRestClient.cs index 0e165d1..e750f9b 100644 --- a/Deepgram/Abstractions/v2/AbstractRestClient.cs +++ b/Deepgram/Abstractions/v2/AbstractRestClient.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved. // Use of this source code is governed by a MIT license that can be found in the LICENSE file. // SPDX-License-Identifier: MIT diff --git a/Deepgram/Abstractions/v2/AbstractWebSocketClient.cs b/Deepgram/Abstractions/v2/AbstractWebSocketClient.cs index 16bdbec..6746567 100644 --- a/Deepgram/Abstractions/v2/AbstractWebSocketClient.cs +++ b/Deepgram/Abstractions/v2/AbstractWebSocketClient.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved. // Use of this source code is governed by a MIT license that can be found in the LICENSE file. // SPDX-License-Identifier: MIT @@ -245,7 +245,7 @@ public async Task Subscribe(EventHandler eventHandler) /// /// Sends a Close message to Deepgram /// - public virtual Task SendClose(bool nullByte = false) + public virtual Task SendClose(bool nullByte = false, CancellationTokenSource? _cancellationToken = null) { throw new DeepgramException("Unimplemented"); } @@ -271,8 +271,8 @@ public virtual void SendMessage(byte[] data, int length = Constants.UseArrayLeng /// /// This method sends a binary message over the WebSocket connection immediately without queueing. - /// - public virtual async Task SendBinaryImmediately(byte[] data, int length = Constants.UseArrayLengthForSend) + /// , + public virtual async Task SendBinaryImmediately(byte[] data, int length = Constants.UseArrayLengthForSend, CancellationTokenSource? _cancellationToken = null) { if (!IsConnected()) { @@ -280,7 +280,10 @@ public virtual async Task SendBinaryImmediately(byte[] data, int length = Consta return; } - await _mutexSend.WaitAsync(_cancellationTokenSource.Token); + // provide a cancellation token, or use the one in the class + var _cancelToken = _cancellationToken ?? _cancellationTokenSource; + + await _mutexSend.WaitAsync(_cancelToken.Token); try { Log.Verbose("SendBinaryImmediately", "Sending binary message immediately..."); @@ -300,7 +303,7 @@ await _clientWebSocket.SendAsync(new ArraySegment(data, 0, length), WebSoc /// /// This method sends a text message over the WebSocket connection immediately without queueing. /// - public virtual async Task SendMessageImmediately(byte[] data, int length = Constants.UseArrayLengthForSend) + public virtual async Task SendMessageImmediately(byte[] data, int length = Constants.UseArrayLengthForSend, CancellationTokenSource? _cancellationToken = null) { if (!IsConnected()) { @@ -308,6 +311,9 @@ public virtual async Task SendMessageImmediately(byte[] data, int length = Const return; } + // provide a cancellation token, or use the one in the class + var _cancelToken = _cancellationToken ?? _cancellationTokenSource; + await _mutexSend.WaitAsync(_cancellationTokenSource.Token); try { @@ -613,7 +619,7 @@ public async Task Stop(CancellationTokenSource? cancelToken = null, bool n if (_clientWebSocket!.State == WebSocketState.Open) { Log.Debug("Stop", "Sending Close message..."); - await SendClose(nullByte); + await SendClose(nullByte, cancelToken); } // small delay to wait for any final transcription diff --git a/Deepgram/Abstractions/v2/Utilities.cs b/Deepgram/Abstractions/v2/Utilities.cs index 65f134d..6f130f8 100644 --- a/Deepgram/Abstractions/v2/Utilities.cs +++ b/Deepgram/Abstractions/v2/Utilities.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved. // Use of this source code is governed by a MIT license that can be found in the LICENSE file. // SPDX-License-Identifier: MIT diff --git a/Deepgram/Abstractions/v2/WebSocketMessage.cs b/Deepgram/Abstractions/v2/WebSocketMessage.cs index 31a5182..410afe2 100644 --- a/Deepgram/Abstractions/v2/WebSocketMessage.cs +++ b/Deepgram/Abstractions/v2/WebSocketMessage.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved. // Use of this source code is governed by a MIT license that can be found in the LICENSE file. // SPDX-License-Identifier: MIT diff --git a/Deepgram/ClientFactory.cs b/Deepgram/ClientFactory.cs index 17e2f7d..6987d74 100644 --- a/Deepgram/ClientFactory.cs +++ b/Deepgram/ClientFactory.cs @@ -27,15 +27,6 @@ public static V1.IAnalyzeClient CreateAnalyzeClient(string apiKey = "", Deepgram return new AnalyzeClient(apiKey, options, httpId); } - /// - /// This method allows you to create an AnalyzeClient with a specific version of the client. - /// - public static object CreateAnalyzeClient(int version, string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) - { - // Currently only a single version of the AnalyzeClient exists - return new AnalyzeClient(apiKey, options, httpId); - } - /// // *********** WARNING *********** // This function creates a LiveClient for the Deepgram API @@ -63,25 +54,6 @@ public static V2.IListenWebSocketClient CreateListenWebSocketClient(string apiKe return new ListenWebSocketClient(apiKey, options); } - /// - /// This method allows you to create an AnalyzeClient with a specific version of the client. - /// - public static object CreateListenWebSocketClient(int version, string apiKey = "", DeepgramWsClientOptions? options = null) - { - // at some point this needs to be changed to use reflection to get the type of the client - switch(version) - { - case 1: - Log.Information("ClientFactory", $"Version 1 of the ListenWebSocketClient is being deprecated in the next major version."); - Log.Information("ClientFactory", $"Transition to the latest version at your earliest convenience."); - return new ListenV1.Client(apiKey, options); - case 2: - return new ListenV2.Client(apiKey, options); - default: - throw new ArgumentException("Invalid version", nameof(version)); - } - } - /// /// Create a new ManageClient /// @@ -94,31 +66,6 @@ public static V1.IManageClient CreateManageClient(string apiKey = "", DeepgramHt return new ManageClient(apiKey, options, httpId); } - /// - /// This method allows you to create an ManageClient with a specific version of the client. - /// - public static object CreateManageClient(int version, string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) - { - // Currently only a single version of the ManageClient exists - return new ManageClient(apiKey, options, httpId); - } - - /// - // *********** WARNING *********** - // This function creates a OnPrem Client for the Deepgram API - // - // Deprecated: This function is deprecated. Use the `CreateSelfHostedClient` function instead. - // This will be removed in a future release. - // - // This class is frozen and no new functionality will be added. - // *********** WARNING *********** - /// - [Obsolete("Please use CreateSelfHostedClient instead", false)] - public static V1.IOnPremClient CreateOnPremClient(string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) - { - return new OnPremClient(apiKey, options, httpId); - } - /// /// Create a new SelfHostedClient /// @@ -132,40 +79,58 @@ public static V1.ISelfHostedClient CreateSelfHostedClient(string apiKey = "", De } /// - /// This method allows you to create an SelfHostedClient with a specific version of the client. + /// Create a new ListenRESTClient /// - public static object CreateSelfHostedClient(int version, string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) + /// + /// + /// + /// + public static V1.IListenRESTClient CreateListenRESTClient(string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) { - // Currently only a single version of the SelfHostedClient exists - return new SelfHostedClient(apiKey, options, httpId); + return new ListenRESTClient(apiKey, options, httpId); } /// - // *********** WARNING *********** - // This function creates a PreRecordedClient for the Deepgram API - // - // Deprecated: This function is deprecated. Use the `CreateListenRESTClient` function instead. - // This will be removed in a future release. - // - // This class is frozen and no new functionality will be added. - // *********** WARNING *********** + /// Create a new SpeakRESTClient /// - [Obsolete("Please use CreateListenRESTClient instead", false)] - public static V1.IPreRecordedClient CreatePreRecordedClient(string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) + /// + /// + /// + /// + public static V1.ISpeakRESTClient CreateSpeakRESTClient(string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) { - return new PreRecordedClient(apiKey, options, httpId); + return new SpeakRESTClient(apiKey, options, httpId); } /// - /// Create a new ListenRESTClient + /// Create a new AnalyzeClient /// /// /// - /// /// - public static V1.IListenRESTClient CreateListenRESTClient(string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) + public static V2.ISpeakWebSocketClient CreateSpeakWebSocketClient(string apiKey = "", DeepgramWsClientOptions? options = null) { - return new ListenRESTClient(apiKey, options, httpId); + return new SpeakWebSocketClient(apiKey, options); + } + + + + /// + /// These functions are only used to create specific and typically older verions of Listen, Speak, etc clients. + /// The functions above always create the latest version of the client. + /// + /// The functions below should only be used to reference an older client with the intnetion of at some point to move away from + /// that version to the latest version (ie transition to the function above). Older clients will be removed at the next major version + /// and the latest version will be renamed to v1. + /// + + /// + /// This method allows you to create an AnalyzeClient with a specific version of the client. + /// + public static object CreateAnalyzeClient(int version, string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) + { + // Currently only a single version of the AnalyzeClient exists + return new AnalyzeClient(apiKey, options, httpId); } /// @@ -178,51 +143,49 @@ public static object CreateListenRESTClient(int version, string apiKey = "", Dee } /// - // *********** WARNING *********** - // This function creates a Speak Client for the Deepgram API - // - // Deprecated: This function is deprecated. Use the `CreateSpeakRESTClient` function instead. - // This will be removed in a future release. - // - // This class is frozen and no new functionality will be added. - // *********** WARNING *********** + /// This method allows you to create an AnalyzeClient with a specific version of the client. /// - [Obsolete("Please use CreateSpeakRESTClient instead", false)] - public static V1.ISpeakClient CreateSpeakClient(string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) + public static object CreateListenWebSocketClient(int version, string apiKey = "", DeepgramWsClientOptions? options = null) { - return new SpeakClient(apiKey, options, httpId); + // at some point this needs to be changed to use reflection to get the type of the client + switch (version) + { + case 1: + Log.Information("ClientFactory", $"Version 1 of the ListenWebSocketClient is being deprecated in the next major version."); + Log.Information("ClientFactory", $"Transition to the latest version at your earliest convenience."); + return new ListenV1.Client(apiKey, options); + case 2: + return new ListenV2.Client(apiKey, options); + default: + throw new ArgumentException("Invalid version", nameof(version)); + } } /// - /// Create a new SpeakRESTClient + /// This method allows you to create an ManageClient with a specific version of the client. /// - /// - /// - /// - /// - public static V1.ISpeakRESTClient CreateSpeakRESTClient(string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) + public static object CreateManageClient(int version, string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) { - return new SpeakRESTClient(apiKey, options, httpId); + // Currently only a single version of the ManageClient exists + return new ManageClient(apiKey, options, httpId); } /// - /// This method allows you to create an SpeakRESTClient with a specific version of the client. + /// This method allows you to create an SelfHostedClient with a specific version of the client. /// - public static object CreateSpeakRESTClient(int version, string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) + public static object CreateSelfHostedClient(int version, string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) { - // Currently only a single version of the SpeakRESTClient exists - return new SpeakRESTClient(apiKey, options, httpId); + // Currently only a single version of the SelfHostedClient exists + return new SelfHostedClient(apiKey, options, httpId); } /// - /// Create a new AnalyzeClient + /// This method allows you to create an SpeakRESTClient with a specific version of the client. /// - /// - /// - /// - public static V2.ISpeakWebSocketClient CreateSpeakWebSocketClient(string apiKey = "", DeepgramWsClientOptions? options = null) + public static object CreateSpeakRESTClient(int version, string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) { - return new SpeakWebSocketClient(apiKey, options); + // Currently only a single version of the SpeakRESTClient exists + return new SpeakRESTClient(apiKey, options, httpId); } /// @@ -243,4 +206,57 @@ public static object CreateSpeakWebSocketClient(int version, string apiKey = "", throw new ArgumentException("Invalid version", nameof(version)); } } + + /// + /// The functions below are deprecated and will be removed in the next major version. You should not use + /// these functions for new projects. They are only here to support older projects that are using these functions. + /// + + /// + // *********** WARNING *********** + // This function creates a Speak Client for the Deepgram API + // + // Deprecated: This function is deprecated. Use the `CreateSpeakRESTClient` function instead. + // This will be removed in a future release. + // + // This class is frozen and no new functionality will be added. + // *********** WARNING *********** + /// + [Obsolete("Please use CreateSpeakRESTClient instead", false)] + public static V1.ISpeakClient CreateSpeakClient(string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) + { + return new SpeakClient(apiKey, options, httpId); + } + + /// + // *********** WARNING *********** + // This function creates a PreRecordedClient for the Deepgram API + // + // Deprecated: This function is deprecated. Use the `CreateListenRESTClient` function instead. + // This will be removed in a future release. + // + // This class is frozen and no new functionality will be added. + // *********** WARNING *********** + /// + [Obsolete("Please use CreateListenRESTClient instead", false)] + public static V1.IPreRecordedClient CreatePreRecordedClient(string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) + { + return new PreRecordedClient(apiKey, options, httpId); + } + + /// + // *********** WARNING *********** + // This function creates a OnPrem Client for the Deepgram API + // + // Deprecated: This function is deprecated. Use the `CreateSelfHostedClient` function instead. + // This will be removed in a future release. + // + // This class is frozen and no new functionality will be added. + // *********** WARNING *********** + /// + [Obsolete("Please use CreateSelfHostedClient instead", false)] + public static V1.IOnPremClient CreateOnPremClient(string apiKey = "", DeepgramHttpClientOptions? options = null, string? httpId = null) + { + return new OnPremClient(apiKey, options, httpId); + } } diff --git a/Deepgram/Clients/Interfaces/v2/IListenWebSocketClient.cs b/Deepgram/Clients/Interfaces/v2/IListenWebSocketClient.cs index d59cce6..6d26ff2 100644 --- a/Deepgram/Clients/Interfaces/v2/IListenWebSocketClient.cs +++ b/Deepgram/Clients/Interfaces/v2/IListenWebSocketClient.cs @@ -84,7 +84,7 @@ public Task Connect(LiveSchema options, CancellationTokenSource? cancelTok /// /// Sends a Close message to Deepgram /// - public Task SendClose(bool nullByte = false); + public Task SendClose(bool nullByte = false, CancellationTokenSource? _cancellationToken = null); /// /// Sends a binary message over the WebSocket connection. @@ -111,14 +111,16 @@ public Task Connect(LiveSchema options, CancellationTokenSource? cancelTok /// /// /// The number of bytes from the data to send. Use `Constants.UseArrayLengthForSend` to send the entire array. - public Task SendBinaryImmediately(byte[] data, int length = Constants.UseArrayLengthForSend); + /// /// Provide a cancel token to be used for the send function or use the internal one + public Task SendBinaryImmediately(byte[] data, int length = Constants.UseArrayLengthForSend, CancellationTokenSource? _cancellationToken = null); /// /// This method sends a text message over the WebSocket connection immediately without queueing. /// /// /// The number of bytes from the data to send. Use `Constants.UseArrayLengthForSend` to send the entire array. - public Task SendMessageImmediately(byte[] data, int length = Constants.UseArrayLengthForSend); + /// /// Provide a cancel token to be used for the send function or use the internal one + public Task SendMessageImmediately(byte[] data, int length = Constants.UseArrayLengthForSend, CancellationTokenSource? _cancellationToken = null); #endregion #region Helpers diff --git a/Deepgram/Clients/Interfaces/v2/ILiveClient.cs b/Deepgram/Clients/Interfaces/v2/ILiveClient.cs deleted file mode 100644 index d9d574c..0000000 --- a/Deepgram/Clients/Interfaces/v2/ILiveClient.cs +++ /dev/null @@ -1,73 +0,0 @@ -// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved. -// Use of this source code is governed by a MIT license that can be found in the LICENSE file. -// SPDX-License-Identifier: MIT - -using Deepgram.Models.Live.v1; - -namespace Deepgram.Clients.Interfaces.v2; - -/// -// *********** WARNING *********** -// This is the ILiveClient interface -// -// Deprecated: This class is deprecated. Use the `IListenWebSocketClient` function instead. -// This will be removed in a future release. -// -// This class is frozen and no new functionality will be added. -// *********** WARNING *********** -/// -[Obsolete("Please use IListenWebSocketClient instead", false)] -public interface ILiveClient : IListenWebSocketClient -{ - #region Subscribe Event - /// - /// Subscribe to an Open event from the Deepgram API - /// - /// - /// True if successful - public Task Subscribe(EventHandler eventHandler); - - /// - /// Subscribe to a Metadata event from the Deepgram API - /// - /// - /// True if successful - public Task Subscribe(EventHandler eventHandler); - - /// - /// Subscribe to a Results event from the Deepgram API - /// - /// True if successful - public Task Subscribe(EventHandler eventHandler); - - /// - /// Subscribe to an UtteranceEnd event from the Deepgram API - /// - /// True if successful - public Task Subscribe(EventHandler eventHandler); - - /// - /// Subscribe to a SpeechStarted event from the Deepgram API - /// - /// True if successful - public Task Subscribe(EventHandler eventHandler); - - /// - /// Subscribe to a Close event from the Deepgram API - /// - /// True if successful - public Task Subscribe(EventHandler eventHandler); - - /// - /// Subscribe to an Unhandled event from the Deepgram API - /// - /// True if successful - public Task Subscribe(EventHandler eventHandler); - - /// - /// Subscribe to an Error event from the Deepgram API - /// - /// True if successful - public Task Subscribe(EventHandler eventHandler); - #endregion -} diff --git a/Deepgram/Clients/Interfaces/v2/IOnPremClient.cs b/Deepgram/Clients/Interfaces/v2/IOnPremClient.cs deleted file mode 100644 index a62f2f2..0000000 --- a/Deepgram/Clients/Interfaces/v2/IOnPremClient.cs +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. -// Use of this source code is governed by a MIT license that can be found in the LICENSE file. -// SPDX-License-Identifier: MIT - -namespace Deepgram.Clients.Interfaces.v2; - -/// -// *********** WARNING *********** -// This class provides the IOnPremClient implementation -// -// Deprecated: This class is deprecated. Use ISelfHostedClient instead. -// This will be removed in a future release. -// -// This package is frozen and no new functionality will be added. -// *********** WARNING *********** -/// -[Obsolete("Please use ISelfHostedClient instead", false)] -public interface IOnPremClient : ISelfHostedClient -{ -} diff --git a/Deepgram/Clients/Interfaces/v2/IPreRecordedClient.cs b/Deepgram/Clients/Interfaces/v2/IPreRecordedClient.cs deleted file mode 100644 index 7fcf874..0000000 --- a/Deepgram/Clients/Interfaces/v2/IPreRecordedClient.cs +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. -// Use of this source code is governed by a MIT license that can be found in the LICENSE file. -// SPDX-License-Identifier: MIT - -namespace Deepgram.Clients.Interfaces.v2; - -/// -// *********** WARNING *********** -// This is the IPreRecordedClient interface -// -// Deprecated: This class is deprecated. Use the `IListenRESTClient` function instead. -// This will be removed in a future release. -// -// This class is frozen and no new functionality will be added. -// *********** WARNING *********** -/// -[Obsolete("Please use IListenRESTClient instead", false)] -public interface IPreRecordedClient : IListenRESTClient -{ -} diff --git a/Deepgram/Clients/Interfaces/v2/ISpeakClient.cs b/Deepgram/Clients/Interfaces/v2/ISpeakClient.cs deleted file mode 100644 index d5ad080..0000000 --- a/Deepgram/Clients/Interfaces/v2/ISpeakClient.cs +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved. -// Use of this source code is governed by a MIT license that can be found in the LICENSE file. -// SPDX-License-Identifier: MIT - -namespace Deepgram.Clients.Interfaces.v2; - -/// -/// *********** WARNING *********** -/// This class provides the ISpeakClient implementation for the Deepgram API -/// -/// Deprecated: This class is deprecated. Use the ISpeakRESTClient interface instead. -/// This will be removed in a future release. -/// -/// This package is frozen and no new functionality will be added. -/// *********** WARNING *********** -/// -[Obsolete("Please use ISpeakRESTClient instead", false)] -public interface ISpeakClient : ISpeakRESTClient -{ -} diff --git a/Deepgram/Clients/Interfaces/v2/ISpeakWebSocketClient.cs b/Deepgram/Clients/Interfaces/v2/ISpeakWebSocketClient.cs index 48f0129..3e4ddd5 100644 --- a/Deepgram/Clients/Interfaces/v2/ISpeakWebSocketClient.cs +++ b/Deepgram/Clients/Interfaces/v2/ISpeakWebSocketClient.cs @@ -108,7 +108,7 @@ public Task Connect(SpeakSchema options, CancellationTokenSource? cancelTo /// /// Sends a Close message to Deepgram /// - public Task SendClose(bool nullByte = false); + public Task SendClose(bool nullByte = false, CancellationTokenSource? _cancellationToken = null); /// /// Sends a binary message over the WebSocket connection. @@ -128,6 +128,7 @@ public Task Connect(SpeakSchema options, CancellationTokenSource? cancelTo /// /// /// The number of bytes from the data to send. Use `Constants.UseArrayLengthForSend` to send the entire array. + /// Provide a cancel token to be used for the send function or use the internal one public void SendMessage(byte[] data, int length = Constants.UseArrayLengthForSend); ///// @@ -140,7 +141,8 @@ public Task Connect(SpeakSchema options, CancellationTokenSource? cancelTo /// /// /// The number of bytes from the data to send. Use `Constants.UseArrayLengthForSend` to send the entire array. - public Task SendMessageImmediately(byte[] data, int length = Constants.UseArrayLengthForSend); + /// /// Provide a cancel token to be used for the send function or use the internal one + public Task SendMessageImmediately(byte[] data, int length = Constants.UseArrayLengthForSend, CancellationTokenSource? _cancellationToken = null); #endregion #region Helpers diff --git a/Deepgram/Clients/Listen/v2/WebSocket/Client.cs b/Deepgram/Clients/Listen/v2/WebSocket/Client.cs index 096cea4..05b7494 100644 --- a/Deepgram/Clients/Listen/v2/WebSocket/Client.cs +++ b/Deepgram/Clients/Listen/v2/WebSocket/Client.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved. // Use of this source code is governed by a MIT license that can be found in the LICENSE file. // SPDX-License-Identifier: MIT @@ -294,7 +294,7 @@ public async Task SendFinalize() /// /// Sends a Close message to Deepgram /// - public override async Task SendClose(bool nullByte = false) + public override async Task SendClose(bool nullByte = false, CancellationTokenSource? _cancellationToken = null) { if (_clientWebSocket == null || !IsConnected()) { @@ -302,11 +302,14 @@ public override async Task SendClose(bool nullByte = false) return; } + // provide a cancellation token, or use the one in the class + var _cancelToken = _cancellationToken ?? _cancellationTokenSource; + Log.Debug("SendClose", "Sending Close Message Immediately..."); if (nullByte) { // send a close to Deepgram - await _mutexSend.WaitAsync(_cancellationTokenSource.Token); + await _mutexSend.WaitAsync(_cancelToken.Token); try { await _clientWebSocket.SendAsync(new ArraySegment(new byte[1] { 0 }), WebSocketMessageType.Binary, true, _cancellationTokenSource.Token) diff --git a/Deepgram/Clients/Listen/v2/WebSocket/ResponseEvent.cs b/Deepgram/Clients/Listen/v2/WebSocket/ResponseEvent.cs index 2cd1a11..2a920e5 100644 --- a/Deepgram/Clients/Listen/v2/WebSocket/ResponseEvent.cs +++ b/Deepgram/Clients/Listen/v2/WebSocket/ResponseEvent.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved. // Use of this source code is governed by a MIT license that can be found in the LICENSE file. // SPDX-License-Identifier: MIT diff --git a/Deepgram/Clients/Listen/v2/WebSocket/UriSegments.cs b/Deepgram/Clients/Listen/v2/WebSocket/UriSegments.cs index a51f2f8..d05e90b 100644 --- a/Deepgram/Clients/Listen/v2/WebSocket/UriSegments.cs +++ b/Deepgram/Clients/Listen/v2/WebSocket/UriSegments.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved. // Use of this source code is governed by a MIT license that can be found in the LICENSE file. // SPDX-License-Identifier: MIT diff --git a/Deepgram/Clients/Speak/v2/WebSocket/Client.cs b/Deepgram/Clients/Speak/v2/WebSocket/Client.cs index 0677917..9569bd6 100644 --- a/Deepgram/Clients/Speak/v2/WebSocket/Client.cs +++ b/Deepgram/Clients/Speak/v2/WebSocket/Client.cs @@ -327,7 +327,7 @@ public void Close(bool nullByte = false) /// This method sends a close message over the WebSocket connection. /// NOTE: This is fine to use the SendImmediately methods because you want to shutdown the websocket ASAP. /// - public override async Task SendClose(bool nullByte = false) + public override async Task SendClose(bool nullByte = false, CancellationTokenSource? _cancellationToken = null) { if (_clientWebSocket == null || !IsConnected()) { @@ -335,11 +335,14 @@ public override async Task SendClose(bool nullByte = false) return; } + // provide a cancellation token, or use the one in the class + var _cancelToken = _cancellationToken ?? _cancellationTokenSource; + Log.Debug("SendClose", "Sending Close Message Immediately..."); if (nullByte) { // send a close to Deepgram - await _mutexSend.WaitAsync(_cancellationTokenSource.Token); + await _mutexSend.WaitAsync(_cancelToken.Token); try { await _clientWebSocket.SendAsync(new ArraySegment(new byte[1] { 0 }), WebSocketMessageType.Binary, true, _cancellationTokenSource.Token) @@ -354,7 +357,7 @@ await _clientWebSocket.SendAsync(new ArraySegment(new byte[1] { 0 }), WebS ControlMessage controlMessage = new ControlMessage(Constants.Close); byte[] data = Encoding.UTF8.GetBytes(controlMessage.ToString()); - await SendMessageImmediately(data); + await SendMessageImmediately(data, Constants.UseArrayLengthForSend, _cancelToken); } /// diff --git a/Deepgram/Models/Common/v2/WebSocket/WebSocketType.cs b/Deepgram/Models/Common/v2/WebSocket/WebSocketType.cs index 1825b19..c1baf52 100644 --- a/Deepgram/Models/Common/v2/WebSocket/WebSocketType.cs +++ b/Deepgram/Models/Common/v2/WebSocket/WebSocketType.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved. // Use of this source code is governed by a MIT license that can be found in the LICENSE file. // SPDX-License-Identifier: MIT diff --git a/Deepgram/Models/Listen/v2/WebSocket/Alternative.cs b/Deepgram/Models/Listen/v2/WebSocket/Alternative.cs index 0a59fb6..39f9ae5 100644 --- a/Deepgram/Models/Listen/v2/WebSocket/Alternative.cs +++ b/Deepgram/Models/Listen/v2/WebSocket/Alternative.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved. // Use of this source code is governed by a MIT license that can be found in the LICENSE file. // SPDX-License-Identifier: MIT diff --git a/Deepgram/Models/Listen/v2/WebSocket/Average.cs b/Deepgram/Models/Listen/v2/WebSocket/Average.cs index 3809bb7..03b0e54 100644 --- a/Deepgram/Models/Listen/v2/WebSocket/Average.cs +++ b/Deepgram/Models/Listen/v2/WebSocket/Average.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved. // Use of this source code is governed by a MIT license that can be found in the LICENSE file. // SPDX-License-Identifier: MIT diff --git a/Deepgram/Models/Listen/v2/WebSocket/Channel.cs b/Deepgram/Models/Listen/v2/WebSocket/Channel.cs index 51fab15..5523d91 100644 --- a/Deepgram/Models/Listen/v2/WebSocket/Channel.cs +++ b/Deepgram/Models/Listen/v2/WebSocket/Channel.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved. // Use of this source code is governed by a MIT license that can be found in the LICENSE file. // SPDX-License-Identifier: MIT diff --git a/Deepgram/Models/Listen/v2/WebSocket/Hit.cs b/Deepgram/Models/Listen/v2/WebSocket/Hit.cs index 4f6dedc..a3a9410 100644 --- a/Deepgram/Models/Listen/v2/WebSocket/Hit.cs +++ b/Deepgram/Models/Listen/v2/WebSocket/Hit.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved. // Use of this source code is governed by a MIT license that can be found in the LICENSE file. // SPDX-License-Identifier: MIT diff --git a/Deepgram/Models/Listen/v2/WebSocket/ListenType.cs b/Deepgram/Models/Listen/v2/WebSocket/ListenType.cs index 5f2744d..8d3e0d3 100644 --- a/Deepgram/Models/Listen/v2/WebSocket/ListenType.cs +++ b/Deepgram/Models/Listen/v2/WebSocket/ListenType.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved. // Use of this source code is governed by a MIT license that can be found in the LICENSE file. // SPDX-License-Identifier: MIT diff --git a/Deepgram/Models/Listen/v2/WebSocket/LiveSchema.cs b/Deepgram/Models/Listen/v2/WebSocket/LiveSchema.cs index 67fe0e8..a36dcea 100644 --- a/Deepgram/Models/Listen/v2/WebSocket/LiveSchema.cs +++ b/Deepgram/Models/Listen/v2/WebSocket/LiveSchema.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved. // Use of this source code is governed by a MIT license that can be found in the LICENSE file. // SPDX-License-Identifier: MIT diff --git a/Deepgram/Models/Listen/v2/WebSocket/Metadata.cs b/Deepgram/Models/Listen/v2/WebSocket/Metadata.cs index 9ace596..8583a79 100644 --- a/Deepgram/Models/Listen/v2/WebSocket/Metadata.cs +++ b/Deepgram/Models/Listen/v2/WebSocket/Metadata.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved. // Use of this source code is governed by a MIT license that can be found in the LICENSE file. // SPDX-License-Identifier: MIT diff --git a/Deepgram/Models/Listen/v2/WebSocket/MetadataResponse.cs b/Deepgram/Models/Listen/v2/WebSocket/MetadataResponse.cs index 170aea3..b31a7a8 100644 --- a/Deepgram/Models/Listen/v2/WebSocket/MetadataResponse.cs +++ b/Deepgram/Models/Listen/v2/WebSocket/MetadataResponse.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved. // Use of this source code is governed by a MIT license that can be found in the LICENSE file. // SPDX-License-Identifier: MIT diff --git a/Deepgram/Models/Listen/v2/WebSocket/ModelInfo.cs b/Deepgram/Models/Listen/v2/WebSocket/ModelInfo.cs index 4bd44eb..8e05e62 100644 --- a/Deepgram/Models/Listen/v2/WebSocket/ModelInfo.cs +++ b/Deepgram/Models/Listen/v2/WebSocket/ModelInfo.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved. // Use of this source code is governed by a MIT license that can be found in the LICENSE file. // SPDX-License-Identifier: MIT diff --git a/Deepgram/Models/Listen/v2/WebSocket/ResultResponse.cs b/Deepgram/Models/Listen/v2/WebSocket/ResultResponse.cs index e55ed77..bc75f64 100644 --- a/Deepgram/Models/Listen/v2/WebSocket/ResultResponse.cs +++ b/Deepgram/Models/Listen/v2/WebSocket/ResultResponse.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved. // Use of this source code is governed by a MIT license that can be found in the LICENSE file. // SPDX-License-Identifier: MIT diff --git a/Deepgram/Models/Listen/v2/WebSocket/Search.cs b/Deepgram/Models/Listen/v2/WebSocket/Search.cs index d003e81..f7549c3 100644 --- a/Deepgram/Models/Listen/v2/WebSocket/Search.cs +++ b/Deepgram/Models/Listen/v2/WebSocket/Search.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved. // Use of this source code is governed by a MIT license that can be found in the LICENSE file. // SPDX-License-Identifier: MIT diff --git a/Deepgram/Models/Listen/v2/WebSocket/SpeechStartedResponse.cs b/Deepgram/Models/Listen/v2/WebSocket/SpeechStartedResponse.cs index 568410c..046faf4 100644 --- a/Deepgram/Models/Listen/v2/WebSocket/SpeechStartedResponse.cs +++ b/Deepgram/Models/Listen/v2/WebSocket/SpeechStartedResponse.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved. // Use of this source code is governed by a MIT license that can be found in the LICENSE file. // SPDX-License-Identifier: MIT diff --git a/Deepgram/Models/Listen/v2/WebSocket/UtteranceEndResponse.cs b/Deepgram/Models/Listen/v2/WebSocket/UtteranceEndResponse.cs index 947c5d6..ab5a3ac 100644 --- a/Deepgram/Models/Listen/v2/WebSocket/UtteranceEndResponse.cs +++ b/Deepgram/Models/Listen/v2/WebSocket/UtteranceEndResponse.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved. // Use of this source code is governed by a MIT license that can be found in the LICENSE file. // SPDX-License-Identifier: MIT diff --git a/Deepgram/Models/Listen/v2/WebSocket/Word.cs b/Deepgram/Models/Listen/v2/WebSocket/Word.cs index 55ebe3c..8ba0c3e 100644 --- a/Deepgram/Models/Listen/v2/WebSocket/Word.cs +++ b/Deepgram/Models/Listen/v2/WebSocket/Word.cs @@ -1,4 +1,4 @@ -// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. +// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved. // Use of this source code is governed by a MIT license that can be found in the LICENSE file. // SPDX-License-Identifier: MIT diff --git a/examples/speech-to-text/websocket/microphone/Program.cs b/examples/speech-to-text/websocket/microphone/Program.cs index f0a4c1c..8e4630f 100644 --- a/examples/speech-to-text/websocket/microphone/Program.cs +++ b/examples/speech-to-text/websocket/microphone/Program.cs @@ -17,9 +17,9 @@ static async Task Main(string[] args) { // Initialize Library with default logging // Normal logging is "Info" level - //Deepgram.Library.Initialize(); + Deepgram.Library.Initialize(); // OR very chatty logging - Deepgram.Library.Initialize(LogLevel.Verbose); // LogLevel.Default, LogLevel.Debug, LogLevel.Verbose + //Deepgram.Library.Initialize(LogLevel.Debug); // LogLevel.Default, LogLevel.Debug, LogLevel.Verbose // Initialize the microphone library Deepgram.Microphone.Library.Initialize(); diff --git a/examples/text-to-speech/websocket/simple/Program.cs b/examples/text-to-speech/websocket/simple/Program.cs index 712bca8..70161fc 100644 --- a/examples/text-to-speech/websocket/simple/Program.cs +++ b/examples/text-to-speech/websocket/simple/Program.cs @@ -17,9 +17,9 @@ static async Task Main(string[] args) { // Initialize Library with default logging // Normal logging is "Info" level - //Library.Initialize(); + Library.Initialize(); // OR very chatty logging - Library.Initialize(LogLevel.Verbose); // LogLevel.Default, LogLevel.Debug, LogLevel.Verbose + //Library.Initialize(LogLevel.Verbose); // LogLevel.Default, LogLevel.Debug, LogLevel.Verbose //// use the client factory with a API Key set with the "DEEPGRAM_API_KEY" environment variable //DeepgramWsClientOptions options = new DeepgramWsClientOptions();