INDExOS / media-for-mobile

Media for Mobile
Other
456 stars 177 forks source link

Capturing failed. Cannot stop capture thread. #41

Open polaris0227 opened 8 years ago

polaris0227 commented 8 years ago

Hi, I've got an issue when finishing record. Sample app also has this issue. Sometimes it works good, but sometimes app freeze about 10 seconds and says "Capturing failed. Cannot stop capture thread." message when finish record, and after that recording doesn't work even restart app. I've found some code in CapturePipeline.java as below:

public void stop() {
    if (!started) {
        return;
    }
    try {
        **//!!!!!DANGER!!!!
        //ask someone for help before removing that stop call, i have to revert it 3 times already
        pipeline.stop();
        //!!!!!DANGER!!!!**

        notifyOnStop();

        synchronized (untillDone){
            untillDone.wait(10 * 1000);
        }

        //stopRequested = true;
        pools.shutdownNow();
        if (!pools.awaitTermination(10, TimeUnit.SECONDS)) {
            notifyOnError(new Exception("Cannot stop capture thread"));
        }

    } catch (Exception e) {
        notifyOnError(e);
    }
    //destroy everithing
    audioEncoder = null;
    videoEncoder = null;

    started = false;
}

I think there is problem at pipeline.stop() in "!!!DANGER!!!" Hope someone help me to resolve this problem. Regards.

jhognon commented 8 years ago

Hi, I just discovered this project and encountered the same problem.

After quickly investigating the issue, I think I found a race condition in CaptureSource.java code. When the capture is stopped in the main thread, stop() is called and Command.EndOfFile is added to the command queue. Meanwhile, getFrame() function may be called from another thread, and if stop() was previously called, the command queue is cleared and I think there is a chance that Command.EndOfFile may be never processed. I tried to comment the line that clears the command queue and it solved the problem in my case.

But I didn't have time to investigate further and see what are the real consequences of commenting this line (it may create a bug elsewhere).

It would be nice if someone with a good knowledge of this code reviews this.

Regards, Julien

deshan commented 7 years ago

I faced the same problem. Any solution?

vtproduction commented 7 years ago

Same problem here, any help ???

deshan commented 7 years ago

@vtproduction jhognon 's solution worked for me

suntabu commented 7 years ago

@jhognon your solution works for me, thank you!!!

jhognon commented 7 years ago

Glad it helped :)

sjvc commented 6 years ago

I opened a new issue related to this one: https://github.com/INDExOS/media-for-mobile/issues/69

Hope it helps