Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/media/mediasink/AudioSinkWrapper.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef AudioSinkWrapper_h_
8
#define AudioSinkWrapper_h_
9
10
#include "mozilla/AbstractThread.h"
11
#include "mozilla/RefPtr.h"
12
#include "mozilla/TimeStamp.h"
13
#include "mozilla/UniquePtr.h"
14
15
#include "MediaSink.h"
16
17
namespace mozilla {
18
class MediaData;
19
template <class T> class MediaQueue;
20
21
namespace media {
22
23
class AudioSink;
24
25
/**
26
 * A wrapper around AudioSink to provide the interface of MediaSink.
27
 */
28
class AudioSinkWrapper : public MediaSink {
29
  // An AudioSink factory.
30
  class Creator {
31
  public:
32
0
    virtual ~Creator() {}
33
    virtual AudioSink* Create() = 0;
34
  };
35
36
  // Wrap around a function object which creates AudioSinks.
37
  template <typename Function>
38
  class CreatorImpl : public Creator {
39
  public:
40
0
    explicit CreatorImpl(const Function& aFunc) : mFunction(aFunc) {}
41
0
    AudioSink* Create() override { return mFunction(); }
42
  private:
43
    Function mFunction;
44
  };
45
46
public:
47
  template <typename Function>
48
  AudioSinkWrapper(AbstractThread* aOwnerThread, const Function& aFunc)
49
    : mOwnerThread(aOwnerThread)
50
    , mCreator(new CreatorImpl<Function>(aFunc))
51
    , mIsStarted(false)
52
    // Give an invalid value to facilitate debug if used before playback starts.
53
    , mPlayDuration(TimeUnit::Invalid())
54
    , mAudioEnded(true)
55
0
  {}
56
57
  const PlaybackParams& GetPlaybackParams() const override;
58
  void SetPlaybackParams(const PlaybackParams& aParams) override;
59
60
  RefPtr<GenericPromise> OnEnded(TrackType aType) override;
61
  TimeUnit GetEndTime(TrackType aType) const override;
62
  TimeUnit GetPosition(TimeStamp* aTimeStamp = nullptr) const override;
63
  bool HasUnplayedFrames(TrackType aType) const override;
64
65
  void SetVolume(double aVolume) override;
66
  void SetPlaybackRate(double aPlaybackRate) override;
67
  void SetPreservesPitch(bool aPreservesPitch) override;
68
  void SetPlaying(bool aPlaying) override;
69
70
  void Start(const TimeUnit& aStartTime, const MediaInfo& aInfo) override;
71
  void Stop() override;
72
  bool IsStarted() const override;
73
  bool IsPlaying() const override;
74
75
  void Shutdown() override;
76
77
  nsCString GetDebugInfo() override;
78
79
private:
80
  virtual ~AudioSinkWrapper();
81
82
0
  void AssertOwnerThread() const {
83
0
    MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
84
0
  }
85
86
  TimeUnit GetVideoPosition(TimeStamp aNow) const;
87
88
  void OnAudioEnded();
89
90
  const RefPtr<AbstractThread> mOwnerThread;
91
  UniquePtr<Creator> mCreator;
92
  UniquePtr<AudioSink> mAudioSink;
93
  RefPtr<GenericPromise> mEndPromise;
94
95
  bool mIsStarted;
96
  PlaybackParams mParams;
97
98
  TimeStamp mPlayStartTime;
99
  TimeUnit mPlayDuration;
100
101
  bool mAudioEnded;
102
  MozPromiseRequestHolder<GenericPromise> mAudioSinkPromise;
103
};
104
105
} // namespace media
106
} // namespace mozilla
107
108
#endif //AudioSinkWrapper_h_