summaryrefslogtreecommitdiffstats
path: root/dom/media/mediasink/AudioSinkWrapper.cpp
blob: a2dfcd8fb391d3731730e2e919ea09112f64095d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "AudioSink.h"
#include "AudioSinkWrapper.h"

namespace mozilla {
namespace media {

AudioSinkWrapper::~AudioSinkWrapper()
{
}

void
AudioSinkWrapper::Shutdown()
{
  AssertOwnerThread();
  MOZ_ASSERT(!mIsStarted, "Must be called after playback stopped.");
  mCreator = nullptr;
}

const MediaSink::PlaybackParams&
AudioSinkWrapper::GetPlaybackParams() const
{
  AssertOwnerThread();
  return mParams;
}

void
AudioSinkWrapper::SetPlaybackParams(const PlaybackParams& aParams)
{
  AssertOwnerThread();
  if (mAudioSink) {
    mAudioSink->SetVolume(aParams.mVolume);
    mAudioSink->SetPlaybackRate(aParams.mPlaybackRate);
    mAudioSink->SetPreservesPitch(aParams.mPreservesPitch);
  }
  mParams = aParams;
}

RefPtr<GenericPromise>
AudioSinkWrapper::OnEnded(TrackType aType)
{
  AssertOwnerThread();
  MOZ_ASSERT(mIsStarted, "Must be called after playback starts.");
  if (aType == TrackInfo::kAudioTrack) {
    return mEndPromise;
  }
  return nullptr;
}

int64_t
AudioSinkWrapper::GetEndTime(TrackType aType) const
{
  AssertOwnerThread();
  MOZ_ASSERT(mIsStarted, "Must be called after playback starts.");
  if (aType == TrackInfo::kAudioTrack && mAudioSink) {
    return mAudioSink->GetEndTime();
  }
  return -1;
}

int64_t
AudioSinkWrapper::GetVideoPosition(TimeStamp aNow) const
{
  AssertOwnerThread();
  MOZ_ASSERT(!mPlayStartTime.IsNull());
  // Time elapsed since we started playing.
  int64_t delta = (aNow - mPlayStartTime).ToMicroseconds();
  // Take playback rate into account.
  return mPlayDuration + delta * mParams.mPlaybackRate;
}

int64_t
AudioSinkWrapper::GetPosition(TimeStamp* aTimeStamp) const
{
  AssertOwnerThread();
  MOZ_ASSERT(mIsStarted, "Must be called after playback starts.");

  int64_t pos = -1;
  TimeStamp t = TimeStamp::Now();

  if (!mAudioEnded) {
    // Rely on the audio sink to report playback position when it is not ended.
    pos = mAudioSink->GetPosition();
  } else if (!mPlayStartTime.IsNull()) {
    // Calculate playback position using system clock if we are still playing.
    pos = GetVideoPosition(t);
  } else {
    // Return how long we've played if we are not playing.
    pos = mPlayDuration;
  }

  if (aTimeStamp) {
    *aTimeStamp = t;
  }

  return pos;
}

bool
AudioSinkWrapper::HasUnplayedFrames(TrackType aType) const
{
  AssertOwnerThread();
  return mAudioSink ? mAudioSink->HasUnplayedFrames() : false;
}

void
AudioSinkWrapper::SetVolume(double aVolume)
{
  AssertOwnerThread();
  mParams.mVolume = aVolume;
  if (mAudioSink) {
    mAudioSink->SetVolume(aVolume);
  }
}

void
AudioSinkWrapper::SetPlaybackRate(double aPlaybackRate)
{
  AssertOwnerThread();
  if (!mAudioEnded) {
    // Pass the playback rate to the audio sink. The underlying AudioStream
    // will handle playback rate changes and report correct audio position.
    mAudioSink->SetPlaybackRate(aPlaybackRate);
  } else if (!mPlayStartTime.IsNull()) {
    // Adjust playback duration and start time when we are still playing.
    TimeStamp now = TimeStamp::Now();
    mPlayDuration = GetVideoPosition(now);
    mPlayStartTime = now;
  }
  // mParams.mPlaybackRate affects GetVideoPosition(). It should be updated
  // after the calls to GetVideoPosition();
  mParams.mPlaybackRate = aPlaybackRate;

  // Do nothing when not playing. Changes in playback rate will be taken into
  // account by GetVideoPosition().
}

void
AudioSinkWrapper::SetPreservesPitch(bool aPreservesPitch)
{
  AssertOwnerThread();
  mParams.mPreservesPitch = aPreservesPitch;
  if (mAudioSink) {
    mAudioSink->SetPreservesPitch(aPreservesPitch);
  }
}

void
AudioSinkWrapper::SetPlaying(bool aPlaying)
{
  AssertOwnerThread();

  // Resume/pause matters only when playback started.
  if (!mIsStarted) {
    return;
  }

  if (mAudioSink) {
    mAudioSink->SetPlaying(aPlaying);
  }

  if (aPlaying) {
    MOZ_ASSERT(mPlayStartTime.IsNull());
    mPlayStartTime = TimeStamp::Now();
  } else {
    // Remember how long we've played.
    mPlayDuration = GetPosition();
    // mPlayStartTime must be updated later since GetPosition()
    // depends on the value of mPlayStartTime.
    mPlayStartTime = TimeStamp();
  }
}

void
AudioSinkWrapper::Start(int64_t aStartTime, const MediaInfo& aInfo)
{
  AssertOwnerThread();
  MOZ_ASSERT(!mIsStarted, "playback already started.");

  mIsStarted = true;
  mPlayDuration = aStartTime;
  mPlayStartTime = TimeStamp::Now();

  // no audio is equivalent to audio ended before video starts.
  mAudioEnded = !aInfo.HasAudio();

  if (aInfo.HasAudio()) {
    mAudioSink = mCreator->Create();
    mEndPromise = mAudioSink->Init(mParams);

    mAudioSinkPromise.Begin(mEndPromise->Then(
      mOwnerThread.get(), __func__, this,
      &AudioSinkWrapper::OnAudioEnded,
      &AudioSinkWrapper::OnAudioEnded));
  }
}

void
AudioSinkWrapper::Stop()
{
  AssertOwnerThread();
  MOZ_ASSERT(mIsStarted, "playback not started.");

  mIsStarted = false;
  mAudioEnded = true;

  if (mAudioSink) {
    mAudioSinkPromise.DisconnectIfExists();
    mAudioSink->Shutdown();
    mAudioSink = nullptr;
    mEndPromise = nullptr;
  }
}

bool
AudioSinkWrapper::IsStarted() const
{
  AssertOwnerThread();
  return mIsStarted;
}

bool
AudioSinkWrapper::IsPlaying() const
{
  AssertOwnerThread();
  return IsStarted() && !mPlayStartTime.IsNull();
}

void
AudioSinkWrapper::OnAudioEnded()
{
  AssertOwnerThread();
  mAudioSinkPromise.Complete();
  mPlayDuration = GetPosition();
  if (!mPlayStartTime.IsNull()) {
    mPlayStartTime = TimeStamp::Now();
  }
  mAudioEnded = true;
}

} // namespace media
} // namespace mozilla