AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
Renderer.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 ACSDKALERTS_RENDERER_RENDERER_H_
17 #define ACSDKALERTS_RENDERER_RENDERER_H_
18 
21 
34 
35 #include <chrono>
36 #include <condition_variable>
37 #include <memory>
38 #include <mutex>
39 #include <string>
40 #include <vector>
41 
42 namespace alexaClientSDK {
43 namespace acsdkAlerts {
44 
46 static const constexpr char* ALERTS_MEDIA_PLAYER_NAME = "AlertsMediaPlayer";
47 
48 namespace renderer {
49 
53 class Renderer
54  : public RendererInterface
58  , public std::enable_shared_from_this<Renderer> {
59 public:
70  static std::shared_ptr<Renderer> createAlertRenderer(
71  const std::shared_ptr<acsdkApplicationAudioPipelineFactoryInterfaces::ApplicationAudioPipelineFactoryInterface>&
72  audioPipelineFactory,
73  const std::shared_ptr<avsCommon::utils::metrics::MetricRecorderInterface>& metricRecorder,
74  const std::shared_ptr<acsdkShutdownManagerInterfaces::ShutdownNotifierInterface>& shutdownNotifier,
75  const std::shared_ptr<avsCommon::sdkInterfaces::InternetConnectionMonitorInterface>& internetConnectionMonitor);
76 
86  static std::shared_ptr<Renderer> create(
87  std::shared_ptr<avsCommon::utils::mediaPlayer::MediaPlayerInterface> mediaPlayer,
88  std::shared_ptr<avsCommon::utils::metrics::MetricRecorderInterface> metricRecorder = nullptr,
89  std::shared_ptr<avsCommon::sdkInterfaces::InternetConnectionMonitorInterface> internetConnectionMonitor =
90  nullptr);
91 
92  void start(
93  std::shared_ptr<RendererObserverInterface> observer,
94  std::function<std::pair<std::unique_ptr<std::istream>, const avsCommon::utils::MediaType>()> audioFactory,
95  bool volumeRampEnabled,
96  const std::vector<std::string>& urls = std::vector<std::string>(),
97  int loopCount = 0,
98  std::chrono::milliseconds loopPause = std::chrono::milliseconds{0},
99  bool startWithPause = false) override;
100 
101  void stop() override;
102 
103  void onFirstByteRead(SourceId sourceId, const avsCommon::utils::mediaPlayer::MediaPlayerState& state) override;
104 
105  void onPlaybackStarted(SourceId sourceId, const avsCommon::utils::mediaPlayer::MediaPlayerState& state) override;
106 
107  void onPlaybackStopped(SourceId sourceId, const avsCommon::utils::mediaPlayer::MediaPlayerState& state) override;
108 
110 
111  void onPlaybackError(
112  SourceId sourceId,
114  std::string error,
116 
117  void onConnectionStatusChanged(bool connected) override;
118 
119 private:
122 
129  Renderer(
130  std::shared_ptr<avsCommon::utils::mediaPlayer::MediaPlayerInterface> mediaPlayer,
131  std::shared_ptr<avsCommon::utils::metrics::MetricRecorderInterface> metricRecorder,
132  std::shared_ptr<avsCommon::sdkInterfaces::InternetConnectionMonitorInterface> internetConnectionMonitor);
133 
141 
159  void executeStart(
160  std::shared_ptr<RendererObserverInterface> observer,
161  std::function<std::pair<std::unique_ptr<std::istream>, const avsCommon::utils::MediaType>()> audioFactory,
162  bool volumeRampEnabled,
163  const std::vector<std::string>& urls,
164  int loopCount,
165  std::chrono::milliseconds loopPause,
166  bool startWithPause);
167 
171  void executeStop();
172 
178  void executeOnPlaybackStarted(SourceId sourceId);
179 
185  void executeOnPlaybackStopped(SourceId sourceId);
186 
192  void executeOnPlaybackFinished(SourceId sourceId);
193 
200  void executeOnPlaybackError(
201  SourceId sourceId,
203  const std::string& error);
204 
211  void notifyObserver(RendererObserverInterface::State state, const std::string& message = "");
212 
217  void resetSourceId();
218 
224  bool shouldPlayDefault();
225 
231  bool shouldMediaPlayerRepeat();
232 
238  bool shouldRenderNext();
239 
245  bool shouldPause();
246 
252  bool isLastSourceInLoop();
253 
259  bool isLastSource();
260 
268  bool pause(std::chrono::milliseconds duration);
269 
273  void play();
274 
280  avsCommon::utils::mediaPlayer::SourceConfig generateMediaConfiguration();
281 
293  bool renderNextAudioAsset(bool* pauseInterruptedOut = nullptr);
294 
301  void handlePlaybackError(const std::string& error);
302 
304 
307  void doShutdown() override;
309 
316 
319  std::shared_ptr<avsCommon::utils::mediaPlayer::MediaPlayerInterface> m_mediaPlayer;
320 
322  std::shared_ptr<avsCommon::utils::metrics::MetricRecorderInterface> m_metricRecorder;
323 
325  std::shared_ptr<RendererObserverInterface> m_observer;
326 
329  std::vector<std::string> m_urls;
330 
332  int m_numberOfStreamsRenderedThisLoop;
333 
335  int m_remainingLoopCount;
336 
338  int m_directiveLoopCount;
339 
341  std::chrono::milliseconds m_loopPause;
342 
344  bool m_shouldPauseBeforeRender;
345 
347  std::chrono::time_point<std::chrono::steady_clock> m_loopStartTime;
348 
351  std::function<std::pair<std::unique_ptr<std::istream>, const avsCommon::utils::MediaType>()> m_defaultAudioFactory;
352 
355  bool m_isStopping;
356 
358  bool m_isStartPending;
359 
361  std::condition_variable m_waitCondition;
362 
364  std::mutex m_waitMutex;
365 
367  SourceId m_currentSourceId;
368 
370  bool m_volumeRampEnabled;
371 
373 
381 
383  std::chrono::steady_clock::time_point m_renderStartTime;
384 
386  std::atomic_bool m_isNetworkConnected;
387 
389  std::shared_ptr<avsCommon::sdkInterfaces::InternetConnectionMonitorInterface> m_internetConnectionMonitor;
390 };
391 
392 } // namespace renderer
393 } // namespace acsdkAlerts
394 } // namespace alexaClientSDK
395 
396 #endif // ACSDKALERTS_RENDERER_RENDERER_H_
static const constexpr char * ALERTS_MEDIA_PLAYER_NAME
String to identify the alerts media player to render audio.
Definition: Renderer.h:46
void onPlaybackError(SourceId sourceId, const avsCommon::utils::mediaPlayer::ErrorType &type, std::string error, const avsCommon::utils::mediaPlayer::MediaPlayerState &state) override
void onFirstByteRead(SourceId sourceId, const avsCommon::utils::mediaPlayer::MediaPlayerState &state) override
uint64_t SourceId
A type that identifies which source is currently being operated on. This must be unique across all in...
Definition: MediaPlayerInterface.h:69
static std::shared_ptr< Renderer > create(std::shared_ptr< avsCommon::utils::mediaPlayer::MediaPlayerInterface > mediaPlayer, std::shared_ptr< avsCommon::utils::metrics::MetricRecorderInterface > metricRecorder=nullptr, std::shared_ptr< avsCommon::sdkInterfaces::InternetConnectionMonitorInterface > internetConnectionMonitor=nullptr)
::std::string string
Definition: gtest-port.h:1097
Single-thread executor implementation.
Definition: Executor.h:45
MediaType
Definition: MediaType.h:29
static std::shared_ptr< avsCommon::utils::metrics::MetricRecorderInterface > metricRecorder
Metric recorder shared ptr.
Definition: BaseAPLCapabilityAgentTest.cpp:261
MediaPlayerInterface::SourceId SourceId
A type that identifies which source is currently being operated on.
Definition: MediaPlayerObserverInterface.h:49
void onPlaybackFinished(SourceId sourceId, const avsCommon::utils::mediaPlayer::MediaPlayerState &state) override
static std::shared_ptr< Renderer > createAlertRenderer(const std::shared_ptr< acsdkApplicationAudioPipelineFactoryInterfaces::ApplicationAudioPipelineFactoryInterface > &audioPipelineFactory, const std::shared_ptr< avsCommon::utils::metrics::MetricRecorderInterface > &metricRecorder, const std::shared_ptr< acsdkShutdownManagerInterfaces::ShutdownNotifierInterface > &shutdownNotifier, const std::shared_ptr< avsCommon::sdkInterfaces::InternetConnectionMonitorInterface > &internetConnectionMonitor)
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
void start(std::shared_ptr< RendererObserverInterface > observer, std::function< std::pair< std::unique_ptr< std::istream >, const avsCommon::utils::MediaType >()> audioFactory, bool volumeRampEnabled, const std::vector< std::string > &urls=std::vector< std::string >(), int loopCount=0, std::chrono::milliseconds loopPause=std::chrono::milliseconds{0}, bool startWithPause=false) override
void onConnectionStatusChanged(bool connected) override
type
Definition: upload.py:443
void onPlaybackStarted(SourceId sourceId, const avsCommon::utils::mediaPlayer::MediaPlayerState &state) override
Definition: InternetConnectionObserverInterface.h:26
ErrorType
Identifies the specific type of error in a PlaybackFailed event.
Definition: ErrorTypes.h:28
void onPlaybackStopped(SourceId sourceId, const avsCommon::utils::mediaPlayer::MediaPlayerState &state) override
tuple message
Definition: gtest_output_test.py:331

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