summaryrefslogtreecommitdiffstats
path: root/dom/media/MediaDecoderStateMachineBase.cpp
blob: 081ed09e6b6d4b6e64b491d2296fda2c3296a848 (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
/* 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 "MediaDecoderStateMachineBase.h"

#include "MediaDecoder.h"
#include "mozilla/ProfilerMarkers.h"
#include "mozilla/TaskQueue.h"
#include "nsThreadUtils.h"

namespace mozilla {

#define INIT_MIRROR(name, val) \
  name(mTaskQueue, val, "MediaDecoderStateMachineBase::" #name " (Mirror)")
#define INIT_CANONICAL(name, val) \
  name(mTaskQueue, val, "MediaDecoderStateMachineBase::" #name " (Canonical)")
#define FMT(x, ...) "Decoder=%p " x, mDecoderID, ##__VA_ARGS__
#define LOG(x, ...)                                                         \
  DDMOZ_LOG(gMediaDecoderLog, LogLevel::Debug, "Decoder=%p " x, mDecoderID, \
            ##__VA_ARGS__)
#define LOGV(x, ...)                                                          \
  DDMOZ_LOG(gMediaDecoderLog, LogLevel::Verbose, "Decoder=%p " x, mDecoderID, \
            ##__VA_ARGS__)
#define LOGW(x, ...) NS_WARNING(nsPrintfCString(FMT(x, ##__VA_ARGS__)).get())
#define LOGE(x, ...)                                                   \
  NS_DebugBreak(NS_DEBUG_WARNING,                                      \
                nsPrintfCString(FMT(x, ##__VA_ARGS__)).get(), nullptr, \
                __FILE__, __LINE__)

MediaDecoderStateMachineBase::MediaDecoderStateMachineBase(
    MediaDecoder* aDecoder, MediaFormatReader* aReader)
    : mDecoderID(aDecoder),
      mAbstractMainThread(aDecoder->AbstractMainThread()),
      mFrameStats(&aDecoder->GetFrameStatistics()),
      mVideoFrameContainer(aDecoder->GetVideoFrameContainer()),
      mTaskQueue(TaskQueue::Create(GetMediaThreadPool(MediaThreadType::MDSM),
                                   "MDSM::mTaskQueue",
                                   /* aSupportsTailDispatch = */ true)),
      mReader(new ReaderProxy(mTaskQueue, aReader)),
      mPlaybackRate(1.0),
      INIT_MIRROR(mBuffered, media::TimeIntervals()),
      INIT_MIRROR(mPlayState, MediaDecoder::PLAY_STATE_LOADING),
      INIT_MIRROR(mVolume, 1.0),
      INIT_MIRROR(mPreservesPitch, true),
      INIT_MIRROR(mLooping, false),
      INIT_MIRROR(mSecondaryVideoContainer, nullptr),
      INIT_CANONICAL(mDuration, media::NullableTimeUnit()),
      INIT_CANONICAL(mCurrentPosition, media::TimeUnit::Zero()),
      INIT_CANONICAL(mIsAudioDataAudible, false),
      mMinimizePreroll(aDecoder->GetMinimizePreroll()),
      mWatchManager(this, mTaskQueue) {}

MediaEventSource<void>& MediaDecoderStateMachineBase::OnMediaNotSeekable()
    const {
  return mReader->OnMediaNotSeekable();
}

AbstractCanonical<media::TimeIntervals>*
MediaDecoderStateMachineBase::CanonicalBuffered() const {
  return mReader->CanonicalBuffered();
}

void MediaDecoderStateMachineBase::DispatchSetFragmentEndTime(
    const media::TimeUnit& aEndTime) {
  OwnerThread()->DispatchStateChange(NewRunnableMethod<media::TimeUnit>(
      "MediaDecoderStateMachineBase::SetFragmentEndTime", this,
      &MediaDecoderStateMachineBase::SetFragmentEndTime, aEndTime));
}

void MediaDecoderStateMachineBase::DispatchCanPlayThrough(
    bool aCanPlayThrough) {
  OwnerThread()->DispatchStateChange(NewRunnableMethod<bool>(
      "MediaDecoderStateMachineBase::SetCanPlayThrough", this,
      &MediaDecoderStateMachineBase::SetCanPlayThrough, aCanPlayThrough));
}

void MediaDecoderStateMachineBase::DispatchIsLiveStream(bool aIsLiveStream) {
  OwnerThread()->DispatchStateChange(NewRunnableMethod<bool>(
      "MediaDecoderStateMachineBase::SetIsLiveStream", this,
      &MediaDecoderStateMachineBase::SetIsLiveStream, aIsLiveStream));
}

void MediaDecoderStateMachineBase::DispatchSetPlaybackRate(
    double aPlaybackRate) {
  OwnerThread()->DispatchStateChange(NewRunnableMethod<double>(
      "MediaDecoderStateMachineBase::SetPlaybackRate", this,
      &MediaDecoderStateMachineBase::SetPlaybackRate, aPlaybackRate));
}

nsresult MediaDecoderStateMachineBase::Init(MediaDecoder* aDecoder) {
  MOZ_ASSERT(NS_IsMainThread());

  // Dispatch initialization that needs to happen on that task queue.
  nsCOMPtr<nsIRunnable> r = NewRunnableMethod<RefPtr<MediaDecoder>>(
      "MediaDecoderStateMachineBase::InitializationTask", this,
      &MediaDecoderStateMachineBase::InitializationTask, aDecoder);
  mTaskQueue->DispatchStateChange(r.forget());
  ;
  nsresult rv = mReader->Init();
  NS_ENSURE_SUCCESS(rv, rv);

  mMetadataManager.Connect(mReader->TimedMetadataEvent(), OwnerThread());
  mReader->SetCanonicalDuration(&mDuration);

  return NS_OK;
}

void MediaDecoderStateMachineBase::InitializationTask(MediaDecoder* aDecoder) {
  MOZ_ASSERT(OnTaskQueue());

  // Connect mirrors.
  mBuffered.Connect(mReader->CanonicalBuffered());
  mPlayState.Connect(aDecoder->CanonicalPlayState());
  mVolume.Connect(aDecoder->CanonicalVolume());
  mPreservesPitch.Connect(aDecoder->CanonicalPreservesPitch());
  mLooping.Connect(aDecoder->CanonicalLooping());
  mSecondaryVideoContainer.Connect(
      aDecoder->CanonicalSecondaryVideoContainer());

  // Initialize watchers.
  mWatchManager.Watch(mBuffered,
                      &MediaDecoderStateMachineBase::BufferedRangeUpdated);
  mWatchManager.Watch(mVolume, &MediaDecoderStateMachineBase::VolumeChanged);
  mWatchManager.Watch(mPreservesPitch,
                      &MediaDecoderStateMachineBase::PreservesPitchChanged);
  mWatchManager.Watch(mPlayState,
                      &MediaDecoderStateMachineBase::PlayStateChanged);
  mWatchManager.Watch(mLooping, &MediaDecoderStateMachineBase::LoopingChanged);
  mWatchManager.Watch(
      mSecondaryVideoContainer,
      &MediaDecoderStateMachineBase::UpdateSecondaryVideoContainer);
}

RefPtr<ShutdownPromise> MediaDecoderStateMachineBase::BeginShutdown() {
  MOZ_ASSERT(NS_IsMainThread());
  return InvokeAsync(
      OwnerThread(), __func__,
      [self = RefPtr<MediaDecoderStateMachineBase>(this), this]() {
        mWatchManager.Shutdown();
        mBuffered.DisconnectIfConnected();
        mPlayState.DisconnectIfConnected();
        mVolume.DisconnectIfConnected();
        mPreservesPitch.DisconnectIfConnected();
        mLooping.DisconnectIfConnected();
        mSecondaryVideoContainer.DisconnectIfConnected();
        return Shutdown();
      });
}

RefPtr<MediaDecoder::SeekPromise> MediaDecoderStateMachineBase::InvokeSeek(
    const SeekTarget& aTarget) {
  return InvokeAsync(OwnerThread(), __func__,
                     [self = RefPtr<MediaDecoderStateMachineBase>(this),
                      target = aTarget]() { return self->Seek(target); });
}

bool MediaDecoderStateMachineBase::OnTaskQueue() const {
  return OwnerThread()->IsCurrentThreadIn();
}

void MediaDecoderStateMachineBase::DecodeError(const MediaResult& aError) {
  MOZ_ASSERT(OnTaskQueue());
  LOGE("Decode error: %s", aError.Description().get());
  PROFILER_MARKER_TEXT("MDSMBase::DecodeError", MEDIA_PLAYBACK, {},
                       aError.Description());
  // Notify the decode error and MediaDecoder will shut down MDSM.
  mOnPlaybackErrorEvent.Notify(aError);
}

RefPtr<SetCDMPromise> MediaDecoderStateMachineBase::SetCDMProxy(
    CDMProxy* aProxy) {
  return mReader->SetCDMProxy(aProxy);
}

#undef INIT_MIRROR
#undef INIT_CANONICAL
#undef FMT
#undef LOG
#undef LOGV
#undef LOGW
#undef LOGE

}  // namespace mozilla