Simplify EncodeAndSendFrame function and fix exit