你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

在通话开始时进行录制

通话录制通常通过通话应用程序的 UI 直接使用,由用户触发录制。 对于银行或医疗保健等行业的应用程序,需要从一开始就进行通话录制。 出于合规性目的,该服务需要自动录制。 此示例演示如何在通话开始时进行录制。 它使用 Azure 通信服务和 Azure 事件网格,在通话开始时触发一个 Azure 函数。 它会自动录制你在 Azure 通信服务资源中的每一次通话。

在本快速入门中,我们重点介绍如何使用事件网格触发器通过 Azure Functions 来处理通话开始事件。 我们使用 Azure 通信服务的通话自动化 SDK 来开始录制。

通话开始时的通话开始事件将按照以下格式进行呈现:


[
  {
    "id": "a8bcd8a3-12d7-46ba-8cde-f6d0bda8feeb",
    "topic": "/subscriptions/{subscription-id}/resourcegroups/{group-name}/providers/microsoft.communication/communicationservices/{communication-services-resource-name}",
    "subject": "call/{serverCallId}/startedBy/8:acs:bc360ba8-d29b-4ef2-b698-769ebef85521_0000000c-1fb9-4878-07fd-0848220077e1",
    "data": {
      "startedBy": {
        "communicationIdentifier": {
          "rawId": "8:acs:bc360ba8-d29b-4ef2-b698-769ebef85521_0000000c-1fb9-4878-07fd-0848220077e1",
          "communicationUser": {
            "id": "8:acs:bc360ba8-d29b-4ef2-b698-769ebef85521_0000000c-1fb9-4878-07fd-0848220077e1"
          }
        },
        "role": "{role}"
      },
      "serverCallId": "{serverCallId}",
      "group": {
        "id": "00000000-0000-0000-0000-000000000000"
      },
      "room": {
        "id": "{roomId}"
      },
      "isTwoParty": false,
      "correlationId": "{correlationId}",
      "isRoomsCall": true
    },
    "eventType": "Microsoft.Communication.CallStarted",
    "dataVersion": "1.0",
    "metadataVersion": "1",
    "eventTime": "2021-09-22T17:02:38.6905856Z"
  }
]

注释

使用 Azure 事件网格会产生额外的费用。 有关详细信息,请参阅 Azure 事件网格定价

先决条件

设置本地环境

  1. 使用 Visual Studio Code 安装 Azure Functions 扩展

  2. 使用扩展,按照以下 说明创建 Azure 函数。

    使用以下说明配置函数:

    • 语言:C#
    • 模板:Azure 事件网格触发器
    • 函数名称:用户定义

    创建后,会看到在目录中创建的函数,如下所示:

    
    using System;
    using Microsoft.Azure.WebJobs;
    using Microsoft.Azure.WebJobs.Extensions.EventGrid;
    using Microsoft.Extensions.Logging;
    using Azure.Messaging.EventGrid;
    using System.Threading.Tasks;
    
    namespace Company.Function
    {
        public static class acs_recording_test
        {
            [FunctionName("acs_recording_test")]
            public static async Task RunAsync([EventGridTrigger]EventGridEvent eventGridEvent, ILogger log)
            {
                log.LogInformation(eventGridEvent.EventType);
            }
    
        }
    }
    
    

配置 Azure 函数以接收 CallStarted 事件

  1. 配置 Azure 函数,以便在 CallStarted 事件触发时执行相应操作。

    public static async Task RunAsync([EventGridTrigger]EventGridEvent eventGridEvent, ILogger log)
    {
        if(eventGridEvent.EventType == "Microsoft.Communication.CallStarted")
        {
            log.LogInformation("Call started");
            var callEvent = eventGridEvent.Data.ToObjectFromJson<CallStartedEvent>();
            
            // CallStartedEvent class is defined in documentation, but the objects looks like this:
            // public class CallStartedEvent
            // {
            //     public StartedBy startedBy { get; set; }
            //     public string serverCallId { get; set; }
            //     public Group group { get; set; }
            //     public bool isTwoParty { get; set; }
            //     public string correlationId { get; set; }
            //     public bool isRoomsCall { get; set; }
            // }
            // public class Group
            // {
            //     public string id { get; set; }
            // }
            // public class StartedBy
            // {
            //     public CommunicationIdentifier communicationIdentifier { get; set; }
            //     public string role { get; set; }
            // }
            // public class CommunicationIdentifier
            // {
            //     public string rawId { get; set; }
            //     public CommunicationUser communicationUser { get; set; }
            // }
            // public class CommunicationUser
            // {
            //     public string id { get; set; }
            // }
        }
    }

开始录制

  1. 创建一个用于处理 CallStarted 事件的方法。 此方法可在通话开始时启动录制。

    public static async Task RunAsync([EventGridTrigger]EventGridEvent eventGridEvent, ILogger log)
    {
        if(eventGridEvent.EventType == "Microsoft.Communication.CallStarted")
        {
            log.LogInformation("Call started");
            var callEvent = eventGridEvent.Data.ToObjectFromJson<CallStartedEvent>();
            await startRecordingAsync(callEvent.serverCallId);
        }
    }

    public static async Task startRecordingAsync (String serverCallId)
    {
        CallAutomationClient callAutomationClient = new CallAutomationClient(Environment.GetEnvironmentVariable("ACS_CONNECTION_STRING"));
        StartRecordingOptions recordingOptions = new StartRecordingOptions(new ServerCallLocator(serverCallId));
        recordingOptions.RecordingChannel = RecordingChannel.Mixed;
        recordingOptions.RecordingContent = RecordingContent.AudioVideo;
        recordingOptions.RecordingFormat = RecordingFormat.Mp4;        
        var startRecordingResponse = await callAutomationClient.GetCallRecording()
            .StartRecordingAsync(recordingOptions).ConfigureAwait(false);
    }

在本地运行

若要在本地运行函数,可以在 Visual Studio Code 中按 F5。 我们使用 ngrok 将本地运行的 Azure 函数与 Azure 事件网格挂钩。

  1. 函数运行后,我们将配置 ngrok。 (需要下载适用于环境的 ngrok。)

     ngrok http 7071
    

    复制在函数运行的位置提供的 ngrok 链接。

  2. 在 Azure 通信服务资源中通过事件网格配置 CallStarted 事件。 我们使用 Azure CLI 执行此作。 您需要在 Azure 门户中找到 Azure 通信服务资源的标识符。 (资源 ID 如下所示:/subscriptions/<<AZURE SUBSCRIPTION ID>>/resourceGroups/<<RESOURCE GROUP NAME>>/providers/Microsoft.Communication/CommunicationServices/<<RESOURCE NAME>>

    
    az eventgrid event-subscription create --name "<<EVENT_SUBSCRIPTION_NAME>>" --endpoint-type webhook --endpoint "<<NGROK URL>>/runtime/webhooks/EventGrid?functionName=<<FUNCTION NAME>> " --source-resource-id "<<RESOURCE_ID>>"  --included-event-types Microsoft.Communication.CallStarted
    
    
  3. 现在所有配置都已完成,可通过在资源中发起通话来测试该流程。 应在运行函数的终端上看到控制台日志。 可以通过使用通话 SDK 中的通话录制功能来检查录制是否已开始,并检查布尔值是否变为 TRUE。

部署到 Azure 云

若要将 Azure 函数部署到 Azure,需要按照以下 说明进行作。 部署后,我们将为 Azure 通信服务资源配置事件网格。 使用部署的 Azure 函数的 URL(在 Azure 门户中的该函数下找到的 URL),我们运行类似的命令:


az eventgrid event-subscription update --name "<<EVENT_SUBSCRIPTION_NAME>>" --endpoint-type azurefunction --endpoint "<<AZ FUNCTION URL>> " --source-resource-id "<<RESOURCE_ID>>"

由于我们正在更新已创建的事件订阅,因此请确保使用上一步骤中所用的同一事件订阅名称。

可以通过在资源中发起通话来进行测试,类似于上一步骤。