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());
}
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());
}
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();
}
}
Aggregations