shithub: openh264

Download patch

ref: e5e7013b73a1d07d86e10e1f44356a8fb35705b3
parent: ad9ca3824f621cf607e1c3f8d775779d14da5824
parent: 5eb18b101e590a572f478c1de764f3d31688f2aa
author: sijchen <sijchen@cisco.com>
date: Mon Feb 8 09:59:38 EST 2016

Merge pull request #2350 from sijchen/th00

[Common] Add sink to IWelsTask

--- a/codec/common/inc/WelsTask.h
+++ b/codec/common/inc/WelsTask.h
@@ -45,11 +45,22 @@
 
 namespace WelsCommon {
 
+class IWelsTaskSink {
+ public:
+  virtual int OnTaskExecuted() = 0;
+  virtual int OnTaskCancelled() = 0;
+};
+
 class IWelsTask {
  public:
+  IWelsTask (IWelsTaskSink* pSink) {
+    m_pSink = pSink;
+  };
   virtual ~IWelsTask() { }
 
   virtual int Execute() = 0;
+ private:
+  IWelsTaskSink*   m_pSink;
 };
 
 }
--- a/codec/common/src/WelsThreadPool.cpp
+++ b/codec/common/src/WelsThreadPool.cpp
@@ -133,7 +133,7 @@
 }
 
 void CWelsThreadPool::ExecuteTask() {
-  //WELS_INFO_TRACE("ThreadPool: schedule tasks");
+  //fprintf(stdout, "ThreadPool: schedule tasks\n");
   CWelsTaskThread* pThread = NULL;
   IWelsTask*    pTask = NULL;
   while (GetWaitedTaskNum() > 0) {
@@ -142,7 +142,7 @@
       break;
     }
     pTask = GetWaitedTask();
-    //WELS_INFO_TRACE("ThreadPool:  ExecuteTask = "<<(uint32_t)(pTask)<<" at thread = "<<(uint32_t)(pThread));
+    //fprintf(stdout, "ThreadPool:  ExecuteTask = %x at thread %x\n", pTask, pThread);
     pThread->SetTask (pTask);
   }
 }
@@ -150,12 +150,12 @@
 WELS_THREAD_ERROR_CODE CWelsThreadPool::QueueTask (IWelsTask* pTask) {
   CWelsAutoLock  cLock (m_cLockPool);
 
-  //WELS_INFO_TRACE("ThreadPool:  QueueTask = "<<(uint32_t)(pTask));
+  //fprintf(stdout, "ThreadPool:  QueueTask = %x\n", pTask);
   if (GetWaitedTaskNum() == 0) {
     CWelsTaskThread* pThread = GetIdleThread();
 
     if (pThread != NULL) {
-      //WELS_INFO_TRACE("ThreadPool:  ExecuteTask = "<<(uint32_t)(pTask));
+      //fprintf(stdout, "ThreadPool:  ExecuteTask = %x\n", pTask);
       pThread->SetTask (pTask);
 
       return WELS_THREAD_ERROR_OK;
@@ -163,7 +163,7 @@
   }
 
   AddTaskToWaitedList (pTask);
-
+  //fprintf(stdout, "ThreadPool:  AddTaskToWaitedList: %x\n", pTask);
   SignalThread();
   return WELS_THREAD_ERROR_OK;
 }
--- a/codec/encoder/core/inc/wels_task_base.h
+++ b/codec/encoder/core/inc/wels_task_base.h
@@ -60,7 +60,7 @@
     WELS_ENC_TASK_ALL = 3,
   };
 
-  CWelsBaseTask();
+  CWelsBaseTask (WelsCommon::IWelsTaskSink* pSink): IWelsTask (pSink) {};
   virtual ~CWelsBaseTask();
 
   virtual uint32_t GetTaskType() const = 0;
--- a/codec/encoder/core/inc/wels_task_encoder.h
+++ b/codec/encoder/core/inc/wels_task_encoder.h
@@ -53,7 +53,7 @@
 
 class CWelsSliceEncodingTask : public CWelsBaseTask {
  public:
-  CWelsSliceEncodingTask (sWelsEncCtx* pCtx, const int32_t iSliceIdx);
+  CWelsSliceEncodingTask (WelsCommon::IWelsTaskSink* pSink, sWelsEncCtx* pCtx, const int32_t iSliceIdx);
   virtual ~CWelsSliceEncodingTask();
 
   CWelsSliceEncodingTask* CreateSliceEncodingTask (sWelsEncCtx* pCtx, const int32_t iSliceIdx);
@@ -92,7 +92,7 @@
 
 class CWelsLoadBalancingSlicingEncodingTask : public CWelsSliceEncodingTask {
  public:
-  CWelsLoadBalancingSlicingEncodingTask (sWelsEncCtx* pCtx, const int32_t iSliceIdx) : CWelsSliceEncodingTask (pCtx,
+  CWelsLoadBalancingSlicingEncodingTask (WelsCommon::IWelsTaskSink* pSink, sWelsEncCtx* pCtx, const int32_t iSliceIdx) : CWelsSliceEncodingTask (pSink, pCtx,
         iSliceIdx) {
   };
 
@@ -109,8 +109,8 @@
 
 class CWelsConstrainedSizeSlicingEncodingTask : public CWelsLoadBalancingSlicingEncodingTask {
  public:
-  CWelsConstrainedSizeSlicingEncodingTask (sWelsEncCtx* pCtx,
-      const int32_t iSliceIdx) : CWelsLoadBalancingSlicingEncodingTask (pCtx, iSliceIdx) {
+  CWelsConstrainedSizeSlicingEncodingTask (WelsCommon::IWelsTaskSink* pSink, sWelsEncCtx* pCtx,
+      const int32_t iSliceIdx) : CWelsLoadBalancingSlicingEncodingTask (pSink, pCtx, iSliceIdx) {
   };
 
   virtual WelsErrorType ExecuteTask();
@@ -124,7 +124,7 @@
 
 class CWelsUpdateMbMapTask : public CWelsBaseTask {
  public:
-  CWelsUpdateMbMapTask (sWelsEncCtx* pCtx, const int32_t iSliceIdx);
+  CWelsUpdateMbMapTask (WelsCommon::IWelsTaskSink* pSink, sWelsEncCtx* pCtx, const int32_t iSliceIdx);
   virtual ~CWelsUpdateMbMapTask();
 
   virtual WelsErrorType Execute();
--- a/codec/encoder/core/inc/wels_task_management.h
+++ b/codec/encoder/core/inc/wels_task_management.h
@@ -56,13 +56,15 @@
   virtual void            Uninit() = 0;
 
   virtual void            InitFrame (const int32_t kiCurDid) {}
-  virtual WelsErrorType   ExecuteTasks(const CWelsBaseTask::ETaskType iTaskType = CWelsBaseTask::WELS_ENC_TASK_ENCODING) = 0;
+  virtual WelsErrorType   ExecuteTasks (const CWelsBaseTask::ETaskType iTaskType = CWelsBaseTask::WELS_ENC_TASK_ENCODING)
+    = 0;
 
   static IWelsTaskManage* CreateTaskManage (sWelsEncCtx* pCtx, const int32_t iSpatialLayer, const bool bNeedLock);
 };
 
 
-class  CWelsTaskManageBase : public IWelsTaskManage, public WelsCommon::IWelsThreadPoolSink {
+class  CWelsTaskManageBase : public IWelsTaskManage, public WelsCommon::IWelsThreadPoolSink,
+  public WelsCommon::IWelsTaskSink {
  public:
   typedef  CWelsCircleQueue<CWelsBaseTask>            TASKLIST_TYPE;
   //typedef  std::pair<int, int>                  SLICE_BOUNDARY_PAIR;
@@ -74,11 +76,19 @@
   virtual WelsErrorType  Init (sWelsEncCtx*   pEncCtx);
   virtual void           InitFrame (const int32_t kiCurDid = 0);
 
-  virtual WelsErrorType  ExecuteTasks(const CWelsBaseTask::ETaskType iTaskType = CWelsBaseTask::WELS_ENC_TASK_ENCODING);
+  virtual WelsErrorType  ExecuteTasks (const CWelsBaseTask::ETaskType iTaskType = CWelsBaseTask::WELS_ENC_TASK_ENCODING);
 
   //IWelsThreadPoolSink
   virtual WelsErrorType  OnTaskExecuted (WelsCommon::IWelsTask* pTask);
   virtual WelsErrorType  OnTaskCancelled (WelsCommon::IWelsTask* pTask);
+
+  //IWelsTaskSink
+  virtual int OnTaskExecuted() {
+    return 0;
+  };
+  virtual int OnTaskCancelled() {
+    return 0;
+  };
 
  protected:
   virtual WelsErrorType  CreateTasks (sWelsEncCtx* pEncCtx, const int32_t kiTaskCount);
--- a/codec/encoder/core/src/wels_task_base.cpp
+++ b/codec/encoder/core/src/wels_task_base.cpp
@@ -41,8 +41,6 @@
 
 namespace WelsEnc {
 
-CWelsBaseTask::CWelsBaseTask() {
-}
 
 CWelsBaseTask::~CWelsBaseTask() {
 }
--- a/codec/encoder/core/src/wels_task_encoder.cpp
+++ b/codec/encoder/core/src/wels_task_encoder.cpp
@@ -55,8 +55,8 @@
 
 namespace WelsEnc {
 
-CWelsSliceEncodingTask::CWelsSliceEncodingTask (sWelsEncCtx* pCtx,
-    const int32_t iSliceIdx) : m_eTaskResult (ENC_RETURN_SUCCESS) {
+CWelsSliceEncodingTask::CWelsSliceEncodingTask (WelsCommon::IWelsTaskSink* pSink, sWelsEncCtx* pCtx,
+    const int32_t iSliceIdx) : CWelsBaseTask(pSink), m_eTaskResult (ENC_RETURN_SUCCESS) {
   m_pCtx = pCtx;
   m_iSliceIdx = iSliceIdx;
 }
@@ -316,7 +316,7 @@
 }
 
 
-CWelsUpdateMbMapTask::CWelsUpdateMbMapTask (sWelsEncCtx* pCtx, const int32_t iSliceIdx) {
+CWelsUpdateMbMapTask::CWelsUpdateMbMapTask (WelsCommon::IWelsTaskSink* pSink, sWelsEncCtx* pCtx, const int32_t iSliceIdx): CWelsBaseTask(pSink) {
   m_pCtx = pCtx;
   m_iSliceIdx = iSliceIdx;
 }
--- a/codec/encoder/core/src/wels_task_management.cpp
+++ b/codec/encoder/core/src/wels_task_management.cpp
@@ -132,7 +132,7 @@
   }
 
   for (int idx = 0; idx < kiTaskCount; idx++) {
-    pTask = WELS_NEW_OP (CWelsUpdateMbMapTask (pEncCtx, idx), CWelsUpdateMbMapTask);
+    pTask = WELS_NEW_OP (CWelsUpdateMbMapTask (this, pEncCtx, idx), CWelsUpdateMbMapTask);
     WELS_VERIFY_RETURN_IF (ENC_RETURN_MEMALLOCERR, NULL == pTask)
     m_cPreEncodingTaskList[kiCurDid]->push_back (pTask);
   }
@@ -139,12 +139,12 @@
 
   for (int idx = 0; idx < kiTaskCount; idx++) {
     if (uiSliceMode==SM_SIZELIMITED_SLICE) {
-      pTask = WELS_NEW_OP (CWelsConstrainedSizeSlicingEncodingTask (pEncCtx, idx), CWelsConstrainedSizeSlicingEncodingTask);
+      pTask = WELS_NEW_OP (CWelsConstrainedSizeSlicingEncodingTask (this, pEncCtx, idx), CWelsConstrainedSizeSlicingEncodingTask);
     } else {
     if (pEncCtx->pSvcParam->bUseLoadBalancing) {
-      pTask = WELS_NEW_OP (CWelsLoadBalancingSlicingEncodingTask (pEncCtx, idx), CWelsLoadBalancingSlicingEncodingTask);
+      pTask = WELS_NEW_OP (CWelsLoadBalancingSlicingEncodingTask (this, pEncCtx, idx), CWelsLoadBalancingSlicingEncodingTask);
     } else {
-      pTask = WELS_NEW_OP (CWelsSliceEncodingTask (pEncCtx, idx), CWelsSliceEncodingTask);
+      pTask = WELS_NEW_OP (CWelsSliceEncodingTask (this, pEncCtx, idx), CWelsSliceEncodingTask);
     }
     }
     WELS_VERIFY_RETURN_IF (ENC_RETURN_MEMALLOCERR, NULL == pTask)
--- a/test/common/WelsThreadPoolTest.cpp
+++ b/test/common/WelsThreadPoolTest.cpp
@@ -16,7 +16,7 @@
  public:
   static uint32_t id;
 
-  CSimpleTask() {
+  CSimpleTask (WelsCommon::IWelsTaskSink* pSink) : IWelsTask (pSink) {
     m_uiID = id ++;
   }
 
@@ -38,18 +38,25 @@
 
 
 TEST (CThreadPoolTest, CThreadPoolTest) {
-  CSimpleTask tasks[TEST_TASK_NUM];
   CThreadPoolTest cThreadPoolTest;
+  CSimpleTask* aTasks[TEST_TASK_NUM];
   CWelsThreadPool  cThreadPool (&cThreadPoolTest);
 
   int32_t  i;
+  for (i = 0; i < TEST_TASK_NUM; i++) {
+    aTasks[i] = new CSimpleTask (&cThreadPoolTest);
+  }
 
   for (i = 0; i < TEST_TASK_NUM; i++) {
-    cThreadPool.QueueTask (&tasks[i]);
+    cThreadPool.QueueTask (aTasks[i]);
   }
 
   while (cThreadPoolTest.GetTaskCount() < TEST_TASK_NUM) {
     WelsSleep (1);
+  }
+
+  for (i = 0; i < TEST_TASK_NUM; i++) {
+    delete aTasks[i];
   }
 }
 
--- a/test/common/WelsThreadPoolTest.h
+++ b/test/common/WelsThreadPoolTest.h
@@ -6,7 +6,7 @@
 
 using namespace WelsCommon;
 
-class CThreadPoolTest : public IWelsThreadPoolSink {
+class CThreadPoolTest : public IWelsThreadPoolSink, public IWelsTaskSink {
  public:
   CThreadPoolTest() {
     m_iTaskCount = 0;
@@ -17,7 +17,7 @@
   virtual int32_t OnTaskExecuted (IWelsTask* pTask) {
     WelsCommon::CWelsAutoLock cAutoLock (m_cTaskCountLock);
     m_iTaskCount ++;
-    //printf("Task execute over count is %d\n", m_iTaskCount);
+    //fprintf(stdout, "Task execute over count is %d\n", m_iTaskCount);
     return cmResultSuccess;
   }
 
@@ -24,7 +24,21 @@
   virtual int32_t OnTaskCancelled (IWelsTask* pTask) {
     WelsCommon::CWelsAutoLock cAutoLock (m_cTaskCountLock);
     m_iTaskCount ++;
-    //printf("Task execute cancelled count is %d\n", m_iTaskCount);
+    //fprintf(stdout, "Task execute cancelled count is %d\n", m_iTaskCount);
+    return cmResultSuccess;
+  }
+
+  virtual int OnTaskExecuted() {
+    WelsCommon::CWelsAutoLock cAutoLock (m_cTaskCountLock);
+    m_iTaskCount ++;
+    //fprintf(stdout, "Task execute over count is %d\n", m_iTaskCount);
+    return cmResultSuccess;
+  }
+
+  virtual int OnTaskCancelled() {
+    WelsCommon::CWelsAutoLock cAutoLock (m_cTaskCountLock);
+    m_iTaskCount ++;
+    //fprintf(stdout, "Task execute cancelled count is %d\n", m_iTaskCount);
     return cmResultSuccess;
   }