<commit_before># Automated tests to verify your gstreamer + OpenCV installation.
# Run with ./run-tests.sh

EXPECT_RESULT=1
DONT_EXPECT_RESULT=0
EXPECT_MOTION=1
DONT_EXPECT_MOTION=0

# Test for stbt-motiondetect element from stbt.
#
# You may need to set GST_PLUGIN_PATH to point where you installed
# stbt-motiondetect element.
#
test_gstreamer_can_find_stbt_motiondetect() {
    gst-inspect-0.10 stbt-motiondetect >/dev/null
}

# Test that stbt-motiondetect does not process the source by default.
#
test_stbt_motiondetect_is_not_active_by_default() {
    run_motiondetect "videotestsrc" "" "$scratchdir/gst-launch.log"
    check_expectations_from_log \
            $DONT_EXPECT_RESULT $DONT_EXPECT_MOTION "$scratchdir/gst-launch.log"
}

# Test that stbt-motiondetect does not process the source when disabled.
#
test_stbt_motiondetect_is_not_active_when_disabled() {
    run_motiondetect "videotestsrc" "enabled=false" "$scratchdir/gst-launch.log"
    check_expectations_from_log \
            $DONT_EXPECT_RESULT $DONT_EXPECT_MOTION "$scratchdir/gst-launch.log"
}

# Test that stbt-motiondetect reports motion from videotestsrc.
#
test_stbt_motiondetect_reports_motion() {
    run_motiondetect "videotestsrc" "enabled=true" "$scratchdir/gst-launch.log"
    check_expectations_from_log \
            $EXPECT_RESULT $EXPECT_MOTION "$scratchdir/gst-launch.log"
}

# Test that stbt-motiondetect does not report motion from a freezed videotestsrc.
#
test_stbt_motiondetect_does_not_report_motion() {
    run_motiondetect \
            "videotestsrc ! imagefreeze" "enabled=true" \
            "$scratchdir/gst-launch.log"
    check_expectations_from_log \
            $EXPECT_RESULT $DONT_EXPECT_MOTION "$scratchdir/gst-launch.log"
}

# Test that stbt-motiondetect reports motion with a mask that selects only the
# moving part from videotestsrc.
#
test_stbt_motiondetect_with_mask_reports_motion() {
    run_motiondetect \
            "videotestsrc" \
            "enabled=true mask=videotestsrc-mask-video.png" \
            "$scratchdir/gst-launch.log"
    check_expectations_from_log \
            $EXPECT_RESULT $EXPECT_MOTION "$scratchdir/gst-launch.log"
}

# Test that stbt-motiondetect does not report motion with a mask that selects 
# only the steady part from videotestsrc.
#
test_stbt_motiondetect_with_mask_does_not_report_motion() {
    run_motiondetect \
            "videotestsrc" \
            "enabled=true mask=videotestsrc-mask-no-video.png" \
            "$scratchdir/gst-launch.log"
    check_expectations_from_log \
            $EXPECT_RESULT $DONT_EXPECT_MOTION "$scratchdir/gst-launch.log"
}

test_stbt_motiondetect_with_high_noisethreshold_reports_motion() {
    run_motiondetect \
        "videotestsrc" \
        "enabled=true noiseThreshold=1.0" \
        "$scratchdir/gst-launch.log"
    check_expectations_from_log \
        $EXPECT_RESULT $EXPECT_MOTION "$scratchdir/gst-launch.log"
}

test_stbt_motiondetect_with_low_noisethreshold_does_not_report_motion() {
    run_motiondetect \
        "videotestsrc" \
        "enabled=true noiseThreshold=0.0" \
        "$scratchdir/gst-launch.log"
    check_expectations_from_log \
        $EXPECT_RESULT $DONT_EXPECT_MOTION "$scratchdir/gst-launch.log"
}

test_stbt_motiondetect_reports_motion_on_progress_dots() {
    run_motiondetect \
        "multifilesrc location=motiondetect-dots-%d.png index=1 \
            caps=image/png,framerate=30/1 ! decodebin2" \
        "enabled=true" \
        "$scratchdir/gst-launch.log"
    check_expectations_from_log \
        $EXPECT_RESULT $EXPECT_MOTION "$scratchdir/gst-launch.log"
}

# Run stbt-motiondetect on the specified source pipeline with the specified
# parameters; "gst-launch --messages" logs the bus messages generated by
# the GStreamer stbt-motiondetect element.
#
run_motiondetect() {
    local source_pipeline="$1"
    local parameters="$2"
    local log="$3"

    timeout 2 gst-launch-0.10 --messages \
        $source_pipeline ! \
        ffmpegcolorspace ! \
        stbt-motiondetect $parameters ! \
        ffmpegcolorspace ! \
        ximagesink \
    > "$log" 2>&1

    local ret=$?
    if [[ $ret -ne 0 && $ret -ne $timedout ]]; then
        echo "Failed to launch gstreamer pipeline; see '$log'"
        return 1
    fi
}

# The motiondetect element sends a bus message for each frame it processes
# from the second frame; "gst-launch --messages" logs the bus messages; and
# the grep command will allow to know whether motion was detected or not.
#
check_expectations_from_log() {
    local expect_result="$1"
    local expect_motion="$2"
    local log="$3"
<commit_msg>test_stbt_motiondetect_*: Fix intermittent failures on heavily-loaded systems<commit_after># Automated tests to verify your gstreamer + OpenCV installation.
# Run with ./run-tests.sh

EXPECT_RESULT=1
DONT_EXPECT_RESULT=0
EXPECT_MOTION=1
DONT_EXPECT_MOTION=0

# Test for stbt-motiondetect element from stbt.
#
# You may need to set GST_PLUGIN_PATH to point where you installed
# stbt-motiondetect element.
#
test_gstreamer_can_find_stbt_motiondetect() {
    gst-inspect-0.10 stbt-motiondetect >/dev/null
}

# Test that stbt-motiondetect does not process the source by default.
#
test_stbt_motiondetect_is_not_active_by_default() {
    run_motiondetect "videotestsrc" "" "$scratchdir/gst-launch.log"
    check_expectations_from_log \
            $DONT_EXPECT_RESULT $DONT_EXPECT_MOTION "$scratchdir/gst-launch.log"
}

# Test that stbt-motiondetect does not process the source when disabled.
#
test_stbt_motiondetect_is_not_active_when_disabled() {
    run_motiondetect "videotestsrc" "enabled=false" "$scratchdir/gst-launch.log"
    check_expectations_from_log \
            $DONT_EXPECT_RESULT $DONT_EXPECT_MOTION "$scratchdir/gst-launch.log"
}

# Test that stbt-motiondetect reports motion from videotestsrc.
#
test_stbt_motiondetect_reports_motion() {
    run_motiondetect "videotestsrc" "enabled=true" "$scratchdir/gst-launch.log"
    check_expectations_from_log \
            $EXPECT_RESULT $EXPECT_MOTION "$scratchdir/gst-launch.log"
}

# Test that stbt-motiondetect does not report motion from a static pattern.
#
test_stbt_motiondetect_does_not_report_motion() {
    run_motiondetect \
            "videotestsrc pattern=gamut" "enabled=true" \
            "$scratchdir/gst-launch.log"
    check_expectations_from_log \
            $EXPECT_RESULT $DONT_EXPECT_MOTION "$scratchdir/gst-launch.log"
}

# Test that stbt-motiondetect reports motion with a mask that selects only the
# moving part from videotestsrc.
#
test_stbt_motiondetect_with_mask_reports_motion() {
    run_motiondetect \
            "videotestsrc" \
            "enabled=true mask=videotestsrc-mask-video.png" \
            "$scratchdir/gst-launch.log"
    check_expectations_from_log \
            $EXPECT_RESULT $EXPECT_MOTION "$scratchdir/gst-launch.log"
}

# Test that stbt-motiondetect does not report motion with a mask that selects 
# only the steady part from videotestsrc.
#
test_stbt_motiondetect_with_mask_does_not_report_motion() {
    run_motiondetect \
            "videotestsrc" \
            "enabled=true mask=videotestsrc-mask-no-video.png" \
            "$scratchdir/gst-launch.log"
    check_expectations_from_log \
            $EXPECT_RESULT $DONT_EXPECT_MOTION "$scratchdir/gst-launch.log"
}

test_stbt_motiondetect_with_high_noisethreshold_reports_motion() {
    run_motiondetect \
        "videotestsrc" \
        "enabled=true noiseThreshold=1.0" \
        "$scratchdir/gst-launch.log"
    check_expectations_from_log \
        $EXPECT_RESULT $EXPECT_MOTION "$scratchdir/gst-launch.log"
}

test_stbt_motiondetect_with_low_noisethreshold_does_not_report_motion() {
    run_motiondetect \
        "videotestsrc" \
        "enabled=true noiseThreshold=0.0" \
        "$scratchdir/gst-launch.log"
    check_expectations_from_log \
        $EXPECT_RESULT $DONT_EXPECT_MOTION "$scratchdir/gst-launch.log"
}

test_stbt_motiondetect_reports_motion_on_progress_dots() {
    run_motiondetect \
        "multifilesrc location=motiondetect-dots-%d.png index=1 \
            caps=image/png,framerate=30/1 ! decodebin2" \
        "enabled=true" \
        "$scratchdir/gst-launch.log"
    check_expectations_from_log \
        $EXPECT_RESULT $EXPECT_MOTION "$scratchdir/gst-launch.log"
}

# Run stbt-motiondetect on the specified source pipeline with the specified
# parameters; "gst-launch --messages" logs the bus messages generated by
# the GStreamer stbt-motiondetect element.
#
run_motiondetect() {
    local source_pipeline="${1/#videotestsrc/videotestsrc num-buffers=5}"
    local parameters="$2"
    local log="$3"

    gst-launch-0.10 --messages \
        $source_pipeline ! \
        ffmpegcolorspace ! \
        stbt-motiondetect $parameters ! \
        ffmpegcolorspace ! \
        ximagesink \
    > "$log" 2>&1

    if [[ $? -ne 0 ]]; then
        echo "Failed to launch gstreamer pipeline; see '$log'"
        return 1
    fi
}

# The motiondetect element sends a bus message for each frame it processes
# from the second frame; "gst-launch --messages" logs the bus messages; and
# the grep command will allow to know whether motion was detected or not.
#
check_expectations_from_log() {
    local expect_result="$1"
    local expect_motion="$2"
    local log="$3"

{'fraction_non_alphanumeric': 0.09111494500754798, 'fraction_numerical': 0.006469700237222342, 'mean_word_length': 3.5689655172413794, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6408084', 'n_tokens_mistral': 3233, 'n_tokens_neox': 3125, 'n_words': 611}