summaryrefslogtreecommitdiffstats
path: root/dom/media/platforms/wmf/WMFMediaDataDecoder.h
blob: b344ba7b658f0d9a593583eeab36afd3c16d1179 (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* 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/. */

#if !defined(WMFMediaDataDecoder_h_)
#  define WMFMediaDataDecoder_h_

#  include <set>

#  include "MFTDecoder.h"
#  include "PlatformDecoderModule.h"
#  include "WMF.h"
#  include "mozilla/RefPtr.h"

namespace mozilla {

// Encapsulates the initialization of the MFTDecoder appropriate for decoding
// a given stream, and the process of converting the IMFSample produced
// by the MFT into a MediaData object.
class MFTManager {
 public:
  virtual ~MFTManager() {}

  // Submit a compressed sample for decoding.
  // This should forward to the MFTDecoder after performing
  // any required sample formatting.
  virtual HRESULT Input(MediaRawData* aSample) = 0;

  // Produces decoded output, if possible. Blocks until output can be produced,
  // or until no more is able to be produced.
  // Returns S_OK on success, or MF_E_TRANSFORM_NEED_MORE_INPUT if there's not
  // enough data to produce more output. If this returns a failure code other
  // than MF_E_TRANSFORM_NEED_MORE_INPUT, an error will be reported to the
  // MP4Reader.
  virtual HRESULT Output(int64_t aStreamOffset, RefPtr<MediaData>& aOutput) = 0;

  virtual void Flush() {
    mDecoder->Flush();
    mSeekTargetThreshold.reset();
  }

  void Drain() {
    if (FAILED(mDecoder->SendMFTMessage(MFT_MESSAGE_COMMAND_DRAIN, 0))) {
      NS_WARNING("Failed to send DRAIN command to MFT");
    }
  }

  // Destroys all resources.
  virtual void Shutdown() = 0;

  virtual bool IsHardwareAccelerated(nsACString& aFailureReason) const {
    return false;
  }

  virtual TrackInfo::TrackType GetType() = 0;

  virtual nsCString GetDescriptionName() const = 0;

  virtual nsCString GetCodecName() const = 0;

  virtual void SetSeekThreshold(const media::TimeUnit& aTime) {
    if (aTime.IsValid()) {
      mSeekTargetThreshold = Some(aTime);
    } else {
      mSeekTargetThreshold.reset();
    }
  }

  virtual bool HasSeekThreshold() const {
    return mSeekTargetThreshold.isSome();
  }

  virtual MediaDataDecoder::ConversionRequired NeedsConversion() const {
    return MediaDataDecoder::ConversionRequired::kNeedNone;
  }

 protected:
  // IMFTransform wrapper that performs the decoding.
  RefPtr<MFTDecoder> mDecoder;

  Maybe<media::TimeUnit> mSeekTargetThreshold;
};

DDLoggedTypeDeclNameAndBase(WMFMediaDataDecoder, MediaDataDecoder);

// Decodes audio and video using Windows Media Foundation. Samples are decoded
// using the MFTDecoder created by the MFTManager. This class implements
// the higher-level logic that drives mapping the MFT to the async
// MediaDataDecoder interface. The specifics of decoding the exact stream
// type are handled by MFTManager and the MFTDecoder it creates.
class WMFMediaDataDecoder final
    : public MediaDataDecoder,
      public DecoderDoctorLifeLogger<WMFMediaDataDecoder> {
 public:
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(WMFMediaDataDecoder, final);

  explicit WMFMediaDataDecoder(MFTManager* aOutputSource);

  RefPtr<MediaDataDecoder::InitPromise> Init() override;

  RefPtr<DecodePromise> Decode(MediaRawData* aSample) override;

  RefPtr<DecodePromise> Drain() override;

  RefPtr<FlushPromise> Flush() override;

  RefPtr<ShutdownPromise> Shutdown() override;

  bool IsHardwareAccelerated(nsACString& aFailureReason) const override;

  nsCString GetDescriptionName() const override {
    return mMFTManager ? mMFTManager->GetDescriptionName() : "unknown"_ns;
  }

  nsCString GetCodecName() const override {
    return mMFTManager ? mMFTManager->GetCodecName() : ""_ns;
  }

  ConversionRequired NeedsConversion() const override {
    MOZ_ASSERT(mMFTManager);
    return mMFTManager->NeedsConversion();
  }

  virtual void SetSeekThreshold(const media::TimeUnit& aTime) override;

 private:
  ~WMFMediaDataDecoder();

  RefPtr<DecodePromise> ProcessError(HRESULT aError, const char* aReason);

  // Called on the task queue. Inserts the sample into the decoder, and
  // extracts output if available.
  RefPtr<DecodePromise> ProcessDecode(MediaRawData* aSample);

  // Called on the task queue. Extracts output if available, and delivers
  // it to the reader. Called after ProcessDecode() and ProcessDrain().
  HRESULT ProcessOutput(DecodedData& aResults);

  // Called on the task queue. Orders the MFT to flush.  There is no output to
  // extract.
  RefPtr<FlushPromise> ProcessFlush();

  // Called on the task queue. Orders the MFT to drain, and then extracts
  // all available output.
  RefPtr<DecodePromise> ProcessDrain();

  // Checks if `aOutput` should be discarded (guarded against) because its a
  // potentially invalid output from the decoder. This is done because the
  // Windows decoder appears to produce invalid outputs under certain
  // conditions.
  bool ShouldGuardAgaintIncorrectFirstSample(MediaData* aOutput) const;

  const RefPtr<TaskQueue> mTaskQueue;

  UniquePtr<MFTManager> mMFTManager;

  // The last offset into the media resource that was passed into Input().
  // This is used to approximate the decoder's position in the media resource.
  int64_t mLastStreamOffset;
  Maybe<media::TimeUnit> mLastTime;
  media::TimeUnit mLastDuration;
  // Before we get the first sample, this records the times of all samples we
  // send to the decoder which is used to validate if the first sample is valid.
  std::set<int64_t> mInputTimesSet;
  int64_t mSamplesCount = 0;
  int64_t mOutputsCount = 0;

  bool mIsShutDown = false;

  enum class DrainStatus {
    DRAINED,
    DRAINABLE,
    DRAINING,
  };
  DrainStatus mDrainStatus = DrainStatus::DRAINED;
};

}  // namespace mozilla

#endif  // WMFMediaDataDecoder_h_