AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
GUIClient.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_GUI_GUICLIENT_H_
17 #define ALEXA_CLIENT_SDK_LIBIPCSERVERSAMPLEAPP_INCLUDE_IPCSERVERSAMPLEAPP_GUI_GUICLIENT_H_
18 
19 #include <thread>
20 #include <atomic>
21 #include <condition_variable>
22 #include <memory>
23 #include <mutex>
24 #include <thread>
25 #include <string>
26 
27 #include <rapidjson/document.h>
28 
34 #ifdef ENABLE_COMMS
36 #endif
48 
65 
66 namespace alexaClientSDK {
67 namespace sampleApplications {
68 namespace ipcServerSampleApp {
69 namespace gui {
74 class GUIClient
84  , public std::enable_shared_from_this<GUIClient>
90 public:
100  static std::shared_ptr<GUIClient> create(
101  std::shared_ptr<MessagingServerInterface> serverImplementation,
102  const std::shared_ptr<registrationManager::CustomerDataManagerInterface> customerDataManager,
103  const std::shared_ptr<ipc::IPCVersionManager> ipcVersionManager,
104  std::shared_ptr<avsCommon::utils::DeviceInfo> deviceInfo);
105 
108  void setGUIManager(std::shared_ptr<gui::GUIServerInterface> guiManager) override;
109  bool acquireFocus(
110  std::string avsInterface,
111  std::string channelName,
112  avsCommon::avs::ContentType contentType,
113  std::shared_ptr<avsCommon::sdkInterfaces::ChannelObserverInterface> channelObserver) override;
114  bool releaseFocus(
115  std::string avsInterface,
116  std::string channelName,
117  std::shared_ptr<avsCommon::sdkInterfaces::ChannelObserverInterface> channelObserver) override;
118 #ifdef ENABLE_COMMS
119  void sendCallStateInfo(
121 #endif
123  bool finalizeClientInitialization(bool errorState) override;
124  void handleInitializeAPLRenderers(rapidjson::Document& payload) override;
125  void handleDisplayMetrics(rapidjson::Document& payload) override;
126  void handleAplEvent(rapidjson::Document& payload) override;
127  void handleRenderComplete(rapidjson::Document& payload) override;
128  void handleLocalRenderDocument(rapidjson::Document& payload) override;
129  void handleLocalExecuteCommands(rapidjson::Document& payload) override;
130  void handleLocalClearDocument(rapidjson::Document& payload) override;
132  void focusAcquireRequest(
133  const GUIToken token,
134  const std::string& avsInterface,
135  const std::string& channelName,
136  avsCommon::avs::ContentType contentType) override;
137  void focusReleaseRequest(const GUIToken token, const std::string& avsInterface, const std::string& channelName)
138  override;
139  void setLocales(const std::string& localeStr) override;
140  std::shared_ptr<ipc::IPCRouter> getIPCRouter() override;
142 
145  bool start() override;
146  void writeMessage(const std::string& payload) override;
147  void setMessageListener(std::shared_ptr<MessageListenerInterface> messageListener) override;
148  void stop() override;
149  bool isReady() override;
150  void setObserver(const std::shared_ptr<communication::MessagingServerObserverInterface>& observer) override;
152 
155  void onConnectionOpened() override;
156  void onConnectionClosed() override;
158 
161  void onMessage(const std::string& jsonMessage) override;
163 
166  void onAuthStateChange(AuthObserverInterface::State newState, AuthObserverInterface::Error newError) override;
168 
174  const std::vector<avsCommon::sdkInterfaces::endpoints::EndpointIdentifier>& addedOrUpdatedEndpoints,
175  const std::vector<avsCommon::sdkInterfaces::endpoints::EndpointIdentifier>& deletedEndpoints) override;
177 
180  void clearData() override;
182 
185  void notifyAuthorizationRequest(const std::string& url, const std::string& code) override;
188 
191  void notifyAlexaState(const std::string& state) override;
193 
200 
203  void onLogout() override;
205 
208  bool dispatch(const std::string& message) override;
210 
213  void acquireChannelRequest(const std::string& message) override;
214  void releaseChannelRequest(const std::string& message) override;
215  void focusChangedReport(const std::string& message) override;
217 
220  void authorizationStateRequest(const std::string& message) override;
221  void alexaStateRequest(const std::string& message) override;
222  void authorizationInfoRequest(const std::string& message) override;
223  void localesRequest(const std::string& message) override;
225 
231  void setAplClientBridge(std::shared_ptr<AplClientBridge> aplClientBridge, bool aplVersionChanged);
232 
238  const std::shared_ptr<APLRuntimePresentationAdapter>& aplRuntimePresentationAdapter);
239 
241  void addToMediaPlayerMap(
242  const std::string& name,
243  const std::shared_ptr<avsCommon::utils::mediaPlayer::MediaPlayerInterface>& mediaPlayer);
244 
246  void initIPCRouter();
247 
255  void sendCompleteAuthorizationMessage(const std::string& url, const std::string& code, const std::string& clientId);
256 
262  void sendSetAlexaStateMessage(const std::string& state);
263 
270 
278  void sendSetLocalesMessage(const std::string& localeStr);
279 
280 private:
281  // @name RequiresShutdown Functions
283  void doShutdown() override;
285 
289  class ProxyFocusObserver : public avsCommon::sdkInterfaces::ChannelObserverInterface {
290  public:
299  ProxyFocusObserver(
300  std::string avsInterface,
301  GUIToken token,
302  std::shared_ptr<GUIClient> focusBridge,
303  std::string channelName);
304 
307  void onFocusChanged(avsCommon::avs::FocusState newFocus, avsCommon::avs::MixingBehavior behavior) override;
309 
310  private:
312  const std::string m_avsInterface;
313 
315  GUIToken m_token;
316 
318  std::shared_ptr<GUIClient> m_focusBridge;
319 
321  const std::string m_channelName;
322  };
323 
327  GUIClient(
328  std::shared_ptr<MessagingServerInterface> serverImplementation,
329  const std::shared_ptr<registrationManager::CustomerDataManagerInterface>& customerDataManager,
330  std::shared_ptr<ipc::IPCVersionManager> ipcVersionManager,
331  std::shared_ptr<avsCommon::utils::DeviceInfo> deviceInfo);
332 
334  void serverThread();
335 
337  void sendInitRequestAndWait();
338 
342 #ifdef ENABLE_COMMS
343  void executeSendCallStateInfo(
345 #endif
346 
353  void executeSendFocusResponse(const GUIToken token, const bool result);
354 
363  void startAutoreleaseTimer(const std::string& avsInterface, const GUIToken token, const std::string& channelName);
364 
372  void autoRelease(const std::string& avsInterface, const GUIToken token, const std::string& channelName);
373 
380  void sendOnFocusChanged(const GUIToken token, const avsCommon::avs::FocusState state);
381 
386  void executeSendMessage(messages::MessageInterface& message);
387 
393  void executeWriteMessage(const std::string& payload);
394 
402  bool executeAcquireFocus(
403  std::string avsInterface,
404  std::string channelName,
405  avsCommon::avs::ContentType contentType,
406  std::shared_ptr<avsCommon::sdkInterfaces::ChannelObserverInterface> channelObserver);
407 
414  bool executeReleaseFocus(
415  std::string avsInterface,
416  std::string channelName,
417  std::shared_ptr<avsCommon::sdkInterfaces::ChannelObserverInterface> channelObserver);
418 
422  void registerNamespaces();
423 
428  void executeHandleAcceptCall(rapidjson::Document& payload);
429 
434  void executeHandleStopCall(rapidjson::Document& payload);
435 
440  void executeHandleEnableLocalVideo(rapidjson::Document& payload);
441 
446  void executeHandleDisableLocalVideo(rapidjson::Document& payload);
447 
448 #ifdef ENABLE_COMMS
449 
453  void executeHandleSendDtmf(rapidjson::Document& payload);
454 #endif
455 
461  void reportInvalidWindowIdRuntimeError(const std::string& errorMsg, const std::string& aplToken);
462 
463  // The GUI manager implementation.
464  std::shared_ptr<gui::GUIServerInterface> m_guiManager;
465 
468 
469  // The server implementation.
470  std::shared_ptr<MessagingServerInterface> m_serverImplementation;
471 
473  std::thread m_serverThread;
474 
475  // The thread used for init messages.
476  std::thread m_initThread;
477 
479  std::mutex m_mutex;
480 
482  std::condition_variable m_cond;
483 
485  std::atomic_bool m_hasServerStarted;
486 
488  std::atomic_bool m_initMessageReceived;
489 
491  std::atomic_bool m_errorState;
492 
494  std::shared_ptr<MessageListenerInterface> m_messageListener;
495 
497  std::atomic_bool m_shouldRestart;
498 
500  std::shared_ptr<communication::MessagingServerObserverInterface> m_observer;
501 
503  std::shared_ptr<AplClientBridge> m_aplClientBridge;
504 
506  std::shared_ptr<ipc::IPCVersionManager> m_ipcVersionManager;
507 
510  std::atomic_bool m_limitedInteraction;
511 
513  std::map<std::string, std::function<void(rapidjson::Document&)>> m_messageHandlers;
514 
516  std::mutex m_mapMutex;
517 
519  std::map<GUIToken, std::shared_ptr<avsCommon::sdkInterfaces::ChannelObserverInterface>> m_focusObservers;
520 
522  std::map<GUIToken, std::shared_ptr<avsCommon::utils::timing::Timer>> m_autoReleaseTimers;
523 
525  std::shared_ptr<ipc::AudioFocusManagerHandler> m_audioFocusManagerIPCHandler;
526 
528  std::shared_ptr<ipc::SystemHandler> m_systemIPCHandler;
529 
531  std::map<std::string, std::shared_ptr<avsCommon::utils::mediaPlayer::MediaPlayerInterface>> m_mediaPlayerMap;
532 
534  std::shared_ptr<APLRuntimePresentationAdapter> m_aplRuntimePresentationAdapter;
535 
537  std::shared_ptr<ipc::IPCRouter> m_ipcRouter;
538 
540  std::shared_ptr<IPCAPLAgent> m_ipcAPLAgent;
541 
543  std::shared_ptr<avsCommon::utils::DeviceInfo> m_deviceInfo;
544 
546  std::string m_alexaState;
547 
549  std::string m_authState;
550 
552  std::string m_authUrl;
553 
555  std::string m_authCode;
556 
558  std::string m_localeStr;
559 
561  std::string m_clientId;
562 };
563 
564 } // namespace gui
565 } // namespace ipcServerSampleApp
566 } // namespace sampleApplications
567 } // namespace alexaClientSDK
568 
569 #endif // ALEXA_CLIENT_SDK_LIBIPCSERVERSAMPLEAPP_INCLUDE_IPCSERVERSAMPLEAPP_GUI_GUICLIENT_H_
MixingBehavior
Definition: MixingBehavior.h:25
void focusReleaseRequest(const GUIToken token, const std::string &avsInterface, const std::string &channelName) override
Internal function to execute.
void notifyAuthorizationStateChange(avsCommon::sdkInterfaces::AuthObserverInterface::State state) override
void handleDisplayMetrics(rapidjson::Document &payload) override
void handleLocalClearDocument(rapidjson::Document &payload) override
void focusChangedReport(const std::string &message) override
bool acquireFocus(std::string avsInterface, std::string channelName, avsCommon::avs::ContentType contentType, std::shared_ptr< avsCommon::sdkInterfaces::ChannelObserverInterface > channelObserver) override
bool releaseFocus(std::string avsInterface, std::string channelName, std::shared_ptr< avsCommon::sdkInterfaces::ChannelObserverInterface > channelObserver) override
::std::string string
Definition: gtest-port.h:1097
void handleLocalRenderDocument(rapidjson::Document &payload) override
std::shared_ptr< ipc::IPCRouter > getIPCRouter() override
Returns a reference to m_ipcRouter.
Single-thread executor implementation.
Definition: Executor.h:45
void handleRenderComplete(rapidjson::Document &payload) override
State
The enum State describes the state of authorization.
Definition: AuthObserverInterface.h:34
Error
The enum Error encodes possible errors which may occur when changing state.
Definition: CapabilitiesDelegateObserverInterface.h:44
void localesRequest(const std::string &message) override
}
void alexaStateRequest(const std::string &message) override
}
FocusState
Definition: FocusState.h:29
void onAuthStateChange(AuthObserverInterface::State newState, AuthObserverInterface::Error newError) override
void acquireChannelRequest(const std::string &message) override
static std::shared_ptr< GUIClient > create(std::shared_ptr< MessagingServerInterface > serverImplementation, const std::shared_ptr< registrationManager::CustomerDataManagerInterface > customerDataManager, const std::shared_ptr< ipc::IPCVersionManager > ipcVersionManager, std::shared_ptr< avsCommon::utils::DeviceInfo > deviceInfo)
Definition: CapabilitiesDelegateObserverInterface.h:29
void setAPLRuntimePresentationAdapter(const std::shared_ptr< APLRuntimePresentationAdapter > &aplRuntimePresentationAdapter)
void onCapabilitiesStateChange(avsCommon::sdkInterfaces::CapabilitiesObserverInterface::State newState, avsCommon::sdkInterfaces::CapabilitiesObserverInterface::Error newError, const std::vector< avsCommon::sdkInterfaces::endpoints::EndpointIdentifier > &addedOrUpdatedEndpoints, const std::vector< avsCommon::sdkInterfaces::endpoints::EndpointIdentifier > &deletedEndpoints) override
An struct containing call state information.
Definition: CallStateObserverInterface.h:45
State
The enum State describes the state of the CapabilitiesDelegate.
Definition: CapabilitiesDelegateObserverInterface.h:32
void setGUIManager(std::shared_ptr< gui::GUIServerInterface > guiManager) override
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
void handleAplEvent(rapidjson::Document &payload) override
SampleAppReturnCode
Definition: SampleApplicationReturnCodes.h:25
ContentType
Definition: ContentType.h:25
void onMessage(const std::string &jsonMessage) override
void sendMessage(messages::MessageInterface &message) override
void authorizationStateRequest(const std::string &message) override
}
void focusAcquireRequest(const GUIToken token, const std::string &avsInterface, const std::string &channelName, avsCommon::avs::ContentType contentType) override
Internal function to execute.
Definition: RegistrationObserverInterface.h:24
void setAplClientBridge(std::shared_ptr< AplClientBridge > aplClientBridge, bool aplVersionChanged)
}
void authorizationInfoRequest(const std::string &message) override
}
void releaseChannelRequest(const std::string &message) override
void handleLocalExecuteCommands(rapidjson::Document &payload) override
uint64_t GUIToken
Alias for GUI provided token.
Definition: GUIClientInterface.h:46
void addToMediaPlayerMap(const std::string &name, const std::shared_ptr< avsCommon::utils::mediaPlayer::MediaPlayerInterface > &mediaPlayer)
Adds a mediaplayer to the m_mediaPlayerMap with the key as the name and value as the mediaplayer...
void handleInitializeAPLRenderers(rapidjson::Document &payload) override
tuple message
Definition: gtest_output_test.py:331
void notifyAuthorizationRequest(const std::string &url, const std::string &code) override
void setObserver(const std::shared_ptr< communication::MessagingServerObserverInterface > &observer) override
void setMessageListener(std::shared_ptr< MessageListenerInterface > messageListener) override
void sendCompleteAuthorizationMessage(const std::string &url, const std::string &code, const std::string &clientId)

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