AlexaClientSDK  1.26.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
SpeakerManager.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_SPEAKERMANAGER_INCLUDE_SPEAKERMANAGER_SPEAKERMANAGER_H_
17 #define ALEXA_CLIENT_SDK_CAPABILITYAGENTS_SPEAKERMANAGER_INCLUDE_SPEAKERMANAGER_SPEAKERMANAGER_H_
18 
19 #include <future>
20 #include <map>
21 #include <memory>
22 #include <unordered_set>
23 #include <vector>
24 
47 
48 namespace alexaClientSDK {
49 namespace capabilityAgents {
50 namespace speakerManager {
51 
76 public:
92  static std::shared_ptr<SpeakerManagerInterface> createSpeakerManagerCapabilityAgent(
93  const std::shared_ptr<avsCommon::sdkInterfaces::storage::MiscStorageInterface>& storage,
94  const std::shared_ptr<avsCommon::sdkInterfaces::ContextManagerInterface>& contextManager,
95  const std::shared_ptr<avsCommon::sdkInterfaces::MessageSenderInterface>& messageSender,
96  const std::shared_ptr<avsCommon::sdkInterfaces::ExceptionEncounteredSenderInterface>&
97  exceptionEncounteredSender,
98  const std::shared_ptr<acsdkShutdownManagerInterfaces::ShutdownNotifierInterface>& shutdownNotifier,
102  const std::shared_ptr<avsCommon::utils::metrics::MetricRecorderInterface>& metricRecorder);
103 
116  static std::shared_ptr<SpeakerManager> create(
117  const std::shared_ptr<SpeakerManagerStorageInterface>& storage,
118  const std::vector<std::shared_ptr<avsCommon::sdkInterfaces::ChannelVolumeInterface>>& volumeInterfaces,
119  std::shared_ptr<avsCommon::sdkInterfaces::ContextManagerInterface> contextManager,
120  std::shared_ptr<avsCommon::sdkInterfaces::MessageSenderInterface> messageSender,
121  std::shared_ptr<avsCommon::sdkInterfaces::ExceptionEncounteredSenderInterface> exceptionEncounteredSender,
122  std::shared_ptr<avsCommon::utils::metrics::MetricRecorderInterface> metricRecorder = nullptr);
123 
127  void handleDirectiveImmediately(std::shared_ptr<avsCommon::avs::AVSDirective> directive) override;
128  void preHandleDirective(std::shared_ptr<avsCommon::avs::CapabilityAgent::DirectiveInfo> info) override;
129  void handleDirective(std::shared_ptr<avsCommon::avs::CapabilityAgent::DirectiveInfo> info) override;
130  void cancelDirective(std::shared_ptr<avsCommon::avs::CapabilityAgent::DirectiveInfo> info) override;
132 
133  // @name RequiresShutdown Functions
135  void doShutdown() override;
137 
138  // @name SpeakerManagerInterface Functions
140  std::future<bool> setVolume(
142  int8_t volume,
143  const NotificationProperties& properties) override;
144  std::future<bool> adjustVolume(
146  int8_t delta,
147  const NotificationProperties& properties) override;
148  std::future<bool> setMute(
150  bool mute,
151  const NotificationProperties& properties) override;
152  std::future<bool> setVolume(
154  int8_t volume,
155  bool forceNoNotifications = false,
158  std::future<bool> adjustVolume(
160  int8_t delta,
161  bool forceNoNotifications = false,
164  std::future<bool> setMute(
166  bool mute,
167  bool forceNoNotifications = false,
170 
171 #ifdef ENABLE_MAXVOLUME_SETTING
172  std::future<bool> setMaximumVolumeLimit(const int8_t maximumVolumeLimit) override;
173 #endif
174  std::future<bool> getSpeakerSettings(
178  std::shared_ptr<avsCommon::sdkInterfaces::SpeakerManagerObserverInterface> observer) override;
180  std::shared_ptr<avsCommon::sdkInterfaces::SpeakerManagerObserverInterface> observer) override;
182  std::shared_ptr<avsCommon::sdkInterfaces::ChannelVolumeInterface> channelVolumeInterface) override;
184 
187  std::unordered_set<std::shared_ptr<avsCommon::avs::CapabilityConfiguration>> getCapabilityConfigurations() override;
189 
190 private:
203  const std::shared_ptr<SpeakerManagerStorageInterface>& speakerManagerStorage,
204  const std::vector<std::shared_ptr<avsCommon::sdkInterfaces::ChannelVolumeInterface>>& groupVolumeInterfaces,
205  std::shared_ptr<avsCommon::sdkInterfaces::ContextManagerInterface> contextManager,
206  std::shared_ptr<avsCommon::sdkInterfaces::MessageSenderInterface> messageSender,
207  std::shared_ptr<avsCommon::sdkInterfaces::ExceptionEncounteredSenderInterface> exceptionEncounteredSender,
208  std::shared_ptr<avsCommon::utils::metrics::MetricRecorderInterface> metricRecorder);
209 
211  struct ChannelVolumeInterfaceHash {
212  public:
213  size_t operator()(const std::shared_ptr<avsCommon::sdkInterfaces::ChannelVolumeInterface>& key) const {
214  if (nullptr == key) {
217  return 0;
218  }
219  return key->getId();
220  }
221  };
222 
224  struct ChannelVolumeInterfaceEqualTo {
225  public:
226  bool operator()(
227  const std::shared_ptr<avsCommon::sdkInterfaces::ChannelVolumeInterface> channelVolumeInterface1,
228  std::shared_ptr<avsCommon::sdkInterfaces::ChannelVolumeInterface> channelVolumeInterface2) const {
229  if (!channelVolumeInterface1 || !channelVolumeInterface2) {
232  return true;
233  }
234  return channelVolumeInterface1->getId() == channelVolumeInterface2->getId();
235  }
236  };
237 
239  using SpeakerSet = std::unordered_set<
240  std::shared_ptr<avsCommon::sdkInterfaces::ChannelVolumeInterface>,
241  ChannelVolumeInterfaceHash,
242  ChannelVolumeInterfaceEqualTo>;
243 
251  void addChannelVolumeInterfaceIntoSpeakerMap(
252  std::shared_ptr<avsCommon::sdkInterfaces::ChannelVolumeInterface> channelVolumeInterface);
253 
261  bool parseDirectivePayload(std::string payload, rapidjson::Document* document);
262 
268  void executeSetHandlingCompleted(std::shared_ptr<DirectiveInfo> info);
269 
275  void removeDirective(std::shared_ptr<DirectiveInfo> info);
276 
284  void sendExceptionEncountered(
285  std::shared_ptr<CapabilityAgent::DirectiveInfo> info,
286  const std::string& message,
288 
296  bool updateContextManager(
299 
306  void executeSendSpeakerSettingsChangedEvent(
307  const std::string& eventName,
309 
319  bool executeSetVolume(
321  int8_t volume,
323 
332  bool executeRestoreVolume(
335 
345  bool executeAdjustVolume(
347  int8_t delta,
349 
359  bool executeSetMute(
361  bool mute,
363 
364 #ifdef ENABLE_MAXVOLUME_SETTING
365 
371  bool executeSetMaximumVolumeLimit(const int8_t maximumVolumeLimit);
372 #endif // ENABLE_MAXVOLUME_SETTING
373 
382  bool executeGetSpeakerSettings(
385 
394  bool executeSetSpeakerSettings(
397 
404  bool executeInitializeSpeakerSettings(avsCommon::sdkInterfaces::ChannelVolumeInterface::Type type);
405 
415  void executeNotifySettingsChanged(
417  const std::string& eventName,
420 
428  void executeNotifyObserver(
432 
436  void executePersistConfiguration();
437 
444  void convertSettingsToChannelState(
447 
453  int8_t getMaximumVolumeLimit();
454 
464  template <typename Task, typename... Args>
465  bool retryAndApplySettings(Task task, Args&&... args);
466 
474  void presetChannelDefaults(
477 
481  void loadConfiguration();
482 
486  void updateChannelSettings();
487 
492  void updateChannelSettings(avsCommon::sdkInterfaces::ChannelVolumeInterface::Type type);
493 
500  int8_t adjustVolumeRange(int64_t volume);
501 
504 
506  std::shared_ptr<avsCommon::utils::metrics::MetricRecorderInterface> m_metricRecorder;
507 
509  std::shared_ptr<avsCommon::sdkInterfaces::ContextManagerInterface> m_contextManager;
510 
512  std::shared_ptr<avsCommon::sdkInterfaces::MessageSenderInterface> m_messageSender;
513 
515  int m_minUnmuteVolume;
516 
520  std::unordered_map<
522  SpeakerSet,
524  m_speakerMap;
525 
527  std::unordered_set<std::shared_ptr<avsCommon::sdkInterfaces::SpeakerManagerObserverInterface>> m_observers;
528 
530  std::unordered_set<std::shared_ptr<avsCommon::avs::CapabilityConfiguration>> m_capabilityConfigurations;
531 
533  avsCommon::utils::WaitEvent m_waitCancelEvent;
534 
536  avsCommon::utils::RetryTimer m_retryTimer;
537 
539  const std::size_t m_maxRetries;
540 
542  int8_t m_maximumVolumeLimit;
543 
545  bool m_restoreMuteState;
546 
548  std::map<
551  m_speakerSettings;
552 
555 };
556 
557 } // namespace speakerManager
558 } // namespace capabilityAgents
559 } // namespace alexaClientSDK
560 
561 #endif // ALEXA_CLIENT_SDK_CAPABILITYAGENTS_SPEAKERMANAGER_INCLUDE_SPEAKERMANAGER_SPEAKERMANAGER_H_
alexaClientSDK::capabilityAgents::speakerManager::SpeakerManager::create
static std::shared_ptr< SpeakerManager > create(const std::shared_ptr< SpeakerManagerStorageInterface > &storage, const std::vector< std::shared_ptr< avsCommon::sdkInterfaces::ChannelVolumeInterface >> &volumeInterfaces, std::shared_ptr< avsCommon::sdkInterfaces::ContextManagerInterface > contextManager, std::shared_ptr< avsCommon::sdkInterfaces::MessageSenderInterface > messageSender, std::shared_ptr< avsCommon::sdkInterfaces::ExceptionEncounteredSenderInterface > exceptionEncounteredSender, std::shared_ptr< avsCommon::utils::metrics::MetricRecorderInterface > metricRecorder=nullptr)
Definition: SpeakerManager.cpp:172
SpeakerManagerObserverInterface.h
alexaClientSDK::avsCommon::utils::RetryTimer
Definition: RetryTimer.h:30
alexaClientSDK::avsCommon::utils::WaitEvent
Definition: WaitEvent.h:32
alexaClientSDK::avsCommon::utils::RequiresShutdown
Definition: RequiresShutdown.h:32
alexaClientSDK::avsCommon::sdkInterfaces::SpeakerInterface::SpeakerSettings
Definition: SpeakerInterface.h:42
alexaClientSDK::capabilityAgents::speakerManager::SpeakerManager::addChannelVolumeInterface
void addChannelVolumeInterface(std::shared_ptr< avsCommon::sdkInterfaces::ChannelVolumeInterface > channelVolumeInterface) override
Definition: SpeakerManager.cpp:1072
alexaClientSDK::avsCommon::sdkInterfaces::endpoints::EndpointCapabilitiesRegistrarInterface
Definition: EndpointCapabilitiesRegistrarInterface.h:35
EndpointCapabilitiesRegistrarInterface.h
alexaClientSDK::capabilityAgents::speakerManager::SpeakerManager::addSpeakerManagerObserver
void addSpeakerManagerObserver(std::shared_ptr< avsCommon::sdkInterfaces::SpeakerManagerObserverInterface > observer) override
Definition: SpeakerManager.cpp:503
alexaClientSDK::capabilityAgents::speakerManager::SpeakerManager::handleDirective
void handleDirective(std::shared_ptr< avsCommon::avs::CapabilityAgent::DirectiveInfo > info) override
Definition: SpeakerManager.cpp:363
alexaClientSDK::capabilityAgents::speakerManager::SpeakerManager::adjustVolume
std::future< bool > adjustVolume(avsCommon::sdkInterfaces::ChannelVolumeInterface::Type type, int8_t delta, const NotificationProperties &properties) override
Definition: SpeakerManager.cpp:716
alexaClientSDK::capabilityAgents::speakerManager::SpeakerManagerConfigHelper
Definition: SpeakerManagerConfigHelper.h:33
alexaClientSDK::avsCommon::sdkInterfaces::CapabilityConfigurationInterface
Definition: CapabilityConfigurationInterface.h:32
MiscStorageInterface.h
alexaClientSDK::capabilityAgents::speakerManager::SpeakerManager::setMute
std::future< bool > setMute(avsCommon::sdkInterfaces::ChannelVolumeInterface::Type type, bool mute, const NotificationProperties &properties) override
Definition: SpeakerManager.cpp:847
alexaClientSDK::capabilityAgents::speakerManager::SpeakerManager::getConfiguration
avsCommon::avs::DirectiveHandlerConfiguration getConfiguration() const override
Definition: SpeakerManager.cpp:270
SpeakerManagerInterface.h
alexaClientSDK::avsCommon::utils::threading::Executor
Definition: Executor.h:40
MetricRecorderInterface.h
ShutdownNotifierInterface.h
alexaClientSDK::settings::test::key
static const std::string key
The database key to be used by the protocol given the METADATA object.
Definition: SharedAVSSettingProtocolTest.cpp:58
alexaClientSDK::capabilityAgents::speakerManager::SpeakerManager::handleDirectiveImmediately
void handleDirectiveImmediately(std::shared_ptr< avsCommon::avs::AVSDirective > directive) override
Definition: SpeakerManager.cpp:292
alexaClientSDK::capabilityAgents::speakerManager::SpeakerManager::cancelDirective
void cancelDirective(std::shared_ptr< avsCommon::avs::CapabilityAgent::DirectiveInfo > info) override
Definition: SpeakerManager.cpp:499
WaitEvent.h
alexaClientSDK::capabilityAgents::speakerManager::SpeakerManager::preHandleDirective
void preHandleDirective(std::shared_ptr< avsCommon::avs::CapabilityAgent::DirectiveInfo > info) override
Definition: SpeakerManager.cpp:288
RequiresShutdown.h
android_test.args
def args
Definition: android_test.py:111
Executor.h
alexaClientSDK::avsCommon::utils::functional::EnumClassHash
Definition: hash.h:51
alexaClientSDK::avsCommon::sdkInterfaces::SpeakerManagerObserverInterface::Source::LOCAL_API
@ LOCAL_API
alexaClientSDK::acsdkManufactory::Annotated
Definition: Annotated.h:35
CapabilityAgent.h
alexaClientSDK::capabilityAgents::speakerManager::SpeakerManager::createSpeakerManagerCapabilityAgent
static std::shared_ptr< SpeakerManagerInterface > createSpeakerManagerCapabilityAgent(const std::shared_ptr< avsCommon::sdkInterfaces::storage::MiscStorageInterface > &storage, const std::shared_ptr< avsCommon::sdkInterfaces::ContextManagerInterface > &contextManager, const std::shared_ptr< avsCommon::sdkInterfaces::MessageSenderInterface > &messageSender, const std::shared_ptr< avsCommon::sdkInterfaces::ExceptionEncounteredSenderInterface > &exceptionEncounteredSender, const std::shared_ptr< acsdkShutdownManagerInterfaces::ShutdownNotifierInterface > &shutdownNotifier, const acsdkManufactory::Annotated< avsCommon::sdkInterfaces::endpoints::DefaultEndpointAnnotation, avsCommon::sdkInterfaces::endpoints::EndpointCapabilitiesRegistrarInterface > &endpointCapabilitiesRegistrar, const std::shared_ptr< avsCommon::utils::metrics::MetricRecorderInterface > &metricRecorder)
Definition: SpeakerManager.cpp:139
alexaClientSDK::avsCommon::sdkInterfaces::SpeakerManagerInterface
Definition: SpeakerManagerInterface.h:33
alexaClientSDK::avsCommon::avs::ExceptionErrorType
ExceptionErrorType
Definition: ExceptionErrorType.h:28
alexaClientSDK
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
alexaClientSDK::avsCommon::sdkInterfaces::SpeakerManagerInterface::NotificationProperties
Definition: SpeakerManagerInterface.h:40
RetryTimer.h
alexaClientSDK::capabilityAgents::speakerManager::SpeakerManager
Definition: SpeakerManager.h:71
alexaClientSDK::avsCommon::avs::DirectiveHandlerConfiguration
std::unordered_map< directiveRoutingRule::DirectiveRoutingRule, BlockingPolicy > DirectiveHandlerConfiguration
Definition: DirectiveHandlerConfiguration.h:32
alexaClientSDK::avsCommon::sdkInterfaces::endpoints::DefaultEndpointAnnotation
Definition: DefaultEndpointAnnotation.h:26
ContextManagerInterface.h
alexaClientSDK::capabilityAgents::speakerManager::SpeakerManager::doShutdown
void doShutdown() override
Definition: SpeakerManager.cpp:279
alexaClientSDK::capabilityAgents::speakerManager::SpeakerManager::removeSpeakerManagerObserver
void removeSpeakerManagerObserver(std::shared_ptr< avsCommon::sdkInterfaces::SpeakerManagerObserverInterface > observer) override
Definition: SpeakerManager.cpp:518
MessageSenderInterface.h
SpeakerManagerConfigHelper.h
alexaClientSDK::capabilityAgents::speakerManager::SpeakerManager::setVolume
std::future< bool > setVolume(avsCommon::sdkInterfaces::ChannelVolumeInterface::Type type, int8_t volume, const NotificationProperties &properties) override
Definition: SpeakerManager.cpp:566
alexaClientSDK::avsCommon::avs::CapabilityAgent
Definition: CapabilityAgent.h:47
SpeakerManagerStorageInterface.h
DefaultEndpointAnnotation.h
hash.h
alexaClientSDK::avsCommon::sdkInterfaces::ChannelVolumeInterface::Type
Type
Definition: ChannelVolumeInterface.h:37
SpeakerManagerStorageState.h
alexaClientSDK::capabilityAgents::speakerManager::SpeakerManagerStorageState::ChannelState
Definition: SpeakerManagerStorageState.h:33
Annotated.h
CapabilityConfiguration.h
alexaClientSDK::capabilityAgents::speakerManager::SpeakerManager::getCapabilityConfigurations
std::unordered_set< std::shared_ptr< avsCommon::avs::CapabilityConfiguration > > getCapabilityConfigurations() override
Definition: SpeakerManager.cpp:1077
AVSDirective.h
alexaClientSDK::capabilityAgents::speakerManager::SpeakerManager::getSpeakerSettings
std::future< bool > getSpeakerSettings(avsCommon::sdkInterfaces::ChannelVolumeInterface::Type type, avsCommon::sdkInterfaces::SpeakerInterface::SpeakerSettings *settings) override
Definition: SpeakerManager.cpp:1009
CapabilityConfigurationInterface.h
alexaClientSDK::avsCommon::sdkInterfaces::SpeakerManagerObserverInterface::Source
Source
Indicates whether the source of the call is from an AVS Directive or through a Local API call.
Definition: SpeakerManagerObserverInterface.h:35

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