microsoft-graph-comms-samples icon indicating copy to clipboard operation
microsoft-graph-comms-samples copied to clipboard

ICommunicationsClient.CreateMediaSession causing Access Violation when running as a Windows Service

Open Drummss opened this issue 2 years ago • 2 comments

Describe the issue Application crashes with access violation when calling CreateMediaSession on the CommunicationClient if being run as a windows service.

I get the following logs in event viewer: image image

This is the exception caught by attaching the Visual Studio debugger: image

Code Snippet Around the exception site:

IMediaSession mediaSession = Guid.TryParse(call.Id, out Guid callId)
    ? CreateLocalMediaSession(callId)
    : CreateLocalMediaSession();

try
{
    _logger.LogInformation("Attempting to answer call {CallId} with scenario {ScenarioId}", call.Id, call.ScenarioId);
    await call.AnswerAsync(mediaSession);
    _logger.LogInformation("Answered call {CallId} successfully", call.Id);
}
catch (Exception ex)
{
    _logger.LogError(ex, "Failed to answer call {CallId} with scenario {ScenarioId}", call.Id, call.ScenarioId);
}
private ILocalMediaSession CreateLocalMediaSession(Guid mediaSessionId = default)
{
    try
    {
        _logger.LogInformation("Creating media session {MediaSessionId}", mediaSessionId);

        return _communicationsClient.CreateMediaSession(
            new AudioSocketSettings
            {
                StreamDirections = StreamDirection.Recvonly,
                // Note! Currently, the only audio format supported when receiving unmixed audio is Pcm16K
                SupportedAudioFormat = AudioFormat.Pcm16K,
                ReceiveUnmixedMeetingAudio = false,
            },
            new VideoSocketSettings
            {
                StreamDirections = StreamDirection.Inactive
            },
            mediaSessionId: mediaSessionId );
    }
    catch (Exception ex)
    {
        _logger.LogError(ex, "Something went wrong when creating a local media session.");
        throw;
    }
}

Graph SDK (please complete the following information):

<PackageReference Include="Microsoft.Graph.Communications.Calls" Version="1.2.0.7270" />
<PackageReference Include="Microsoft.Graph.Communications.Calls.Media" Version="1.2.0.7270" />
<PackageReference Include="Microsoft.Graph.Communications.Client" Version="1.2.0.7270" />
<PackageReference Include="Microsoft.Skype.Bots.Media" Version="1.27.0.2-alpha" />

Call ID

Both of the following times are UTC +1

[16:06:54][Information] Creating call record for call 6d1f5e00-6c52-49f5-9dd5-fc6102c395cb
[16:11:38][Information] Creating call record for call 791f5f00-c6f4-49ef-8cb6-cf8176756ab7

Logs The ILoggers log out to a file and the last log in the file each time it crashes is something similar to this, implying that the application code doesn't execute past CreateMediaSession as the Attempting to answer call log does not appear.

[16:11:38][Information] Creating call record for call 791f5f00-c6f4-49ef-8cb6-cf8176756ab7

The application appears to be hard crashing and not triggering the try/catch so no exceptions are logged.

Additional context I created the service with the following command:

sc.exe create "Teams Recording Bot Service" binpath="C:\TeamsRecordingBot\TeamsRecordingBot.exe WindowsService"

I tried creating a new local user and added it to the administrator group, and had the windows service login as that. I also tested as the default system user when creating the service, and the network service user.

The bot works without issue when running as a console application, and also appears to work with the windows service services added and then run through Visual Studio. I switch between the two types using a launch argument - which I have confirmed to be working.

[ArgActionMethod, ArgDescription("Starts the application in CLI mode.")]
public static async ValueTask Console()
{
    CreateBootstrapLogger();

    WebApplicationBuilder builder = await CreateBuilderAsync();

    await BuildAndStartAsync(builder);
}

[ArgActionMethod, ArgDescription("Starts the application as a Windows Service.")]
public static async ValueTask WindowsService()
{
    CreateBootstrapLogger();

    Log.Information("Running as a Windows Service");

    WebApplicationBuilder builder = await CreateBuilderAsync();

    builder.Services.AddWindowsService(
        options => { options.ServiceName = "Teams Recording Bot"; });

    builder.Host.UseWindowsService();

    await BuildAndStartAsync(builder);
}

Drummss avatar Oct 05 '23 15:10 Drummss

After a some more investigation we managed to narrow down the cause of the error a bit. The windows service appears to run as intended if we remove the VideoSocketSettings argument when initializing the new media session.

This is a bit of a workaround, and fortunately, we don't need access to any video stream so this works fine as a solution. Perhaps it is still something that you would want to look into though.

private ILocalMediaSession CreateLocalMediaSession(Guid mediaSessionId = default)
{
    try
    {
        _logger.LogInformation("Creating media session {MediaSessionId}", mediaSessionId);

        return _communicationsClient.CreateMediaSession(
            new AudioSocketSettings
            {
                StreamDirections = StreamDirection.Recvonly,
                // Note! Currently, the only audio format supported when receiving unmixed audio is Pcm16K
                SupportedAudioFormat = AudioFormat.Pcm16K,
                ReceiveUnmixedMeetingAudio = false,
            },
-            new VideoSocketSettings
-            {
-                StreamDirections = StreamDirection.Inactive
-            },
            mediaSessionId: mediaSessionId );
    }
    catch (Exception ex)
    {
        _logger.LogError(ex, "Something went wrong when creating a local media session.");
        throw;
    }
}

Drummss avatar Oct 09 '23 11:10 Drummss

Hi @Drummss What version of Windows are you using? (Server 2019, Server Core 2019, ...?) If you don't need to use video, then you should be able to set the videoSocketSettings parameter to null. Thanks.

ssulzer avatar Nov 08 '23 05:11 ssulzer