private void startRecordingJob()

in src/main/java/com/aws/iot/edgeconnectorforkvs/EdgeConnectorForKVSService.java [297:367]


    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);
                        if (status.equals(RecorderStatus.FAILED)) {
                            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 the component by Constants.setFatalStatus(true); 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 component to restart the process.");
            Constants.setFatalStatus(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());
            Constants.setFatalStatus(true);
        }
    }