use of com.aws.iot.edgeconnectorforkvs.videorecorder.callback.GStreamerAppDataCallback in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.
the class EdgeConnectorForKVSService method startRecordingJob.
private void startRecordingJob(EdgeConnectorForKVSConfiguration edgeConnectorForKVSConfiguration) {
VideoRecorder videoRecorder = null;
ReentrantLock processLock = edgeConnectorForKVSConfiguration.getProcessLock();
try {
if (processLock.tryLock(INIT_LOCK_TIMEOUT_IN_SECONDS, TimeUnit.SECONDS)) {
log.info("Start Recording called for " + edgeConnectorForKVSConfiguration.getKinesisVideoStreamName());
log.info("Calling function " + Constants.getCallingFunctionName(2));
edgeConnectorForKVSConfiguration.setRecordingRequestsCount(edgeConnectorForKVSConfiguration.getRecordingRequestsCount() + 1);
if (edgeConnectorForKVSConfiguration.getRecordingRequestsCount() > 1) {
log.info("Recording already running. Requests Count: " + edgeConnectorForKVSConfiguration.getRecordingRequestsCount());
return;
}
VideoRecorderBuilder builder = new VideoRecorderBuilder(new StatusCallback() {
@Override
public void notifyStatus(VideoRecorderBase recorder, RecorderStatus status, String description) {
String pipeLineName = recorder.getPipeline().getName();
log.info("Recorder[" + pipeLineName + "] status changed callback: " + status);
// Camera level restart is in progress
if (status.equals(RecorderStatus.FAILED) && edgeConnectorForKVSConfiguration.getRecordingRequestsCount() > 0) {
log.warn("Recorder failed due to errors. Pipeline name: " + pipeLineName);
log.warn("Trying restart recorder");
recorderService.submit(() -> {
restartRecorder(recorder);
});
}
}
private void restartRecorder(@NonNull VideoRecorderBase recorder) {
recorder.stopRecording();
try {
Thread.sleep(restartSleepTime);
} catch (InterruptedException e) {
log.error("Thread sleep interrupted.");
}
recorder.startRecording();
log.info("Restart Recording for pipeline recorder " + recorder.getPipeline().getName());
}
});
PipedOutputStream outputStream = new PipedOutputStream();
builder.registerCamera(CameraType.RTSP, edgeConnectorForKVSConfiguration.getRtspStreamURL());
builder.registerFileSink(ContainerType.MATROSKA, videoRecordingRootPath + edgeConnectorForKVSConfiguration.getSiteWiseAssetId() + PATH_DELIMITER + "video");
builder.registerAppDataCallback(ContainerType.MATROSKA, new GStreamerAppDataCallback());
builder.registerAppDataOutputStream(ContainerType.MATROSKA, outputStream);
videoRecorder = builder.construct();
edgeConnectorForKVSConfiguration.setVideoRecorder(videoRecorder);
edgeConnectorForKVSConfiguration.setOutputStream(outputStream);
log.info("Recorder for " + edgeConnectorForKVSConfiguration.getKinesisVideoStreamName() + " has been initialized");
} else {
// Recorder cannot init. Will retry in the method end
log.error("Fail to init recorder for " + edgeConnectorForKVSConfiguration.getKinesisVideoStreamName());
}
} catch (InterruptedException e) {
log.error("Init recorder process for " + edgeConnectorForKVSConfiguration.getKinesisVideoStreamName() + " has been interrupted, re-init camera to restart the process.");
edgeConnectorForKVSConfiguration.getFatalStatus().set(true);
} finally {
if (processLock.isHeldByCurrentThread())
processLock.unlock();
}
if (videoRecorder != null) {
log.info("Start recording for " + edgeConnectorForKVSConfiguration.getKinesisVideoStreamName());
videoRecorder.startRecording();
} else {
log.error("Fail to init recorder for " + edgeConnectorForKVSConfiguration.getKinesisVideoStreamName());
edgeConnectorForKVSConfiguration.getFatalStatus().set(true);
}
}
Aggregations