Search in sources :

Example 11 with VideoRecorder

use of com.aws.iot.edgeconnectorforkvs.videorecorder.VideoRecorder 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 12 with VideoRecorder

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

the class EdgeConnectorForKVSServiceTest method test_StopLiveVideoStreaming_JobStop.

@Test
public void test_StopLiveVideoStreaming_JobStop(@TempDir Path tempDir) throws IOException, InterruptedException {
    // when
    List<EdgeConnectorForKVSConfiguration> edgeConnectorForKVSConfigurationList = new ArrayList();
    VideoRecorder videoRecorder = Mockito.mock(VideoRecorder.class);
    VideoUploader videoUploader = Mockito.mock(VideoUploader.class);
    PipedInputStream pipedInputStream = Mockito.mock(PipedInputStream.class);
    PipedOutputStream pipedOutputStream = Mockito.mock(PipedOutputStream.class);
    doNothing().when(videoUploader).close();
    doNothing().when(pipedOutputStream).flush();
    doNothing().when(pipedOutputStream).close();
    doNothing().when(pipedInputStream).close();
    doNothing().when(videoRecorder).stopRecording();
    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(START_TIME_ALWAYS);
    doNothing().when(edgeConnectorForKVSConfiguration).setVideoRecorder(any());
    when(edgeConnectorForKVSConfiguration.getVideoRecorder()).thenReturn(videoRecorder);
    when(edgeConnectorForKVSConfiguration.getVideoRecordFolderPath()).thenReturn(tempDir);
    doNothing().when(edgeConnectorForKVSConfiguration).setVideoUploader(any());
    when(edgeConnectorForKVSConfiguration.getVideoUploader()).thenReturn(videoUploader);
    when(edgeConnectorForKVSConfiguration.getRecordingRequestsCount()).thenReturn(1);
    doNothing().when(edgeConnectorForKVSConfiguration).setInputStream(any());
    when(edgeConnectorForKVSConfiguration.getInputStream()).thenReturn(pipedInputStream);
    doNothing().when(edgeConnectorForKVSConfiguration).setOutputStream(any());
    when(edgeConnectorForKVSConfiguration.getOutputStream()).thenReturn(pipedOutputStream);
    edgeConnectorForKVSConfigurationList.add(edgeConnectorForKVSConfiguration);
    // Mock for initConfiguration
    when(siteWiseManager.initEdgeConnectorForKVSServiceConfiguration(any())).thenReturn(edgeConnectorForKVSConfigurationList);
    // Mock for initSecretsManager
    when(secretsClient.getSecretValue(any())).thenReturn(gson.toJson(secretMap));
    // then
    edgeConnectorForKVSService.setUpSharedEdgeConnectorForKVSService();
    edgeConnectorForKVSService.setUpCameraLevelEdgeConnectorForKVSService(edgeConnectorForKVSConfiguration);
    Thread.sleep(2000);
    edgeConnectorForKVSService.schedulerStopTaskCallback(Constants.JobType.LIVE_VIDEO_STREAMING, MOCK_KINESIS_VIDEO_STREAM_NAME);
    Thread.sleep(1000);
    // verify
    assertEquals(0, edgeConnectorForKVSConfigurationList.get(0).getLiveStreamingRequestsCount());
}
Also used : 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) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) Test(org.junit.jupiter.api.Test)

Example 13 with VideoRecorder

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

the class EdgeConnectorForKVSService method stopRecordingJob.

private void stopRecordingJob(EdgeConnectorForKVSConfiguration edgeConnectorForKVSConfiguration) {
    ReentrantLock processLock = edgeConnectorForKVSConfiguration.getProcessLock();
    try {
        if (processLock.tryLock(INIT_LOCK_TIMEOUT_IN_SECONDS, TimeUnit.SECONDS)) {
            log.info("Stop Recording called for " + edgeConnectorForKVSConfiguration.getKinesisVideoStreamName());
            log.info("Calling function " + Constants.getCallingFunctionName(2));
            VideoRecorder videoRecorder = edgeConnectorForKVSConfiguration.getVideoRecorder();
            // Stop video recording provided there's no live streaming / scheduled recording in progress
            edgeConnectorForKVSConfiguration.setRecordingRequestsCount(edgeConnectorForKVSConfiguration.getRecordingRequestsCount() - 1);
            if (edgeConnectorForKVSConfiguration.getRecordingRequestsCount() > 0) {
                log.info("Recording is requested by multiple tasks. Requests Count " + edgeConnectorForKVSConfiguration.getRecordingRequestsCount());
                return;
            }
            log.info("Recorder Requests Count is 0. Stopping.");
            int maxRetry = 5;
            while (!videoRecorder.getStatus().equals(RecorderStatus.STOPPED)) {
                videoRecorder.stopRecording();
                if (maxRetry > 0) {
                    maxRetry--;
                } else {
                    log.error("Max retry reached to stop recorder for " + edgeConnectorForKVSConfiguration.getKinesisVideoStreamName());
                    edgeConnectorForKVSConfiguration.getFatalStatus().set(true);
                    break;
                }
                Thread.sleep(WAIT_TIME_BEFORE_RESTART_IN_MILLISECS);
            }
            // Close the pipeline, so we don't have duplicate videos after restart
            if (videoRecorder != null && videoRecorder.getPipeline() != null) {
                edgeConnectorForKVSConfiguration.getVideoRecorder().getPipeline().close();
            }
        } else {
            log.error("Fail to stop recorder for " + edgeConnectorForKVSConfiguration.getKinesisVideoStreamName());
            edgeConnectorForKVSConfiguration.getFatalStatus().set(true);
        }
    } catch (InterruptedException e) {
        log.error("Stop recorder for " + edgeConnectorForKVSConfiguration.getKinesisVideoStreamName() + " has been interrupted, re-init camera to restart the process.");
        edgeConnectorForKVSConfiguration.getFatalStatus().set(true);
    } finally {
        if (processLock.isHeldByCurrentThread())
            processLock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) VideoRecorder(com.aws.iot.edgeconnectorforkvs.videorecorder.VideoRecorder)

Aggregations

VideoRecorder (com.aws.iot.edgeconnectorforkvs.videorecorder.VideoRecorder)13 VideoUploader (com.aws.iot.edgeconnectorforkvs.videouploader.VideoUploader)10 EdgeConnectorForKVSConfiguration (com.aws.iot.edgeconnectorforkvs.model.EdgeConnectorForKVSConfiguration)8 ArrayList (java.util.ArrayList)8 Test (org.junit.jupiter.api.Test)8 PipedOutputStream (java.io.PipedOutputStream)7 ReentrantLock (java.util.concurrent.locks.ReentrantLock)7 PipedInputStream (java.io.PipedInputStream)4 VideoUploadRequestEvent (com.aws.iot.edgeconnectorforkvs.handler.VideoUploadRequestEvent)2 KvsStreamingException (com.aws.iot.edgeconnectorforkvs.videouploader.model.exceptions.KvsStreamingException)2 EdgeConnectorForKVSException (com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSException)1 EdgeConnectorForKVSUnrecoverableException (com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSUnrecoverableException)1 VideoRecorderBuilder (com.aws.iot.edgeconnectorforkvs.videorecorder.VideoRecorderBuilder)1 VideoRecorderBase (com.aws.iot.edgeconnectorforkvs.videorecorder.base.VideoRecorderBase)1 GStreamerAppDataCallback (com.aws.iot.edgeconnectorforkvs.videorecorder.callback.GStreamerAppDataCallback)1 StatusCallback (com.aws.iot.edgeconnectorforkvs.videorecorder.callback.StatusCallback)1 RecorderStatus (com.aws.iot.edgeconnectorforkvs.videorecorder.model.RecorderStatus)1 StatusChangedCallBack (com.aws.iot.edgeconnectorforkvs.videouploader.callback.StatusChangedCallBack)1 UploadCallBack (com.aws.iot.edgeconnectorforkvs.videouploader.callback.UploadCallBack)1 IOException (java.io.IOException)1