[Node.js] AWS Kinesis Video Streamのclipのmp4をDownloadする

### kvs起動
gst-launch-1.0 avfvideosrc device-index=0 ! videoconvert ! video/x-raw,format=I420,width=1280,height=720 ! vtenc_h264_hw allow-frame-reordering=FALSE realtime=TRUE max-keyframe-interval=45 bitrate=512 ! h264parse ! video/x-h264,stream-format=avc,alignment=au,profile=baseline ! kvssink stream-name=MyKinesisVideoStream storage-size=512 access-key=”YourAccessKeyId” secret-key=”YourSecretAccessKey” aws-region=”ap-northeast-1″

### node.js側

const fs = require('fs');
const AWS = require('aws-sdk')

AWS.config.update({accessKeyId: "***",
  secretAccessKey: "***",region: "ap-northeast-1"});

async function main(){

	const streamName = 'MyKinesisVideoStream';
	const kinesisvideo = new AWS.KinesisVideo();

	const param = {
		APIName: "GET_CLIP",
		StreamName: streamName
	};
	const e = await kinesisvideo.getDataEndpoint(param).promise();
	const kinesisvideoarchivedmedia = new AWS.KinesisVideoArchivedMedia({endpoint: e.DataEndpoint});

	startTime = new Date(2021, 10, 13, 23, 40, 30); // UTC時間
	endTime = new Date(2021, 10, 13, 23, 41, 38); // UTC時間

	const params = {
        ClipFragmentSelector: { 
          FragmentSelectorType: "PRODUCER_TIMESTAMP",
          TimestampRange: {
            EndTimestamp: endTime,
            StartTimestamp: startTime 
          }
        },
        StreamName: streamName
    };
    const data = await kinesisvideoarchivedmedia.getClip(params).promise();

	const filePath = "test.mp4"
	fs.writeFileSync(filePath, data.Payload);
}

main()

$ node get-mp4.js
うおおおおおおおおおおおおおおおおおおおお

### トラブルシューティング
ずっとこれが出てたんだが、何かなと思ってたら
$ node get-mp4.js
(node:137900) UnhandledPromiseRejectionWarning: ResourceNotFoundException: No fragments found in the stream for the clip request.

jsでtimestampを作るとき、monthは -1なのね。
new Date(2021, 10, 13, 23, 40, 30); // 2021/11/13 23:40:30

new Date(2021, 11, 13, 23, 40, 30) でやってたから、No fragments foundだった。。あほやわ
よっしゃあああああああああああああああああああ

[AWS] RaspberryPI4でKVSを利用

公式のドキュメントを見ながらやると
https://github.com/awslabs/amazon-kinesis-video-streams-producer-sdk-cpp/blob/master/docs/raspberry-pi.md

これだとうまくいかない
$ gst-device-monitor-1.0
$ gst-launch-1.0 v4l2src device=/dev/video0 ! h264parse ! video/x-h264,stream-format=avc,alignment=au ! kvssink stream-name=MyKinesisVideoStream storage-size=128 access-key=”***” secret-key=”***” aws-region=”ap-northeast-1″
> The audio recording device is represented by hw:card_number,device_numer. So to use the second device in the example, use hw:3,0 as the device in gst-launch-1.0 command.

これだとOK
$ gst-launch-1.0 -v v4l2src device=/dev/video0 ! videoconvert ! video/x-raw,format=I420,width=640,height=480,framerate=30/1 ! x264enc bframes=0 key-int-max=45 bitrate=500 tune=zerolatency ! video/x-h264,stream-format=avc,alignment=au ! kvssink stream-name=MyKinesisVideoStream storage-size=128 access-key=”***” secret-key=”***” aws-region=”ap-northeast-1″
> if your camera supports outputting h264 encoded stream directly, then you can use this command:

h264エンコードがすごく大事なのね

[Amazon Kinesis Video Stream] VideoJSでHTML側で取得して表示

KVSのHLSStreamingSessionURLを取得してvideojsでストリーミング表示する

credentials.js

const AWS_ACCESS_KEY_ID = '';
const AWS_SECRET_ACCESS_KEY = '';
const AWS_REGION = 'ap-northeast-1';
const AWS_STREAM_NAME = 'MyKinesisVideoStream';

html

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link href="https://vjs.zencdn.net/7.15.4/video-js.css" rel="stylesheet" />
    <script src="https://sdk.amazonaws.com/js/aws-sdk-2.642.0.min.js"></script>
    <script src="credentials.js"></script>
</head>
<body>
    <video id="videojs" class="player video-js vjs-default-skin" data-setup='{"fluid":true}' controls autoplay muted></video>

    <script src="https://vjs.zencdn.net/7.15.4/video.min.js"></script>
    <script>
        window.addEventListener("unhandledrejection", function(event){
            console.warn("WARNING: Unhandled promise rejection." + event.reason);
            location.reload()
        });
        window.onerror = (message, file, lineNo, colNo, error) => {
            console.error('window.onerror', message, file, lineNo, colNo, error);
            location.reload()
        }

        async function getURL(){
            const accessKeyId = AWS_ACCESS_KEY_ID;
            const secretAccessKey = AWS_SECRET_ACCESS_KEY;
            const region = AWS_REGION;
            const streamName = AWS_STREAM_NAME;

            const options = {
                accessKeyId: accessKeyId,
                secretAccessKey: secretAccessKey,
                region: region,
            }
            const kinesisVideoClient = new AWS.KinesisVideo(options);
            const kinesisVideoArchivedMediaClient = new AWS.KinesisVideoArchivedMedia(options);

            const e = await kinesisVideoClient.getDataEndpoint({
                APIName: 'GET_HLS_STREAMING_SESSION_URL',
                StreamName: streamName
            }).promise();
            kinesisVideoArchivedMediaClient.endpoint = new AWS.Endpoint(e.DataEndpoint);

            const d = await kinesisVideoArchivedMediaClient.getHLSStreamingSessionURL({
                DisplayFragmentTimestamp: 'ALWAYS',
                StreamName: streamName
            }).promise();
            return d.HLSStreamingSessionURL;
        }

        document.addEventListener('DOMContentLoaded', async() => {
            const url = await getURL();
            const player = videojs('videojs');
            player.src({
                src: url,
                type: 'application/x-mpegURL'
            });
            player.on('error', function(){
                console.log(player.error());
                location.reload();
            });
            setInterval(() => {
                const t = player.currentTime();
                console.log("current Time is "+ t +" seconds");
            }, 5000)
        });
    </script>
</body>
</html>

### mac
kvsのsdkをdownloadした状態とする
https://github.com/awslabs/amazon-kinesis-video-streams-producer-sdk-cpp.git

macのカメラからKVSに映像を送る
$ gst-launch-1.0 avfvideosrc device-index=0 ! videoconvert ! video/x-raw,format=I420,width=1280,height=720 ! vtenc_h264_hw allow-frame-reordering=FALSE realtime=TRUE max-keyframe-interval=45 bitrate=512 ! h264parse ! video/x-h264,stream-format=avc,alignment=au,profile=baseline ! kvssink stream-name=MyKinesisVideoStream storage-size=512 access-key=”${access-key}” secret-key=”${secret-key}” aws-region=”ap-northeast-1″

$ php -S 192.168.34.10:8000
http://192.168.34.10:8000/

ちょっと待ってくれ
なんか凄い事やってる様な気がする…🥺

RaspberryPI4でAmazon Kinesis Video Streamに配信したい

まずラズパイでsudo apt-get updateとしたいが、エラーになる。以下のコマンドでupdate

$ sudo apt-get update –allow-releaseinfo-change

続いて、mac同様、c++のSDKをラズパイに入れていく
$ sudo apt-get install cmake m4 git build-essential
$ sudo apt-get install gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-tools
$ sudo apt-get install gstreamer1.0-omx

$ git clone https://github.com/awslabs/amazon-kinesis-video-streams-producer-sdk-cpp.git
$ cd amazon-kinesis-video-streams-producer-sdk-cpp
$ mkdir build
$ cd build

$ cmake .. -DBUILD_DEPENDENCIES=OFF -DBUILD_GSTREAMER_PLUGIN=ON
$ make
$ cd ..
$ export GST_PLUGIN_PATH=`pwd`/build
$ export LD_LIBRARY_PATH=`pwd`/open-source/local/lib
$ gst-inspect-1.0 kvssink

インストラクションの通りにやってもエラーになることがあるので、適時インストールする

$ gst-device-monitor-1.0
$ gst-launch-1.0 rtspsrc location=rtsp://@224.0.0.1:1900 short-header=TRUE ! rtph264depay ! h264parse ! kvssink stream-name=MyKinesisVideoStream storage-size=128 access-key=”YourAccessKeyId” secret-key=”YourSecretAccessKey” aws-region=”ap-northeast-1″

これで接続できればOK

rtspではなく、ラズパイのカメラモジュールの映像を送ることもできるが、今、カメラモジュールは到着待ちなので、今日はテストのみ。

ほう、中々勉強になった。
OK、これからネットワークカメラの接続についてまとめる

[RTSP]macでKinesis Video Streamsを使う

$ git clone https://github.com/awslabs/amazon-kinesis-video-streams-producer-sdk-cpp.git
$ cd amazon-kinesis-video-streams-producer-sdk-cpp
$ mkdir build
$ cd build
$ brew link openssl –force
$ brew install pkg-config openssl cmake gstreamer gst-plugins-base gst-plugins-good gst-plugins-bad gst-plugins-ugly log4cplus gst-libav
$ make
$ export GST_PLUGIN_PATH=`pwd`/build
$ export LD_LIBRARY_PATH=`pwd`/open-source/local/lib
$ gst-inspect-1.0 kvssink

$ gst-device-monitor-1.0
Probing devices…

Device found:

name : FaceTime HD Camera
class : Video/Source
caps : video/x-raw(memory:GLMemory), width=1280, height=720, format=UYVY, framerate=[ 1/1, 30/1 ], texture-target=rectangle
video/x-raw, width=1280, height=720, format={ (string)UYVY, (string)YUY2, (string)NV12, (string)BGRA }, framerate=[ 1/1, 30/1 ]
properties:
device.api = avf
avf.unique_id = CC28053MGKXGJJM3Z
avf.model_id = “Apple\ Camera\ VendorID_0x106B\ ProductID_0x1570”
avf.has_flash = false
avf.has_torch = false
avf.manufacturer = “Apple\ Inc.”
gst-launch-1.0 avfvideosrc device-index=0 ! …

最後に、gst-launch-1.0 avfvideosrc device-index=0 と書いてあるので、これを使う

$ gst-launch-1.0 avfvideosrc device-index=0 ! videoconvert ! video/x-raw,format=I420,width=1280,height=720 ! vtenc_h264_hw allow-frame-reordering=FALSE realtime=TRUE max-keyframe-interval=45 bitrate=512 ! h264parse ! video/x-h264,stream-format=avc,alignment=au,profile=baseline ! kvssink stream-name=MyKinesisVideoStream storage-size=512 access-key=”YourAccessKeyId” secret-key=”YourSecretAccessKey” aws-region=”ap-northeast-1″

うおおおおおおおおおお
まじか、、、
sugeeeeeeeeeeee

[RTSP]Kinesis Video Streamsを使いたい

C++プロデューサーSDKをGStreamerプラグインとして使用する
ubuntu20.4にbuildする

$ git clone https://github.com/awslabs/amazon-kinesis-video-streams-producer-sdk-cpp.git
$ cd amazon-kinesis-video-streams-producer-sdk-cpp
$ mkdir build
$ sudo apt-get install libssl-dev libcurl4-openssl-dev liblog4cplus-dev libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev gstreamer1.0-plugins-base-apps gstreamer1.0-plugins-bad gstreamer1.0-plugins-good gstreamer1.0-plugins-ugly gstreamer1.0-tools
$ cmake .. -DBUILD_DEPENDENCIES=OFF -DBUILD_GSTREAMER_PLUGIN=ON
$ make
// 省略
[ 97%] Built target gstkvssink
Scanning dependencies of target kvs_gstreamer_sample
[ 98%] Building CXX object CMakeFiles/kvs_gstreamer_sample.dir/samples/kvs_gstreamer_sample.cpp.o
[100%] Linking CXX executable kvs_gstreamer_sample
[100%] Built target kvs_gstreamer_sample

$ cd ..
$ export GST_PLUGIN_PATH=`pwd`/build
$ export LD_LIBRARY_PATH=`pwd`/open-source/local/lib

AWS IAMで、AmazonKinesisVideoStreamsFullAccessのユーザを作成し、access key, secret keyをコピー

$ gst-inspect-1.0 kvssink
$ gst-launch-1.0 autovideosrc ! videoconvert ! video/x-raw,format=I420,width=1280,height=720 ! vtenc_h264_hw allow-frame-reordering=FALSE realtime=TRUE max-keyframe-interval=45 bitrate=512 ! h264parse ! video/x-h264,stream-format=avc,alignment=au,profile=baseline ! kvssink stream-name=MyKinesisVideoStream storage-size=512 access-key=”YourAccessKeyId” secret-key=”YourSecretAccessKey” aws-region=”ap-northeast-1″

no element “vtenc_h264_hw”

OS xのみで使えるのでエラーになるとのこと
>@Raghava248 vtenc_h264_hw is a hardware codec which is available on OSx only. Please try your platform specific encoder or use a software encoder like x264
https://github.com/awslabs/amazon-kinesis-video-streams-producer-sdk-cpp/issues/138

[RTSP]VLCを使ってRTP配信を行いたい

macにvlcをdownloadします
https://vlc-media-player.jp.uptodown.com/mac/download

1. VLCを起動し、ファイル -> キャプチャーデバイスを開く

2. ストリーミングの設定
RTCで224.0.0.1でポートを1900に設定

### 受信側
稼働しているvlcとは別にもう一つのvlcを起動します

ファイル -> ネットワークを開く
rtp://@224.0.0.1:1900 を設定します

すると、rtpで受信できることがわかります

おおおおおおおおおおおおおおお
Sugeeeeeeeeeeeeeeeeee

RTSPでは224.0.0.0 ~ 239.255.255.255のアドレスを指定する

うん、これをAmazon Kinesis Video Streams で受信したい