AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
MockMediaPlayer.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_AVSCOMMON_UTILS_TEST_AVSCOMMON_UTILS_MEDIAPLAYER_MOCKMEDIAPLAYER_H_
17 #define ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_TEST_AVSCOMMON_UTILS_MEDIAPLAYER_MOCKMEDIAPLAYER_H_
18 
19 #include <unordered_set>
20 
21 #include <gmock/gmock.h>
22 
28 
29 namespace alexaClientSDK {
30 namespace avsCommon {
31 namespace utils {
32 namespace mediaPlayer {
33 namespace test {
34 
36 static const std::chrono::milliseconds DEFAULT_TIME(50);
37 
43 public:
52  std::shared_ptr<avsCommon::avs::attachment::AttachmentReader> attachmentReader,
53  const avsCommon::utils::AudioFormat* audioFormat) = 0;
61  virtual SourceId streamSetSource(std::shared_ptr<std::istream> stream, bool repeat) = 0;
68  virtual SourceId urlSetSource(const std::string& url) = 0;
69 };
70 
73  : public MockMediaPlayerHelper
74  , public RequiresShutdown {
75 public:
78 
84  static std::shared_ptr<testing::NiceMock<MockMediaPlayer>> create();
85 
94  static void enableConcurrentMediaPlayers();
95 
97 
98  // 'override' commented out below to avoid needless warnings generated because MOCK_METHOD* does not use it.
99 
103  std::shared_ptr<avsCommon::avs::attachment::AttachmentReader> attachmentReader,
104  const avsCommon::utils::AudioFormat* audioFormat = nullptr,
105  const SourceConfig& config = emptySourceConfig()) /*override*/;
107  std::shared_ptr<avsCommon::avs::attachment::AttachmentReader> attachmentReader,
108  std::chrono::milliseconds offsetAdjustment,
109  const avsCommon::utils::AudioFormat* audioFormat = nullptr,
110  const SourceConfig& config = emptySourceConfig()) /*override*/;
112  const std::string& url,
113  std::chrono::milliseconds offset = std::chrono::milliseconds::zero(),
114  const SourceConfig& config = emptySourceConfig(),
115  bool repeat = false,
116  const PlaybackContext& playbackContext = PlaybackContext()) /* override */;
118  std::shared_ptr<std::istream> stream,
119  bool repeat = false,
120  const SourceConfig& config = emptySourceConfig(),
122  void addObserver(std::shared_ptr<observer> playerObserver) /*override*/;
123  void removeObserver(std::shared_ptr<observer> playerObserver) /*override*/;
125 
126  MOCK_METHOD1(play, bool(SourceId));
127  MOCK_METHOD1(pause, bool(SourceId));
128  MOCK_METHOD1(resume, bool(SourceId));
129  MOCK_METHOD3(seekTo, bool(SourceId, std::chrono::milliseconds location, bool fromStart));
130  MOCK_METHOD1(stop, bool(SourceId));
131  MOCK_METHOD2(stop, bool(SourceId, std::chrono::seconds));
132  MOCK_METHOD1(getOffset, std::chrono::milliseconds(SourceId));
133  MOCK_METHOD0(getNumBytesBuffered, uint64_t());
134  MOCK_METHOD1(
138  MOCK_METHOD0(getPlaybackReports, std::vector<PlaybackReport>());
140 
143  void doShutdown() /*override*/;
145 
146  MOCK_METHOD2(
148  SourceId(
149  std::shared_ptr<avsCommon::avs::attachment::AttachmentReader> attachmentReader,
150  const avsCommon::utils::AudioFormat* audioFormat));
151  MOCK_METHOD2(streamSetSource, SourceId(std::shared_ptr<std::istream> stream, bool repeat));
153 
159  SourceId mockSetSource();
160 
166  bool mockPlay(SourceId sourceId);
167 
173  bool mockPause(SourceId sourceId);
174 
180  bool mockResume(SourceId sourceId);
181 
187  bool mockStop(SourceId sourceId);
193  bool mockStop2(SourceId sourceId, std::chrono::seconds closePipleineTime);
194 
200  bool mockFinished(SourceId sourceId);
201 
207  bool mockError(SourceId sourceId);
208 
212  bool mockSetOffset(SourceId sourceId, std::chrono::milliseconds offset);
213 
217  std::chrono::milliseconds mockGetOffset(SourceId id);
218 
223 
229  bool mockSeek(SourceId sourceId, std::chrono::milliseconds location, bool fromStart);
230 
235  void resetWaitTimer();
236 
245  bool waitUntilNextSetSource(const std::chrono::milliseconds timeout = std::chrono::milliseconds(DEFAULT_TIME));
246 
254  bool waitUntilPlaybackStarted(const std::chrono::milliseconds timeout = std::chrono::milliseconds(DEFAULT_TIME));
262  bool waitUntilPlaybackStarted(
263  SourceId id,
264  const std::chrono::milliseconds timeout = std::chrono::milliseconds(DEFAULT_TIME));
265 
272  bool waitUntilPlaybackPaused(const std::chrono::milliseconds timeout = std::chrono::milliseconds(DEFAULT_TIME));
273 
280  bool waitUntilPlaybackResumed(const std::chrono::milliseconds timeout = std::chrono::milliseconds(DEFAULT_TIME));
281 
288  bool waitUntilPlaybackStopped(const std::chrono::milliseconds timeout = std::chrono::milliseconds(DEFAULT_TIME));
289 
296  bool waitUntilPlaybackFinished(const std::chrono::milliseconds timeout = std::chrono::milliseconds(DEFAULT_TIME));
297 
304  bool waitUntilPlaybackError(const std::chrono::milliseconds timeout = std::chrono::milliseconds(DEFAULT_TIME));
305 
312  bool waitUntilSeeked(const std::chrono::milliseconds timeout = std::chrono::milliseconds(DEFAULT_TIME));
313 
319  SourceId getCurrentSourceId();
320 
326  SourceId getSourceId();
327 
333  SourceId getLatestSourceId();
334 
340  std::unordered_set<std::shared_ptr<avsCommon::utils::mediaPlayer::MediaPlayerObserverInterface>> getObservers()
341  const;
342 
343 private:
344  struct Source;
345 
349  class SourceState {
350  public:
358  SourceState(
359  Source* source,
360  const std::string& name,
361  std::function<void(std::shared_ptr<observer>, SourceId, const MediaPlayerState&)> notifyFunction);
362 
367  ~SourceState();
368 
373  void trigger(const MediaPlayerState = MediaPlayerState(DEFAULT_TIME));
374 
381  bool wait(std::chrono::milliseconds timeout);
382 
386  void resetStateReached();
387 
388  private:
396  void notify(
397  const std::unordered_set<std::shared_ptr<MediaPlayerObserverInterface>>& observer,
398  std::chrono::milliseconds timeout,
399  const MediaPlayerState state);
400 
402  Source* m_source;
403 
405  std::string m_name;
406 
408  std::function<void(std::shared_ptr<observer>, SourceId, const MediaPlayerState&)> m_notifyFunction;
409 
411  std::mutex m_mutex;
412 
414  bool m_stateReached;
415 
417  std::condition_variable m_wake;
418 
420  std::thread m_thread;
421 
423  bool m_shutdown;
424  };
425 
429  struct Source {
430  public:
437  Source(MockMediaPlayer* player, SourceId id);
438 
440  MockMediaPlayer* mockMediaPlayer;
441 
443  SourceId sourceId;
444 
446  std::chrono::milliseconds offset;
447 
449  SourceState started;
450 
452  SourceState paused;
453 
455  SourceState resumed;
456 
458  SourceState stopped;
459 
461  SourceState finished;
462 
464  SourceState seekComplete;
465 
467  SourceState error;
468 
471  };
472 
479  std::shared_ptr<Source> getCurrentSource(SourceId sourceId);
480 
487  std::shared_ptr<Source> getPreviousSource(SourceId sourceId);
488 
495  bool isValidSourceId(SourceId sourceId);
496 
498  mutable std::mutex m_mutex;
499 
501  static std::mutex m_globalMutex;
502 
504  static std::vector<std::shared_ptr<Source>> m_sources;
505 
507  static std::atomic<SourceId> m_currentSourceId;
508 
510  static SourceId m_previousSourceId;
511 
513  static bool m_isConcurrentEnabled;
514 
516  std::unordered_set<std::shared_ptr<avsCommon::utils::mediaPlayer::MediaPlayerObserverInterface>> m_playerObservers;
517 };
518 
519 } // namespace test
520 } // namespace mediaPlayer
521 } // namespace utils
522 } // namespace avsCommon
523 } // namespace alexaClientSDK
524 
525 #endif // ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_TEST_AVSCOMMON_UTILS_MEDIAPLAYER_MOCKMEDIAPLAYER_H_
static const std::chrono::milliseconds DEFAULT_TIME(50)
Default time parameter.
virtual void removeObserver(std::shared_ptr< avsCommon::utils::mediaPlayer::MediaPlayerObserverInterface > playerObserver)=0
SourceConfig emptySourceConfig()
Definition: SourceConfig.h:98
#define MOCK_METHOD3(m,...)
Definition: gmock-generated-function-mockers.h:678
virtual utils::Optional< Fingerprint > getFingerprint()
Definition: MediaPlayerInterface.h:380
virtual bool seekTo(SourceId id, std::chrono::milliseconds location, bool fromStart)
Definition: MediaPlayerInterface.h:296
virtual utils::Optional< avsCommon::utils::mediaPlayer::MediaPlayerState > getMediaPlayerState(SourceId id)=0
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
Definition: AmdMetricWrapperTest.cpp:24
::std::string string
Definition: gtest-port.h:1097
virtual std::vector< PlaybackReport > getPlaybackReports()
Definition: MediaPlayerInterface.h:376
#define MOCK_METHOD2(m,...)
Definition: gmock-generated-function-mockers.h:677
A mock MediaPlayer for unit tests.
Definition: MockMediaPlayer.h:72
MediaType
Definition: MediaType.h:29
SLresult mockGetState(SLAndroidSimpleBufferQueueItf self, SLAndroidSimpleBufferQueueState *pState)
Definition: AndroidSLESBufferQueueTest.cpp:146
virtual SourceId setSource(std::shared_ptr< avsCommon::avs::attachment::AttachmentReader > attachmentReader, const avsCommon::utils::AudioFormat *format=nullptr, const SourceConfig &config=emptySourceConfig())=0
virtual std::chrono::milliseconds getOffset(SourceId id)=0
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
virtual SourceId attachmentSetSource(std::shared_ptr< avsCommon::avs::attachment::AttachmentReader > attachmentReader, const avsCommon::utils::AudioFormat *audioFormat)=0
#define MOCK_METHOD0(m,...)
Definition: gmock-generated-function-mockers.h:675
virtual void addObserver(std::shared_ptr< avsCommon::utils::mediaPlayer::MediaPlayerObserverInterface > playerObserver)=0
avsCommon::utils::mediaPlayer::MediaPlayerState MediaPlayerState
Definition: RendererTest.cpp:38
#define MOCK_METHOD1(m,...)
Definition: gmock-generated-function-mockers.h:676
virtual utils::Optional< PlaybackAttributes > getPlaybackAttributes()
Definition: MediaPlayerInterface.h:372
virtual SourceId streamSetSource(std::shared_ptr< std::istream > stream, bool repeat)=0

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