AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
ProgressTimer.h
Go to the documentation of this file.
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0/
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 #ifndef ACSDKAUDIOPLAYER_PROGRESSTIMER_H_
17 #define ACSDKAUDIOPLAYER_PROGRESSTIMER_H_
18 
19 #include <chrono>
20 #include <condition_variable>
21 #include <cstdint>
22 #include <memory>
23 #include <mutex>
24 #include <thread>
25 
26 namespace alexaClientSDK {
27 namespace acsdkAudioPlayer {
28 
34 public:
41  public:
45  virtual void requestProgress() = 0;
46 
50  virtual void onProgressReportDelayElapsed() = 0;
51 
55  virtual void onProgressReportIntervalElapsed() = 0;
56 
60  virtual void onProgressReportIntervalUpdated() = 0;
61 
65  virtual ~ContextInterface() = default;
66  };
67 
69  static std::chrono::milliseconds getNoDelay() {
70  return std::chrono::milliseconds::max();
71  }
72 
74  static std::chrono::milliseconds getNoInterval() {
75  return std::chrono::milliseconds::max();
76  }
77 
81  ProgressTimer();
82 
86  virtual ~ProgressTimer();
87 
103  void init(
104  const std::shared_ptr<ContextInterface>& context,
105  std::chrono::milliseconds delay,
106  std::chrono::milliseconds interval,
107  std::chrono::milliseconds offset = std::chrono::milliseconds::zero());
108 
114  void start();
115 
121  void pause();
122 
128  void resume();
129 
135  void stop();
136 
142  void updateInterval(const std::chrono::milliseconds& newInterval);
143 
149  void onProgress(std::chrono::milliseconds progress);
150 
151 private:
155  enum class State {
157  IDLE,
159  INITIALIZED,
161  RUNNING,
163  PAUSED,
165  STOPPING,
166  };
167 
169  friend std::ostream& operator<<(std::ostream& stream, ProgressTimer::State state);
170 
177  bool setState(State newState);
178 
182  void startThread();
183 
187  void mainLoop();
188 
195  bool updateTargetLocked();
196 
200  void pauseLocked();
201 
205  void resumeLocked();
206 
208  std::mutex m_callMutex;
209 
211  std::mutex m_stateMutex;
212 
214  State m_state;
215 
217  std::shared_ptr<ContextInterface> m_context;
218 
220  std::chrono::milliseconds m_delay;
221 
223  std::chrono::milliseconds m_interval;
224 
226  std::chrono::milliseconds m_offset;
227 
229  std::chrono::milliseconds m_target;
230 
232  bool m_gotProgress;
233 
235  std::chrono::milliseconds m_progress;
236 
238  std::condition_variable m_wake;
239 
241  std::thread m_thread;
242 };
243 
244 } // namespace acsdkAudioPlayer
245 } // namespace alexaClientSDK
246 
247 #endif // ACSDKAUDIOPLAYER_PROGRESSTIMER_H_
void init(const std::shared_ptr< ContextInterface > &context, std::chrono::milliseconds delay, std::chrono::milliseconds interval, std::chrono::milliseconds offset=std::chrono::milliseconds::zero())
friend std::ostream & operator<<(std::ostream &stream, ProgressTimer::State state)
Friend declaration to allow streaming State values.
void updateInterval(const std::chrono::milliseconds &newInterval)
void onProgress(std::chrono::milliseconds progress)
static std::chrono::milliseconds getNoInterval()
Static function member to get interval value for no ProgressReportIntervalElapsed notifications...
Definition: ProgressTimer.h:74
static std::chrono::milliseconds getNoDelay()
Static function member to get delay value for no ProgressReportDelayElapsed notifications..
Definition: ProgressTimer.h:69
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36

AlexaClientSDK 3.0.0 - Copyright 2016-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0