RDK Documentation (Open Sourced RDK Components)
AampScheduler.h
Go to the documentation of this file.
1 /*
2  * If not stated otherwise in this file or this component's license file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2020 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 
20 /**
21  * @file AampScheduler.h
22  * @brief Class to schedule commands for async execution
23  */
24 
25 #ifndef __AAMP_SCHEDULER_H__
26 #define __AAMP_SCHEDULER_H__
27 
28 //#define __UNIT_TESTING__
29 
30 #include <functional>
31 #include <mutex>
32 #include <condition_variable>
33 #include <deque>
34 #include <thread>
35 #include <utility>
36 #include "AampDefine.h"
37 #include "AampEvent.h"
38 #ifndef __UNIT_TESTING__
39 #include "AampConfig.h"
40 #include "AampLogManager.h"
41 #else
42 #include <unistd.h>
43 #define AAMPLOG_WARN(FORMAT, ...) { printf(FORMAT,##__VA_ARGS__);printf("\n");}
44 #define AAMPLOG_INFO(FORMAT, ...) { printf(FORMAT,##__VA_ARGS__);printf("\n");}
45 #define AAMPLOG_ERR(FORMAT, ...) { printf(FORMAT,##__VA_ARGS__);printf("\n");}
46 #define AAMPLOG_TRACE(FORMAT, ...) { printf(FORMAT,##__VA_ARGS__);printf("\n");}
47 #endif
48 #define AAMP_SCHEDULER_ID_MAX_VALUE INT_MAX // 10000
49 #define AAMP_SCHEDULER_ID_DEFAULT 1 //ID ranges from DEFAULT to MAX
50 
51 
52 typedef std::function<void (void *)> AsyncTask;
53 
54 /**
55  * @brief Async task operations
56  */
58 {
59  AsyncTask mTask;
60  void * mData;
61  int mId;
62  std::string mTaskName;
63 
64  AsyncTaskObj(AsyncTask task, void *data, std::string tskName="", int id = AAMP_TASK_ID_INVALID) :
65  mTask(task), mData(data), mId(id),mTaskName(tskName)
66  {
67  }
68 
69  AsyncTaskObj(const AsyncTaskObj &other) : mTask(other.mTask), mData(other.mData), mId(other.mId),mTaskName(other.mTaskName)
70  {
71  }
72 
73  AsyncTaskObj& operator=(const AsyncTaskObj& other)
74  {
75  mTask = other.mTask;
76  mData = other.mData;
77  mId = other.mId;
78  mTaskName = other.mTaskName;
79  return *this;
80  }
81 };
82 
83 /**
84  * @brief Scheduler class for asynchronous operations
85  */
87 {
88 public:
89  /**
90  * @fn AampScheduler
91  */
92  AampScheduler();
93 
94  AampScheduler(const AampScheduler&) = delete;
95  AampScheduler& operator=(const AampScheduler&) = delete;
96  /**
97  * @fn ~AampScheduler
98  */
99  virtual ~AampScheduler();
100 
101  /**
102  * @fn ScheduleTask
103  *
104  * @param[in] obj - object to be scheduled
105  * @return int - scheduled task id
106  */
107  int ScheduleTask(AsyncTaskObj obj);
108 
109  /**
110  * @fn RemoveAllTasks
111  *
112  * @return void
113  */
114  void RemoveAllTasks();
115 
116  /**
117  * @fn RemoveTask
118  *
119  * @param[in] id - ID of task to be removed
120  * @return bool true if removed, false otherwise
121  */
122  bool RemoveTask(int id);
123 
124  /**
125  * @fn StartScheduler
126  *
127  * @return void
128  */
129  void StartScheduler();
130 
131  /**
132  * @fn StopScheduler
133  *
134  * @return void
135  */
136  void StopScheduler();
137 
138  /**
139  * @fn SuspendScheduler
140  *
141  * @return void
142  */
143  void SuspendScheduler();
144 
145  /**
146  * @fn ResumeScheduler
147  *
148  * @return void
149  */
150  void ResumeScheduler();
151 
152  /**
153  * @fn EnableScheduleTask
154  *
155  * @return void
156  */
157  void EnableScheduleTask();
158  /**
159  * @fn SetState
160  *
161  * @return void
162  */
163  void SetState(PrivAAMPState sstate);
164  /**
165  * @brief Set the logger instance for the Scheduler
166  *
167  * @return void
168  */
169  void SetLogger(AampLogManager *logObj) { mLogObj = logObj;}
170 protected:
171 #ifndef __UNIT_TESTING__
172  AampLogManager *mLogObj;
173 #else
174  void *mLogObj;
175 #endif
176 
177  /**
178  * @fn ExecuteAsyncTask
179  *
180  * @return void
181  */
182  void ExecuteAsyncTask();
183 
184  std::deque<AsyncTaskObj> mTaskQueue; /**< Queue for storing scheduled tasks */
185  std::mutex mQMutex; /**< Mutex for accessing mTaskQueue */
186  std::condition_variable mQCond; /**< To notify when a task is queued in mTaskQueue */
187  bool mSchedulerRunning; /**< Flag denotes if scheduler thread is running */
188  std::thread mSchedulerThread; /**< Scheduler thread */
189  std::mutex mExMutex; /**< Execution mutex for synchronization */
190  std::unique_lock<std::mutex> mExLock; /**< Lock to be used by SuspendScheduler and ResumeScheduler */
191  int mNextTaskId; /**< counter that holds ID value of next task to be scheduled */
192  int mCurrentTaskId; /**< ID of current executed task */
193  bool mLockOut; /**< flag indicates if the queue is locked out or not */
194  PrivAAMPState mState; /**< Player State */
195 };
196 
197 #endif /* __AAMP_SCHEDULER_H__ */
AampLogManager.h
Log managed for Aamp.
AampScheduler::ScheduleTask
int ScheduleTask(AsyncTaskObj obj)
To schedule a task to be executed later.
Definition: AampScheduler.cpp:64
AampDefine.h
Macros for Aamp.
AampScheduler::mCurrentTaskId
int mCurrentTaskId
Definition: AampScheduler.h:192
AampScheduler::mSchedulerThread
std::thread mSchedulerThread
Definition: AampScheduler.h:188
AampScheduler::mLockOut
bool mLockOut
Definition: AampScheduler.h:193
AampScheduler::EnableScheduleTask
void EnableScheduleTask()
To enable scheduler to queue new tasks.
Definition: AampScheduler.cpp:245
AampScheduler::SetState
void SetState(PrivAAMPState sstate)
To player state to Scheduler.
Definition: AampScheduler.cpp:254
AampLogManager
AampLogManager Class.
Definition: AampLogManager.h:150
AampScheduler::AampScheduler
AampScheduler()
AampScheduler Constructor.
Definition: AampScheduler.cpp:30
AampScheduler::mExLock
std::unique_lock< std::mutex > mExLock
Definition: AampScheduler.h:190
AampScheduler::SetLogger
void SetLogger(AampLogManager *logObj)
Set the logger instance for the Scheduler.
Definition: AampScheduler.h:169
AampScheduler::StartScheduler
void StartScheduler()
To start scheduler thread.
Definition: AampScheduler.cpp:52
AampScheduler
Scheduler class for asynchronous operations.
Definition: AampScheduler.h:86
AsyncTaskObj
Async task operations.
Definition: AampScheduler.h:57
AampScheduler::~AampScheduler
virtual ~AampScheduler()
AampScheduler Destructor.
Definition: AampScheduler.cpp:41
AampScheduler::ResumeScheduler
void ResumeScheduler()
To release execution lock.
Definition: AampScheduler.cpp:208
AampScheduler::mSchedulerRunning
bool mSchedulerRunning
Definition: AampScheduler.h:187
AampScheduler::RemoveTask
bool RemoveTask(int id)
To remove a scheduled tasks with ID.
Definition: AampScheduler.cpp:218
AampScheduler::mQMutex
std::mutex mQMutex
Definition: AampScheduler.h:185
AampScheduler::mState
PrivAAMPState mState
Definition: AampScheduler.h:194
AampScheduler::StopScheduler
void StopScheduler()
To stop scheduler and associated resources.
Definition: AampScheduler.cpp:173
AampConfig.h
Configurations for AAMP.
AampScheduler::mQCond
std::condition_variable mQCond
Definition: AampScheduler.h:186
PrivAAMPState
PrivAAMPState
Mapping all required status codes based on JS player requirement. These requirements may be forced by...
Definition: AampEvent.h:156
AampScheduler::SuspendScheduler
void SuspendScheduler()
To acquire execution lock for synchronisation purposes.
Definition: AampScheduler.cpp:198
AampEvent.h
Events supported by the AAMP player.
AampScheduler::mNextTaskId
int mNextTaskId
Definition: AampScheduler.h:191
AampScheduler::mTaskQueue
std::deque< AsyncTaskObj > mTaskQueue
Definition: AampScheduler.h:184
AampScheduler::RemoveAllTasks
void RemoveAllTasks()
To remove all scheduled tasks and prevent further tasks from scheduling.
Definition: AampScheduler.cpp:156
AampScheduler::mExMutex
std::mutex mExMutex
Definition: AampScheduler.h:189
AampScheduler::ExecuteAsyncTask
void ExecuteAsyncTask()
Executes scheduled tasks - invoked by thread.
Definition: AampScheduler.cpp:102