다음을 통해 공유


빠른 시작: 서버 쪽 오디오 스트리밍

Important

이 문서에 설명된 기능은 현재 공개 미리 보기로 제공됩니다. 이 미리 보기 버전은 서비스 수준 계약 없이 제공되며, 프로덕션 워크로드에는 권장되지 않습니다. 특정 기능이 지원되지 않거나 기능이 제한될 수 있습니다. 자세한 내용은 Microsoft Azure Preview에 대한 추가 사용 약관을 참조하세요.

Azure Communication Services 오디오 스트리밍 API를 통해 오디오 스트림 사용을 시작합니다. 이 빠른 시작에서는 자동화된 통화 라우팅 솔루션을 빌드하기 위해 통화 자동화 API에 이미 익숙하다고 가정합니다.

이 빠른 시작에서 설명하는 기능은 현재 공개 미리 보기로 제공됩니다.

필수 조건

  • 활성 구독이 있는 Azure 계정은 자세한 내용은 무료로 계정 만들기를 참조 하세요.
  • Azure Communication Services 리소스. Azure Communication Services 리소스 만들기를 참조하세요.
  • 호출 자동화 SDK사용하여 만든 새 웹 서비스 애플리케이션입니다.
  • 운영 체제용 최신 .NET 라이브러리.
  • 미디어 스트림을 받을 수 있는 websocket 서버입니다.

websocket 서버 설정

Azure Communication Services를 사용하려면 서버 애플리케이션에서 WebSocket 서버를 설정하여 오디오를 실시간으로 스트리밍해야 합니다. WebSocket은 단일 TCP 연결을 통해 전체 이중 통신 채널을 제공하는 표준화된 프로토콜입니다. 필요에 따라 websocket 연결을 통해 오디오 스트림을 수신하는 애플리케이션을 만들 수 있는 Azure 서비스 Azure WebApps를 사용할 수 있습니다. 이 빠른 시작을 따릅니다.

통화 설정

통화 설정 및 스트리밍 세부 정보 제공

MediaStreamingOptions mediaStreamingOptions = new MediaStreamingOptions( 
    new Uri("<WEBSOCKET URL>"), 
    MediaStreamingContent.Audio, 
    MediaStreamingAudioChannel.Mixed, 
    MediaStreamingTransport.Websocket, 
    false); 

 var createCallOptions = new CreateCallOptions(callInvite, callbackUri) 
 { 
     CallIntelligenceOptions = new CallIntelligenceOptions() { CognitiveServicesEndpoint = new Uri(cognitiveServiceEndpoint) }, 
     MediaStreamingOptions = mediaStreamingOptions, 
 }; 

 CreateCallResult createCallResult = await callAutomationClient.CreateCallAsync(createCallOptions); 

오디오 스트리밍 시작

오디오 스트리밍을 시작하는 방법:

StartMediaStreamingOptions options = new StartMediaStreamingOptions() 
    { 
        OperationCallbackUri = new Uri(callbackUriHost), 
        OperationContext = "startMediaStreamingContext" 
    };
    await callMedia.StartMediaStreamingAsync(options); 

Azure Communication Services가 WebSocket 서버의 URL을 받으면 연결이 만들어집니다. Azure Communication Services가 WebSocket 서버에 성공적으로 연결되고 스트리밍이 시작되면 들어오는 미디어 패킷에 대한 메타데이터를 포함하는 첫 번째 데이터 패킷을 통해 전송됩니다.

메타데이터 패킷은 다음과 같습니다.

{ 
    "kind": <string> // What kind of data this is, e.g. AudioMetadata, AudioData. 
    "audioMetadata": { 
        "subscriptionId": <string>, // unique identifier for a subscription request 
        "encoding":<string>, // PCM only supported 
        "sampleRate": <int>, // 16000 default 
        "channels": <int>, // 1 default 
        "length": <int> // 640 default 
    } 
} 

오디오 스트리밍 중지

오디오 스트리밍을 중지하는 방법

StopMediaStreamingOptions stopOptions = new StopMediaStreamingOptions() 
    { 
        OperationCallbackUri = new Uri(callbackUriHost) 
    }; 
    await callMedia.StopMediaStreamingAsync(stopOptions); 

websocket 서버에서 오디오 스트림 처리

아래 샘플에서는 websocket 서버를 사용하여 오디오 스트림을 수신 대기하는 방법을 보여 줍니다.

HttpListener httpListener = new HttpListener(); 
httpListener.Prefixes.Add("http://localhost:80/"); 
httpListener.Start(); 

while (true) 
{ 
    HttpListenerContext httpListenerContext = await httpListener.GetContextAsync(); 
    if (httpListenerContext.Request.IsWebSocketRequest) 
    { 
        WebSocketContext websocketContext; 
        try 
        { 
            websocketContext = await httpListenerContext.AcceptWebSocketAsync(subProtocol: null); 
        } 
        catch (Exception ex) 
        { 
            return; 
        } 
        WebSocket webSocket = websocketContext.WebSocket; 
        try 
        { 
            while (webSocket.State == WebSocketState.Open || webSocket.State == WebSocketState.CloseSent) 
            { 
                byte[] receiveBuffer = new byte[2048]; 
                var cancellationToken = new CancellationTokenSource(TimeSpan.FromSeconds(60)).Token; 
                WebSocketReceiveResult receiveResult = await webSocket.ReceiveAsync(new ArraySegment<byte>(receiveBuffer), cancellationToken); 
                if (receiveResult.MessageType != WebSocketMessageType.Close) 
                { 
                    var data = Encoding.UTF8.GetString(receiveBuffer).TrimEnd('\0'); 
                    try 
                    { 
                        var eventData = JsonConvert.DeserializeObject<AudioBaseClass>(data); 
                        if (eventData != null) 
                        { 
                            if(eventData.kind == "AudioMetadata") 
                            { 
                                //Process audio metadata 
                            } 
                            else if(eventData.kind == "AudioData")  
                            { 
                                //Process audio data 
                                var byteArray = eventData.audioData.data; 
                               //use audio byteArray as you want 
                            } 
                        } 
                    } 
                    catch { } 
                } 
            } 
        } 
        catch (Exception ex) { } 
    } 
} 

필수 조건

websocket 서버 설정

Azure Communication Services를 사용하려면 서버 애플리케이션에서 WebSocket 서버를 설정하여 오디오를 실시간으로 스트리밍해야 합니다. WebSocket은 단일 TCP 연결을 통해 전체 이중 통신 채널을 제공하는 표준화된 프로토콜입니다. 필요에 따라 websocket 연결을 통해 오디오 스트림을 수신하는 애플리케이션을 만들 수 있는 Azure 서비스 Azure WebApps를 사용할 수 있습니다. 이 빠른 시작을 따릅니다.

통화 설정

통화 설정 및 스트리밍 세부 정보 제공

CallInvite callInvite = new CallInvite(target, caller);  
              
            CallIntelligenceOptions callIntelligenceOptions = new CallIntelligenceOptions().setCognitiveServicesEndpoint(appConfig.getCognitiveServiceEndpoint());  
            MediaStreamingOptions mediaStreamingOptions = new MediaStreamingOptions(appConfig.getWebSocketUrl(), MediaStreamingTransport.WEBSOCKET, MediaStreamingContentType.AUDIO, MediaStreamingAudioChannel.UNMIXED);  
            mediaStreamingOptions.setStartMediaStreaming(false);  
          
            CreateCallOptions createCallOptions = new CreateCallOptions(callInvite, appConfig.getCallBackUri());  
            createCallOptions.setCallIntelligenceOptions(callIntelligenceOptions);  
            createCallOptions.setMediaStreamingOptions(mediaStreamingOptions);  
  
            Response<CreateCallResult> result = client.createCallWithResponse(createCallOptions, Context.NONE);  
            return result.getValue().getCallConnectionProperties().getCallConnectionId();  

오디오 스트리밍 시작

오디오 스트리밍을 시작하는 방법:

StartMediaStreamingOptions startOptions = new StartMediaStreamingOptions()  
                                                        .setOperationContext("startMediaStreamingContext")  
                                                        .setOperationCallbackUrl(appConfig.getBasecallbackuri());  
         client.getCallConnection(callConnectionId)  
                     .getCallMedia()  
                     .startMediaStreamingWithResponse(startOptions, Context.NONE);      

Azure Communication Services가 WebSocket 서버의 URL을 받으면 연결이 만들어집니다. Azure Communication Services가 WebSocket 서버에 성공적으로 연결되고 스트리밍이 시작되면 들어오는 미디어 패킷에 대한 메타데이터를 포함하는 첫 번째 데이터 패킷을 통해 전송됩니다.

메타데이터 패킷은 다음과 같습니다.

{ 
    "kind": <string> // What kind of data this is, e.g. AudioMetadata, AudioData. 
    "audioMetadata": { 
        "subscriptionId": <string>, // unique identifier for a subscription request 
        "encoding":<string>, // PCM only supported 
        "sampleRate": <int>, // 16000 default 
        "channels": <int>, // 1 default 
        "length": <int> // 640 default 
    } 
} 

오디오 스트리밍 중지

오디오 스트리밍을 중지하는 방법

StopMediaStreamingOptions stopOptions = new StopMediaStreamingOptions()  
                                                        .setOperationCallbackUrl(appConfig.getBasecallbackuri());  
         client.getCallConnection(callConnectionId)  
                     .getCallMedia()  
                     .stopMediaStreamingWithResponse(stopOptions, Context.NONE);

websocket 서버에서 미디어 스트림 처리

아래 샘플에서는 websocket 서버를 사용하여 미디어 스트림을 수신 대기하는 방법을 보여 줍니다. App.java 및 WebSocketServer.java 두 개의 파일을 실행해야 합니다.

package com.example;

import org.glassfish.tyrus.server.Server;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class App {
    public static void main(String[] args) {

        Server server = new Server("localhost", 8081, "/ws", null, WebSocketServer.class);

        try {
            server.start();
            System.out.println("Web socket running on port 8081...");
            System.out.println("wss://localhost:8081/ws/server");
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            reader.readLine();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            server.stop();
        }
    }
}
package com.example;

import javax.websocket.OnMessage;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

import com.azure.communication.callautomation.models.streaming.StreamingData;
import com.azure.communication.callautomation.models.streaming.StreamingDataParser;
import com.azure.communication.callautomation.models.streaming.media.AudioData;
import com.azure.communication.callautomation.models.streaming.media.AudioMetadata;

@ServerEndpoint("/server")
public class WebSocketServer {
    @OnMessage
    public void onMessage(String message, Session session) {

        // System.out.println("Received message: " + message);

        StreamingData data = StreamingDataParser.parse(message);

        if (data instanceof AudioMetadata) {
            AudioMetadata audioMetaData = (AudioMetadata) data;
            System.out.println("----------------------------------------------------------------");
            System.out.println("SUBSCRIPTION ID:-->" + audioMetaData.getMediaSubscriptionId());
            System.out.println("ENCODING:-->" + audioMetaData.getEncoding());
            System.out.println("SAMPLE RATE:-->" + audioMetaData.getSampleRate());
            System.out.println("CHANNELS:-->" + audioMetaData.getChannels());
            System.out.println("LENGTH:-->" + audioMetaData.getLength());
            System.out.println("----------------------------------------------------------------");
        }
        if (data instanceof AudioData) {
            System.out.println("----------------------------------------------------------------");
            AudioData audioData = (AudioData) data;
            System.out.println("DATA:-->" + audioData.getData());
            System.out.println("TIMESTAMP:-->" + audioData.getTimestamp());
            // System.out.println("PARTICIPANT:-->" + audioData.getParticipant().getRawId()
            // != null
            // ? audioData.getParticipant().getRawId()
            // : "");
            System.out.println("IS SILENT:-->" + audioData.isSilent());
            System.out.println("----------------------------------------------------------------");
        }
    }
}

필수 조건

  • 활성 구독이 있는 Azure 계정입니다. 자세한 내용은 무료 계정 만들기를 참조하세요.
  • Azure Communication Services 리소스. Azure Communication Services 리소스 만들기를 참조하세요.
  • 호출 자동화 SDK사용하여 만든 새 웹 서비스 애플리케이션입니다.
  • Node.js LTS 설치
  • 미디어 스트림을 받을 수 있는 websocket 서버입니다.

websocket 서버 설정

Azure Communication Services를 사용하려면 서버 애플리케이션에서 WebSocket 서버를 설정하여 오디오를 실시간으로 스트리밍해야 합니다. WebSocket은 단일 TCP 연결을 통해 전체 이중 통신 채널을 제공하는 표준화된 프로토콜입니다. 필요에 따라 websocket 연결을 통해 오디오 스트림을 수신하는 애플리케이션을 만들 수 있는 Azure 서비스 Azure WebApps를 사용할 수 있습니다. 이 빠른 시작을 따릅니다.

통화 설정

통화 설정 및 스트리밍 세부 정보 제공

const mediaStreamingOptions: MediaStreamingOptions = { 
          transportUrl: "<WEBSOCKET URL>", 
          transportType: "websocket", 
          contentType: "audio", 
          audioChannelType: "unmixed", 
          startMediaStreaming: false 
} 
const options: CreateCallOptions = { 
          callIntelligenceOptions: { cognitiveServicesEndpoint: process.env.COGNITIVE_SERVICES_ENDPOINT }, 
          mediaStreamingOptions: mediaStreamingOptions 
}; 

오디오 스트리밍 시작

오디오 스트리밍을 시작하는 방법:

const streamingOptions: StartMediaStreamingOptions = { 
        operationContext: "startMediaStreamingContext", 
        operationCallbackUrl: process.env.CALLBACK_URI + "/api/callbacks" 
    } 
await callMedia.startMediaStreaming(streamingOptions); 

Azure Communication Services가 WebSocket 서버의 URL을 받으면 연결이 만들어집니다. Azure Communication Services가 WebSocket 서버에 성공적으로 연결되고 스트리밍이 시작되면 들어오는 미디어 패킷에 대한 메타데이터를 포함하는 첫 번째 데이터 패킷을 통해 전송됩니다.

메타데이터 패킷은 다음과 같습니다.

{ 
    "kind": <string> // What kind of data this is, e.g. AudioMetadata, AudioData. 
    "audioMetadata": { 
        "subscriptionId": <string>, // unique identifier for a subscription request 
        "encoding":<string>, // PCM only supported 
        "sampleRate": <int>, // 16000 default 
        "channels": <int>, // 1 default 
        "length": <int> // 640 default 
    } 
} 

오디오 스트리밍 중지

오디오 스트리밍을 중지하는 방법

const stopMediaStreamingOptions: StopMediaStreamingOptions = { 
        operationCallbackUrl: process.env.CALLBACK_URI + "/api/callbacks" 
        } 
await callMedia.stopMediaStreaming(stopMediaStreamingOptions); 

websocket 서버에서 오디오 스트림 처리

아래 샘플에서는 websocket 서버를 사용하여 오디오 스트림을 수신 대기하는 방법을 보여 줍니다.

import WebSocket from 'ws'; 
import { streamingData } from '@azure/communication-call-automation/src/utli/streamingDataParser' 
const wss = new WebSocket.Server({ port: 8081 }); 

wss.on('connection', (ws: WebSocket) => { 
    console.log('Client connected'); 
    ws.on('message', (packetData: ArrayBuffer) => { 
        const decoder = new TextDecoder(); 
        const stringJson = decoder.decode(packetData); 
        console.log("STRING JSON=>--" + stringJson) 

        //var response = streamingData(stringJson); 

        var response = streamingData(packetData); 
        if ('locale' in response) { 
            console.log("Transcription Metadata") 
            console.log(response.callConnectionId); 
            console.log(response.correlationId); 
            console.log(response.locale); 
            console.log(response.subscriptionId); 
        } 
        if ('text' in response) { 
            console.log("Transcription Data") 
            console.log(response.text); 
            console.log(response.format); 
            console.log(response.confidence); 
            console.log(response.offset); 
            console.log(response.duration); 
            console.log(response.resultStatus); 
            if ('phoneNumber' in response.participant) { 
                console.log(response.participant.phoneNumber); 
            } 
            response.words.forEach(element => { 
                console.log(element.text) 
                console.log(element.duration) 
                console.log(element.offset) 
            }); 
        } 
    }); 

    ws.on('close', () => { 
        console.log('Client disconnected'); 
    }); 
}); 

// function processData(data: ArrayBuffer) { 
//  const byteArray = new Uint8Array(data); 
// } 

console.log('WebSocket server running on port 8081'); 

필수 조건

websocket 서버 설정

Azure Communication Services를 사용하려면 서버 애플리케이션에서 WebSocket 서버를 설정하여 오디오를 실시간으로 스트리밍해야 합니다. WebSocket은 단일 TCP 연결을 통해 전체 이중 통신 채널을 제공하는 표준화된 프로토콜입니다. 필요에 따라 websocket 연결을 통해 오디오 스트림을 수신하는 애플리케이션을 만들 수 있는 Azure 서비스 Azure WebApps를 사용할 수 있습니다. 이 빠른 시작을 따릅니다.

통화 설정

통화 설정 및 스트리밍 세부 정보 제공

media_streaming_options = MediaStreamingOptions( 
         transport_url="wss://e063-2409-40c2-4004-eced-9487-4dfb-b0e4-10fb.ngrok-free.app", 
         transport_type=MediaStreamingTransportType.WEBSOCKET, 
         content_type=MediaStreamingContentType.AUDIO, 
         audio_channel_type=MediaStreamingAudioChannelType.UNMIXED, 
         start_media_streaming=False 
         ) 

call_connection_properties = call_automation_client.create_call(target_participant,  
                                                                    CALLBACK_EVENTS_URI, 
                                                                    cognitive_services_endpoint=COGNITIVE_SERVICES_ENDPOINT, 
                                                                    source_caller_id_number=source_caller, 
                                                                    media_streaming=media_streaming_options
) 

오디오 스트리밍 시작

오디오 스트리밍을 시작하는 방법:

call_connection_client.start_media_streaming() 

Azure Communication Services가 WebSocket 서버의 URL을 받으면 연결이 만들어집니다. Azure Communication Services가 WebSocket 서버에 성공적으로 연결되고 스트리밍이 시작되면 들어오는 미디어 패킷에 대한 메타데이터를 포함하는 첫 번째 데이터 패킷을 통해 전송됩니다.

메타데이터 패킷은 다음과 같습니다.

{ 
    "kind": <string> // What kind of data this is, e.g. AudioMetadata, AudioData. 
    "audioMetadata": { 
        "subscriptionId": <string>, // unique identifier for a subscription request 
        "encoding":<string>, // PCM only supported 
        "sampleRate": <int>, // 16000 default 
        "channels": <int>, // 1 default 
        "length": <int> // 640 default 
    } 
} 

오디오 스트리밍 중지

오디오 스트리밍을 중지하는 방법

call_connection_client.stop_media_streaming() 

websocket 서버에서 오디오 스트림 처리

아래 샘플에서는 websocket 서버를 사용하여 오디오 스트림을 수신 대기하는 방법을 보여 줍니다.

import asyncio 
import json 
import websockets 

async def handle_client(websocket, path): 
    print("Client connected") 
    try: 
        async for message in websocket: 
            print(message) 
            packet_data = json.loads(message) 
            packet_data = message.encode('utf-8') 
            print("Packet DATA:-->",packet_data) 

    except websockets.exceptions.ConnectionClosedOK: 
        print("Client disconnected") 

start_server = websockets.serve(handle_client, "localhost", 8081) 

print('WebSocket server running on port 8081') 

asyncio.get_event_loop().run_until_complete(start_server) 
asyncio.get_event_loop().run_forever() 

오디오 스트리밍 스키마

메타데이터 패킷을 통해 보낸 후 Azure Communication Services는 오디오 미디어를 WebSocket 서버로 스트리밍하기 시작합니다. 다음은 서버에서 수신할 미디어 개체의 예입니다.

{
    "kind": <string>, // What kind of data this is, e.g. AudioMetadata, AudioData.
    "audioData":{
        "data": <string>, // Base64 Encoded audio buffer data
        "timestamp": <string>, // In ISO 8601 format (yyyy-mm-ddThh:mm:ssZ) 
        "participantRawID": <string>, 
        "silent": <boolean> // Indicates if the received audio buffer contains only silence.
    }
}

리소스 정리

Communication Services 구독을 정리하고 제거하려면 리소스 또는 리소스 그룹을 삭제하면 됩니다. 리소스 그룹을 삭제하면 해당 리소스 그룹에 연결된 다른 모든 리소스가 함께 삭제됩니다. 리소스 정리에 대해 자세히 알아보세요.

다음 단계