AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
AudioPlayer.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_ACSDKAUDIOPLAYER_INCLUDE_ACSDKAUDIOPLAYER_AUDIOPLAYER_H_
17 #define ALEXA_CLIENT_SDK_ACSDKAUDIOPLAYER_INCLUDE_ACSDKAUDIOPLAYER_AUDIOPLAYER_H_
18 
19 #include <deque>
20 #include <list>
21 #include <memory>
22 
57 
58 #include "AudioItem.h"
59 #include "ClearBehavior.h"
60 #include "ProgressTimer.h"
61 
62 namespace alexaClientSDK {
63 namespace acsdkAudioPlayer {
64 
68 enum class AudioPlayerState {
70  IDLE,
72  BUFFERING,
77  PLAYING,
82  STOPPED,
84  PAUSED,
86  FINISHED
87 };
88 
89 /*
90  * Convert a @c AudioPlayerState to @c std::string.
91  *
92  * @param state The @c AudioPlayerState to convert.
93  * @return The string representation of @c AudioPlayerState.
94  */
96  switch (state) {
98  return "IDLE";
100  return "PLAYING";
102  return "STOPPED";
104  return "PAUSED";
106  return "BUFFERING";
108  return "BUFFER_UNDERRUN";
110  return "FINISHED";
111  }
112  return "unknown AudioPlayerState";
113 }
114 
133  , public std::enable_shared_from_this<AudioPlayer> {
134 public:
138  virtual ~AudioPlayer();
139 
160  static std::shared_ptr<acsdkAudioPlayerInterfaces::AudioPlayerInterface> createAudioPlayerInterface(
161  const std::shared_ptr<avsCommon::utils::mediaPlayer::PooledMediaResourceProviderInterface>&
162  mediaResourceProvider,
163  const std::shared_ptr<avsCommon::sdkInterfaces::MessageSenderInterface>& messageSender,
166  avsCommon::sdkInterfaces::FocusManagerInterface>& annotatedFocusManager,
167  const std::shared_ptr<avsCommon::sdkInterfaces::ContextManagerInterface>& contextManager,
168  const std::shared_ptr<avsCommon::sdkInterfaces::ExceptionEncounteredSenderInterface>& exceptionSender,
169  const std::shared_ptr<avsCommon::sdkInterfaces::PlaybackRouterInterface>& playbackRouter,
170  const std::shared_ptr<captions::CaptionManagerInterface>& captionManager,
171  const std::shared_ptr<avsCommon::utils::metrics::MetricRecorderInterface>& metricRecorder,
172  const std::shared_ptr<acsdkShutdownManagerInterfaces::ShutdownNotifierInterface>& shutdownNotifier,
176  const std::shared_ptr<avsCommon::sdkInterfaces::RenderPlayerInfoCardsProviderRegistrarInterface>&
177  renderPlayerInfoCardsProviderRegistrar,
178  const std::shared_ptr<cryptoInterfaces::CryptoFactoryInterface>& cryptoFactory);
179 
195  static std::shared_ptr<AudioPlayer> create(
196  std::shared_ptr<avsCommon::utils::mediaPlayer::PooledMediaResourceProviderInterface> mediaResourceProvider,
197  std::shared_ptr<avsCommon::sdkInterfaces::MessageSenderInterface> messageSender,
198  std::shared_ptr<avsCommon::sdkInterfaces::FocusManagerInterface> focusManager,
199  std::shared_ptr<avsCommon::sdkInterfaces::ContextManagerInterface> contextManager,
200  std::shared_ptr<avsCommon::sdkInterfaces::ExceptionEncounteredSenderInterface> exceptionSender,
201  std::shared_ptr<avsCommon::sdkInterfaces::PlaybackRouterInterface> playbackRouter,
202  std::shared_ptr<cryptoInterfaces::CryptoFactoryInterface> cryptoFactory,
203  std::shared_ptr<captions::CaptionManagerInterface> captionManager = nullptr,
204  std::shared_ptr<avsCommon::utils::metrics::MetricRecorderInterface> metricRecorder = nullptr);
205 
208  void provideState(const avsCommon::avs::NamespaceAndName& stateProviderName, unsigned int stateRequestToken)
209  override;
211 
214  void handleDirectiveImmediately(std::shared_ptr<avsCommon::avs::AVSDirective> directive) override;
215  void preHandleDirective(std::shared_ptr<DirectiveInfo> info) override;
216  void handleDirective(std::shared_ptr<DirectiveInfo> info) override;
217  void cancelDirective(std::shared_ptr<DirectiveInfo> info) override;
218  void onDeregistered() override;
219  avsCommon::avs::DirectiveHandlerConfiguration getConfiguration() const override;
221 
224  void onFocusChanged(avsCommon::avs::FocusState newFocus, avsCommon::avs::MixingBehavior behavior) override;
226 
229  void onFirstByteRead(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState& state) override;
230  void onPlaybackStarted(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState& state) override;
231  void onPlaybackStopped(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState& state) override;
232  void onPlaybackFinished(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState& state) override;
233  void onPlaybackError(
234  SourceId id,
236  std::string error,
238  void onPlaybackPaused(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState& state) override;
239  void onPlaybackResumed(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState& state) override;
240  void onBufferUnderrun(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState& state) override;
241  void onBufferRefilled(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState& state) override;
242  void onBufferingComplete(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState& state) override;
243  void onSeeked(
244  SourceId id,
246  const avsCommon::utils::mediaPlayer::MediaPlayerState& endState) override;
247  void onTags(
248  SourceId id,
249  std::unique_ptr<const VectorOfTags> vectorOfTags,
252 
255  void onReadyToProvideNextPlayer() override;
257 
260  void onProgressReportDelayElapsed() override;
261  void onProgressReportIntervalElapsed() override;
262  void onProgressReportIntervalUpdated() override;
263  void requestProgress() override;
265 
268  void addObserver(std::shared_ptr<acsdkAudioPlayerInterfaces::AudioPlayerObserverInterface> observer) override;
269  void removeObserver(std::shared_ptr<acsdkAudioPlayerInterfaces::AudioPlayerObserverInterface> observer) override;
270  void stopPlayback() override;
272 
275  void setObserver(
276  std::shared_ptr<avsCommon::sdkInterfaces::RenderPlayerInfoCardsObserverInterface> observer) override;
278 
281  bool localOperation(PlaybackOperation op) override;
282  bool localSeekTo(std::chrono::milliseconds location, bool fromStart) override;
284 
287  std::chrono::milliseconds getAudioItemOffset() override;
288  std::chrono::milliseconds getAudioItemDuration() override;
290 
293  std::unordered_set<std::shared_ptr<avsCommon::avs::CapabilityConfiguration>> getCapabilityConfigurations() override;
295 
298  void doShutdown() override;
300 
301 private:
305  class MessageRequestObserver : public avsCommon::avs::MessageRequest {
306  public:
315  MessageRequestObserver(
316  std::shared_ptr<avsCommon::utils::metrics::MetricRecorderInterface> metricRecorder,
317  const std::string& jsonContent,
318  const std::string& uriPathExtension = "");
319 
322  void sendCompleted(
325  private:
327  std::shared_ptr<avsCommon::utils::metrics::MetricRecorderInterface> mMetricRecorder;
328  };
332  struct PlayDirectiveInfo {
334  const std::string messageId;
335 
337  const std::string dialogRequestId;
338 
340  std::string stopMessageId;
341 
343  avsCommon::avs::PlayRequestor playRequestor;
344 
346  AudioItem audioItem;
347 
349  avsCommon::avs::PlayBehavior playBehavior;
350 
353 
356  SourceId sourceId;
357 
359  std::shared_ptr<avsCommon::utils::mediaPlayer::MediaPlayerInterface> mediaPlayer;
360 
362  std::chrono::milliseconds initialOffset;
363 
365  std::string errorMsg;
366 
369 
372  bool isBuffered;
373 
375  bool isPNFSent;
376 
378  bool normalizationEnabled;
379 
382 
384  std::shared_ptr<const VectorOfTags> cachedMetadata;
385 
387  std::vector<avsCommon::utils::audioAnalyzer::AudioAnalyzerState> analyzersData;
388 
395  PlayDirectiveInfo(const std::string& messageId, const std::string& dialogRequestId);
396  };
397 
416  AudioPlayer(
417  std::shared_ptr<avsCommon::utils::mediaPlayer::PooledMediaResourceProviderInterface> mediaResourceProvider,
418  std::shared_ptr<avsCommon::sdkInterfaces::MessageSenderInterface> messageSender,
419  std::shared_ptr<avsCommon::sdkInterfaces::FocusManagerInterface> focusManager,
420  std::shared_ptr<avsCommon::sdkInterfaces::ContextManagerInterface> contextManager,
421  std::shared_ptr<avsCommon::sdkInterfaces::ExceptionEncounteredSenderInterface> exceptionSender,
422  std::shared_ptr<avsCommon::sdkInterfaces::PlaybackRouterInterface> playbackRouter,
423  std::vector<std::shared_ptr<avsCommon::sdkInterfaces::ChannelVolumeInterface>> audioChannelVolumeInterfaces,
424  std::shared_ptr<cryptoInterfaces::CryptoFactoryInterface> cryptoFactory,
425  std::shared_ptr<captions::CaptionManagerInterface> captionManager = nullptr,
426  std::shared_ptr<avsCommon::utils::metrics::MetricRecorderInterface> metricRecorder = nullptr);
427 
435  bool parseDirectivePayload(std::shared_ptr<DirectiveInfo> info, rapidjson::Document* document);
436 
442  void preHandlePlayDirective(std::shared_ptr<DirectiveInfo> info);
443 
449  void handlePlayDirective(std::shared_ptr<DirectiveInfo> info);
450 
456  void handleStopDirective(std::shared_ptr<DirectiveInfo> info);
457 
463  void handleClearQueueDirective(std::shared_ptr<DirectiveInfo> info);
464 
470  void handleUpdateProgressReportIntervalDirective(std::shared_ptr<DirectiveInfo> info);
471 
477  void removeDirective(std::shared_ptr<DirectiveInfo> info);
478 
484  void setHandlingCompleted(std::shared_ptr<DirectiveInfo> info);
485 
493 
504  void executeProvideState(bool sendToken = false, unsigned int stateRequestToken = 0);
505 
520  void executeOnFocusChanged(avsCommon::avs::FocusState newFocus, avsCommon::avs::MixingBehavior behavior);
521 
528  void executeOnPlaybackStarted(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState& state);
529 
536  void executeOnPlaybackStopped(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState& state);
537 
544  void executeOnPlaybackFinished(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState& state);
545 
552  void executeOnBufferingComplete(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState& state);
553 
562  void executeOnSeeked(
563  SourceId id,
566 
568  void handlePlaybackCompleted();
569 
575  void callProgressTimer(std::function<void()> call);
576 
582  void setIsInProgress(bool isInProgress);
583 
592  void executeOnPlaybackError(
593  SourceId id,
595  std::string error,
597 
604  void executeOnPlaybackPaused(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState& state);
605 
612  void executeOnPlaybackResumed(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState& state);
613 
620  void executeOnBufferUnderrun(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState& state);
621 
628  void executeOnBufferRefilled(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState& state);
629 
637  void executeOnTags(
638  SourceId id,
639  std::shared_ptr<const VectorOfTags> vectorOfTags,
641 
645  void executeOnReadyToProvideNextPlayer();
646 
652  void executePrePlay(std::shared_ptr<PlayDirectiveInfo> info);
653 
659  void executePlay(const std::string& messageId);
660 
662  void playNextItem();
663 
670  void executeStop(const std::string& messageId = "", bool startNextSong = false);
671 
677  void executeClearQueue(ClearBehavior clearBehavior);
678 
684  void executeUpdateProgressReportInterval(std::chrono::milliseconds progressReportInterval);
685 
689  void executeLocalOperation(PlaybackOperation op, std::promise<bool> success);
690 
694  void executeLocalOperationTimedout();
695 
702  bool executeLocalSeekTo(std::chrono::milliseconds location, bool fromStart);
703 
709  std::chrono::milliseconds getDuration();
710 
716  void changeState(AudioPlayerState state);
717 
727  void sendEventWithTokenAndOffset(
728  const std::string& eventName,
729  bool includePlaybackReports = false,
730  std::chrono::milliseconds offset = avsCommon::utils::mediaPlayer::MEDIA_PLAYER_INVALID_OFFSET);
731 
737  void sendPlaybackStartedEvent(const avsCommon::utils::mediaPlayer::MediaPlayerState& state);
738 
744  void sendPlaybackNearlyFinishedEvent(const avsCommon::utils::mediaPlayer::MediaPlayerState& state);
745 
751  void sendPlaybackStutterStartedEvent(const avsCommon::utils::mediaPlayer::MediaPlayerState& state);
752 
758  void sendPlaybackStutterFinishedEvent(const avsCommon::utils::mediaPlayer::MediaPlayerState& state);
759 
765  void sendPlaybackFinishedEvent(const avsCommon::utils::mediaPlayer::MediaPlayerState& state);
766 
774  void sendPlaybackSeekedEvent(
777 
786  void sendPlaybackFailedEvent(
787  const std::string& failingToken,
789  const std::string& message,
791 
797  void sendPlaybackStoppedEvent(const avsCommon::utils::mediaPlayer::MediaPlayerState& state);
798 
804  void sendPlaybackPausedEvent(const avsCommon::utils::mediaPlayer::MediaPlayerState& state);
805 
811  void sendPlaybackResumedEvent(const avsCommon::utils::mediaPlayer::MediaPlayerState& state);
812 
814  void sendPlaybackQueueClearedEvent();
815 
824  void sendEvent(
825  const std::string& eventName,
826  const std::string& dialogRequestIdString = "",
827  const std::string& payload = "{}",
828  const std::string& context = "");
829 
837  void sendStreamMetadataExtractedEvent(
838  AudioItem& audioItem,
839  std::shared_ptr<const VectorOfTags> vectorOfTags,
841 
843  void notifyObserver();
844 
850  void notifySeekActivity(
852  std::chrono::milliseconds offset);
853 
856  std::chrono::milliseconds offset = std::chrono::milliseconds(-1));
857 
868  std::chrono::milliseconds getOffset();
869 
876  void attachPlaybackAttributesIfAvailable(rapidjson::Value& parent, rapidjson::Document::AllocatorType& allocator);
877 
884  void attachPlaybackReportsIfAvailable(rapidjson::Value& parent, rapidjson::Document::AllocatorType& allocator);
885 
892 
898  void clearPlayQueue(const bool stopCurrentPlayer);
899 
906  void stopAndReleaseMediaPlayer(std::shared_ptr<PlayDirectiveInfo> playbackItem);
907 
913  void releaseMediaPlayer(std::shared_ptr<PlayDirectiveInfo> playbackItem);
914 
921  bool configureMediaPlayer(std::shared_ptr<PlayDirectiveInfo>& playbackItem);
922 
929  bool isMessageInQueue(const std::string& messageId);
930 
937  bool executeStopDucking();
938 
945  bool executeStartDucking();
946 
953  void parseHeadersFromPlayDirective(const rapidjson::Value& httpHeaders, AudioItem* audioItem);
954 
962  std::string getTrackProtectionName(
964 
972  std::string getTrackProtectionName(const avsCommon::utils::mediaPlayer::MediaPlayerState& mediaPlayerState) const;
973 
981  std::string getPlaylistType(const avsCommon::utils::mediaPlayer::MediaPlayerState& mediaPlayerState) const;
982 
990  std::string getPlaylistType(const std::string& playlistType) const;
991 
999  std::string packageContextForEvent(
1000  std::chrono::milliseconds offsetOverride = avsCommon::utils::mediaPlayer::MEDIA_PLAYER_INVALID_OFFSET) const;
1001 
1008  avsCommon::avs::PlayerActivity activityFromState(AudioPlayerState state) const;
1009 
1016  std::string getDomainNameHash(const std::string& url) const;
1017 
1024  bool isPlaybackActive() const;
1025 
1028 
1031  std::shared_ptr<avsCommon::utils::mediaPlayer::PooledMediaResourceProviderInterface> m_mediaResourceProvider;
1032 
1034  std::shared_ptr<avsCommon::sdkInterfaces::MessageSenderInterface> m_messageSender;
1035 
1037  std::shared_ptr<avsCommon::sdkInterfaces::FocusManagerInterface> m_focusManager;
1038 
1040  std::shared_ptr<avsCommon::sdkInterfaces::ContextManagerInterface> m_contextManager;
1041 
1043  std::shared_ptr<avsCommon::sdkInterfaces::PlaybackRouterInterface> m_playbackRouter;
1044 
1046  std::shared_ptr<cryptoInterfaces::CryptoFactoryInterface> m_cryptoFactory;
1047 
1049  std::shared_ptr<captions::CaptionManagerInterface> m_captionManager;
1050 
1052  std::shared_ptr<avsCommon::utils::metrics::MetricRecorderInterface> m_metricRecorder;
1053 
1068  AudioPlayerState m_currentState;
1069 
1071  mutable std::mutex m_currentStateMutex;
1072 
1074  std::condition_variable m_currentStateConditionVariable;
1075 
1082 
1086 
1087  /*
1088  * The queue of @c PlayDirectiveInfo to play. The @c PlayBehavior is already resolved when items are
1089  * added to the queue. This queue is used to find the next @c AudioItem to play when @c playNextItem() is called.
1090  */
1091  std::deque<std::shared_ptr<PlayDirectiveInfo>> m_audioPlayQueue;
1092 
1096  std::shared_ptr<PlayDirectiveInfo> m_currentlyPlaying;
1097 
1102  std::shared_ptr<PlayDirectiveInfo> m_itemPendingPlaybackStart;
1103 
1105  std::chrono::steady_clock::time_point m_bufferUnderrunTimestamp;
1106 
1108  ProgressTimer m_progressTimer;
1109 
1115  std::chrono::milliseconds m_offset;
1116 
1118  std::unordered_set<std::shared_ptr<acsdkAudioPlayerInterfaces::AudioPlayerObserverInterface>> m_observers;
1119 
1121  std::shared_ptr<avsCommon::sdkInterfaces::RenderPlayerInfoCardsObserverInterface> m_renderPlayerObserver;
1122 
1127  bool m_playNextItemAfterStopped;
1128 
1133  bool m_isStopCalled;
1134 
1138  bool m_okToRequestNextTrack;
1139 
1146 
1150  std::string m_currentPlaylistType;
1152 
1159  bool m_isAcquireChannelRequestPending;
1160 
1162 
1164  std::unordered_set<std::shared_ptr<avsCommon::avs::CapabilityConfiguration>> m_capabilityConfigurations;
1165 
1167  avsCommon::avs::ContentType m_currentMixability;
1168 
1170  avsCommon::avs::MixingBehavior m_mixingBehavior;
1171 
1173  avsCommon::utils::metrics::DataPointDurationBuilder m_playbackTimeMetricData;
1174 
1176  avsCommon::utils::metrics::DataPointDurationBuilder m_autoProgressTimeMetricData;
1177 
1179  avsCommon::utils::metrics::DataPointDurationBuilder m_playCommandToPlayingTimeMetricData;
1180 
1182  bool m_isRecordingTimeToPlayback;
1183 
1185  bool m_isAutoProgressing;
1186 
1188  bool m_isLocalResumePending;
1189 
1191  std::promise<bool> m_localResumeSuccess;
1192 
1194  bool m_isStartingPlayback;
1195 
1197  bool m_isPausingPlayback;
1198 
1200  std::vector<std::shared_ptr<avsCommon::sdkInterfaces::ChannelVolumeInterface>> m_audioChannelVolumeInterfaces;
1201 
1203  std::string m_cachedContext;
1204 
1212 
1217  std::string m_lastDialogRequestId;
1218 };
1219 
1220 } // namespace acsdkAudioPlayer
1221 } // namespace alexaClientSDK
1222 
1223 #endif // ALEXA_CLIENT_SDK_ACSDKAUDIOPLAYER_INCLUDE_ACSDKAUDIOPLAYER_AUDIOPLAYER_H_
Definition: CapabilityAgent.h:47
MixingBehavior
Definition: MixingBehavior.h:25
Definition: NamespaceAndName.h:32
PlayBehavior
Used to determine how a client must handle queueing and playback of a stream.
Definition: PlayBehavior.h:29
Definition: MessageRequest.h:40
Indicates that an audio stream is pre-buffering, but is not ready to play.
::std::string string
Definition: gtest-port.h:1097
std::string playerStateToString(AudioPlayerState state)
Definition: AudioPlayer.h:95
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
static const std::chrono::milliseconds MEDIA_PLAYER_INVALID_OFFSET
Represents offset returned when MediaPlayer is in an invalid state.
Definition: MediaPlayerInterface.h:42
FocusState
Definition: FocusState.h:29
Indicates that audio is currently playing.
Initial state, prior to acting on the first Play directive, or after the current queue is finished...
static std::shared_ptr< avsCommon::utils::metrics::MetricRecorderInterface > metricRecorder
Metric recorder shared ptr.
Definition: BaseAPLCapabilityAgentTest.cpp:261
ClearBehavior
Used to determine clear queue behavior.
Definition: ClearBehavior.h:28
bool Value(const T &value, M matcher)
Definition: gmock-matchers.h:4347
std::unordered_map< directiveRoutingRule::DirectiveRoutingRule, BlockingPolicy > DirectiveHandlerConfiguration
Definition: DirectiveHandlerConfiguration.h:32
PlaybackOperation
Definition: LocalPlaybackHandlerInterface.h:40
Indicates that the audio stream has been paused.
MixingBehavior
Definition: MixingBehavior.h:27
MediaPlayerInterface::SourceId SourceId
A type that identifies which source is currently being operated on.
Definition: MediaPlayerObserverInterface.h:49
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
AudioPlayerState
Definition: AudioPlayer.h:68
ContentType
Definition: ContentType.h:25
type
Definition: upload.py:443
ErrorType
Identifies the specific type of error in a PlaybackFailed event.
Definition: ErrorTypes.h:28
Definition: PlayRequestor.h:29
Status
Definition: MessageRequestObserverInterface.h:33
PlayerActivity
Identifies the player state.
Definition: PlayerActivity.h:27
tuple message
Definition: gtest_output_test.py:331
Definition: RenderPlayerInfoCardsProviderInterface.h:30
SeekStatus
Enum representing Seek activities.
Definition: AudioPlayerObserverInterface.h:63
Struct which contains all the fields which define an audio item for a Play directive.
Definition: AudioItem.h:33

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