Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PointerScope not work #1425

Closed
lixiangflyin opened this issue May 11, 2020 · 9 comments
Closed

PointerScope not work #1425

lixiangflyin opened this issue May 11, 2020 · 9 comments

Comments

@lixiangflyin
Copy link

lixiangflyin commented May 11, 2020

I use javacv to sement audio and operation with muti thread.
Offen OOM. AND use pointerScope, but still OOM.

try (PointerScope scope = new PointerScope()) {
    FFmpegLogCallback.set();
    CustomFFmpegFrameGrabber customFFmpegFrameGrabber = new 
   CustomFFmpegFrameGrabber(rtmpUrl, 3));
    customFFmpegFrameGrabber.getFFmpegFrameGrabber().subscribeBlockCallBack(this::onTaskInterrupt);
    ByteArrayOutputStream outputStream = null;
    CustomFFmpegFrameRecorder recorder = null;
    try {
        if (!canConnected(customFFmpegFrameGrabber)) {
            return;
        }

        outputStream = new ByteArrayOutputStream();
        recorder = getFrameRecorder(outputStream, customFFmpegFrameGrabber.getFFmpegFrameGrabber());
        recorder.startUnsafe(customFFmpegFrameGrabber.getFFmpegFrameGrabber().getFormatContext());
       

       while (true) {
          AVPacket frame = customFFmpegFrameGrabber.getFFmpegFrameGrabber().grabPacket();
          //if null and continue
          if (frame == null) {
            break;
          }

        recorder.recordPacket(frame);

        ThreadUtils.sleep(0);
    }

    } catch (FrameGrabber.Exception e) {
        
    } catch (PgFFmpegFrameRecorder.Exception e) {
        log.error("recorder exception ", e);
    } catch (Exception e) {
        log.error("other error ", e);
    } finally {
        //close recorder
        if (recorder != null) {
            stopRecord(recorder);
        }
        //close grabber
        if (customFFmpegFrameGrabber.getFFmpegFrameGrabber() != null) {
            try {
                customFFmpegFrameGrabber.getFFmpegFrameGrabber().releaseUnsafe();
            } catch (FrameGrabber.Exception e) {
                log.error("grabber shutdown error ", e);
            }
        }
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (Exception e) {
                log.error("output stream close error", e);
            }
        }
    }

}

@saudet
Copy link
Member

saudet commented May 11, 2020

FFmpegFrameGrabber and FFmpegFrameRecorder are not thread safe. Do not try to use them from multiple threads.

@lixiangflyin
Copy link
Author

but I must operation many streams at the same time.

If i use thread-safe, programs will block.

@saudet
Copy link
Member

saudet commented May 11, 2020

Ok, but please reproduce the issue with a single stream.

@lixiangflyin
Copy link
Author

Can you detail? Share your idea? I have not understand.

@saudet
Copy link
Member

saudet commented May 11, 2020

If it doesn't happen with a single stream, then the problem is with your threads. So, please make sure your issue happens with a single stream.

@lixiangflyin
Copy link
Author

ok, I try

@saudet
Copy link
Member

saudet commented Jun 12, 2020

I've added synchronized and PointerScope to the methods of FFmpegFrameGrabber and FFmpegFrameRecorder in commits cbe7ccd and 28b90ef. That should solve the problems you're having so please give it a try with the snapshots: http://bytedeco.org/builds/

@saudet
Copy link
Member

saudet commented Jun 12, 2020

Duplicate of #1434

@saudet saudet marked this as a duplicate of #1434 Jun 12, 2020
@saudet
Copy link
Member

saudet commented Jun 12, 2020

Duplicate of #1383

@saudet saudet marked this as a duplicate of #1383 Jun 12, 2020
@saudet saudet closed this as completed Jun 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants