AlexaClientSDK  1.26.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 
56 
57 #include "AudioItem.h"
58 #include "ClearBehavior.h"
59 #include "ProgressTimer.h"
60 
61 namespace alexaClientSDK {
62 namespace acsdkAudioPlayer {
63 
67 enum class AudioPlayerState {
69  IDLE,
71  BUFFERING,
76  PLAYING,
81  STOPPED,
83  PAUSED,
85  FINISHED
86 };
87 
88 /*
89  * Convert a @c AudioPlayerState to @c std::string.
90  *
91  * @param state The @c AudioPlayerState to convert.
92  * @return The string representation of @c AudioPlayerState.
93  */
94 inline std::string playerStateToString(AudioPlayerState state) {
95  switch (state) {
97  return "IDLE";
99  return "PLAYING";
101  return "STOPPED";
103  return "PAUSED";
105  return "BUFFERING";
107  return "BUFFER_UNDERRUN";
109  return "FINISHED";
110  }
111  return "unknown AudioPlayerState";
112 }
113 
132  , public std::enable_shared_from_this<AudioPlayer> {
133 public:
137  virtual ~AudioPlayer() = default;
138 
158  static std::shared_ptr<acsdkAudioPlayerInterfaces::AudioPlayerInterface> createAudioPlayerInterface(
159  const std::shared_ptr<avsCommon::utils::mediaPlayer::PooledMediaResourceProviderInterface>&
160  mediaResourceProvider,
161  const std::shared_ptr<avsCommon::sdkInterfaces::MessageSenderInterface>& messageSender,
164  avsCommon::sdkInterfaces::FocusManagerInterface>& annotatedFocusManager,
165  const std::shared_ptr<avsCommon::sdkInterfaces::ContextManagerInterface>& contextManager,
166  const std::shared_ptr<avsCommon::sdkInterfaces::ExceptionEncounteredSenderInterface>& exceptionSender,
167  const std::shared_ptr<avsCommon::sdkInterfaces::PlaybackRouterInterface>& playbackRouter,
168  const std::shared_ptr<captions::CaptionManagerInterface>& captionManager,
169  const std::shared_ptr<avsCommon::utils::metrics::MetricRecorderInterface>& metricRecorder,
170  const std::shared_ptr<acsdkShutdownManagerInterfaces::ShutdownNotifierInterface>& shutdownNotifier,
174  const std::shared_ptr<avsCommon::sdkInterfaces::RenderPlayerInfoCardsProviderRegistrarInterface>&
175  renderPlayerInfoCardsProviderRegistrar);
176 
191  static std::shared_ptr<AudioPlayer> create(
192  std::shared_ptr<avsCommon::utils::mediaPlayer::PooledMediaResourceProviderInterface> mediaResourceProvider,
193  std::shared_ptr<avsCommon::sdkInterfaces::MessageSenderInterface> messageSender,
194  std::shared_ptr<avsCommon::sdkInterfaces::FocusManagerInterface> focusManager,
195  std::shared_ptr<avsCommon::sdkInterfaces::ContextManagerInterface> contextManager,
196  std::shared_ptr<avsCommon::sdkInterfaces::ExceptionEncounteredSenderInterface> exceptionSender,
197  std::shared_ptr<avsCommon::sdkInterfaces::PlaybackRouterInterface> playbackRouter,
198  std::shared_ptr<captions::CaptionManagerInterface> captionManager = nullptr,
199  std::shared_ptr<avsCommon::utils::metrics::MetricRecorderInterface> metricRecorder = nullptr);
200 
203  void provideState(const avsCommon::avs::NamespaceAndName& stateProviderName, unsigned int stateRequestToken)
204  override;
206 
209  void handleDirectiveImmediately(std::shared_ptr<avsCommon::avs::AVSDirective> directive) override;
210  void preHandleDirective(std::shared_ptr<DirectiveInfo> info) override;
211  void handleDirective(std::shared_ptr<DirectiveInfo> info) override;
212  void cancelDirective(std::shared_ptr<DirectiveInfo> info) override;
213  void onDeregistered() override;
214  avsCommon::avs::DirectiveHandlerConfiguration getConfiguration() const override;
216 
219  void onFocusChanged(avsCommon::avs::FocusState newFocus, avsCommon::avs::MixingBehavior behavior) override;
221 
224  void onFirstByteRead(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState& state) override;
225  void onPlaybackStarted(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState& state) override;
226  void onPlaybackStopped(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState& state) override;
227  void onPlaybackFinished(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState& state) override;
228  void onPlaybackError(
229  SourceId id,
231  std::string error,
233  void onPlaybackPaused(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState& state) override;
234  void onPlaybackResumed(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState& state) override;
235  void onBufferUnderrun(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState& state) override;
236  void onBufferRefilled(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState& state) override;
237  void onBufferingComplete(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState& state) override;
238  void onSeeked(
239  SourceId id,
241  const avsCommon::utils::mediaPlayer::MediaPlayerState& endState) override;
242  void onTags(
243  SourceId id,
244  std::unique_ptr<const VectorOfTags> vectorOfTags,
247 
250  void onReadyToProvideNextPlayer() override;
252 
255  void onProgressReportDelayElapsed() override;
256  void onProgressReportIntervalElapsed() override;
257  void onProgressReportIntervalUpdated() override;
258  void requestProgress() override;
260 
263  void addObserver(std::shared_ptr<acsdkAudioPlayerInterfaces::AudioPlayerObserverInterface> observer) override;
264  void removeObserver(std::shared_ptr<acsdkAudioPlayerInterfaces::AudioPlayerObserverInterface> observer) override;
265  void stopPlayback() override;
267 
270  void setObserver(
271  std::shared_ptr<avsCommon::sdkInterfaces::RenderPlayerInfoCardsObserverInterface> observer) override;
273 
276  bool localOperation(PlaybackOperation op) override;
277  bool localSeekTo(std::chrono::milliseconds location, bool fromStart) override;
279 
282  std::chrono::milliseconds getAudioItemOffset() override;
283  std::chrono::milliseconds getAudioItemDuration() override;
285 
288  std::unordered_set<std::shared_ptr<avsCommon::avs::CapabilityConfiguration>> getCapabilityConfigurations() override;
290 
291 private:
295  class MessageRequestObserver : public avsCommon::avs::MessageRequest {
296  public:
305  MessageRequestObserver(
306  std::shared_ptr<avsCommon::utils::metrics::MetricRecorderInterface> metricRecorder,
307  const std::string& jsonContent,
308  const std::string& uriPathExtension = "");
309 
312  void sendCompleted(
315  private:
317  std::shared_ptr<avsCommon::utils::metrics::MetricRecorderInterface> mMetricRecorder;
318  };
322  struct PlayDirectiveInfo {
324  const std::string messageId;
325 
327  const std::string dialogRequestId;
328 
330  std::string stopMessageId;
331 
333  avsCommon::avs::PlayRequestor playRequestor;
334 
336  AudioItem audioItem;
337 
339  avsCommon::avs::PlayBehavior playBehavior;
340 
343 
346  SourceId sourceId;
347 
349  std::shared_ptr<avsCommon::utils::mediaPlayer::MediaPlayerInterface> mediaPlayer;
350 
352  std::chrono::milliseconds initialOffset;
353 
355  std::string errorMsg;
356 
359 
362  bool isBuffered;
363 
365  bool isPNFSent;
366 
368  bool normalizationEnabled;
369 
372 
374  std::shared_ptr<const VectorOfTags> cachedMetadata;
375 
377  std::vector<avsCommon::utils::audioAnalyzer::AudioAnalyzerState> analyzersData;
378 
386  PlayDirectiveInfo(const std::string& messageId, const std::string& dialogRequestId);
387  };
388 
406  AudioPlayer(
407  std::shared_ptr<avsCommon::utils::mediaPlayer::PooledMediaResourceProviderInterface> mediaResourceProvider,
408  std::shared_ptr<avsCommon::sdkInterfaces::MessageSenderInterface> messageSender,
409  std::shared_ptr<avsCommon::sdkInterfaces::FocusManagerInterface> focusManager,
410  std::shared_ptr<avsCommon::sdkInterfaces::ContextManagerInterface> contextManager,
411  std::shared_ptr<avsCommon::sdkInterfaces::ExceptionEncounteredSenderInterface> exceptionSender,
412  std::shared_ptr<avsCommon::sdkInterfaces::PlaybackRouterInterface> playbackRouter,
413  std::vector<std::shared_ptr<avsCommon::sdkInterfaces::ChannelVolumeInterface>> audioChannelVolumeInterfaces,
414  std::shared_ptr<captions::CaptionManagerInterface> captionManager = nullptr,
415  std::shared_ptr<avsCommon::utils::metrics::MetricRecorderInterface> metricRecorder = nullptr);
416 
419  void doShutdown() override;
421 
429  bool parseDirectivePayload(std::shared_ptr<DirectiveInfo> info, rapidjson::Document* document);
430 
436  void preHandlePlayDirective(std::shared_ptr<DirectiveInfo> info);
437 
443  void handlePlayDirective(std::shared_ptr<DirectiveInfo> info);
444 
450  void handleStopDirective(std::shared_ptr<DirectiveInfo> info);
451 
457  void handleClearQueueDirective(std::shared_ptr<DirectiveInfo> info);
458 
464  void handleUpdateProgressReportIntervalDirective(std::shared_ptr<DirectiveInfo> info);
465 
471  void removeDirective(std::shared_ptr<DirectiveInfo> info);
472 
478  void setHandlingCompleted(std::shared_ptr<DirectiveInfo> info);
479 
487 
498  void executeProvideState(bool sendToken = false, unsigned int stateRequestToken = 0);
499 
514  void executeOnFocusChanged(avsCommon::avs::FocusState newFocus, avsCommon::avs::MixingBehavior behavior);
515 
522  void executeOnPlaybackStarted(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState& state);
523 
530  void executeOnPlaybackStopped(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState& state);
531 
538  void executeOnPlaybackFinished(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState& state);
539 
546  void executeOnBufferingComplete(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState& state);
547 
556  void executeOnSeeked(
557  SourceId id,
560 
562  void handlePlaybackCompleted();
563 
569  void callProgressTimer(std::function<void()> call);
570 
576  void setIsInProgress(bool isInProgress);
577 
586  void executeOnPlaybackError(
587  SourceId id,
589  std::string error,
591 
598  void executeOnPlaybackPaused(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState& state);
599 
606  void executeOnPlaybackResumed(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState& state);
607 
614  void executeOnBufferUnderrun(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState& state);
615 
622  void executeOnBufferRefilled(SourceId id, const avsCommon::utils::mediaPlayer::MediaPlayerState& state);
623 
631  void executeOnTags(
632  SourceId id,
633  std::shared_ptr<const VectorOfTags> vectorOfTags,
635 
639  void executeOnReadyToProvideNextPlayer();
640 
646  void executePrePlay(std::shared_ptr<PlayDirectiveInfo> info);
647 
653  void executePlay(const std::string& messageId);
654 
656  void playNextItem();
657 
664  void executeStop(const std::string& messageId = "", bool startNextSong = false);
665 
671  void executeClearQueue(ClearBehavior clearBehavior);
672 
678  void executeUpdateProgressReportInterval(std::chrono::milliseconds progressReportInterval);
679 
683  void executeLocalOperation(PlaybackOperation op, std::promise<bool> success);
684 
688  void executeLocalOperationTimedout();
689 
696  bool executeLocalSeekTo(std::chrono::milliseconds location, bool fromStart);
697 
703  std::chrono::milliseconds getDuration();
704 
710  void changeState(AudioPlayerState state);
711 
721  void sendEventWithTokenAndOffset(
722  const std::string& eventName,
723  bool includePlaybackReports = false,
724  std::chrono::milliseconds offset = avsCommon::utils::mediaPlayer::MEDIA_PLAYER_INVALID_OFFSET);
725 
731  void sendPlaybackStartedEvent(const avsCommon::utils::mediaPlayer::MediaPlayerState& state);
732 
738  void sendPlaybackNearlyFinishedEvent(const avsCommon::utils::mediaPlayer::MediaPlayerState& state);
739 
745  void sendPlaybackStutterStartedEvent(const avsCommon::utils::mediaPlayer::MediaPlayerState& state);
746 
752  void sendPlaybackStutterFinishedEvent(const avsCommon::utils::mediaPlayer::MediaPlayerState& state);
753 
759  void sendPlaybackFinishedEvent(const avsCommon::utils::mediaPlayer::MediaPlayerState& state);
760 
768  void sendPlaybackSeekedEvent(
771 
780  void sendPlaybackFailedEvent(
781  const std::string& failingToken,
783  const std::string& message,
785 
791  void sendPlaybackStoppedEvent(const avsCommon::utils::mediaPlayer::MediaPlayerState& state);
792 
798  void sendPlaybackPausedEvent(const avsCommon::utils::mediaPlayer::MediaPlayerState& state);
799 
805  void sendPlaybackResumedEvent(const avsCommon::utils::mediaPlayer::MediaPlayerState& state);
806 
808  void sendPlaybackQueueClearedEvent();
809 
819  void sendEvent(
820  const std::string& eventName,
821  const std::string& dialogRequestIdString = "",
822  const std::string& payload = "{}",
823  const std::string& context = "");
824 
833  void sendStreamMetadataExtractedEvent(
834  AudioItem& audioItem,
835  std::shared_ptr<const VectorOfTags> vectorOfTags,
837 
839  void notifyObserver();
840 
846  void notifySeekActivity(
848  std::chrono::milliseconds offset);
849 
852  std::chrono::milliseconds offset = std::chrono::milliseconds(-1));
853 
864  std::chrono::milliseconds getOffset();
865 
872  void attachPlaybackAttributesIfAvailable(rapidjson::Value& parent, rapidjson::Document::AllocatorType& allocator);
873 
880  void attachPlaybackReportsIfAvailable(rapidjson::Value& parent, rapidjson::Document::AllocatorType& allocator);
881 
888 
894  void clearPlayQueue(const bool stopCurrentPlayer);
895 
902  void stopAndReleaseMediaPlayer(std::shared_ptr<PlayDirectiveInfo> playbackItem);
903 
909  void releaseMediaPlayer(std::shared_ptr<PlayDirectiveInfo> playbackItem);
910 
917  bool configureMediaPlayer(std::shared_ptr<PlayDirectiveInfo>& playbackItem);
918 
925  bool isMessageInQueue(const std::string& messageId);
926 
933  bool executeStopDucking();
934 
941  bool executeStartDucking();
942 
949  void parseHeadersFromPlayDirective(const rapidjson::Value& httpHeaders, AudioItem* audioItem);
950 
958  std::string getTrackProtectionName(
960 
968  std::string getTrackProtectionName(const avsCommon::utils::mediaPlayer::MediaPlayerState& mediaPlayerState) const;
969 
977  std::string getPlaylistType(const avsCommon::utils::mediaPlayer::MediaPlayerState& mediaPlayerState) const;
978 
986  std::string getPlaylistType(const std::string& playlistType) const;
987 
995  std::string packageContextForEvent(
996  std::chrono::milliseconds offsetOverride = avsCommon::utils::mediaPlayer::MEDIA_PLAYER_INVALID_OFFSET) const;
997 
1004  avsCommon::avs::PlayerActivity activityFromState(AudioPlayerState state) const;
1005 
1012  std::string getDomainNameHash(const std::string& url) const;
1013 
1016 
1019  std::shared_ptr<avsCommon::utils::mediaPlayer::PooledMediaResourceProviderInterface> m_mediaResourceProvider;
1020 
1022  std::shared_ptr<avsCommon::sdkInterfaces::MessageSenderInterface> m_messageSender;
1023 
1025  std::shared_ptr<avsCommon::sdkInterfaces::FocusManagerInterface> m_focusManager;
1026 
1028  std::shared_ptr<avsCommon::sdkInterfaces::ContextManagerInterface> m_contextManager;
1029 
1031  std::shared_ptr<avsCommon::sdkInterfaces::PlaybackRouterInterface> m_playbackRouter;
1032 
1034  std::shared_ptr<captions::CaptionManagerInterface> m_captionManager;
1035 
1037  std::shared_ptr<avsCommon::utils::metrics::MetricRecorderInterface> m_metricRecorder;
1038 
1053  AudioPlayerState m_currentState;
1054 
1056  std::mutex m_currentStateMutex;
1057 
1059  std::condition_variable m_currentStateConditionVariable;
1060 
1067 
1071 
1072  /*
1073  * The queue of @c PlayDirectiveInfo to play. The @c PlayBehavior is already resolved when items are
1074  * added to the queue. This queue is used to find the next @c AudioItem to play when @c playNextItem() is called.
1075  */
1076  std::deque<std::shared_ptr<PlayDirectiveInfo>> m_audioPlayQueue;
1077 
1081  std::shared_ptr<PlayDirectiveInfo> m_currentlyPlaying;
1082 
1087  std::shared_ptr<PlayDirectiveInfo> m_itemPendingPlaybackStart;
1088 
1090  std::chrono::steady_clock::time_point m_bufferUnderrunTimestamp;
1091 
1093  ProgressTimer m_progressTimer;
1094 
1100  std::chrono::milliseconds m_offset;
1101 
1103  std::unordered_set<std::shared_ptr<acsdkAudioPlayerInterfaces::AudioPlayerObserverInterface>> m_observers;
1104 
1106  std::shared_ptr<avsCommon::sdkInterfaces::RenderPlayerInfoCardsObserverInterface> m_renderPlayerObserver;
1107 
1112  bool m_playNextItemAfterStopped;
1113 
1118  bool m_isStopCalled;
1119 
1123  bool m_okToRequestNextTrack;
1124 
1131 
1135  std::string m_currentPlaylistType;
1137 
1144  bool m_isAcquireChannelRequestPending;
1145 
1147 
1149  std::unordered_set<std::shared_ptr<avsCommon::avs::CapabilityConfiguration>> m_capabilityConfigurations;
1150 
1152  avsCommon::avs::ContentType m_currentMixability;
1153 
1155  avsCommon::avs::MixingBehavior m_mixingBehavior;
1156 
1158  avsCommon::utils::metrics::DataPointDurationBuilder m_playbackTimeMetricData;
1159 
1161  avsCommon::utils::metrics::DataPointDurationBuilder m_autoProgressTimeMetricData;
1162 
1164  avsCommon::utils::metrics::DataPointDurationBuilder m_playCommandToPlayingTimeMetricData;
1165 
1167  bool m_isRecordingTimeToPlayback;
1168 
1170  bool m_isAutoProgressing;
1171 
1173  bool m_isLocalResumePending;
1174 
1176  std::promise<bool> m_localResumeSuccess;
1177 
1179  bool m_isStartingPlayback;
1180 
1182  bool m_isPausingPlayback;
1183 
1185  std::vector<std::shared_ptr<avsCommon::sdkInterfaces::ChannelVolumeInterface>> m_audioChannelVolumeInterfaces;
1186 
1188  std::string m_cachedContext;
1189 
1197 
1202  std::string m_lastDialogRequestId;
1203 };
1204 
1205 } // namespace acsdkAudioPlayer
1206 } // namespace alexaClientSDK
1207 
1208 #endif // ALEXA_CLIENT_SDK_ACSDKAUDIOPLAYER_INCLUDE_ACSDKAUDIOPLAYER_AUDIOPLAYER_H_
Definition: CapabilityAgent.h:47
static bool sendEvent(const std::shared_ptr< SettingEventSenderInterface > &sender, SettingStatus status, const std::string &jsonString)
Definition: LocaleWakeWordsSetting.cpp:74
MixingBehavior
Definition: MixingBehavior.h:25
avsCommon::utils::Optional< InputControllerFactoryInterfaces > create(const std::shared_ptr< acsdkInputControllerInterfaces::InputControllerHandlerInterface > &handler, const std::shared_ptr< avsCommon::sdkInterfaces::ExceptionEncounteredSenderInterface > &exceptionSender)
Definition: InputControllerFactory.cpp:24
static bool parseDirectivePayload(const std::string &payload, Document *document)
Definition: Bluetooth.cpp:1470
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 playerStateToString(AudioPlayerState state)
Definition: AudioPlayer.h:94
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...
ClearBehavior
Used to determine clear queue behavior.
Definition: ClearBehavior.h:28
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:67
ContentType
Definition: ContentType.h:25
ErrorType
Identifies the specific type of error in a PlaybackFailed event.
Definition: ErrorTypes.h:27
Definition: PlayRequestor.h:29
Status
Definition: MessageRequestObserverInterface.h:33
PlayerActivity
Identifies the player state.
Definition: PlayerActivity.h:27
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 1.26.0 - Copyright 2016-2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0