-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathAampTrackWorker.hpp
More file actions
173 lines (150 loc) · 5.09 KB
/
AampTrackWorker.hpp
File metadata and controls
173 lines (150 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* If not stated otherwise in this file or this component's license file the
* following copyright and licenses apply:
*
* Copyright 2024 RDK Management
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef AAMP_TRACK_WORKER_HPP
#define AAMP_TRACK_WORKER_HPP
/**
* @file AampTrackWorker.hpp
* @brief Implementation of the AampTrackWorker class.
*
* This file contains the implementation of the AampTrackWorker class, which is responsible for
* managing a worker thread that processes jobs submitted to it. The worker thread waits for jobs
* to be submitted, processes them, and signals their completion.
*/
#include <thread>
#include <condition_variable>
#include <functional>
#include <vector>
#include <mutex>
#include <future>
#include <deque>
#include "AampUtils.h"
#include "AampLogManager.h"
#include "AampConfig.h"
#include "AampMediaType.h"
// Forward declaration to avoid recursive include
class PrivateInstanceAAMP;
namespace aamp
{
class AampTrackWorkerJob
{
public:
/**
* @brief Default constructor for AampTrackWorkerJob.
*/
AampTrackWorkerJob();
/**
* @brief Destructor for AampTrackWorkerJob.
*
* Cleans up resources used by the job.
*/
virtual ~AampTrackWorkerJob();
// Delete copy constructor and copy assignment operator
AampTrackWorkerJob(const AampTrackWorkerJob&) = delete;
AampTrackWorkerJob& operator=(const AampTrackWorkerJob&) = delete;
// Default move constructor and move assignment operator
AampTrackWorkerJob(AampTrackWorkerJob&&) = delete;
AampTrackWorkerJob& operator=(AampTrackWorkerJob&&) = delete;
/**
* @brief Called by the worker thread to run the job.
*/
void Run();
/**
* @brief Virtual Execute method to override in subclasses.
*/
virtual void Execute();
/**
* @brief Clones the job for worker pool.
*/
virtual std::unique_ptr<AampTrackWorkerJob> Clone() const;
/**
* @brief Cancels the job by setting the cancelled flag.
*/
void SetCancelled();
/**
* @brief Check if job has been cancelled.
*
* @return true if cancelled
*/
bool IsCancelled() const;
/**
* @brief Get a future to wait for job completion.
*/
std::shared_future<void> GetFuture() const;
private:
std::atomic<bool> mCancelled{false};
std::shared_future<void> mSharedFuture;
std::promise<void> mPromise;
};
/**
* @typedef AampTrackWorkerJobSharedPtr
* @typedef AampTrackWorkerJobUniquePtr
* @brief Represents a job to download a media fragment.
*
* The DownloadJob typedef encapsulates the job to download a media fragment.
**/
typedef std::shared_ptr<AampTrackWorkerJob> AampTrackWorkerJobSharedPtr;
typedef std::unique_ptr<AampTrackWorkerJob> AampTrackWorkerJobUniquePtr;
/**
* Forward declaration of AampTrackWorker to resolve unknown type error.
*/
class AampTrackWorker;
/**
* @typedef AampTrackWorkerWeakPtr
* @brief Represents a weak pointer to an AampTrackWorker instance.
*
* This typedef is used to avoid circular references between the worker and the job.
*/
typedef std::weak_ptr<AampTrackWorker> AampTrackWorkerWeakPtr;
/**
* @class AampTrackWorker
* @brief A class that manages a worker thread for processing jobs.
*
* The AampTrackWorker class creates a worker thread that waits for jobs to be submitted,
* processes them, and signals their completion. The class provides methods to submit jobs,
* wait for job completion, and clean up the worker thread.
*/
class AampTrackWorker : public std::enable_shared_from_this<AampTrackWorker>
{
public:
AampTrackWorker(PrivateInstanceAAMP *_aamp, AampMediaType _mediaType);
~AampTrackWorker();
std::shared_future<void> SubmitJob(AampTrackWorkerJobSharedPtr job, bool highPriority = false);
void Pause();
void Resume();
void ClearJobs();
void RescheduleActiveJob();
void StartWorker();
void StopWorker() noexcept;
bool IsStopped() const noexcept { return mStopped; }
AampMediaType GetMediaType() const noexcept { return mMediaType; }
protected:
AampMediaType mMediaType;
std::thread mWorkerThread;
std::mutex mQueueMutex; // Mutex to protect job queue and worker state
std::condition_variable mCondVar; // Condition variable to notify worker thread
std::deque<AampTrackWorkerJobSharedPtr> mJobQueue; // Job queue
PrivateInstanceAAMP *aamp;
bool mStopped; // Flag to indicate if the worker is stopped (protected by mQueueMutex)
bool mPaused; // Flag to pause the worker threads (protected by mQueueMutex)
private:
static void ProcessJob(AampTrackWorkerWeakPtr weakSelf);
AampTrackWorkerJobSharedPtr mActiveJob; // Active job being processed
};
} // namespace aamp
#endif // AAMP_TRACK_WORKER_HPP