Opened 4 years ago

Last modified 4 years ago

#8893 new enhancement

multiple calls to srt_startup/srt_cleanup within a process

Reported by: malcolmb Owned by:
Priority: normal Component: avformat
Version: git-master Keywords: srt
Cc: Blocked By:
Blocking: Reproduced by developer: no
Analyzed by developer: no

Description

For software using libavformat directly, allocating and deallocating a context that's using libsrt will cause srt_startup() and srt_cleanup() to be called. Unless you always have SRT context in existence, this will cause the srt startup/cleanup to actually execute multiple times within one process. It's reference counted so if you have one context in existence all the time then it's fine, the work won't be done multiple times in a single process.
As mentioned by a SRT developer here:
https://github.com/Haivision/srt/issues/928#issuecomment-550432768
This is not a recommended workflow (and it's untested).
It would be great if somehow (maybe via avformat_network_init(), avformat_network_deinit()), a calling app could avoid the actual extra startup/cleanups until the process is shutting down.

Change History (2)

comment:1 by Marton Balint, 4 years ago

IMHO SRT should be fixed instead to support srt_startup() srt_cleanup() cycles properly. What is the purpose of reference counting if srt_cleanup() can only be called at the very end of an application? It does not make a lot of sense.

Clearly it is not OK to call srt_startup() in any of the global init functions (which are by the way depreacated), because nobody wants the srt library to spawn a thread when it is not even known that if srt will be used at all.

Meanwhile you can call srt_startup() yourself from your application before doing any work. This will keep the reference counter above 0, so global state should never be destroyed.

comment:2 by malcolmb, 4 years ago

The purpose of reference counting is to avoid incorrect duplicate buildups or premature tear downs in the event that multiple usage points of a library are active within a larger library. Even if they fix the issue, a way to buildup/teardown only once per process is useful for performance considerations.

Agreed it should not be done in all cases, but potentially a flag, or another exposed function as a solution?

I don't see anything about the network init functions being deprecated. I see they are optional now, which is a nice design addition. I certainly would hope they don't get deprecated since for the same reason as above, I wouldn't want multiple buildup/teardowns of network libraries during a single process run.

Calling srt_startup() myself would only work if I was linking with a dynamic library of libsrt. Currently it's statically linked so the only way would be to expose a function through libavformat's API. Otherwise I'm not initializing the same resources that libavformat is using.
Besides, calling a library I'm not using directly to affect the behavior of a different library isn't good design.

I do think a general way for users of libavformat to say 'I'm using this feature, don't tear it down until I tell you to' is useful, as already exists with avformat_network_init()

Note: See TracTickets for help on using tickets.