Search in sources :

Example 1 with KvsStreamingException

use of com.aws.iot.edgeconnectorforkvs.videouploader.model.exceptions.KvsStreamingException in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.

the class VideoUploaderClient method doUploadStream.

private void doUploadStream(InputStream inputStream, Date videoUploadingStartTime, Runnable statusChangedCallBack, UploadCallBack uploadCallBack) throws KvsStreamingException {
    if (dataEndpoint == null) {
        dataEndpoint = getDataEndpoint();
    }
    putMediaLatch = new CountDownLatch(1);
    PutMediaAckResponseHandler rspHandler = createResponseHandler(putMediaLatch, statusChangedCallBack, uploadCallBack);
    if (kvsDataClient == null) {
        kvsDataClient = AmazonKinesisVideoPutMediaClient.builder().withRegion(region.getName()).withEndpoint(URI.create(dataEndpoint)).withCredentials(awsCredentialsProvider).withConnectionTimeoutInMillis(CONNECTION_TIMEOUT_IN_MILLIS).withNumberOfThreads(1).build();
    }
    log.info("Uploading from input stream, timestamp: " + videoUploadingStartTime.getTime());
    kvsDataClient.putMedia(new PutMediaRequest().withStreamName(kvsStreamName).withFragmentTimecodeType(FragmentTimecodeType.RELATIVE).withPayload(inputStream).withProducerStartTimestamp(videoUploadingStartTime), rspHandler);
    try {
        putMediaLatch.await();
        log.info("putMedia end from latch");
    } catch (InterruptedException e) {
        log.debug("Put media is interrupted");
    }
    if (lastKvsStreamingException == null && isTaskTerminating) {
        /* It's ending from close request, let's wait a little to receive ACKs. */
        try {
            inputStream.close();
            Thread.sleep(Constants.UPLOADER_WAIT_FOR_ACKS_DELAY_MILLI_SECONDS);
        } catch (IOException exception) {
            log.error(exception.getMessage());
        } catch (InterruptedException exception) {
            log.error(exception.getMessage());
        } catch (Exception exception) {
            // It happens when input stream is not able to be closed
            log.error("Unhandled exception caught: " + exception);
        }
    }
    kvsDataClient.close();
    kvsDataClient = null;
}
Also used : PutMediaAckResponseHandler(com.amazonaws.services.kinesisvideo.PutMediaAckResponseHandler) PutMediaRequest(com.amazonaws.services.kinesisvideo.model.PutMediaRequest) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) VideoUploaderException(com.aws.iot.edgeconnectorforkvs.videouploader.model.exceptions.VideoUploaderException) KvsStreamingException(com.aws.iot.edgeconnectorforkvs.videouploader.model.exceptions.KvsStreamingException) IOException(java.io.IOException)

Example 2 with KvsStreamingException

use of com.aws.iot.edgeconnectorforkvs.videouploader.model.exceptions.KvsStreamingException in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.

the class EdgeConnectorForKVSServiceTest method testInitVideoUploaders_ThrowKvsStreamingException_ThenGetProcessLockTimeout.

@Test
public void testInitVideoUploaders_ThrowKvsStreamingException_ThenGetProcessLockTimeout(@TempDir Path tempDir) throws IOException, InterruptedException {
    // when
    List<EdgeConnectorForKVSConfiguration> edgeConnectorForKVSConfigurationList = new ArrayList();
    VideoRecorder videoRecorder = Mockito.mock(VideoRecorder.class);
    VideoUploader videoUploader = Mockito.mock(VideoUploader.class);
    ReentrantLock processLock = Mockito.mock(ReentrantLock.class);
    EdgeConnectorForKVSConfiguration edgeConnectorForKVSConfiguration = Mockito.spy(EdgeConnectorForKVSConfiguration.class);
    when(edgeConnectorForKVSConfiguration.getKinesisVideoStreamName()).thenReturn(MOCK_KINESIS_VIDEO_STREAM_NAME);
    when(edgeConnectorForKVSConfiguration.getLiveStreamingStartTime()).thenReturn(START_TIME_ALWAYS);
    when(edgeConnectorForKVSConfiguration.getCaptureStartTime()).thenReturn("");
    doNothing().when(edgeConnectorForKVSConfiguration).setVideoRecorder(any());
    when(edgeConnectorForKVSConfiguration.getVideoRecordFolderPath()).thenReturn(tempDir);
    doNothing().when(edgeConnectorForKVSConfiguration).setVideoUploader(any());
    when(edgeConnectorForKVSConfiguration.getVideoRecorder()).thenReturn(videoRecorder);
    when(edgeConnectorForKVSConfiguration.getVideoUploader()).thenReturn(videoUploader);
    when(edgeConnectorForKVSConfiguration.getRecordingRequestsCount()).thenReturn(2);
    doNothing().when(edgeConnectorForKVSConfiguration).setProcessLock(any());
    when(edgeConnectorForKVSConfiguration.getProcessLock()).thenReturn(processLock);
    when(processLock.tryLock(anyLong(), any())).thenReturn(false);
    edgeConnectorForKVSConfigurationList.add(edgeConnectorForKVSConfiguration);
    doThrow(new KvsStreamingException("")).doThrow(new RuntimeException()).when(videoUploader).uploadStream(any(), any(), any(), any());
    // Mock for initConfiguration
    when(siteWiseManager.initEdgeConnectorForKVSServiceConfiguration(any())).thenReturn(edgeConnectorForKVSConfigurationList);
    // Mock for initSecretsManager
    when(secretsClient.getSecretValue(any())).thenReturn(gson.toJson(secretMap));
    EdgeConnectorForKVSService service = EdgeConnectorForKVSService.builder().regionName(MOCK_REGION_NAME).siteWiseClient(siteWiseClient).siteWiseManager(siteWiseManager).secretsClient(secretsClient).kvsClient(kvsClient).awsCredentialsProviderV1(awsCredentialsProvider).videoRecordingRootPath(tempDir.toString()).streamManagerBuilder(streamManagerBuilder).videoUploadRequestHandler(videoUploadRequestHandler).retryOnFail(false).build();
    // then
    service.setUpSharedEdgeConnectorForKVSService();
    service.setUpCameraLevelEdgeConnectorForKVSService(edgeConnectorForKVSConfiguration);
    Thread.sleep(2000);
    // verify
    verify(videoUploader, atLeast(1)).uploadStream(any(), any(), any(), any());
    assertTrue(edgeConnectorForKVSConfiguration.getFatalStatus().get());
    assertFalse(edgeConnectorForKVSConfiguration.getProcessLock().isLocked());
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) KvsStreamingException(com.aws.iot.edgeconnectorforkvs.videouploader.model.exceptions.KvsStreamingException) VideoUploader(com.aws.iot.edgeconnectorforkvs.videouploader.VideoUploader) ArrayList(java.util.ArrayList) EdgeConnectorForKVSConfiguration(com.aws.iot.edgeconnectorforkvs.model.EdgeConnectorForKVSConfiguration) VideoRecorder(com.aws.iot.edgeconnectorforkvs.videorecorder.VideoRecorder) Test(org.junit.jupiter.api.Test)

Example 3 with KvsStreamingException

use of com.aws.iot.edgeconnectorforkvs.videouploader.model.exceptions.KvsStreamingException in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.

the class EdgeConnectorForKVSServiceTest method testInitVideoUploaders_ThrowKvsStreamingException.

@Test
public void testInitVideoUploaders_ThrowKvsStreamingException(@TempDir Path tempDir) throws IOException, InterruptedException {
    // when
    List<EdgeConnectorForKVSConfiguration> edgeConnectorForKVSConfigurationList = new ArrayList();
    VideoRecorder videoRecorder = Mockito.mock(VideoRecorder.class);
    VideoUploader videoUploader = Mockito.mock(VideoUploader.class);
    EdgeConnectorForKVSConfiguration edgeConnectorForKVSConfiguration = Mockito.spy(EdgeConnectorForKVSConfiguration.class);
    when(edgeConnectorForKVSConfiguration.getKinesisVideoStreamName()).thenReturn(MOCK_KINESIS_VIDEO_STREAM_NAME);
    when(edgeConnectorForKVSConfiguration.getLiveStreamingStartTime()).thenReturn(START_TIME_ALWAYS);
    when(edgeConnectorForKVSConfiguration.getCaptureStartTime()).thenReturn("");
    doNothing().when(edgeConnectorForKVSConfiguration).setVideoRecorder(any());
    when(edgeConnectorForKVSConfiguration.getVideoRecordFolderPath()).thenReturn(tempDir);
    doNothing().when(edgeConnectorForKVSConfiguration).setVideoUploader(any());
    when(edgeConnectorForKVSConfiguration.getVideoRecorder()).thenReturn(videoRecorder);
    when(edgeConnectorForKVSConfiguration.getVideoUploader()).thenReturn(videoUploader);
    when(edgeConnectorForKVSConfiguration.getRecordingRequestsCount()).thenReturn(2);
    edgeConnectorForKVSConfigurationList.add(edgeConnectorForKVSConfiguration);
    doThrow(new KvsStreamingException("")).doThrow(new RuntimeException()).when(videoUploader).uploadStream(any(), any(), any(), any());
    // Mock for initConfiguration
    when(siteWiseManager.initEdgeConnectorForKVSServiceConfiguration(any())).thenReturn(edgeConnectorForKVSConfigurationList);
    // Mock for initSecretsManager
    when(secretsClient.getSecretValue(any())).thenReturn(gson.toJson(secretMap));
    EdgeConnectorForKVSService service = EdgeConnectorForKVSService.builder().regionName(MOCK_REGION_NAME).siteWiseClient(siteWiseClient).siteWiseManager(siteWiseManager).secretsClient(secretsClient).kvsClient(kvsClient).awsCredentialsProviderV1(awsCredentialsProvider).videoRecordingRootPath(tempDir.toString()).streamManagerBuilder(streamManagerBuilder).videoUploadRequestHandler(videoUploadRequestHandler).retryOnFail(false).build();
    // then
    service.setUpSharedEdgeConnectorForKVSService();
    service.setUpCameraLevelEdgeConnectorForKVSService(edgeConnectorForKVSConfiguration);
    Thread.sleep(2000);
    // verify
    verify(videoUploader, atLeast(1)).uploadStream(any(), any(), any(), any());
}
Also used : KvsStreamingException(com.aws.iot.edgeconnectorforkvs.videouploader.model.exceptions.KvsStreamingException) VideoUploader(com.aws.iot.edgeconnectorforkvs.videouploader.VideoUploader) ArrayList(java.util.ArrayList) EdgeConnectorForKVSConfiguration(com.aws.iot.edgeconnectorforkvs.model.EdgeConnectorForKVSConfiguration) VideoRecorder(com.aws.iot.edgeconnectorforkvs.videorecorder.VideoRecorder) Test(org.junit.jupiter.api.Test)

Example 4 with KvsStreamingException

use of com.aws.iot.edgeconnectorforkvs.videouploader.model.exceptions.KvsStreamingException in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.

the class VideoUploaderClient method createResponseHandler.

/**
 * Create a {@link PutMediaAckResponseHandler} that can handle messages while doing put media.
 *
 * @param latch A latch for handling asynchronous interrupt
 * @return a {@link PutMediaAckResponseHandler}
 */
private PutMediaAckResponseHandler createResponseHandler(CountDownLatch latch, @SuppressWarnings("unused") Runnable statusChangedCallBack, UploadCallBack uploadCallBack) {
    return new PutMediaAckResponseHandler() {

        @Override
        public void onAckEvent(AckEvent event) {
            log.trace("onAckEvent " + event);
            if (AckEventType.Values.PERSISTED.equals(event.getAckEventType().getEnumValue())) {
                log.info("Fragment pushed to KVS " + event.getFragmentNumber());
                if (uploadCallBack != null) {
                    updateUploadCallbackStatus(uploadCallBack, event);
                }
            }
            if (AckEventType.Values.ERROR.equals(event.getAckEventType().getEnumValue())) {
                lastKvsStreamingException = new KvsStreamingException(event.toString());
            }
        }

        @Override
        public void onFailure(Throwable t) {
            log.info("onFailure");
            lastKvsStreamingException = new KvsStreamingException(t.getMessage());
            latch.countDown();
        }

        @Override
        public void onComplete() {
            log.info("onComplete");
            if (uploadCallBack != null) {
                uploadCallBack.onComplete();
            }
            latch.countDown();
        }
    };
}
Also used : KvsStreamingException(com.aws.iot.edgeconnectorforkvs.videouploader.model.exceptions.KvsStreamingException) PutMediaAckResponseHandler(com.amazonaws.services.kinesisvideo.PutMediaAckResponseHandler) AckEvent(com.amazonaws.services.kinesisvideo.model.AckEvent)

Aggregations

KvsStreamingException (com.aws.iot.edgeconnectorforkvs.videouploader.model.exceptions.KvsStreamingException)4 PutMediaAckResponseHandler (com.amazonaws.services.kinesisvideo.PutMediaAckResponseHandler)2 EdgeConnectorForKVSConfiguration (com.aws.iot.edgeconnectorforkvs.model.EdgeConnectorForKVSConfiguration)2 VideoRecorder (com.aws.iot.edgeconnectorforkvs.videorecorder.VideoRecorder)2 VideoUploader (com.aws.iot.edgeconnectorforkvs.videouploader.VideoUploader)2 ArrayList (java.util.ArrayList)2 Test (org.junit.jupiter.api.Test)2 AckEvent (com.amazonaws.services.kinesisvideo.model.AckEvent)1 PutMediaRequest (com.amazonaws.services.kinesisvideo.model.PutMediaRequest)1 VideoUploaderException (com.aws.iot.edgeconnectorforkvs.videouploader.model.exceptions.VideoUploaderException)1 IOException (java.io.IOException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1