UNPKG

microsoft-cognitiveservices-speech-sdk

Version:
1 lines 6.92 kB
{"version":3,"sources":["src/common.speech/Transcription/ConversationTranslatorConnectionFactory.ts"],"names":[],"mappings":"AAQA,OAAO,EACH,WAAW,EAEd,MAAM,yBAAyB,CAAC;AAMjC,OAAO,EACH,gBAAgB,EACnB,MAAM,yCAAyC,CAAC;AAGjD,OAAO,EACH,qBAAqB,EACxB,MAAM,+BAA+B,CAAC;AACvC,OAAO,EACH,QAAQ,EACR,gBAAgB,EAInB,MAAM,iBAAiB,CAAC;AAEzB;;;GAGG;AACH,qBAAa,uCAAwC,SAAQ,qBAAqB;IAE9E,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,iBAAiB,CAA8C;IAEvF,OAAO,CAAC,cAAc,CAAyB;gBAE5B,UAAU,EAAE,MAAM,gBAAgB;IAO9C,MAAM,CAAC,MAAM,EAAE,gBAAgB,EAAE,QAAQ,EAAE,QAAQ,EAAE,YAAY,CAAC,EAAE,MAAM,GAAG,WAAW;CAgFlG","file":"ConversationTranslatorConnectionFactory.d.ts","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved.\r\n// Licensed under the MIT license.\r\n\r\nimport {\r\n ProxyInfo,\r\n RestConfigBase,\r\n WebsocketConnection,\r\n} from \"../../common.browser/Exports.js\";\r\nimport {\r\n IConnection,\r\n IStringDictionary,\r\n} from \"../../common/Exports.js\";\r\nimport { StringUtils } from \"../../common/StringUtils.js\";\r\nimport { Contracts } from \"../../sdk/Contracts.js\";\r\nimport {\r\n PropertyId\r\n} from \"../../sdk/Exports.js\";\r\nimport {\r\n ConversationImpl\r\n} from \"../../sdk/Transcription/Conversation.js\";\r\nimport { HeaderNames } from \"../HeaderNames.js\";\r\nimport { QueryParameterNames } from \"../QueryParameterNames.js\";\r\nimport {\r\n ConnectionFactoryBase\r\n} from \"./../ConnectionFactoryBase.js\";\r\nimport {\r\n AuthInfo,\r\n RecognizerConfig,\r\n TranscriberConnectionFactory,\r\n TranslationConnectionFactory,\r\n WebsocketMessageFormatter,\r\n} from \"./../Exports.js\";\r\n\r\n/**\r\n * Connection factory for the conversation translator. Handles connecting to the regular translator endpoint,\r\n * as well as the virtual microphone array transcription endpoint\r\n */\r\nexport class ConversationTranslatorConnectionFactory extends ConnectionFactoryBase {\r\n\r\n private static readonly CTS_VIRT_MIC_PATH: string = \"/speech/recognition/dynamicaudio\";\r\n\r\n private privConvGetter: () => ConversationImpl;\r\n\r\n public constructor(convGetter: () => ConversationImpl) {\r\n super();\r\n\r\n Contracts.throwIfNullOrUndefined(convGetter, \"convGetter\");\r\n this.privConvGetter = convGetter;\r\n }\r\n\r\n public create(config: RecognizerConfig, authInfo: AuthInfo, connectionId?: string): IConnection {\r\n const isVirtMicArrayEndpoint = config.parameters.getProperty(\"ConversationTranslator_MultiChannelAudio\", \"\").toUpperCase() === \"TRUE\";\r\n\r\n const convInfo = this.privConvGetter().room;\r\n const region = convInfo.cognitiveSpeechRegion || config.parameters.getProperty(PropertyId.SpeechServiceConnection_Region, \"\");\r\n\r\n const replacementValues: IStringDictionary<string> = {\r\n hostSuffix: ConnectionFactoryBase.getHostSuffix(region),\r\n path: ConversationTranslatorConnectionFactory.CTS_VIRT_MIC_PATH,\r\n region: encodeURIComponent(region)\r\n };\r\n replacementValues[QueryParameterNames.Language] = encodeURIComponent(config.parameters.getProperty(PropertyId.SpeechServiceConnection_RecoLanguage, \"\"));\r\n replacementValues[QueryParameterNames.CtsMeetingId] = encodeURIComponent(convInfo.roomId);\r\n replacementValues[QueryParameterNames.CtsDeviceId] = encodeURIComponent(convInfo.participantId);\r\n replacementValues[QueryParameterNames.CtsIsParticipant] = convInfo.isHost ? \"\" : (\"&\" + QueryParameterNames.CtsIsParticipant);\r\n\r\n let endpointUrl: string = \"\";\r\n const queryParams: IStringDictionary<string> = {};\r\n const headers: IStringDictionary<string> = {};\r\n\r\n if (isVirtMicArrayEndpoint) {\r\n // connecting to the conversation transcription virtual microphone array endpoint\r\n endpointUrl = config.parameters.getProperty(PropertyId.SpeechServiceConnection_Endpoint);\r\n if (!endpointUrl) {\r\n const hostName = config.parameters.getProperty(\r\n PropertyId.SpeechServiceConnection_Host,\r\n \"transcribe.{region}.cts.speech{hostSuffix}\");\r\n\r\n endpointUrl = \"wss://\" + hostName + \"{path}\";\r\n }\r\n\r\n // because the region can change during a session, we support being passed a format string which we can then\r\n // replace with the correct information.\r\n endpointUrl = StringUtils.formatString(endpointUrl, replacementValues);\r\n\r\n const parsedUrl = new URL(endpointUrl);\r\n parsedUrl.searchParams.forEach((val: string, key: string): void => {\r\n queryParams[key] = val;\r\n });\r\n\r\n const connFactory = new TranscriberConnectionFactory();\r\n connFactory.setQueryParams(queryParams, config, endpointUrl);\r\n\r\n // Some query parameters are required for the CTS endpoint, let's explicity set them here\r\n queryParams[QueryParameterNames.CtsMeetingId] = replacementValues[QueryParameterNames.CtsMeetingId];\r\n queryParams[QueryParameterNames.CtsDeviceId] = replacementValues[QueryParameterNames.CtsDeviceId];\r\n if (!convInfo.isHost) {\r\n queryParams[QueryParameterNames.CtsIsParticipant] = \"\"; // this doesn't have a value so set to an empty string\r\n }\r\n\r\n if (!(QueryParameterNames.Format in queryParams)) {\r\n queryParams[QueryParameterNames.Format] = \"simple\";\r\n }\r\n\r\n parsedUrl.searchParams.forEach((val: string, key: string): void => {\r\n parsedUrl.searchParams.set(key, queryParams[key]);\r\n delete queryParams[key];\r\n });\r\n\r\n endpointUrl = parsedUrl.toString();\r\n\r\n } else {\r\n // connecting to regular translation endpoint\r\n const connFactory = new TranslationConnectionFactory();\r\n\r\n endpointUrl = connFactory.getEndpointUrl(config, true);\r\n endpointUrl = StringUtils.formatString(endpointUrl, replacementValues);\r\n\r\n connFactory.setQueryParams(queryParams, config, endpointUrl);\r\n }\r\n\r\n headers[HeaderNames.ConnectionId] = connectionId;\r\n headers[RestConfigBase.configParams.token] = convInfo.token;\r\n if (!!authInfo.token) {\r\n headers[authInfo.headerName] = authInfo.token;\r\n }\r\n\r\n const enableCompression = config.parameters.getProperty(\"SPEECH-EnableWebsocketCompression\", \"\").toUpperCase() === \"TRUE\";\r\n return new WebsocketConnection(endpointUrl, queryParams, headers, new WebsocketMessageFormatter(), ProxyInfo.fromRecognizerConfig(config), enableCompression, connectionId);\r\n }\r\n}\r\n"]}