AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
SpeechSynthesizer.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_SPEECHSYNTHESIZER_INCLUDE_SPEECHSYNTHESIZER_SPEECHSYNTHESIZER_H_
17 #define ALEXA_CLIENT_SDK_CAPABILITYAGENTS_SPEECHSYNTHESIZER_INCLUDE_SPEECHSYNTHESIZER_SPEECHSYNTHESIZER_H_
18 
19 #include <memory>
20 #include <mutex>
21 #include <string>
22 #include <unordered_set>
23 #include <deque>
24 
47 #include <Captions/CaptionData.h>
49 
50 namespace alexaClientSDK {
51 namespace capabilityAgents {
52 namespace speechSynthesizer {
53 
55 static const constexpr char* SPEAK_MEDIA_PLAYER_NAME = "SpeakMediaPlayer";
56 
67  , public std::enable_shared_from_this<SpeechSynthesizer> {
68 public:
71 
89  static std::shared_ptr<SpeechSynthesizer> createSpeechSynthesizer(
90  std::shared_ptr<acsdkApplicationAudioPipelineFactoryInterfaces::ApplicationAudioPipelineFactoryInterface>
91  audioPipelineFactory,
92  std::shared_ptr<avsCommon::sdkInterfaces::MessageSenderInterface> messageSender,
93  std::shared_ptr<avsCommon::sdkInterfaces::FocusManagerInterface> focusManager,
94  std::shared_ptr<avsCommon::sdkInterfaces::ContextManagerInterface> contextManager,
95  std::shared_ptr<avsCommon::sdkInterfaces::ExceptionEncounteredSenderInterface> exceptionSender,
96  std::shared_ptr<avsCommon::utils::metrics::MetricRecorderInterface> metricRecorder,
97  std::shared_ptr<avsCommon::avs::DialogUXStateAggregator> dialogUXStateAggregator,
98  std::shared_ptr<captions::CaptionManagerInterface> captionManager = nullptr,
99  std::shared_ptr<avsCommon::sdkInterfaces::PowerResourceManagerInterface> powerResourceManager = nullptr);
100 
117  static std::shared_ptr<SpeechSynthesizer> create(
118  std::shared_ptr<avsCommon::utils::mediaPlayer::MediaPlayerInterface> mediaPlayer,
119  std::shared_ptr<avsCommon::sdkInterfaces::MessageSenderInterface> messageSender,
120  std::shared_ptr<avsCommon::sdkInterfaces::FocusManagerInterface> focusManager,
121  std::shared_ptr<avsCommon::sdkInterfaces::ContextManagerInterface> contextManager,
122  std::shared_ptr<avsCommon::sdkInterfaces::ExceptionEncounteredSenderInterface> exceptionSender,
123  std::shared_ptr<avsCommon::utils::metrics::MetricRecorderInterface> metricRecorder,
124  std::shared_ptr<avsCommon::avs::DialogUXStateAggregator> dialogUXStateAggregator,
125  std::shared_ptr<captions::CaptionManagerInterface> captionManager = nullptr,
126  std::shared_ptr<avsCommon::sdkInterfaces::PowerResourceManagerInterface> powerResourceManager = nullptr);
127 
128  void onDialogUXStateChanged(DialogUXState newState) override;
129 
131 
137  void addObserver(std::shared_ptr<SpeechSynthesizerObserverInterface> observer);
138 
147  void removeObserver(std::shared_ptr<SpeechSynthesizerObserverInterface> observer);
148 
149  void onDeregistered() override;
150 
151  void handleDirectiveImmediately(std::shared_ptr<avsCommon::avs::AVSDirective> directive) override;
152 
153  void preHandleDirective(std::shared_ptr<DirectiveInfo> info) override;
154 
155  void handleDirective(std::shared_ptr<DirectiveInfo> info) override;
156 
157  void cancelDirective(std::shared_ptr<DirectiveInfo> info) override;
158 
163 
164  void provideState(const avsCommon::avs::NamespaceAndName& stateProviderName, const unsigned int stateRequestToken)
165  override;
166 
167  void onContextAvailable(const std::string& jsonContext) override;
168 
170 
176  void onPlaybackError(
177  SourceId id,
179  std::string error,
184 
187  std::unordered_set<std::shared_ptr<avsCommon::avs::CapabilityConfiguration>> getCapabilityConfigurations() override;
189 
190 private:
194  struct SpeakDirectiveInfo {
200  SpeakDirectiveInfo(std::shared_ptr<DirectiveInfo> directiveInfo);
201 
203  void clear();
204 
206  std::shared_ptr<avsCommon::avs::AVSDirective> directive;
207 
209  std::shared_ptr<avsCommon::sdkInterfaces::DirectiveHandlerResultInterface> result;
210 
212  std::string token;
213 
215  std::unique_ptr<avsCommon::avs::attachment::AttachmentReader> attachmentReader;
216 
218  bool sendPlaybackStartedMessage;
219 
221  bool sendPlaybackFinishedMessage;
222 
224  bool sendCompletedMessage;
225 
227  bool isSetFailedCalled;
228 
230  bool isPlaybackInitiated;
231 
233  bool isHandled;
234 
236  bool isCancelInitiated;
237 
239  avsCommon::avs::PlayBehavior playBehavior;
240 
242  captions::CaptionData captionData;
243 
245  std::vector<avsCommon::utils::audioAnalyzer::AudioAnalyzerState> analyzersData;
246  };
247 
263  std::shared_ptr<avsCommon::utils::mediaPlayer::MediaPlayerInterface> mediaPlayer,
264  std::shared_ptr<avsCommon::sdkInterfaces::MessageSenderInterface> messageSender,
265  std::shared_ptr<avsCommon::sdkInterfaces::FocusManagerInterface> focusManager,
266  std::shared_ptr<avsCommon::sdkInterfaces::ContextManagerInterface> contextManager,
267  std::shared_ptr<avsCommon::utils::metrics::MetricRecorderInterface> metricRecorder,
268  std::shared_ptr<avsCommon::sdkInterfaces::ExceptionEncounteredSenderInterface> exceptionSender,
269  std::shared_ptr<captions::CaptionManagerInterface> captionManager = nullptr,
270  std::shared_ptr<avsCommon::sdkInterfaces::PowerResourceManagerInterface> powerResourceManager = nullptr);
271 
272  void doShutdown() override;
273 
279  void init();
280 
287  void executeHandleImmediately(std::shared_ptr<DirectiveInfo> info);
288 
297  void executePreHandleAfterValidation(std::shared_ptr<SpeakDirectiveInfo> speakInfo);
298 
307  void executeHandleAfterValidation(std::shared_ptr<SpeakDirectiveInfo> speakInfo);
308 
315  void executePreHandle(std::shared_ptr<DirectiveInfo> info);
316 
323  void executeHandle(std::shared_ptr<DirectiveInfo> info);
324 
330  void executeCancel(std::shared_ptr<DirectiveInfo> info);
331 
337  void executeCancel(std::shared_ptr<SpeakDirectiveInfo> speakInfo, bool internalCancel);
338 
346  void executeStateChange(SpeechSynthesizerObserverInterface::SpeechSynthesizerState newState);
347 
354  void executeProvideStateLocked(const unsigned int& stateRequestToken);
355 
359  void executePlaybackStarted();
360 
364  void executePlaybackFinished();
365 
371  void executePlaybackStopped(SourceId id);
372 
378  void executePlaybackError(const avsCommon::utils::mediaPlayer::ErrorType& type, std::string error);
379 
384  void submitMetric(avsCommon::utils::metrics::MetricEventBuilder& metricEventBuilder);
385 
392  void submitInstanceEntryMetric(
393  const std::string& segmentId,
394  const std::string& name,
395  const std::map<std::string, std::string>& metadata = {});
396 
403  void executeOnDialogUXStateChanged(
405 
413  std::string buildState(std::string& token, int64_t offsetInMilliseconds) const;
414 
420  void sendEvent(const std::string& eventName, const std::string& payload) const;
421 
428  static std::string buildPayload(std::string& token);
429 
437  static std::string buildPayload(std::string& token, int64_t offsetInMilliseconds);
438 
442  void startPlaying();
443 
447  void stopPlaying();
448 
458  void setCurrentStateLocked(SpeechSynthesizerObserverInterface::SpeechSynthesizerState newState);
459 
466  void setDesiredState(SpeechSynthesizerObserverInterface::SpeechSynthesizerState desiredState);
467 
472  void resetCurrentInfo();
473 
477  void setHandlingCompleted();
478 
484  void setHandlingFailed(const std::string& description);
485 
493  void sendExceptionEncounteredAndReportFailed(
494  std::shared_ptr<SpeakDirectiveInfo> info,
496  const std::string& message);
497 
504  void sendExceptionEncounteredAndReportMissingProperty(
505  std::shared_ptr<SpeakDirectiveInfo> info,
506  const std::string& missingProperty);
507 
514  void sendExceptionEncounteredAndReportUnexpectedPropertyType(
515  std::shared_ptr<SpeakDirectiveInfo> info,
516  const std::string& unexpectedProperty);
517 
521  void releaseForegroundFocus();
522 
531  std::shared_ptr<SpeakDirectiveInfo> validateInfo(
532  const std::string& caller,
533  std::shared_ptr<DirectiveInfo> info,
534  bool checkResult = true);
535 
542  std::shared_ptr<SpeakDirectiveInfo> getSpeakDirectiveInfo(const std::string& messageId);
543 
551  bool setSpeakDirectiveInfo(
552  const std::string& messageId,
553  std::shared_ptr<SpeechSynthesizer::SpeakDirectiveInfo> speakDirectiveInfo);
554 
560  void addToDirectiveQueue(std::shared_ptr<SpeakDirectiveInfo> speakInfo);
561 
567  void removeSpeakDirectiveInfo(const std::string& messageId);
568 
573  void resetMediaSourceId();
574 
580  void clearPendingDirectivesLocked();
581 
586  void managePowerResource(SpeechSynthesizerObserverInterface::SpeechSynthesizerState newState);
587 
594 
596  int64_t m_offsetInMilliseconds;
597 
599  std::shared_ptr<avsCommon::utils::mediaPlayer::MediaPlayerInterface> m_speechPlayer;
600 
602  std::shared_ptr<avsCommon::utils::metrics::MetricRecorderInterface> m_metricRecorder;
603 
605  std::shared_ptr<avsCommon::sdkInterfaces::MessageSenderInterface> m_messageSender;
606 
608  std::shared_ptr<avsCommon::sdkInterfaces::FocusManagerInterface> m_focusManager;
609 
611  std::shared_ptr<avsCommon::sdkInterfaces::ContextManagerInterface> m_contextManager;
612 
614  std::shared_ptr<captions::CaptionManagerInterface> m_captionManager;
615 
617  std::unordered_set<std::shared_ptr<SpeechSynthesizerObserverInterface>> m_observers;
618 
624 
630 
632  avsCommon::avs::FocusState m_currentFocus;
633 
636  std::shared_ptr<SpeakDirectiveInfo> m_currentInfo;
637 
639  std::mutex m_mutex;
640 
642  std::condition_variable m_waitOnStateChange;
643 
645  std::unordered_map<std::string, std::shared_ptr<SpeakDirectiveInfo>> m_speakDirectiveInfoMap;
646 
651  std::mutex m_speakDirectiveInfoMutex;
652 
655  std::deque<std::shared_ptr<SpeakDirectiveInfo>> m_speakInfoQueue;
656 
659  bool m_isShuttingDown;
660 
662  std::mutex m_speakInfoQueueMutex;
663 
665  bool m_initialDialogUXStateReceived;
666 
668  std::unordered_set<std::shared_ptr<avsCommon::avs::CapabilityConfiguration>> m_capabilityConfigurations;
669 
671  std::shared_ptr<avsCommon::sdkInterfaces::PowerResourceManagerInterface::PowerResourceId> m_powerResourceId;
672 
674  std::shared_ptr<avsCommon::sdkInterfaces::PowerResourceManagerInterface> m_powerResourceManager;
675 
683 };
684 
685 } // namespace speechSynthesizer
686 } // namespace capabilityAgents
687 } // namespace alexaClientSDK
688 
689 #endif // ALEXA_CLIENT_SDK_CAPABILITYAGENTS_SPEECHSYNTHESIZER_INCLUDE_SPEECHSYNTHESIZER_SPEECHSYNTHESIZER_H_
Definition: CapabilityAgent.h:47
void onContextAvailable(const std::string &jsonContext) override
MixingBehavior
Definition: MixingBehavior.h:25
Definition: NamespaceAndName.h:32
void onContextFailure(const avsCommon::sdkInterfaces::ContextRequestError error) override
PlayBehavior
Used to determine how a client must handle queueing and playback of a stream.
Definition: PlayBehavior.h:29
void onPlaybackError(SourceId id, const avsCommon::utils::mediaPlayer::ErrorType &type, std::string error, const avsCommon::utils::mediaPlayer::MediaPlayerState &state) override
uint64_t SourceId
A type that identifies which source is currently being operated on. This must be unique across all in...
Definition: MediaPlayerInterface.h:69
::std::string string
Definition: gtest-port.h:1097
void addObserver(std::shared_ptr< SpeechSynthesizerObserverInterface > observer)
ContextRequestError
Definition: ContextRequesterInterface.h:40
Single-thread executor implementation.
Definition: Executor.h:45
ExceptionErrorType
Definition: ExceptionErrorType.h:28
void onBufferUnderrun(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState &state) override
avsCommon::avs::DirectiveHandlerConfiguration getConfiguration() const override
void onFirstByteRead(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState &state) override
FocusState
Definition: FocusState.h:29
DialogUXState
The different dialog specific AVS UX states.
Definition: DialogUXStateObserverInterface.h:32
Definition: CaptionData.h:30
static std::shared_ptr< avsCommon::utils::metrics::MetricRecorderInterface > metricRecorder
Metric recorder shared ptr.
Definition: BaseAPLCapabilityAgentTest.cpp:261
static std::shared_ptr< SpeechSynthesizer > createSpeechSynthesizer(std::shared_ptr< acsdkApplicationAudioPipelineFactoryInterfaces::ApplicationAudioPipelineFactoryInterface > audioPipelineFactory, std::shared_ptr< avsCommon::sdkInterfaces::MessageSenderInterface > messageSender, std::shared_ptr< avsCommon::sdkInterfaces::FocusManagerInterface > focusManager, std::shared_ptr< avsCommon::sdkInterfaces::ContextManagerInterface > contextManager, std::shared_ptr< avsCommon::sdkInterfaces::ExceptionEncounteredSenderInterface > exceptionSender, std::shared_ptr< avsCommon::utils::metrics::MetricRecorderInterface > metricRecorder, std::shared_ptr< avsCommon::avs::DialogUXStateAggregator > dialogUXStateAggregator, std::shared_ptr< captions::CaptionManagerInterface > captionManager=nullptr, std::shared_ptr< avsCommon::sdkInterfaces::PowerResourceManagerInterface > powerResourceManager=nullptr)
static const constexpr char * SPEAK_MEDIA_PLAYER_NAME
String to identify the speak media player to render audio.
Definition: SpeechSynthesizer.h:55
std::unordered_map< directiveRoutingRule::DirectiveRoutingRule, BlockingPolicy > DirectiveHandlerConfiguration
Definition: DirectiveHandlerConfiguration.h:32
void onFocusChanged(avsCommon::avs::FocusState newFocus, avsCommon::avs::MixingBehavior behavior) override
MediaPlayerInterface::SourceId SourceId
A type that identifies which source is currently being operated on.
Definition: MediaPlayerObserverInterface.h:49
void provideState(const avsCommon::avs::NamespaceAndName &stateProviderName, const unsigned int stateRequestToken) override
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
void onPlaybackStopped(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState &state) override
void cancelDirective(std::shared_ptr< DirectiveInfo > info) override
void onPlaybackFinished(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState &state) override
void preHandleDirective(std::shared_ptr< DirectiveInfo > info) override
void removeObserver(std::shared_ptr< SpeechSynthesizerObserverInterface > observer)
type
Definition: upload.py:443
ErrorType
Identifies the specific type of error in a PlaybackFailed event.
Definition: ErrorTypes.h:28
Definition: SpeechSynthesizerObserverInterface.h:32
SpeechSynthesizerState
Definition: SpeechSynthesizerObserverInterface.h:37
std::unordered_set< std::shared_ptr< avsCommon::avs::CapabilityConfiguration > > getCapabilityConfigurations() override
void onPlaybackStarted(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState &state) override
tuple message
Definition: gtest_output_test.py:331
void handleDirective(std::shared_ptr< DirectiveInfo > info) override
void handleDirectiveImmediately(std::shared_ptr< avsCommon::avs::AVSDirective > directive) override
static std::shared_ptr< SpeechSynthesizer > create(std::shared_ptr< avsCommon::utils::mediaPlayer::MediaPlayerInterface > mediaPlayer, std::shared_ptr< avsCommon::sdkInterfaces::MessageSenderInterface > messageSender, std::shared_ptr< avsCommon::sdkInterfaces::FocusManagerInterface > focusManager, std::shared_ptr< avsCommon::sdkInterfaces::ContextManagerInterface > contextManager, std::shared_ptr< avsCommon::sdkInterfaces::ExceptionEncounteredSenderInterface > exceptionSender, std::shared_ptr< avsCommon::utils::metrics::MetricRecorderInterface > metricRecorder, std::shared_ptr< avsCommon::avs::DialogUXStateAggregator > dialogUXStateAggregator, std::shared_ptr< captions::CaptionManagerInterface > captionManager=nullptr, std::shared_ptr< avsCommon::sdkInterfaces::PowerResourceManagerInterface > powerResourceManager=nullptr)
A dialog specific UX state observer.
Definition: DialogUXStateObserverInterface.h:29

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