/* * Copyright (c) Hisilicon Technologies Co., Ltd. 2019-2020. All rights reserved. * Description: NxMediaMsg class * Author: NxPlayer software group * Create: 2019-07-15 */ #ifndef NXMESSAGE_H #define NXMESSAGE_H #include #include #include #include #include "NxLooper.h" namespace android { const int NX_MSG_EXIT = 0xFFFE; class NxMediaPlayerManage; const int POLL_WAKE = -1; const int POLL_CALLBACK = -2; struct NxMessage { NxMessage() : mMsg(0), mPara1(0), mPara2(0), mBuffer(nullptr), mDataLen(0) { } void Init(int msgid, int ext1, int ext2, const Parcel &obj) { mMsg = msgid; mPara1 = ext1; mPara2 = ext2; if (obj.data() && obj.dataSize() > 0) { mBuffer = new (std::nothrow) unsigned char[obj.dataSize()]; /* memory will be freed after send event */ if (mBuffer != nullptr) { memcpy_s(mBuffer, obj.dataSize(), obj.data(), obj.dataSize()); mDataLen = obj.dataSize(); } } else { mBuffer = nullptr; mDataLen = 0; } } /* The message type. (interpretation is left up to the handler) */ int mMsg; int mPara1; int mPara2; unsigned char *mBuffer; int mDataLen; }; class NxMessageHandler : public virtual RefBase { public: virtual void HandleMessage(NxMessage &message) = 0; protected: ~NxMessageHandler() override; }; struct NxMessagePackage { NxMessagePackage(): handler(nullptr) { } NxMessagePackage(const sp h, const NxMessage &m) : handler(h), message(m) { } sp handler; NxMessage message; }; class NxMediaMsg : public Thread { friend class NxMediaMsgHandle; public: explicit NxMediaMsg(int idx); ~NxMediaMsg() override; status_t EnableNxMediaMsg(bool enable); status_t PostMessage(const sp &handler, NxMessage &msg); private: void MessageProcess(); bool threadLoop() override; void Init(); int ReadMsg(int timeoutMs); int Flush(int timeoutMs); int ProcessMessagePackages(int result); private: sp m_looper; int m_isExit; bool m_enableNxMediaMsg; int m_instIdx; /* instance number */ Mutex m_lock; Vector mMsgPackages; }; class NxMediaMsgHandle : public NxMessageHandler { public: NxMediaMsgHandle(sp &msg, NxMediaPlayerManage *player, int idx); ~NxMediaMsgHandle() override; void SetCbPlayer(NxMediaPlayerManage *player); void HandleMessage(NxMessage &message) override; private: sp m_nxMsg; NxMediaPlayerManage *m_hmplayer; Mutex m_cbLock; int m_instIdx; /* instance number */ }; }; #endif