Insufficient system resources, capture video from web camera java

7

I'm trying to capture the video using the jxcapture. I manage to do so just for once but when I'm trying to capture second time the video in the same program I got into troubles. My code is the following:

public VideoCapture videoCapture = VideoCapture.create(VideoFormat.WMV);
public CaptureVideoFromWebCamera(){}

public void start(String filename){


    List<VideoSource> availableVideoSources = VideoSource.getAvailable();
    System.out.println("availableVideoSources = " + availableVideoSources);

    if (availableVideoSources.isEmpty()) {
        throw new IllegalStateException("No external video sources available");
    }
    VideoSource webCamera = availableVideoSources.get(0);
    System.out.println("webCamera = " + webCamera);

    videoCapture.setVideoSource(webCamera);

    java.util.List<Codec> videoCodecs = videoCapture.getVideoCodecs();
    System.out.println("videoCodecs = " + videoCodecs);
    if (videoCodecs.isEmpty()) {
        throw new IllegalStateException("No video codecs available");
    }

    Codec videoCodec = videoCodecs.get(2);
    System.out.println("videoCodec = " + videoCodec);

    EncodingParameters encodingParameters = new EncodingParameters(new File("WebCamera.wmv"));
    encodingParameters.setBitrate(500000);
    encodingParameters.setFramerate(10);
    encodingParameters.setKeyFrameInterval(1);
    encodingParameters.setCodec(videoCodec);

    videoCapture.setEncodingParameters(encodingParameters);
    videoCapture.start();
    System.out.println("Recording started. Press 'Enter' to terminate.");

}

public void stop(String filename) throws IOException{
 System.in.read();
 videoCapture.stop();
}


public static void main(String[] args) throws Throwable {

    CaptureVideoFromWebCamera obj = new CaptureVideoFromWebCamera();
    obj.start("");
    obj.stop("");

    CaptureVideoFromWebCamera obj1 = new CaptureVideoFromWebCamera();       
    obj1.start("");
    obj1.stop("");

}

}

When I'm trying to do so I'm reveiving the following error (Insufficient system resources exist to complete the requested service web camera):

Exception in thread "main" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException at com.teamdev.jxcapture.video.win.BaseDirectShowCapture.doStart(SourceFile:103) at com.teamdev.jxcapture.VideoCapture.start(SourceFile:146) at capturer.CaptureVideoFromWebCamera.start(CaptureVideoFromWebCamera.java:58) at capturer.CaptureVideoFromWebCamera.main(CaptureVideoFromWebCamera.java:76) Caused by: java.lang.reflect.InvocationTargetException at com.teamdev.jxdesktop.win32.g.doInvokeAndWait(Unknown Source) at com.teamdev.jxcapture.video.win.BaseDirectShowCapture.doStart(SourceFile:97) ... 3 more Caused by: com.teamdev.jxdesktop.win32.com.ComException: COM object method returns error code: 0x800705AA; Insufficient system resources exist to complete the requested service.

EDIT2: I tried to do add some thread sleep to the code in order to wait for the second capturing process.

CaptureVideoFromWebCamera obj = new CaptureVideoFromWebCamera();
    obj.start("1.wmv");
    obj.stop("");
    Thread.sleep(5000);
    CaptureVideoFromWebCamera obj1 = new CaptureVideoFromWebCamera();       
    obj1.start("2.wmv");
    obj1.stop("");

I got the same error.

EDIT3: When I am trying to use the same object for the capturing I got the following message:

Exception in thread "main" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException at com.teamdev.jxcapture.video.win.BaseDirectShowCapture.doStart(SourceFile:103) at com.teamdev.jxcapture.VideoCapture.start(SourceFile:146) at CaptureVideoFromWebCamera.start(CaptureVideoFromWebCamera.java:47) //videoCapture.start(); at CaptureVideoFromWebCamera.main(CaptureVideoFromWebCamera.java:64) /obj.start("2.wmv"); Caused by: java.lang.reflect.InvocationTargetException at com.teamdev.jxdesktop.win32.g.doInvokeAndWait(Unknown Source) at com.teamdev.jxcapture.video.win.BaseDirectShowCapture.doStart(SourceFile:97) ... 3 more

java
asked on Stack Overflow Jul 15, 2016 by Jose Ramon • edited Jul 23, 2016 by dieter

3 Answers

1

Actually, you are getting error message because your resource has been already locked by another thread and the lock is not released while you try to utilize the same resource from the another thread.

Here, you have to do two main things :

Step 1 : In your program, your have setup Thread.Sleep(5000); but it actually pause your thread instead and you have not setup any statement to release the resource. So, Try resetting camera socket and closing object in a finally statement.

Step 2 : try Synchronizedthread instead using normal one as only one process can able to use your resource at a time.

answered on Stack Overflow Jul 22, 2016 by Karthick Ramasamy
1

Can it help you? I think you need to release a resource after first capturing that next capture process could take it freely.

 private VideoSource webCamera; // make it as object field accessible both start and stop methods

 public void start(String file name) {
    ...
    webCamera = availableVideoSources.get(0);
    ...
   }

 public void stop(String filename) throws IOException{
   System.in.read();
   videoCapture.stop();
   webCamera.release();
 }
answered on Stack Overflow Jul 23, 2016 by Yan Pak • edited Jul 23, 2016 by Yan Pak
1

Try to reshuffle your code a little bit, so you don't initialize the video system twice:

public VideoCapture videoCapture = VideoCapture.create(VideoFormat.WMV);

public void init() {

    List<VideoSource> availableVideoSources = VideoSource.getAvailable();
    System.out.println("availableVideoSources = " + availableVideoSources);

    if (availableVideoSources.isEmpty()) {
        throw new IllegalStateException("No external video sources available");
    }
    VideoSource webCamera = availableVideoSources.get(0);
    System.out.println("webCamera = " + webCamera);

    videoCapture.setVideoSource(webCamera);

    java.util.List<Codec> videoCodecs = videoCapture.getVideoCodecs();
    System.out.println("videoCodecs = " + videoCodecs);
    if (videoCodecs.isEmpty()) {
        throw new IllegalStateException("No video codecs available");
    }

    Codec videoCodec = videoCodecs.get(2);
    System.out.println("videoCodec = " + videoCodec);
}

public void start(String fileName) {
    EncodingParameters encodingParameters = new EncodingParameters(new File(fileName));
    encodingParameters.setBitrate(500000);
    encodingParameters.setFramerate(10);
    encodingParameters.setKeyFrameInterval(1);
    encodingParameters.setCodec(videoCodec);

    videoCapture.setEncodingParameters(encodingParameters);
    videoCapture.start();
    System.out.println("Recording started. Press 'Enter' to terminate.");

}

public void stop() throws IOException{
    System.in.read();
    videoCapture.stop();
}


public static void main(String[] args) throws Throwable {

    CaptureVideoFromWebCamera videoCapture = new CaptureVideoFromWebCamera();
    videoCapture.init();
    videoCapture.start("video1.wmv");
    videoCapture.stop();

    Thread.sleep(5000);

    videoCapture.start("viedo2.wmv");
    videoCapture.stop("");
}

I hope this helps, I don't have JxCapture's license (nor the web cam :)) to check this.

answered on Stack Overflow Jul 23, 2016 by MirMasej

User contributions licensed under CC BY-SA 3.0