Files
UA-.NETStandard/samples/Core/ConsoleDataChannelAudio/AudioStreamingSource.cs
T
MarcandCopilot cd1ba90295 Address the PR review: sample layout, audio sample, docs, logging and net8.0
Nine review comments; two were answered by the previous commit (the generic
extension seam and the file named after it are both gone). The rest:

Sample layout and a second sample

The data channel samples now live under samples/Core, and a new
ConsoleDataChannelAudio joins the throughput benchmark. It stands up a Server
and a Client in one process, synthesises a short melody as 16-bit PCM once at
startup and streams it on repeat over a data channel while the Client plays it
back. Nothing binary enters the repository.

The source writes in real time rather than as fast as the channel will take it,
because a media source is paced by its own clock and writing faster only adds
latency; the progress line reports frames, bytes and credit stalls so a
consumer that cannot keep up is visible rather than silently buffered. Measured
over 31 seconds: 1582 frames, 2725 KiB, zero credit stalls, which is exactly
real time for 20 ms frames.

Playback uses NAudio 2.3.0 (MIT), referenced only by that sample - nothing
under src/ gains a dependency. NAudio's output devices are Windows interfaces
and 2.x has no ALSA or CoreAudio backend, so on Linux and macOS the sample
writes the received stream to a WAV instead and says which it is doing on
startup. The Windows-only paths sit behind OperatingSystem.IsWindows() rather
than a CA1416 suppression.

Documentation

"Where the code lives" describes where the code is rather than where it moved
from, and the same edit removes "rather than in ...", "remains" and the rest of
the change narration. Two sections that were a development diary - the tables
of defects found while writing tests and while running the sample - are gone
entirely: they recorded history rather than the state of the implementation.

Logging

The response-dispatched callback logged through a message that reads "Failed to
send fault response to client", which is not what happened, and on the HTTPS
listener it invented a requestId of 0 to fit that signature. All three
listeners now have a named message that says what actually failed.

net8.0 for the QUIC binding

Rather than excluding net8.0, the binding targets it and enables preview
features, so a net8.0 consumer opts in the same way for System.Net.Quic. The
three file-wide NET9_0_OR_GREATER guards are gone; only
QuicServerConnectionOptions.HandshakeTimeout stays guarded because it is
genuinely .NET 9+, and the listener's own admission expiry still bounds a
stalled handshake there.

Removing the #if around [Experimental]

ExperimentalAttribute is a .NET 8 type, and the repo's rule is to polyfill
rather than fork with #if, so it joins the existing polyfills in
Opc.Ua.Types. Verified that this is a real opt-in and not just something that
compiles: with the suppression removed, a net48 caller of a data channel
Service fails the build with "error DataChannels", which is the same behaviour
as net10.0.

Validated with the matrix CI runs: all seven pinned UA.slnx builds (net10.0,
net9.0, net8.0, net48, net472, netstandard2.0, netstandard2.1), 316 data
channel tests on net10.0 and 242 on net48, Opc.Ua.Server.Tests 4072,
Opc.Ua.Client.Tests 2126, Opc.Ua.Redundancy.Client.Tests 123, and the reference
server Docker image end to end.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 5d026b47-265d-4c97-8715-26e207dfa03c
2026-08-12 14:51:18 +02:00

206 lines
7.5 KiB
C#

/* ========================================================================
* Copyright (c) 2005-2026 The OPC Foundation, Inc. All rights reserved.
*
* OPC Foundation MIT License 1.00
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* The complete license agreement can be found here:
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/
using System;
using System.Threading;
using System.Threading.Tasks;
using Opc.Ua;
using Opc.Ua.Bindings;
namespace Quickstarts.ConsoleDataChannelAudio
{
/// <summary>
/// A data channel source that plays a canned melody on repeat.
/// </summary>
/// <remarks>
/// The source writes in real time rather than as fast as the channel will
/// take it: a media source is paced by its own clock, and writing faster
/// would only fill the receiver's buffer and add latency. Credit still
/// bounds it if the consumer falls behind, and the channel reports the
/// stall through its diagnostics rather than growing without limit.
/// </remarks>
internal sealed class AudioStreamingSource : IDataChannelSource, IDisposable
{
/// <summary>
/// Creates the source.
/// </summary>
/// <param name="frameMilliseconds">How much audio one frame carries.</param>
public AudioStreamingSource(int frameMilliseconds)
{
m_pcm = MelodyGenerator.Render();
m_frameBytes = AudioFormat.BytesForDuration(frameMilliseconds);
m_frameMilliseconds = frameMilliseconds;
SourceNodeId = new NodeId("Speaker1", 1);
Capabilities = new DataChannelSourceCapabilities
{
Direction = DataChannelDirection.SourceToSink,
SupportedDeliveryModes = [DataChannelDeliveryMode.ReliableOrdered],
ContentType = AudioFormat.ContentType,
MaxFrameSize = (uint)m_frameBytes,
MaxChannels = 1,
Priority = 1
};
}
/// <summary>
/// The Node a Client opens the channel on.
/// </summary>
public NodeId SourceNodeId { get; }
/// <inheritdoc/>
public NodeId NodeId => SourceNodeId;
/// <inheritdoc/>
public DataChannelSourceCapabilities Capabilities { get; }
/// <inheritdoc/>
public int ActiveChannelCount => m_channel == null ? 0 : 1;
/// <summary>
/// The length of the rendered melody, for the console banner.
/// </summary>
public TimeSpan LoopDuration
=> TimeSpan.FromSeconds(
(double)m_pcm.Length / (AudioFormat.SampleRate * AudioFormat.BytesPerSample));
/// <inheritdoc/>
public void OnChannelOpened(DataChannel channel)
{
m_channel = channel;
m_stop = new CancellationTokenSource();
m_pump = Task.Run(() => PumpAsync(channel, m_stop.Token));
}
/// <inheritdoc/>
public void OnChannelClosed(DataChannel channel, StatusCode reason)
{
m_channel = null;
m_stop?.Cancel();
}
/// <summary>
/// Stops the pump and waits for it, so the sample shuts down cleanly.
/// </summary>
public async Task StopAsync()
{
m_stop?.Cancel();
if (m_pump != null)
{
await m_pump.ConfigureAwait(false);
}
}
/// <inheritdoc/>
public void Dispose()
{
m_stop?.Dispose();
m_stop = null;
}
private async Task PumpAsync(DataChannel channel, CancellationToken ct)
{
int offset = 0;
var next = DateTime.UtcNow;
try
{
while (!ct.IsCancellationRequested &&
channel.State is DataChannelState.Open or DataChannelState.Opening)
{
if (channel.State == DataChannelState.Opening)
{
await Task.Delay(5, ct).ConfigureAwait(false);
continue;
}
int count = Math.Min(m_frameBytes, m_pcm.Length - offset);
// MessageStart and MessageEnd on every frame: each one is a
// self contained unit of audio, so a sink can play what it
// has without waiting for a boundary.
channel.Write(
m_pcm.AsSpan(offset, count),
DataChannelFrameFlags.MessageStart | DataChannelFrameFlags.MessageEnd);
offset += count;
if (offset >= m_pcm.Length)
{
offset = 0;
}
// Paced against a fixed schedule rather than by sleeping for
// the frame duration, so the send rate does not drift by the
// time each iteration takes.
next = next.AddMilliseconds(m_frameMilliseconds);
TimeSpan delay = next - DateTime.UtcNow;
if (delay > TimeSpan.Zero)
{
await Task.Delay(delay, ct).ConfigureAwait(false);
}
else
{
next = DateTime.UtcNow;
}
}
}
catch (OperationCanceledException)
{
// Shutting down.
}
catch (ServiceResultException e)
{
// The channel closed underneath the pump, which is how a
// CloseDataChannel while writing surfaces. Anything else is
// worth seeing rather than losing in an unobserved task.
if (e.StatusCode != StatusCodes.BadDataChannelClosed)
{
Console.Error.WriteLine($"audio pump stopped: {e.Message}");
}
}
#pragma warning disable CA1031 // A sample must not die silently.
catch (Exception e)
#pragma warning restore CA1031
{
Console.Error.WriteLine($"audio pump failed: {e}");
}
}
private readonly byte[] m_pcm;
private readonly int m_frameBytes;
private readonly int m_frameMilliseconds;
private DataChannel? m_channel;
private CancellationTokenSource? m_stop;
private Task? m_pump;
}
}