适用于 JavaScript 的 AWS Kinesis PutMedia 操作

时间:2021-04-08 06:47:19

标签: javascript amazon-web-services ecmascript-6 amazon-kinesis

所以我使用 AWS SDK v2 创建一个视频流,我在其中通过浏览器发布我的网络摄像头。

目标是使用脚本在 HTML 页面上完成整个过程,因为它是 Chrome 扩展程序的后台脚本。

我已经成功导入了我需要的 SDK,我想将我的相机视频推送到服务器。

稍后将通过用于机器学习的 Lambda 函数在后端处理视频源。

  const AWS = window.AWS
  const formValues = getFormValues()
  const video = document.getElementById('video')
  var options = {
    accessKeyId: formValues.accessKeyId,
    secretAccessKey: formValues.secretAccessKey,
    sessionToken: formValues.sessionToken,
    region: formValues.region,
    endpdpoint: formValues.endpoint,
  }
  const kinesisVideo = new AWS.KinesisVideo(options)

  async function sendFeedToServer() {
    let streamName = `stream-${getRandomID()}`
    await createVideoStream(streamName)
    setTimeout(() => {
      getEndpoint(streamName, 'PUT_MEDIA')
    }, 1000)
  }

  async function getEndpoint(streamName, operation) {
    var params = {
      APIName: operation,
      // StreamARN: arn,
      StreamName: streamName,
    }
    console.log(params)
    kinesisVideo.getDataEndpoint(params, function (err, data) {
      if (err) console.log(err, err.stack)
      else console.log(data) // endpoint to read and write data
    })
  }

  async function createVideoStream(streamName) {
    const params = {
      StreamName: streamName /* required */,
      // DataRetentionInHours: 'NUMBER_VALUE',
      DeviceName: 'Extension',
      KmsKeyId: 'KmsKeyId',
      // MediaType: 'STRING_VALUE',
      // Tags: {
      //   '<TagKey>': 'STRING_VALUE',
      //   /* '<TagKey>': ... */
      // },
    }
    kinesisVideo.createStream(params, function (err, data) {
      if (err) console.log(err, err.stack)
      // an error occurred
      else console.log(data) // successful response
    })
  }

sendFeedToServer() 函数在相机打开并且我们有视频流时触发。

getEndpoint() 函数返回我可以写入数据的端点。 如 API 中所述:https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/KinesisVideo.html#getDataEndpoint-property

不过,我在文档中找不到使用 putMediaJavaScript 的方法,我是否遗漏了什么?

是否有任何解决方法可以在浏览器中实现此功能?

1 个答案:

答案 0 :(得分:0)

我想这是 PutMedia 文档的链接,用于与从上面代码中的 getEndpoint 获得的端点一起使用:

https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/kinesisvideo/AmazonKinesisVideoPutMedia.html