AlexaClientSDK  1.25.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
FocusManager.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_AFML_INCLUDE_AFML_FOCUSMANAGER_H_
17 #define ALEXA_CLIENT_SDK_AFML_INCLUDE_AFML_FOCUSMANAGER_H_
18 
19 #include <algorithm>
20 #include <map>
21 #include <mutex>
22 #include <set>
23 #include <unordered_map>
24 #include <unordered_set>
25 #include <vector>
26 
33 
34 #include "AFML/Channel.h"
36 
37 namespace alexaClientSDK {
38 namespace afml {
39 
58 public:
64  public:
72  ChannelConfiguration(const std::string& configName, unsigned int configPriority) :
73  name{configName},
74  priority{configPriority} {
75  }
76 
82  std::string toString() const {
83  return "name:'" + name + "', priority:" + std::to_string(priority);
84  }
85 
87  std::string name;
88 
90  unsigned int priority;
91 
100  static bool readChannelConfiguration(
101  const std::string& channelTypeKey,
102  std::vector<afml::FocusManager::ChannelConfiguration>* virtualChannelConfigurations);
103  };
104 
119  FocusManager(
120  const std::vector<ChannelConfiguration>& channelConfigurations,
121  std::shared_ptr<ActivityTrackerInterface> activityTrackerInterface = nullptr,
122  const std::vector<ChannelConfiguration>& virtualChannelConfigurations = std::vector<ChannelConfiguration>(),
123  std::shared_ptr<interruptModel::InterruptModel> interruptModel = nullptr);
124 
125  bool acquireChannel(
126  const std::string& channelName,
127  std::shared_ptr<avsCommon::sdkInterfaces::ChannelObserverInterface> channelObserver,
128  const std::string& interfaceName) override;
129 
130  bool acquireChannel(
131  const std::string& channelName,
132  std::shared_ptr<avsCommon::sdkInterfaces::FocusManagerInterface::Activity> channelActivity) override;
133 
134  std::future<bool> releaseChannel(
135  const std::string& channelName,
136  std::shared_ptr<avsCommon::sdkInterfaces::ChannelObserverInterface> channelObserver) override;
137 
138  void stopForegroundActivity() override;
139 
140  void stopAllActivities() override;
141 
142  void addObserver(const std::shared_ptr<avsCommon::sdkInterfaces::FocusManagerObserverInterface>& observer) override;
143 
144  void removeObserver(
145  const std::shared_ptr<avsCommon::sdkInterfaces::FocusManagerObserverInterface>& observer) override;
146 
147  void modifyContentType(
148  const std::string& channelName,
149  const std::string& interfaceName,
150  avsCommon::avs::ContentType contentType) override;
151 
157  static const std::vector<FocusManager::ChannelConfiguration> getDefaultAudioChannels();
158 
164  static const std::vector<FocusManager::ChannelConfiguration> getDefaultVisualChannels();
165 
166 private:
168  using ChannelsToInterfaceNamesMap = std::map<std::shared_ptr<Channel>, std::string>;
172  struct ChannelPtrComparator {
181  bool operator()(const std::shared_ptr<Channel>& first, const std::shared_ptr<Channel>& second) const {
182  return *first > *second;
183  }
184  };
185 
193  void readChannelConfiguration(const std::vector<ChannelConfiguration>& channelConfigurations, bool isVirtual);
194 
204  void setChannelFocus(
205  const std::shared_ptr<Channel>& channel,
208  bool forceUpdate = false);
209 
217  void acquireChannelHelper(
218  std::shared_ptr<Channel> channelToAcquire,
219  std::shared_ptr<avsCommon::sdkInterfaces::FocusManagerInterface::Activity> channelActivity);
220 
230  void releaseChannelHelper(
231  std::shared_ptr<Channel> channelToRelease,
232  std::shared_ptr<avsCommon::sdkInterfaces::ChannelObserverInterface> channelObserver,
233  std::shared_ptr<std::promise<bool>> releaseChannelSuccess,
234  const std::string& channelName);
235 
244  void stopForegroundActivityHelper(
245  std::shared_ptr<Channel> foregroundChannel,
246  std::string foregroundChannelInterface);
247 
257  void stopAllActivitiesHelper(const ChannelsToInterfaceNamesMap& channelsOwnersMap);
258 
265  std::shared_ptr<Channel> getChannel(const std::string& channelName) const;
266 
272  std::shared_ptr<Channel> getHighestPriorityActiveChannelLocked() const;
273 
280  bool isChannelForegroundedLocked(const std::shared_ptr<Channel>& channel) const;
281 
289  bool doesChannelNameExist(const std::string& name) const;
290 
298  bool doesChannelPriorityExist(const unsigned int priority) const;
299 
303  void foregroundHighestPriorityActiveChannel();
304 
309  void notifyActivityTracker();
310 
320  std::shared_ptr<Channel> lowPrioChannel,
321  std::shared_ptr<Channel> highPrioChannel);
322 
330  void setBackgroundChannelMixingBehavior(std::shared_ptr<Channel> foregroundChannel);
331 
333  std::mutex m_mutex;
334 
336  std::unordered_map<std::string, std::shared_ptr<Channel>> m_allChannels;
337 
339  std::set<std::shared_ptr<Channel>, ChannelPtrComparator> m_activeChannels;
340 
342  std::unordered_set<std::shared_ptr<avsCommon::sdkInterfaces::FocusManagerObserverInterface>> m_observers;
343 
344  /*
345  * A vector of channel's State that has been updated due to @c acquireChannel(), @c releaseChannel() or
346  * stopForegroundActivity(). This is accessed by functions in the @c m_executor worker thread, and do not require
347  * any synchronization.
348  */
349  std::vector<Channel::State> m_activityUpdates;
350 
352  std::shared_ptr<ActivityTrackerInterface> m_activityTracker;
353 
355  std::shared_ptr<interruptModel::InterruptModel> m_interruptModel;
356 
364 };
365 
366 } // namespace afml
367 } // namespace alexaClientSDK
368 
369 #endif // ALEXA_CLIENT_SDK_AFML_INCLUDE_AFML_FOCUSMANAGER_H_
Definition: FocusManager.h:57
void stopForegroundActivity() override
Definition: FocusManager.cpp:127
MixingBehavior
Definition: MixingBehavior.h:25
void stopAllActivities() override
Definition: FocusManager.cpp:144
bool acquireChannel(const std::string &channelName, std::shared_ptr< avsCommon::sdkInterfaces::ChannelObserverInterface > channelObserver, const std::string &interfaceName) override
std::string toString() const
Definition: FocusManager.h:82
void addObserver(const std::shared_ptr< avsCommon::sdkInterfaces::FocusManagerObserverInterface > &observer) override
Definition: FocusManager.cpp:166
void modifyContentType(const std::string &channelName, const std::string &interfaceName, avsCommon::avs::ContentType contentType) override
Definition: FocusManager.cpp:424
ChannelConfiguration(const std::string &configName, unsigned int configPriority)
Definition: FocusManager.h:72
FocusManager(const std::vector< ChannelConfiguration > &channelConfigurations, std::shared_ptr< ActivityTrackerInterface > activityTrackerInterface=nullptr, const std::vector< ChannelConfiguration > &virtualChannelConfigurations=std::vector< ChannelConfiguration >(), std::shared_ptr< interruptModel::InterruptModel > interruptModel=nullptr)
Definition: FocusManager.cpp:50
unsigned int priority
The priority of the channel.
Definition: FocusManager.h:90
FocusState
Definition: FocusState.h:29
static bool readChannelConfiguration(const std::string &channelTypeKey, std::vector< afml::FocusManager::ChannelConfiguration > *virtualChannelConfigurations)
Definition: FocusManager.cpp:529
static const std::vector< FocusManager::ChannelConfiguration > getDefaultAudioChannels()
Definition: FocusManager.cpp:512
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
std::future< bool > releaseChannel(const std::string &channelName, std::shared_ptr< avsCommon::sdkInterfaces::ChannelObserverInterface > channelObserver) override
Definition: FocusManager.cpp:105
ContentType
Definition: ContentType.h:25
void removeObserver(const std::shared_ptr< avsCommon::sdkInterfaces::FocusManagerObserverInterface > &observer) override
Definition: FocusManager.cpp:171
MixingBehavior getMixingBehavior(const std::string &input)
Definition: MixingBehavior.h:72
static const std::vector< FocusManager::ChannelConfiguration > getDefaultVisualChannels()
Definition: FocusManager.cpp:522
std::string name
The name of the channel.
Definition: FocusManager.h:87

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