[Libav-user] variable scope into callback av_log_set_callback and multi threading

Dan Egnor egnor at ofb.net
Mon Mar 14 17:13:11 EET 2022

On Mon, Mar 14, 2022, 2:35 AM Algarve Branqueira <branqueira at gmail.com>

> So my question is this,
> How can I in the callback method (for me wrapper_logger) retrieve the
> "jobj" variable (make it any variable of the main method) when the decoder
> or the encoder is in another thread?
> I hope I explained my problem well.

In libav as in many frameworks, logging is understood to be a global,
context free operation. Other than the implicit context of thread ID (which
as you note isn't as useful when more threads are created) there is nothing
to distinguish a log event made by one of your instances from a log event
made by another.

Your options as I know them (unless there's some secret inside libav I
don't know about):

1. Yes, use globals. Maybe have one singleton call from the Java side that
sets up the logging hook, and store its env and jobj value for the logging
callback. Within Java, you'll need to figure out what to do with those
callbacks, but I don't know your requirements there.

2. More complicated: Try to wrap thread creation in libav (I think there's
a hook?) to copy thread local logging context into every thread. I don't
really recommend this path.

3. Also more complicated: Store various env/jobj pairs for every active
object in some global structure on the C side, and try to figure out which
one to "dispatch" to. I don't recommend this path either.

I don't know the details of your application, but my advice would be to
understand that logging is a stream of information that comes globally
without attribution to any particular object or instance, and architect
accordingly. That's not very helpful, I'm sorry!

-- egnor

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://ffmpeg.org/pipermail/libav-user/attachments/20220314/f5300c5a/attachment.htm>

More information about the Libav-user mailing list