Search in sources :

Example 1 with VideoUploadRequestEvent

use of com.aws.iot.edgeconnectorforkvs.handler.VideoUploadRequestEvent in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.

the class EdgeConnectorForKVSServiceTest method test_initMQTTSubscription_onError.

@Test
public void test_initMQTTSubscription_onError(@TempDir Path tempDir) throws IOException, InterruptedException {
    // when
    List<EdgeConnectorForKVSConfiguration> edgeConnectorForKVSConfigurationList = new ArrayList();
    EdgeConnectorForKVSConfiguration edgeConnectorForKVSConfiguration = EdgeConnectorForKVSConfiguration.builder().kinesisVideoStreamName(MOCK_KINESIS_VIDEO_STREAM_NAME).captureStartTime(START_TIME_NEVER).liveStreamingStartTime(START_TIME_NEVER).videoUploadRequestMqttTopic(VIDEO_UPLOAD_REQUEST_MQTT_TOPIC).videoRecordFolderPath(tempDir).recordingRequestsCount(0).liveStreamingRequestsCount(0).build();
    edgeConnectorForKVSConfigurationList.add(edgeConnectorForKVSConfiguration);
    // Mock for initConfiguration
    when(siteWiseManager.initEdgeConnectorForKVSServiceConfiguration(any())).thenReturn(edgeConnectorForKVSConfigurationList);
    // Mock for initSecretsManager
    when(secretsClient.getSecretValue(any())).thenReturn(gson.toJson(secretMap));
    doAnswer(invocationOnMock -> {
        VideoUploadRequestEvent event = invocationOnMock.getArgument(1);
        event.onError("Test Error");
        return null;
    }).when(videoUploadRequestHandler).subscribeToMqttTopic(any(), any());
    // then
    edgeConnectorForKVSService.setUpSharedEdgeConnectorForKVSService();
    edgeConnectorForKVSService.setUpCameraLevelEdgeConnectorForKVSService(edgeConnectorForKVSConfiguration);
    Thread.sleep(3000);
    assertEquals(0, edgeConnectorForKVSConfiguration.getLiveStreamingRequestsCount());
    assertEquals(0, edgeConnectorForKVSConfiguration.getRecordingRequestsCount());
}
Also used : ArrayList(java.util.ArrayList) VideoUploadRequestEvent(com.aws.iot.edgeconnectorforkvs.handler.VideoUploadRequestEvent) EdgeConnectorForKVSConfiguration(com.aws.iot.edgeconnectorforkvs.model.EdgeConnectorForKVSConfiguration) Test(org.junit.jupiter.api.Test)

Example 2 with VideoUploadRequestEvent

use of com.aws.iot.edgeconnectorforkvs.handler.VideoUploadRequestEvent in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.

the class EdgeConnectorForKVSServiceTest method test_initMQTTSubscription_onStartHistorical.

@Test
public void test_initMQTTSubscription_onStartHistorical(@TempDir Path tempDir) throws IOException, InterruptedException {
    // when
    List<EdgeConnectorForKVSConfiguration> edgeConnectorForKVSConfigurationList = new ArrayList();
    EdgeConnectorForKVSConfiguration edgeConnectorForKVSConfiguration = EdgeConnectorForKVSConfiguration.builder().kinesisVideoStreamName(MOCK_KINESIS_VIDEO_STREAM_NAME).captureStartTime(START_TIME_NEVER).liveStreamingStartTime(START_TIME_NEVER).videoUploadRequestMqttTopic(VIDEO_UPLOAD_REQUEST_MQTT_TOPIC).videoRecordFolderPath(tempDir).recordingRequestsCount(0).liveStreamingRequestsCount(0).build();
    edgeConnectorForKVSConfigurationList.add(edgeConnectorForKVSConfiguration);
    // Mock for initConfiguration
    when(siteWiseManager.initEdgeConnectorForKVSServiceConfiguration(any())).thenReturn(edgeConnectorForKVSConfigurationList);
    // Mock for initSecretsManager
    when(secretsClient.getSecretValue(any())).thenReturn(gson.toJson(secretMap));
    doAnswer(invocationOnMock -> {
        VideoUploadRequestEvent event = invocationOnMock.getArgument(1);
        event.onStart(IS_LIVE_FALSE, EVENT_TIMESTAMP, START_TIME, END_TIME);
        return null;
    }).when(videoUploadRequestHandler).subscribeToMqttTopic(any(), any());
    // then
    edgeConnectorForKVSService.setUpSharedEdgeConnectorForKVSService();
    edgeConnectorForKVSService.setUpCameraLevelEdgeConnectorForKVSService(edgeConnectorForKVSConfiguration);
    Thread.sleep(3000);
    assertEquals(0, edgeConnectorForKVSConfiguration.getLiveStreamingRequestsCount());
    assertEquals(0, edgeConnectorForKVSConfiguration.getRecordingRequestsCount());
}
Also used : ArrayList(java.util.ArrayList) VideoUploadRequestEvent(com.aws.iot.edgeconnectorforkvs.handler.VideoUploadRequestEvent) EdgeConnectorForKVSConfiguration(com.aws.iot.edgeconnectorforkvs.model.EdgeConnectorForKVSConfiguration) Test(org.junit.jupiter.api.Test)

Example 3 with VideoUploadRequestEvent

use of com.aws.iot.edgeconnectorforkvs.handler.VideoUploadRequestEvent in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.

the class EdgeConnectorForKVSService method initMQTTSubscription.

private void initMQTTSubscription(EdgeConnectorForKVSConfiguration configuration) {
    VideoUploadRequestEvent event = new VideoUploadRequestEvent() {

        @Override
        public void onStart(boolean isLive, long updateTimestamp, long startTime, long endTime) {
            if (isLive) {
                log.info("Received Live Streaming request for stream: " + configuration.getKinesisVideoStreamName());
                ScheduledFuture<?> future = configuration.getStopLiveStreamingTaskFuture();
                if (future == null) {
                    // Kick-off Live Streaming for 5 mins
                    // do it only for the first request
                    log.info("Start Live Streaming");
                    liveStreamingExecutor.submit(() -> {
                        try {
                            startLiveVideoStreaming(configuration);
                        } catch (Exception ex) {
                            log.error("Error starting live video streaming." + ex.getMessage());
                            configuration.getFatalStatus().set(true);
                        }
                    });
                } else {
                    log.info("Live Streaming was already started. Continue Streaming.");
                    // Cancel the previously started scheduled task
                    // and restart the task below
                    future.cancel(false);
                }
                Runnable task = getStopLiveStreamingTask(configuration);
                future = stopLiveStreamingExecutor.schedule(task, LIVE_STREAMING_STOP_TIMER_DELAY_IN_SECONDS, TimeUnit.SECONDS);
                configuration.setStopLiveStreamingTaskFuture(future);
                log.info("Schedule Live Streaming to stop after " + LIVE_STREAMING_STOP_TIMER_DELAY_IN_SECONDS + "s for stream: " + configuration.getKinesisVideoStreamName());
            } else {
                try {
                    startHistoricalVideoUploading(configuration, startTime, endTime);
                } catch (Exception ex) {
                    log.error("Error starting historical video uploading." + ex.getMessage());
                    configuration.getFatalStatus().set(true);
                }
            }
        }

        @Override
        public void onError(String errMessage) {
            log.info("MQTT Error " + errMessage + " for stream " + configuration.getKinesisVideoStreamName());
        }
    };
    if (configuration.getVideoUploadRequestMqttTopic() != null) {
        videoUploadRequestHandler.subscribeToMqttTopic(configuration.getVideoUploadRequestMqttTopic(), event);
    }
}
Also used : VideoUploadRequestEvent(com.aws.iot.edgeconnectorforkvs.handler.VideoUploadRequestEvent) TimeoutException(java.util.concurrent.TimeoutException) EdgeConnectorForKVSException(com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSException) EdgeConnectorForKVSUnrecoverableException(com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSUnrecoverableException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 4 with VideoUploadRequestEvent

use of com.aws.iot.edgeconnectorforkvs.handler.VideoUploadRequestEvent in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.

the class EdgeConnectorForKVSServiceTest method test_initMQTTSubscription_onStartLive.

@Test
public void test_initMQTTSubscription_onStartLive(@TempDir Path tempDir) throws IOException, InterruptedException {
    // when
    List<EdgeConnectorForKVSConfiguration> edgeConnectorForKVSConfigurationList = new ArrayList();
    EdgeConnectorForKVSConfiguration edgeConnectorForKVSConfiguration = EdgeConnectorForKVSConfiguration.builder().kinesisVideoStreamName(MOCK_KINESIS_VIDEO_STREAM_NAME).captureStartTime(START_TIME_NEVER).liveStreamingStartTime(START_TIME_NEVER).videoUploadRequestMqttTopic(VIDEO_UPLOAD_REQUEST_MQTT_TOPIC).videoRecordFolderPath(tempDir).recordingRequestsCount(0).liveStreamingRequestsCount(0).build();
    edgeConnectorForKVSConfigurationList.add(edgeConnectorForKVSConfiguration);
    // Mock for initConfiguration
    when(siteWiseManager.initEdgeConnectorForKVSServiceConfiguration(any())).thenReturn(edgeConnectorForKVSConfigurationList);
    // Mock for initSecretsManager
    when(secretsClient.getSecretValue(any())).thenReturn(gson.toJson(secretMap));
    doAnswer(invocationOnMock -> {
        VideoUploadRequestEvent event = invocationOnMock.getArgument(1);
        event.onStart(IS_LIVE_TRUE, EVENT_TIMESTAMP, START_TIME, END_TIME);
        return null;
    }).when(videoUploadRequestHandler).subscribeToMqttTopic(any(), any());
    // then
    edgeConnectorForKVSService.setUpSharedEdgeConnectorForKVSService();
    edgeConnectorForKVSService.setUpCameraLevelEdgeConnectorForKVSService(edgeConnectorForKVSConfiguration);
    Thread.sleep(3000);
    assertEquals(1, edgeConnectorForKVSConfiguration.getLiveStreamingRequestsCount());
    assertEquals(1, edgeConnectorForKVSConfiguration.getRecordingRequestsCount());
}
Also used : ArrayList(java.util.ArrayList) VideoUploadRequestEvent(com.aws.iot.edgeconnectorforkvs.handler.VideoUploadRequestEvent) EdgeConnectorForKVSConfiguration(com.aws.iot.edgeconnectorforkvs.model.EdgeConnectorForKVSConfiguration) Test(org.junit.jupiter.api.Test)

Example 5 with VideoUploadRequestEvent

use of com.aws.iot.edgeconnectorforkvs.handler.VideoUploadRequestEvent in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.

the class EdgeConnectorForKVSServiceTest method test_cleanUp_singleCamera_happyCase.

@Test
public void test_cleanUp_singleCamera_happyCase(@TempDir Path tempDir) throws IOException, InterruptedException {
    // mock
    List<EdgeConnectorForKVSConfiguration> edgeConnectorForKVSConfigurationList = new ArrayList();
    VideoRecorder videoRecorder = Mockito.mock(VideoRecorder.class);
    VideoUploader videoUploader = Mockito.mock(VideoUploader.class);
    PipedOutputStream pipedOutputStream = Mockito.mock(PipedOutputStream.class);
    EdgeConnectorForKVSConfiguration edgeConnectorForKVSConfiguration = EdgeConnectorForKVSConfiguration.builder().kinesisVideoStreamName(MOCK_KINESIS_VIDEO_STREAM_NAME).captureStartTime(START_TIME_NEVER).liveStreamingStartTime(START_TIME_NEVER).videoUploadRequestMqttTopic(VIDEO_UPLOAD_REQUEST_MQTT_TOPIC).videoRecordFolderPath(tempDir).recordingRequestsCount(0).liveStreamingRequestsCount(0).videoRecorder(videoRecorder).build();
    edgeConnectorForKVSConfigurationList.add(edgeConnectorForKVSConfiguration);
    // when
    // Mock for initConfiguration
    when(siteWiseManager.initEdgeConnectorForKVSServiceConfiguration(any())).thenReturn(edgeConnectorForKVSConfigurationList);
    // Mock for initSecretsManager
    when(secretsClient.getSecretValue(any())).thenReturn(gson.toJson(secretMap));
    doNothing().when(videoUploader).close();
    doNothing().when(pipedOutputStream).flush();
    doNothing().when(pipedOutputStream).close();
    doNothing().when(videoRecorder).stopRecording();
    when(videoRecorder.getStatus()).thenReturn(RecorderStatus.STOPPED);
    doAnswer(invocationOnMock -> {
        VideoUploadRequestEvent event = invocationOnMock.getArgument(1);
        event.onStart(IS_LIVE_TRUE, EVENT_TIMESTAMP, START_TIME, END_TIME);
        return null;
    }).when(videoUploadRequestHandler).subscribeToMqttTopic(any(), any());
    // then
    edgeConnectorForKVSService.setUpSharedEdgeConnectorForKVSService();
    edgeConnectorForKVSService.setUpCameraLevelEdgeConnectorForKVSService(edgeConnectorForKVSConfiguration);
    Thread.sleep(3000);
    assertEquals(1, edgeConnectorForKVSConfiguration.getLiveStreamingRequestsCount());
    assertEquals(1, edgeConnectorForKVSConfiguration.getRecordingRequestsCount());
    edgeConnectorForKVSService.cleanUpEdgeConnectorForKVSService(edgeConnectorForKVSConfiguration);
    // verify
    assertEquals(0, edgeConnectorForKVSConfiguration.getLiveStreamingRequestsCount());
    assertEquals(0, edgeConnectorForKVSConfiguration.getRecordingRequestsCount());
}
Also used : VideoUploader(com.aws.iot.edgeconnectorforkvs.videouploader.VideoUploader) ArrayList(java.util.ArrayList) VideoUploadRequestEvent(com.aws.iot.edgeconnectorforkvs.handler.VideoUploadRequestEvent) EdgeConnectorForKVSConfiguration(com.aws.iot.edgeconnectorforkvs.model.EdgeConnectorForKVSConfiguration) VideoRecorder(com.aws.iot.edgeconnectorforkvs.videorecorder.VideoRecorder) PipedOutputStream(java.io.PipedOutputStream) Test(org.junit.jupiter.api.Test)

Aggregations

VideoUploadRequestEvent (com.aws.iot.edgeconnectorforkvs.handler.VideoUploadRequestEvent)6 EdgeConnectorForKVSConfiguration (com.aws.iot.edgeconnectorforkvs.model.EdgeConnectorForKVSConfiguration)5 ArrayList (java.util.ArrayList)5 Test (org.junit.jupiter.api.Test)5 VideoRecorder (com.aws.iot.edgeconnectorforkvs.videorecorder.VideoRecorder)2 VideoUploader (com.aws.iot.edgeconnectorforkvs.videouploader.VideoUploader)2 PipedOutputStream (java.io.PipedOutputStream)2 EdgeConnectorForKVSException (com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSException)1 EdgeConnectorForKVSUnrecoverableException (com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSUnrecoverableException)1 IOException (java.io.IOException)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1