<commit_before>package org.mbari.m3.vars.annotation.mediaplayers.macos;

import org.mbari.m3.blackmagic.BlackmagicImageCapture;
import org.mbari.m3.vars.annotation.Initializer;
import org.mbari.m3.vars.annotation.mediaplayers.MediaPlayer;
import org.mbari.m3.vars.annotation.model.Framegrab;
import org.mbari.m3.vars.annotation.services.ImageCaptureService;
import org.mbari.vcr4j.VideoError;
import org.mbari.vcr4j.VideoIndex;
import org.mbari.vcr4j.VideoState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.*;
import java.io.File;
import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
import java.util.Collection;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

/**
 * @author <NAME>
 * @since 2018-04-24T16:11:00
 */
public class BMImageCaptureService implements SelectableImageCaptureService {

    private final Logger log = LoggerFactory.getLogger(getClass());
    private BlackmagicImageCapture imageCapture;
    public static BMImageCaptureService imageCaptureService;
    private String currentDevice = "";

    protected BMImageCaptureService() {
        imageCapture = new BlackmagicImageCapture();
    }

    public Collection<String> listDevices() {
        return Arrays.asList(imageCapture.videoDevicesAsStrings());
    }

    public void setDevice(String device) {
        String cd = currentDevice == null ? "" : currentDevice;
        if (!cd.equals(device)) {
            imageCapture.stopSession();
        }
        if (device != null) {
            imageCapture.startSessionWithNamedDevice(device);
        }
        currentDevice = device;
    }

    @Override
    public Framegrab capture(File file) {
        Framegrab framegrab = new Framegrab();
        //imageCapture.startSessionWithNamedDevice(currentDevice);
        Optional<Image> imageOpt = imageCapture.capture(file, Duration.ofSeconds(10));
        if (imageOpt.isPresent()) {
            framegrab.setImage(imageOpt.get());

            MediaPlayer<? extends VideoState, ? extends VideoError> mediaPlayer = Initializer.getToolBox().getMediaPlayer();
            if (mediaPlayer != null) {
                try {

                    // HACK - Use a 3 second timeout
                    mediaPlayer.requestVideoIndex()
                            .thenAccept(framegrab::setVideoIndex)
                            .get(3000, TimeUnit.MILLISECONDS);
                }
                catch (Exception e) {
                    log.warn("Problem with requesting videoIndex while capturing a framegrab", e);
                    framegrab.setVideoIndex(new VideoIndex(Instant.now()));
                }
            }

            // If, for some reason, getting the video index fails. Fall back to a timestamp
            if (!framegrab.getVideoIndex().isPresent()) {
                log.warn("Failed to get video index. Using current timestamp for video index");
                framegrab.setVideoIndex(new VideoIndex(Instant.now()));
            }


        }<commit_msg>Added error handling for unsatisfied links<commit_after>package org.mbari.m3.vars.annotation.mediaplayers.macos;

import org.mbari.m3.blackmagic.BlackmagicImageCapture;
import org.mbari.m3.vars.annotation.Initializer;
import org.mbari.m3.vars.annotation.mediaplayers.MediaPlayer;
import org.mbari.m3.vars.annotation.model.Framegrab;
import org.mbari.m3.vars.annotation.services.ImageCaptureService;
import org.mbari.vcr4j.VideoError;
import org.mbari.vcr4j.VideoIndex;
import org.mbari.vcr4j.VideoState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.*;
import java.io.File;
import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author <NAME>
 * @since 2018-04-24T16:11:00
 */
public class BMImageCaptureService implements SelectableImageCaptureService {

    private final Logger log = LoggerFactory.getLogger(getClass());
    private BlackmagicImageCapture imageCapture;
    public static BMImageCaptureService imageCaptureService;
    private String currentDevice = "";

    protected BMImageCaptureService() {
        imageCapture = new BlackmagicImageCapture();
    }

    public Collection<String> listDevices() {
        try {
            return Arrays.asList(imageCapture.videoDevicesAsStrings());
        }
        catch (UnsatisfiedLinkError e) {
            log.warn("Something is wrong with the BlackMagic libraries", e);
            return new ArrayList<>();
        }
    }

    public void setDevice(String device) {
        String cd = currentDevice == null ? "" : currentDevice;
        try {
            if (!cd.equals(device)) {
                imageCapture.stopSession();
            }
            if (device != null) {
                imageCapture.startSessionWithNamedDevice(device);
            }
        }
        catch (UnsatisfiedLinkError e) {
            log.warn("Something is wrong with the BlackMagic libraries", e);
        }
        currentDevice = device;
    }

    @Override
    public Framegrab capture(File file) {
        Framegrab framegrab = new Framegrab();
        //imageCapture.startSessionWithNamedDevice(currentDevice);
        Optional<Image> imageOpt = Optional.empty();
        try {
            imageOpt = imageCapture.capture(file, Duration.ofSeconds(10));
        }
        catch (UnsatisfiedLinkError e) {
            log.warn("Something is wrong with the BlackMagic libraries", e);
        }
        if (imageOpt.isPresent()) {
            framegrab.setImage(imageOpt.get());

            MediaPlayer<? extends VideoState, ? extends VideoError> mediaPlayer = Initializer.getToolBox().getMediaPlayer();
            if (mediaPlayer != null) {
                try {

                    // HACK - Use a 3 second timeout
                    mediaPlayer.requestVideoIndex()
                            .thenAccept(framegrab::setVideoIndex)
                            .get(3000, TimeUnit.MILLISECONDS);
                }
                catch (Exception e) {
                    log.warn("Problem with requesting videoIndex while capturing a framegrab", e);
                    framegrab.setVideoIndex(new VideoIndex(Instant.now()));
                }
            }

            // If, for some reason, getting the video index fails. Fall back to a timestamp
            if (!framegrab.getVideoIndex().isPresent()) {
                log.warn("Failed to get video index. Using current timestamp for video index");
                framegrab.setVideoIndex(new VideoIndex(Instant.now()));
            }


        }
{'fraction_non_alphanumeric': 0.09836314034555926, 'fraction_numerical': 0.009699909063352531, 'mean_word_length': 2.4841605068637804, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, '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': '380856', 'n_tokens_mistral': 1840, 'n_tokens_neox': 1745, 'n_words': 386}