msdk: Fix increasing memory usage in dynamic pipelines
authorNirbheek Chauhan <nirbheek@centricular.com>
Fri, 17 Jan 2020 05:37:47 +0000 (11:07 +0530)
committerHaihao Xiang <haihao.xiang@intel.com>
Tue, 21 Jan 2020 00:38:41 +0000 (00:38 +0000)
commit1bcf44bb1f38ca71709f2f6439f558aac1c9692b
treef570e10f4eefb1a6f2c779e6eeb7584af73dc57e
parent6834a12112e33761fbf3a4fa58410a2bfc92b83c
msdk: Fix increasing memory usage in dynamic pipelines

Our context is non-persistent, and we propagate it throughout the
pipeline. This means that if we try to reuse any gstmsdk element by
removing it from the pipeline and then re-adding it, we'll clone the
mfxSession and create a new gstmsdk context as a child of the old one
inside `gst_msdk_context_new_with_parent()`.

Normally this only allocates a few KB inside the driver, but on
Windows it seems to allocate tens of MBs which leads to linearly
increasing memory usage for each PLAYING->NULL->PLAYING state cycle
for the process. The contexts will only be freed when the pipeline
itself goes to `NULL`, which would defeat the purpose of dynamic
pipelines.

Essentially, we need to optimize the case in which the element is
removed from the pipeline and re-added and the same context is re-set
on it. To detect that case, we set the context on `old_context`, and
compare it to the new one when preparing the context. If they're the
same, we don't need to do anything.

Fixes https://gitlab.freedesktop.org/gstreamer/gst-plugins-bad/issues/946
sys/msdk/gstmsdkcontextutil.c
sys/msdk/gstmsdkdec.c
sys/msdk/gstmsdkdec.h
sys/msdk/gstmsdkenc.c
sys/msdk/gstmsdkenc.h
sys/msdk/gstmsdkvpp.c
sys/msdk/gstmsdkvpp.h