AlexaClientSDK  1.26.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
TemplateRuntime.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 ALEXA_CLIENT_SDK_CAPABILITYAGENTS_TEMPLATERUNTIME_INCLUDE_TEMPLATERUNTIME_TEMPLATERUNTIME_H_
17 #define ALEXA_CLIENT_SDK_CAPABILITYAGENTS_TEMPLATERUNTIME_INCLUDE_TEMPLATERUNTIME_TEMPLATERUNTIME_H_
18 
19 #include <chrono>
20 #include <deque>
21 #include <memory>
22 #include <string>
23 #include <unordered_map>
24 #include <unordered_set>
25 
38 
39 namespace alexaClientSDK {
40 namespace capabilityAgents {
41 namespace templateRuntime {
42 
62  , public std::enable_shared_from_this<TemplateRuntime> {
63 public:
72  static std::shared_ptr<TemplateRuntime> createTemplateRuntime(
73  const std::shared_ptr<avsCommon::sdkInterfaces::RenderPlayerInfoCardsProviderRegistrarInterface>&
74  renderPlayerInfoCardsInterfaces,
75  std::shared_ptr<avsCommon::sdkInterfaces::FocusManagerInterface> focusManager,
76  std::shared_ptr<avsCommon::sdkInterfaces::ExceptionEncounteredSenderInterface> exceptionSender);
77 
86  static std::shared_ptr<TemplateRuntime> create(
87  const std::unordered_set<std::shared_ptr<avsCommon::sdkInterfaces::RenderPlayerInfoCardsProviderInterface>>&
88  renderPlayerInfoCardsInterfaces,
89  std::shared_ptr<avsCommon::sdkInterfaces::FocusManagerInterface> focusManager,
90  std::shared_ptr<avsCommon::sdkInterfaces::ExceptionEncounteredSenderInterface> exceptionSender);
91 
95  virtual ~TemplateRuntime() = default;
96 
99  void handleDirectiveImmediately(std::shared_ptr<avsCommon::avs::AVSDirective> directive) override;
100  void preHandleDirective(std::shared_ptr<DirectiveInfo> info) override;
101  void handleDirective(std::shared_ptr<DirectiveInfo> info) override;
102  void cancelDirective(std::shared_ptr<DirectiveInfo> info) override;
105 
110 
113  void onRenderPlayerCardsInfoChanged(avsCommon::avs::PlayerActivity state, const Context& context) override;
115 
121 
128  void addObserver(std::shared_ptr<avsCommon::sdkInterfaces::TemplateRuntimeObserverInterface> observer);
129 
136  void removeObserver(std::shared_ptr<avsCommon::sdkInterfaces::TemplateRuntimeObserverInterface> observer);
137 
142  void displayCardCleared();
143 
146  std::unordered_set<std::shared_ptr<avsCommon::avs::CapabilityConfiguration>> getCapabilityConfigurations() override;
148 
156  std::shared_ptr<avsCommon::sdkInterfaces::RenderPlayerInfoCardsProviderInterface> cardsProvider);
157 
158 private:
162  enum class State {
164  IDLE,
165 
166  /*
167  * The @c TemplateRuntime has received a displayCard event is acquiring the visual channel from @c
168  * FocusManager.
169  */
170  ACQUIRING,
171 
172  /*
173  * The @c TemplateRuntime has focus, either background or foreground, of the channel and has
174  * notified its observers of a displayCard. @TemplateRuntime will remain in this state until there is a
175  * timeout, clearCard, or focusChanged(NONE) event.
176  */
177  DISPLAYING,
178 
179  /*
180  * The @c TemplateRuntime has received a timeout or a clearCard event and is releasing the
181  * channel and has notified its observers to clear the display.
182  */
183  RELEASING,
184 
185  /*
186  * The @c TemplateRuntime has received a displayCard event during releasing of the channel and is trying to
187  * acquire the visual channel again.
188  */
189  REACQUIRING
190  };
191 
195  struct AudioItemPair {
199  AudioItemPair() = default;
200 
207  AudioItemPair(
208  std::string itemId,
209  std::shared_ptr<alexaClientSDK::avsCommon::avs::CapabilityAgent::DirectiveInfo> renderPlayerInfoDirective) :
210  audioItemId{itemId},
211  directive{renderPlayerInfoDirective} {};
212 
214  std::string audioItemId;
215 
217  std::shared_ptr<alexaClientSDK::avsCommon::avs::CapabilityAgent::DirectiveInfo> directive;
218  };
219 
228  const std::unordered_set<std::shared_ptr<avsCommon::sdkInterfaces::RenderPlayerInfoCardsProviderInterface>>&
229  renderPlayerInfoCardsInterfaces,
230  std::shared_ptr<avsCommon::sdkInterfaces::FocusManagerInterface> focusManager,
231  std::shared_ptr<avsCommon::sdkInterfaces::ExceptionEncounteredSenderInterface> exceptionSender);
232 
233  // @name RequiresShutdown Functions
235  void doShutdown() override;
237 
241  bool initialize();
242 
248  void removeDirective(std::shared_ptr<DirectiveInfo> info);
249 
255  void setHandlingCompleted(std::shared_ptr<DirectiveInfo> info);
256 
262  void handleRenderTemplateDirective(std::shared_ptr<DirectiveInfo> info);
263 
269  void handleRenderPlayerInfoDirective(std::shared_ptr<DirectiveInfo> info);
270 
276  void handleUnknownDirective(std::shared_ptr<DirectiveInfo> info);
277 
286  void executeAudioPlayerInfoUpdates(avsCommon::avs::PlayerActivity state, const Context& context);
287 
294  void executeAudioPlayerStartTimer(avsCommon::avs::PlayerActivity state);
295 
300  void executeRenderPlayerInfoCallbacks(bool isClearCard);
301 
306  void executeRenderTemplateCallbacks(bool isClearCard);
307 
312  void executeDisplayCard();
313 
318  void executeClearCard();
319 
325  void executeStartTimer(std::chrono::milliseconds timeout);
326 
330  void executeStopTimer();
331 
335  std::string stateToString(const TemplateRuntime::State state);
336 
340  void executeTimerEvent();
341 
345  void executeOnFocusChangedEvent(avsCommon::avs::FocusState newFocus);
346 
350  void executeDisplayCardEvent(
351  const std::shared_ptr<alexaClientSDK::avsCommon::avs::CapabilityAgent::DirectiveInfo> info);
352 
356  void executeCardClearedEvent();
357 
359  avsCommon::utils::timing::Timer m_clearDisplayTimer;
360 
367  std::unordered_set<std::shared_ptr<avsCommon::sdkInterfaces::TemplateRuntimeObserverInterface>> m_observers;
370 
371  /*
372  * This is a map that is used to store the current executing @c AudioItem based on the callbacks from the
373  * @c RenderPlayerInfoCardsProviderInterface.
374  */
375  std::unordered_map<std::shared_ptr<avsCommon::sdkInterfaces::MediaPropertiesInterface>, AudioItemPair>
376  m_audioItemsInExecution;
377 
379  std::shared_ptr<avsCommon::sdkInterfaces::MediaPropertiesInterface> m_activeRenderPlayerInfoCardsProvider;
380 
381  /*
382  * This queue is for storing the @c RenderPlayerInfo directives when its audioItemId does not match the audioItemId
383  * in execution in the @c AudioPlayer.
384  */
385  std::deque<AudioItemPair> m_audioItems;
386 
388  std::unordered_map<
389  std::shared_ptr<avsCommon::sdkInterfaces::MediaPropertiesInterface>,
391  m_audioPlayerInfo;
392 
394  std::shared_ptr<alexaClientSDK::avsCommon::avs::CapabilityAgent::DirectiveInfo> m_lastDisplayedDirective;
395 
397  bool m_isRenderTemplateLastReceived;
398 
401 
403  State m_state;
405 
406  /*
407  * This is a set of interfaces to the @c RenderPlayerInfoCardsProviderInterface. The @c TemplateRuntime CA
408  * used this interface to add and remove itself as an observer.
409  */
410  std::unordered_set<std::shared_ptr<avsCommon::sdkInterfaces::RenderPlayerInfoCardsProviderInterface>>
411  m_renderPlayerInfoCardsInterfaces;
412 
414  std::shared_ptr<avsCommon::sdkInterfaces::FocusManagerInterface> m_focusManager;
415 
417  std::unordered_set<std::shared_ptr<avsCommon::avs::CapabilityConfiguration>> m_capabilityConfigurations;
418 
421 
423  std::chrono::milliseconds m_ttsFinishedTimeout;
424 
426  std::chrono::milliseconds m_audioPlaybackFinishedTimeout;
427 
429  std::chrono::milliseconds m_audioPlaybackStoppedPausedTimeout;
430 };
431 
432 } // namespace templateRuntime
433 } // namespace capabilityAgents
434 } // namespace alexaClientSDK
435 
436 #endif // ALEXA_CLIENT_SDK_CAPABILITYAGENTS_TEMPLATERUNTIME_INCLUDE_TEMPLATERUNTIME_TEMPLATERUNTIME_H_
Timer.h
alexaClientSDK::avsCommon::avs::FocusState
FocusState
Definition: FocusState.h:29
alexaClientSDK::avsCommon::utils::RequiresShutdown
Definition: RequiresShutdown.h:32
FocusManagerInterface.h
alexaClientSDK::capabilityAgents::templateRuntime::TemplateRuntime::onFocusChanged
void onFocusChanged(avsCommon::avs::FocusState newFocus, avsCommon::avs::MixingBehavior behavior) override
Definition: TemplateRuntime.cpp:216
alexaClientSDK::capabilityAgents::templateRuntime::TemplateRuntime::preHandleDirective
void preHandleDirective(std::shared_ptr< DirectiveInfo > info) override
Definition: TemplateRuntime.cpp:182
alexaClientSDK::capabilityAgents::templateRuntime::TemplateRuntime::cancelDirective
void cancelDirective(std::shared_ptr< DirectiveInfo > info) override
Definition: TemplateRuntime.cpp:202
alexaClientSDK::avsCommon::utils::timing::Timer
Definition: Timer.h:39
alexaClientSDK::avsCommon::avs::PlayerActivity
PlayerActivity
Identifies the player state.
Definition: PlayerActivity.h:27
alexaClientSDK::avsCommon::sdkInterfaces::CapabilityConfigurationInterface
Definition: CapabilityConfigurationInterface.h:32
alexaClientSDK::capabilityAgents::templateRuntime::TemplateRuntime::getCapabilityConfigurations
std::unordered_set< std::shared_ptr< avsCommon::avs::CapabilityConfiguration > > getCapabilityConfigurations() override
Definition: TemplateRuntime.cpp:801
RenderPlayerInfoCardsProviderInterface.h
alexaClientSDK::avsCommon::utils::threading::Executor
Definition: Executor.h:40
alexaClientSDK::capabilityAgents::templateRuntime::TemplateRuntime::removeObserver
void removeObserver(std::shared_ptr< avsCommon::sdkInterfaces::TemplateRuntimeObserverInterface > observer)
Definition: TemplateRuntime.cpp:260
alexaClientSDK::capabilityAgents::templateRuntime::TemplateRuntime::getConfiguration
avsCommon::avs::DirectiveHandlerConfiguration getConfiguration() const override
Definition: TemplateRuntime.cpp:206
alexaClientSDK::capabilityAgents::templateRuntime::TemplateRuntime::displayCardCleared
void displayCardCleared()
Definition: TemplateRuntime.cpp:322
alexaClientSDK::capabilityAgents::templateRuntime::TemplateRuntime::addRenderPlayerInfoCardsProvider
void addRenderPlayerInfoCardsProvider(std::shared_ptr< avsCommon::sdkInterfaces::RenderPlayerInfoCardsProviderInterface > cardsProvider)
Definition: TemplateRuntime.cpp:805
alexaClientSDK::capabilityAgents::templateRuntime::TemplateRuntime::create
static std::shared_ptr< TemplateRuntime > create(const std::unordered_set< std::shared_ptr< avsCommon::sdkInterfaces::RenderPlayerInfoCardsProviderInterface >> &renderPlayerInfoCardsInterfaces, std::shared_ptr< avsCommon::sdkInterfaces::FocusManagerInterface > focusManager, std::shared_ptr< avsCommon::sdkInterfaces::ExceptionEncounteredSenderInterface > exceptionSender)
Definition: TemplateRuntime.cpp:121
RenderPlayerInfoCardsProviderRegistrarInterface.h
RequiresShutdown.h
alexaClientSDK::avsCommon::avs::PlayerActivity::IDLE
@ IDLE
Initial state, prior to acting on the first Play directive.
Executor.h
alexaClientSDK::capabilityAgents::templateRuntime::TemplateRuntime::addObserver
void addObserver(std::shared_ptr< avsCommon::sdkInterfaces::TemplateRuntimeObserverInterface > observer)
Definition: TemplateRuntime.cpp:245
CapabilityAgent.h
RenderPlayerInfoCardsObserverInterface.h
alexaClientSDK
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
alexaClientSDK::avsCommon::sdkInterfaces::RenderPlayerInfoCardsObserverInterface::Context
The context of the RenderPlayerInfoCards from the RenderPlayerInfoCards provider.
Definition: RenderPlayerInfoCardsObserverInterface.h:42
alexaClientSDK::avsCommon::avs::DirectiveHandlerConfiguration
std::unordered_map< directiveRoutingRule::DirectiveRoutingRule, BlockingPolicy > DirectiveHandlerConfiguration
Definition: DirectiveHandlerConfiguration.h:32
alexaClientSDK::capabilityAgents::templateRuntime::TemplateRuntime
Definition: TemplateRuntime.h:56
alexaClientSDK::avsCommon::sdkInterfaces::RenderPlayerInfoCardsObserverInterface
Definition: RenderPlayerInfoCardsObserverInterface.h:34
DialogUXStateObserverInterface.h
alexaClientSDK::avsCommon::avs::CapabilityAgent
Definition: CapabilityAgent.h:47
alexaClientSDK::avsCommon::sdkInterfaces::TemplateRuntimeObserverInterface::AudioPlayerInfo
Definition: TemplateRuntimeObserverInterface.h:40
alexaClientSDK::capabilityAgents::templateRuntime::TemplateRuntime::~TemplateRuntime
virtual ~TemplateRuntime()=default
alexaClientSDK::capabilityAgents::templateRuntime::TemplateRuntime::onDialogUXStateChanged
void onDialogUXStateChanged(avsCommon::sdkInterfaces::DialogUXStateObserverInterface::DialogUXState newState) override
Definition: TemplateRuntime.cpp:228
alexaClientSDK::capabilityAgents::templateRuntime::TemplateRuntime::createTemplateRuntime
static std::shared_ptr< TemplateRuntime > createTemplateRuntime(const std::shared_ptr< avsCommon::sdkInterfaces::RenderPlayerInfoCardsProviderRegistrarInterface > &renderPlayerInfoCardsInterfaces, std::shared_ptr< avsCommon::sdkInterfaces::FocusManagerInterface > focusManager, std::shared_ptr< avsCommon::sdkInterfaces::ExceptionEncounteredSenderInterface > exceptionSender)
Definition: TemplateRuntime.cpp:104
alexaClientSDK::capabilityAgents::templateRuntime::TemplateRuntime::handleDirective
void handleDirective(std::shared_ptr< DirectiveInfo > info) override
Definition: TemplateRuntime.cpp:187
alexaClientSDK::avsCommon::sdkInterfaces::DialogUXStateObserverInterface
A dialog specific UX state observer.
Definition: DialogUXStateObserverInterface.h:29
TemplateRuntimeObserverInterface.h
CapabilityConfiguration.h
alexaClientSDK::avsCommon::avs::MixingBehavior
MixingBehavior
Definition: MixingBehavior.h:25
CapabilityConfigurationInterface.h
alexaClientSDK::capabilityAgents::templateRuntime::TemplateRuntime::onRenderPlayerCardsInfoChanged
void onRenderPlayerCardsInfoChanged(avsCommon::avs::PlayerActivity state, const Context &context) override
Definition: TemplateRuntime.cpp:220
alexaClientSDK::avsCommon::sdkInterfaces::DialogUXStateObserverInterface::DialogUXState
DialogUXState
The different dialog specific AVS UX states.
Definition: DialogUXStateObserverInterface.h:32
alexaClientSDK::capabilityAgents::templateRuntime::TemplateRuntime::handleDirectiveImmediately
void handleDirectiveImmediately(std::shared_ptr< avsCommon::avs::AVSDirective > directive) override
Definition: TemplateRuntime.cpp:177

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