Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon Transcribe Streaming service request in Node.js with Http/2 gives no response

I am trying to use Amazon Transcribe Streaming Service with a http2 request from Node.js, Here is the documentation links that I am following Streaming request format. According to this document end point is https://transcribe-streaming.<'region'>.amazonaws.com, but making a request to this url gives url not found error. But in the Java Example found end point as https://transcribestreaming.''.amazonaws.com, so making a request to this url does not give any error or response back. I am trying from us-east-1 region.

Here is the code I am trying with.

const http2 = require('http2');
var aws4  = require('aws4');

var opts = {
  service: 'transcribe', 
  region: 'us-east-1', 
  path: '/stream-transcription', 
  headers:{
   'content-type': 'application/json',
   'x-amz-target': 'com.amazonaws.transcribe.Transcribe.StartStreamTranscription'
  }
}

var urlObj = aws4.sign(opts, {accessKeyId: '<access key>', secretAccessKey: '<aws secret>'});
const client = http2.connect('https://transcribestreaming.<region>.amazonaws.com');
client.on('error', function(err){
  console.error("error in request ",err);
});

const req = client.request({
  ':method': 'POST',
  ':path': '/stream-transcription',
  'authorization': urlObj.headers.Authorization,  
  'content-type': 'application/json',
  'x-amz-content-sha256': 'STREAMING-AWS4-HMAC-SHA256-EVENTS',
  'x-amz-target': 'com.amazonaws.transcribe.Transcribe.StartStreamTranscription',
  'x-amz-date': urlObj['headers']['X-Amz-Date'],
  'x-amz-transcribe-language-code': 'en-US',
  'x-amz-transcribe-media-encoding': 'pcm',
  'x-amz-transcribe-sample-rate': 44100
});

req.on('response', (headers, flags) => {
  for (const name in headers) {
    console.log(`${name}: ${headers[name]}`);
  }
});
let data = '';
req.on('data', (chunk) => { data += chunk; });
req.on('end', () => {
  console.log(`\n${data}`);
  client.close();
});
req.end();

Can anyone point out what I am missing here. I couldn't find any examples implementing this with HTTP/2 either.

Update: Changing the Content-type to application/json came back with response status 200 but with following exception:

`{"Output":{"__type":"com.amazon.coral.service#SerializationException"},"Version":"1.0"}`

Update (April-22-2019):

req.setEncoding('utf8');
req.write(audioBlob);

var audioBlob = new Buffer(JSON.stringify({
    "AudioStream": { 
       "AudioEvent": { 
          "AudioChunk": audioBufferData
     }
 }

Before ending the request I am adding a "audioblod" as payload by serializing. My "audioBufferData" is in raw PCM audio format from browser. I see from documentation payload has to be encoded to "Event Stream Encoding", but couldn't figure out how to implement it.

So with out this event stream encoding currently i am getting this following exception with 200 response status.

{"Output":{"__type":"com.amazon.coral.service#UnknownOperationException"},"Version":"1.0"}
like image 805
Manoj Avatar asked Mar 09 '19 01:03

Manoj


2 Answers

I reached out to AWS support and they don't seem to be able to get a HTTP/2 implementation working with NodeJS Either.

However, they have now provided a way to interact with the Transcribe streaming API directly via websockets (blog post here)

If this fits your usecase I would highly recommend checking out the new example repo at: https://github.com/aws-samples/amazon-transcribe-websocket-static

If you're using it in a public-facing page I'd recommend using unauthenticated Cognito sessions to handle credential retrieval. I've got this working in a production app so feel free to tag me in any other questions.

like image 171
Calvin Avatar answered Oct 18 '22 08:10

Calvin


This doesn't directly answer the question but I think it's useful enough to post as an answer rather than a comment.

AWS just announced WebSocket support for Amazon Transcribe. Here are the docs, and here is a client-side sample app. The biggest difference, and one that I think makes integrating with WebSockets more straightforward, is that you do not need to sign each audio chunk like http/2 requires.

The relevant code for authorizing and initiating the connection using a pre-signed URL is in lib/aws-signature-v4.js

exports.createPresignedURL = function(method, host, path, service, payload, options) {
  options = options || {};
  options.key = options.key || process.env.AWS_ACCESS_KEY_ID;
  options.secret = options.secret || process.env.AWS_SECRET_ACCESS_KEY;
  options.protocol = options.protocol || 'https';
  options.headers = options.headers || {};
  options.timestamp = options.timestamp || Date.now();
  options.region = options.region || process.env.AWS_REGION || 'us-east-1';
  options.expires = options.expires || 86400; // 24 hours
  options.headers = options.headers || {};

  // host is required
  options.headers.Host = host;

  var query = options.query ? querystring.parse(options.query) : {};
  query['X-Amz-Algorithm'] = 'AWS4-HMAC-SHA256';
  query['X-Amz-Credential'] = options.key + '/' + exports.createCredentialScope(options.timestamp, options.region, service);
  query['X-Amz-Date'] = toTime(options.timestamp);
  query['X-Amz-Expires'] = options.expires;
  query['X-Amz-SignedHeaders'] = exports.createSignedHeaders(options.headers);

  var canonicalRequest = exports.createCanonicalRequest(method, path, query, options.headers, payload);
  var stringToSign = exports.createStringToSign(options.timestamp, options.region, service, canonicalRequest);
  var signature = exports.createSignature(options.secret, options.timestamp, options.region, service, stringToSign);
  query['X-Amz-Signature'] = signature;
  return options.protocol + '://' + host + path + '?' + querystring.stringify(query);
};

And we invoke it in lib/main.js:

function createPresignedUrl() {
    let endpoint = "transcribestreaming." + region + ".amazonaws.com:8443";

    // get a preauthenticated URL that we can use to establish our WebSocket
    return v4.createPresignedURL(
        'GET',
        endpoint,
        '/stream-transcription-websocket',
        'transcribe',
        crypto.createHash('sha256').update('', 'utf8').digest('hex'), {
            'key': $('#access_id').val(),
            'secret': $('#secret_key').val(),
            'protocol': 'wss',
            'expires': 15,
            'region': region,
            'query': "language-code=" + languageCode + "&media-encoding=pcm&sample-rate=" + sampleRate
        }
    );
}

To package things in the event stream message format we need, we wrap the PCM-encoded audio in a JSON envelope and convert it to binary

function convertAudioToBinaryMessage(audioChunk) {
    let raw = mic.toRaw(audioChunk);

    if (raw == null)
        return;

    // downsample and convert the raw audio bytes to PCM
    let downsampledBuffer = audioUtils.downsampleBuffer(raw, sampleRate);
    let pcmEncodedBuffer = audioUtils.pcmEncode(downsampledBuffer);

    // add the right JSON headers and structure to the message
    let audioEventMessage = getAudioEventMessage(Buffer.from(pcmEncodedBuffer));

    //convert the JSON object + headers into a binary event stream message
    let binary = eventStreamMarshaller.marshall(audioEventMessage);

    return binary;
}

function getAudioEventMessage(buffer) {
    // wrap the audio data in a JSON envelope
    return {
        headers: {
            ':message-type': {
                type: 'string',
                value: 'event'
            },
            ':event-type': {
                type: 'string',
                value: 'AudioEvent'
            }
        },
        body: buffer
    };
}
like image 42
bwest Avatar answered Oct 18 '22 08:10

bwest