FFmpeg plugin: Add a few checks to be safe

Make sure it locks the write mutex before freeing the packets, and put
the detach code in the main thread loop rather than off in a separate
function for clarity