fix streams not being ended correctly
When closing a screencast session, the closed
signal is emitted too late, this MR addresses that issue.
Tested in Firefox and OBS using window & screen sharing, ending the recording now always either freezes the stream, or disables it (depending on app).
(cherry picked from commit eb95725e)