Skip to content

Instantly share code, notes, and snippets.

@davehorton
Created May 19, 2021
Embed
What would you like to do?
StartStreamTranscriptionAsync example
Aws::String key(awsAccessKeyId);
Aws::String secret(awsSecretAccessKey);
Aws::Client::ClientConfiguration config;
if (region != nullptr && strlen(region) > 0) config.region = region;
if (*awsAccessKeyId && *awsSecretAccessKey) {
m_client = Aws::MakeUnique<TranscribeStreamingServiceClient>(ALLOC_TAG, AWSCredentials(awsAccessKeyId, awsSecretAccessKey), config);
}
else {
m_client = Aws::MakeUnique<TranscribeStreamingServiceClient>(ALLOC_TAG, config);
}
m_handler.SetTranscriptEventCallback([this](const TranscriptEvent& ev)
{
switch_core_session_t* psession = switch_core_session_locate(m_sessionId.c_str());
if (psession) {
switch_channel_t* channel = switch_core_session_get_channel(psession);
std::lock_guard<std::mutex> lk(m_mutex);
m_transcript = ev;
m_cond.notify_one();
switch_core_session_rwunlock(psession);
}
});
m_request.SetMediaSampleRateHertz(16000);
m_request.SetLanguageCode(LanguageCodeMapper::GetLanguageCodeForName(lang));
m_request.SetMediaEncoding(MediaEncoding::pcm);
m_request.SetEventStreamHandler(m_handler);
if (channels > 1) m_request.SetNumberOfChannels(channels);
const char* var;
switch_core_session_t* session = switch_core_session_locate(sessionId);
switch_channel_t *channel = switch_core_session_get_channel(session);
if (var = switch_channel_get_variable(channel, "AWS_SHOW_SPEAKER_LABEL")) {
m_request.SetShowSpeakerLabel(true);
}
if (var = switch_channel_get_variable(channel, "AWS_ENABLE_CHANNEL_IDENTIFICATION")) {
m_request.SetEnableChannelIdentification(true);
}
if (var = switch_channel_get_variable(channel, "AWS_VOCABULARY_NAME")) {
m_request.SetVocabularyName(var);
}
if (var = switch_channel_get_variable(channel, "AWS_VOCABULARY_FILTER_NAME")) {
m_request.SetVocabularyFilterName(var);
}
if (var = switch_channel_get_variable(channel, "AWS_VOCABULARY_FILTER_METHOD")) {
m_request.SetVocabularyFilterMethod(VocabularyFilterMethodMapper::GetVocabularyFilterMethodForName(var));
}
// NB: this was added in AWS SDK 1.9
if (var = switch_channel_get_variable(channel, "AWS_ENABLE_PARTIAL_RESULTS_STABILITY")) {
m_request.SetEnablePartialResultsStabilization(true);
m_request.SetPartialResultsStability(PartialResultsStabilityMapper::GetPartialResultsStabilityForName(var));
}
auto OnStreamReady = [this](Model::AudioStream& stream)
{
switch_core_session_t* psession = switch_core_session_locate(m_sessionId.c_str());
if (psession) {
switch_channel_t* channel = switch_core_session_get_channel(psession);
m_pStream = &stream;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "GStreamer %p got stream ready\n", this);
switch_core_session_rwunlock(psession);
}
};
auto OnResponseCallback = [this](const TranscribeStreamingServiceClient* pClient,
const Model::StartStreamTranscriptionRequest& request,
const Model::StartStreamTranscriptionOutcome& outcome,
const std::shared_ptr<const Aws::Client::AsyncCallerContext>& context)
{
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "GStreamer %p stream got final response\n", this);
switch_core_session_t* psession = switch_core_session_locate(m_sessionId.c_str());
if (psession) {
if (!outcome.IsSuccess()) {
const TranscribeStreamingServiceError& err = outcome.GetError();
auto message = err.GetMessage();
auto exception = err.GetExceptionName();
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "GStreamer %p stream got error response %s : %s\n", this, message.c_str(), exception.c_str());
}
std::lock_guard<std::mutex> lk(m_mutex);
m_finished = true;
m_cond.notify_one();
switch_core_session_rwunlock(psession);
}
};
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "GStreamer %p starting transcribe\n", this);
m_client->StartStreamTranscriptionAsync(m_request, OnStreamReady, OnResponseCallback, nullptr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment