How to determine if a buffer in AppSink is a key frame

I want to check if the buffer in AppSink is a key frame. I am using the code below for this purpose. Is this approach correct, or should I use a different method?

gboolean is_key_frame = !GST_BUFFER_FLAG_IS_SET(buffer, GST_BUFFER_FLAG_DELTA_UNIT);

It looks correct in principle, but it will depend on the media format (caps) and previous elements whether that will be set reliably or not.

For example, for raw video the decoder won’t necessarily signal which decoded frames came from an encoded key frame and which did not.