AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
AplClientBridge.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_LIBIPCSERVERSAMPLEAPP_INCLUDE_IPCSERVERSAMPLEAPP_ALEXAPRESENTATION_APLCLIENTBRIDGE_H_
17 #define ALEXA_CLIENT_SDK_LIBIPCSERVERSAMPLEAPP_INCLUDE_IPCSERVERSAMPLEAPP_ALEXAPRESENTATION_APLCLIENTBRIDGE_H_
18 
23 
24 // Disable warnings from APLClient
25 #pragma GCC diagnostic push
26 #pragma GCC diagnostic ignored "-Wsign-compare"
27 #pragma GCC diagnostic ignored "-Wunused-variable"
28 #include <APLClient/AplClientBinding.h>
29 #include <APLClient/AplRenderingEvent.h>
30 #include <APLClient/AplRenderingEventObserver.h>
31 #include <APLClient/Extensions/AplCoreExtensionInterface.h>
32 #include <APLClient/Extensions/AudioPlayer/AplAudioPlayerExtension.h>
33 #include <APLClient/Extensions/AudioPlayer/AplAudioPlayerExtensionObserverInterface.h>
34 #include <APLClient/Extensions/Backstack/AplBackstackExtension.h>
35 #include <APLClient/Extensions/Backstack/AplBackstackExtensionObserver.h>
36 #pragma GCC diagnostic pop
47 
48 namespace alexaClientSDK {
49 namespace sampleApplications {
50 namespace ipcServerSampleApp {
51 
56  // Maximum number of concurrent downloads allowed.
58 };
59 
61  : public APLClient::AplOptionsInterface
62  , public APLViewhostInterface
67  , public APLClient::Extensions::Backstack::AplBackstackExtensionObserverInterface
68  , public APLClient::Extensions::AudioPlayer::AplAudioPlayerExtensionObserverInterface
71  , public std::enable_shared_from_this<AplClientBridge> {
72 public:
73  static std::shared_ptr<AplClientBridge> create(
74  std::shared_ptr<CachingDownloadManager> contentDownloadManager,
75  std::shared_ptr<gui::GUIClientInterface> guiClient,
76  AplClientBridgeParameter parameters);
77 
80  void doShutdown() override;
82 
85  void sendMessage(const std::string& token, const std::string& payload) override;
86 
87  void resetViewhost(const std::string& token) override;
88 
89  std::string downloadResource(const std::string& source) override;
90 
91  std::chrono::milliseconds getTimezoneOffset() override;
92 
93  void onActivityStarted(const std::string& token, const std::string& source) override;
94 
95  void onActivityEnded(const std::string& token, const std::string& source) override;
96 
97  void onSendEvent(const std::string& token, const std::string& event) override;
98 
99  void onCommandExecutionComplete(
100  const std::string& token,
101  APLClient::AplCommandExecutionEvent event,
102  const std::string& message) override;
103 
104  void onRenderDocumentComplete(const std::string& token, bool result, const std::string& error) override;
105 
106  void onVisualContextAvailable(const std::string& token, unsigned int stateRequestToken, const std::string& context)
107  override;
108 
109  void onSetDocumentIdleTimeout(const std::string& token, const std::chrono::milliseconds& timeout) override;
110 
111  void onRenderingEvent(const std::string& token, APLClient::AplRenderingEvent event) override;
112 
113  void onFinish(const std::string& token) override;
114 
115  void onDataSourceFetchRequestEvent(const std::string& token, const std::string& type, const std::string& payload)
116  override;
117 
118  void onRuntimeErrorEvent(const std::string& token, const std::string& payload) override;
119 
120  void onExtensionEvent(
121  const std::string& aplToken,
122  const std::string& uri,
123  const std::string& name,
124  const std::string& source,
125  const std::string& params,
126  unsigned int event,
127  std::shared_ptr<APLClient::Extensions::AplCoreExtensionEventCallbackResultInterface> resultCallback) override;
128 
129  void logMessage(APLClient::LogLevel level, const std::string& source, const std::string& message) override;
130 
131  int getMaxNumberOfConcurrentDownloads() override;
132 
134 
137  void onConnectionOpened() override;
138 
139  void onConnectionClosed() override;
141 
144  void onPlayerActivityChanged(avsCommon::avs::PlayerActivity state, const Context& context) override;
146 
149  void renderTemplateCard(const std::string& jsonPayload) override;
150  void renderPlayerInfoCard(
151  const std::string& jsonPayload,
153  void clearRenderTemplateCard() override;
154  void clearPlayerInfoCard() override;
156 
159  void onLoginStateProvided(
160  const std::string& playerId,
162 
163  void onPlaybackStateProvided(
164  const std::string& playerId,
167 
170  void onRestoreDocumentState(std::shared_ptr<APLClient::AplDocumentState> documentState) override;
172 
175  void onAudioPlayerPlay() override;
176 
177  void onAudioPlayerPause() override;
178 
179  void onAudioPlayerNext() override;
180 
181  void onAudioPlayerPrevious() override;
182 
183  void onAudioPlayerSeekToPosition(int offsetInMilliseconds) override;
184 
185  void onAudioPlayerToggle(const std::string& name, bool checked) override;
186 
187  void onAudioPlayerSkipForward() override;
188 
189  void onAudioPlayerSkipBackward() override;
190 
191  void onAudioPlayerLyricDataFlushed(
192  const std::string& token,
193  long durationInMilliseconds,
194  const std::string& lyricData) override;
196 
197  void onUpdateTimer();
198 
199  void setGUIManager(std::shared_ptr<gui::GUIServerInterface> guiManager);
200 
203  void renderDocument(
204  const aplCapabilityCommonInterfaces::PresentationSession& presentationSession,
205  const std::string& token,
206  const std::string& document,
207  const std::string& datasources,
208  const std::string& supportedViewports,
209  const std::string& windowId) override;
210 
211  void clearDocument(const std::string& token) override;
212 
213  void executeCommands(const std::string& jsonPayload, const std::string& token) override;
214 
215  void interruptCommandSequence(const std::string& token) override;
216 
217  void dataSourceUpdate(const std::string& sourceType, const std::string& jsonPayload, const std::string& token)
218  override;
219 
220  void addObserver(const APLViewhostObserverInterfacePtr& observer) override;
221 
222  void removeObserver(const APLViewhostObserverInterfacePtr& observer) override;
223 
224  void onRenderDirectiveReceived(const std::string& token, const std::chrono::steady_clock::time_point& receiveTime)
225  override;
226 
227  std::string getMaxAPLVersion() const override;
228 
229  void setMetricRecorder(std::shared_ptr<avsCommon::utils::metrics::MetricRecorderInterface> metricRecorder) override;
230 
231  void provideState(const std::string& aplToken, const unsigned int stateRequestToken) override;
232 
233  bool handleBack(const std::string& windowId) override;
235 
238  void initializeRenderersRequest(const std::string& message) override;
239  void metricsReport(const std::string& message) override;
240  void viewhostEvent(const std::string& message) override;
241  void renderCompleted(const std::string& message) override;
242  void renderDocumentRequest(const std::string& message) override;
243  void executeCommandsRequest(const std::string& message) override;
244  void clearDocumentRequest(const std::string& message) override;
246 
247  void onMessage(const std::string& windowId, const std::string& message);
248 
249  void handleRenderingEvent(const std::string& token, APLClient::AplRenderingEvent event);
250 
251  void handleDisplayMetrics(const std::string& windowId, const std::string& jsonPayload);
252 
258  void initializeRenderer(const std::string& windowId, const std::set<std::string>& supportedExtensions);
259 
265  void registerSharedExtension(const std::shared_ptr<APLClient::Extensions::AplCoreExtensionInterface>& extension);
266 
274  std::shared_ptr<APLClient::AplClientRenderer> getAplClientRendererFromAplToken(const std::string& aplToken);
275 
283  std::shared_ptr<APLClient::AplClientRenderer> getAplClientRendererFromWindowId(const std::string& windowId);
284 
289  void setMediaProperties(std::shared_ptr<avsCommon::sdkInterfaces::MediaPropertiesInterface> mediaProperties);
290 
294  void initialize();
295 
296 private:
298  std::shared_ptr<CachingDownloadManager> contentDownloadManager,
299  std::shared_ptr<gui::GUIClientInterface> guiClient,
300  AplClientBridgeParameter parameters);
301 
307  void setTokenToWindow(const std::string& token, const std::string& windowId);
308 
315  static std::shared_ptr<APLClient::Extensions::Backstack::AplBackstackExtension> getBackExtensionForRenderer(
316  const std::shared_ptr<APLClient::AplClientRenderer>& aplClientRenderer);
317 
319  template <typename... Args>
320  using APLViewhostObserverFunc = void (APLViewhostObserverInterface::*)(Args...);
321 
329  template <typename... Args1, typename... Args2>
330  void executeNotifyObservers(APLViewhostObserverFunc<Args1...> func, Args2&&... args);
331 
337  void executeOnPresentationSessionChanged(
338  const std::string& windowId,
339  const aplCapabilityCommonInterfaces::PresentationSession& presentationSession);
340 
342  class TokenWindowBiMap {
343  public:
349  void insert(const std::string& token, const std::string& window) {
350  m_tokenToWindow[token] = window;
351  m_windowToToken[window].insert(token);
352  }
353 
358  void eraseToken(const std::string& token) {
359  auto tokenToWindowIt = m_tokenToWindow.find(token);
360  if (tokenToWindowIt == m_tokenToWindow.end()) {
361  return;
362  }
363 
364  auto windowId = tokenToWindowIt->second;
365  m_tokenToWindow.erase(tokenToWindowIt);
366  m_windowToToken[windowId].erase(token);
367  }
368 
373  void eraseWindow(const std::string& window) {
374  for (auto& token : m_windowToToken[window]) {
375  m_tokenToWindow.erase(token);
376  }
377  m_windowToToken.erase(window);
378  }
379 
385  std::string getWindowByToken(const std::string& token) {
386  auto tokenToWindowIt = m_tokenToWindow.find(token);
387  if (tokenToWindowIt == m_tokenToWindow.end()) {
388  return "";
389  }
390 
391  return tokenToWindowIt->second;
392  }
393 
394  private:
396  std::unordered_map<std::string, std::string> m_tokenToWindow;
397 
399  std::unordered_map<std::string, std::unordered_set<std::string>> m_windowToToken;
400  };
401 
403  std::shared_ptr<CachingDownloadManager> m_contentDownloadManager;
404 
406  avsCommon::utils::timing::Timer m_updateTimer;
407 
409  std::unique_ptr<APLClient::AplClientBinding> m_aplClientBinding;
410 
412  std::shared_ptr<gui::GUIServerInterface> m_guiManager;
413 
415  std::shared_ptr<gui::GUIClientInterface> m_guiClient;
416 
418  std::shared_ptr<ipc::APLClientHandler> m_aplClientHandler;
419 
421  std::atomic_bool m_renderQueued;
422 
424  AplClientBridgeParameter m_parameters;
425 
427  std::vector<std::shared_ptr<APLClient::Extensions::AudioPlayer::AplAudioPlayerExtension>> m_audioPlayerExtensions;
428 
430  avsCommon::avs::PlayerActivity m_playerActivityState;
431 
433  std::unordered_map<std::string, std::shared_ptr<APLClient::AplClientRenderer>> m_aplClientRendererMap;
434 
436  std::unordered_map<std::string, aplCapabilityCommonInterfaces::PresentationSession> m_windowIdToPresentationSession;
437 
439  std::unordered_map<std::string, std::shared_ptr<APLClient::Extensions::AplCoreExtensionInterface>>
440  m_sharedRegisteredExtensions;
441 
443  std::vector<std::weak_ptr<APLViewhostObserverInterface>> m_observers;
444 
446  TokenWindowBiMap m_windowTokenMapping;
447 
450 
452  std::shared_ptr<avsCommon::sdkInterfaces::MediaPropertiesInterface> m_mediaProperties;
453 };
454 
455 } // namespace ipcServerSampleApp
456 } // namespace sampleApplications
457 } // namespace alexaClientSDK
458 
459 #endif // ALEXA_CLIENT_SDK_LIBIPCSERVERSAMPLEAPP_INCLUDE_IPCSERVERSAMPLEAPP_ALEXAPRESENTATION_APLCLIENTBRIDGE_H_
def args
Definition: android_test.py:111
std::shared_ptr< APLViewhostObserverInterface > APLViewhostObserverInterfacePtr
Definition: APLViewhostObserverInterface.h:155
Definition: ExternalMediaPlayerObserverInterface.h:29
::std::string string
Definition: gtest-port.h:1097
Single-thread executor implementation.
Definition: Executor.h:45
The context of the AudioPlayer when the observer is notified of the PlayerActivity state change...
Definition: AudioPlayerObserverInterface.h:39
internal::ArgsMatcher< InnerMatcher > Args(const InnerMatcher &matcher)
Definition: gmock-generated-matchers.h:481
static std::shared_ptr< avsCommon::utils::metrics::MetricRecorderInterface > metricRecorder
Metric recorder shared ptr.
Definition: BaseAPLCapabilityAgentTest.cpp:261
Timer to schedule task for delayed and periodic execution.
Definition: Timer.h:39
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
type
Definition: upload.py:443
PlayerActivity
Identifies the player state.
Definition: PlayerActivity.h:27
tuple message
Definition: gtest_output_test.py:331
Definition: TemplateRuntimePresentationAdapterObserverInterface.h:30

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