<commit_before>/*
 * Copyright @ 2018 Atlassian Pty Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.jitsi.jibri.capture.ffmpeg

import org.jitsi.jibri.sink.Sink
import org.jitsi.jibri.capture.Capturer
import org.jitsi.jibri.capture.UnsupportedOsException
import org.jitsi.jibri.capture.ffmpeg.executor.FfmpegExecutor
import org.jitsi.jibri.capture.ffmpeg.executor.FfmpegExecutorParams
import org.jitsi.jibri.capture.ffmpeg.executor.impl.LinuxFfmpegExecutor
import org.jitsi.jibri.capture.ffmpeg.executor.impl.MacFfmpegExecutor
import org.jitsi.jibri.util.extensions.debug
import org.jitsi.jibri.util.extensions.error
import java.util.logging.Logger

/**
 * [FfmpegCapturer] is responsible for launching ffmpeg, capturing from the
 * configured audio and video devices, and writing to the given [Sink]
 */
class FfmpegCapturer : Capturer {
    private val logger = Logger.getLogger(this::class.qualifiedName)
    private val ffmpegExecutor: FfmpegExecutor

    init
    {
        logger.debug("OS: ${System.getProperty("os.name")}")
        ffmpegExecutor = when (System.getProperty("os.name")) {
            "Mac OS X" -> MacFfmpegExecutor()
            "Linux" -> LinuxFfmpegExecutor()
            else -> throw UnsupportedOsException()
        }
    }

    override fun start(sink: Sink): Boolean {
        if (!ffmpegExecutor.launchFfmpeg(FfmpegExecutorParams(), sink)) {
            return false
        }
        // Now make sure ffmpeg is actually healthy before returning that start
        // was successful in case it starts up (and stays alive) but fails to
        // start encoding successfully
        for (i in 1..15) {
            if (isHealthy()) {
                return true
            }
            Thread.sleep(1000)
        }
        logger.error("Ffmpeg started up but did not start encoding after 15 tries, giving up")
        return false
    }

    override fun isHealthy(): Boolean = ffmpegExecutor.isHealthy()

    override fun getExitCode(): Int? = ffmpegExecutor.getExitCode()

    override fun stop() {
        ffmpegExecutor.stopFfmpeg()
    }
}<commit_msg>fail faster when ffmpeg fails quickly after starting<commit_after>/*
 * Copyright @ 2018 Atlassian Pty Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.jitsi.jibri.capture.ffmpeg

import org.jitsi.jibri.sink.Sink
import org.jitsi.jibri.capture.Capturer
import org.jitsi.jibri.capture.UnsupportedOsException
import org.jitsi.jibri.capture.ffmpeg.executor.FfmpegExecutor
import org.jitsi.jibri.capture.ffmpeg.executor.FfmpegExecutorParams
import org.jitsi.jibri.capture.ffmpeg.executor.impl.LinuxFfmpegExecutor
import org.jitsi.jibri.capture.ffmpeg.executor.impl.MacFfmpegExecutor
import org.jitsi.jibri.util.extensions.debug
import org.jitsi.jibri.util.extensions.error
import java.util.logging.Logger

/**
 * [FfmpegCapturer] is responsible for launching ffmpeg, capturing from the
 * configured audio and video devices, and writing to the given [Sink]
 */
class FfmpegCapturer : Capturer {
    private val logger = Logger.getLogger(this::class.qualifiedName)
    private val ffmpegExecutor: FfmpegExecutor

    init
    {
        logger.debug("OS: ${System.getProperty("os.name")}")
        ffmpegExecutor = when (System.getProperty("os.name")) {
            "Mac OS X" -> MacFfmpegExecutor()
            "Linux" -> LinuxFfmpegExecutor()
            else -> throw UnsupportedOsException()
        }
    }

    override fun start(sink: Sink): Boolean {
        if (!ffmpegExecutor.launchFfmpeg(FfmpegExecutorParams(), sink)) {
            return false
        }
        // Now make sure ffmpeg is actually healthy before returning that start
        // was successful in case it starts up (and stays alive) but fails to
        // start encoding successfully
        for (i in 1..15) {
            if (isHealthy()) {
                return true
            } else if (getExitCode() != null) {
                logger.error("Ffmpeg already exited")
                break
            }
            Thread.sleep(1000)
        }
        logger.error("Ffmpeg started up but did not start encoding after 15 tries, giving up")
        return false
    }

    override fun isHealthy(): Boolean = ffmpegExecutor.isHealthy()

    override fun getExitCode(): Int? = ffmpegExecutor.getExitCode()

    override fun stop() {
        ffmpegExecutor.stopFfmpeg()
    }
}
{'fraction_non_alphanumeric': 0.09003512664078388, 'fraction_numerical': 0.006285819929746719, 'mean_word_length': 2.989675516224189, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5043827', 'n_tokens_mistral': 1602, 'n_tokens_neox': 1561, 'n_words': 469}