summaryrefslogtreecommitdiffstats
path: root/dom/media/platforms/gonk/GonkMediaDataDecoder.h
blob: bba2a8645d7a4286757fe35eb7aacbe0ed525c57 (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
/* -*- 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(GonkMediaDataDecoder_h_)
#define GonkMediaDataDecoder_h_
#include "PlatformDecoderModule.h"
#include <stagefright/foundation/AHandler.h>

namespace android {
struct ALooper;
class MediaBuffer;
class MediaCodecProxy;
} // namespace android

namespace mozilla {
class MediaRawData;

// Manage the data flow from inputting encoded data and outputting decode data.
class GonkDecoderManager : public android::AHandler {
public:
  typedef TrackInfo::TrackType TrackType;
  typedef MediaDataDecoder::InitPromise InitPromise;

  virtual ~GonkDecoderManager() {}

  virtual RefPtr<InitPromise> Init() = 0;
  virtual const char* GetDescriptionName() const = 0;

  // Asynchronously send sample into mDecoder. If out of input buffer, aSample
  // will be queued for later re-send.
  nsresult Input(MediaRawData* aSample);

  // Flush the queued samples and signal decoder to throw all pending input/output away.
  nsresult Flush();

  // Shutdown decoder and rejects the init promise.
  virtual nsresult Shutdown();

  // How many samples are waiting for processing.
  size_t NumQueuedSamples();

  // Set callback for decoder events, such as requesting more input,
  // returning output, or reporting error.
  void SetDecodeCallback(MediaDataDecoderCallback* aCallback)
  {
    mDecodeCallback = aCallback;
  }

protected:
  GonkDecoderManager()
    : mMutex("GonkDecoderManager")
    , mLastTime(INT64_MIN)
    , mFlushMonitor("GonkDecoderManager::Flush")
    , mIsFlushing(false)
    , mDecodeCallback(nullptr)
  {}

  bool InitLoopers(MediaData::Type aType);

  void onMessageReceived(const android::sp<android::AMessage> &aMessage) override;

  // Produces decoded output. It returns NS_OK on success, or NS_ERROR_NOT_AVAILABLE
  // when output is not produced yet.
  // If this returns a failure code other than NS_ERROR_NOT_AVAILABLE, an error
  // will be reported through mDecodeCallback.
  virtual nsresult Output(int64_t aStreamOffset,
                          RefPtr<MediaData>& aOutput) = 0;

  // Send queued samples to OMX. It returns how many samples are still in
  // queue after processing, or negative error code if failed.
  int32_t ProcessQueuedSamples();

  void ProcessInput(bool aEndOfStream);
  virtual void ProcessFlush();
  void ProcessToDo(bool aEndOfStream);
  virtual void ResetEOS();

  RefPtr<MediaByteBuffer> mCodecSpecificData;

  nsAutoCString mMimeType;

  // MediaCodedc's wrapper that performs the decoding.
  android::sp<android::MediaCodecProxy> mDecoder;
  // Looper for mDecoder to run on.
  android::sp<android::ALooper> mDecodeLooper;
  // Looper to run decode tasks such as processing input, output, flush, and
  // recycling output buffers.
  android::sp<android::ALooper> mTaskLooper;
  // Message codes for tasks running on mTaskLooper.
  enum {
    // Decoder will send this to indicate internal state change such as input or
    // output buffers availability. Used to run pending input & output tasks.
    kNotifyDecoderActivity = 'nda ',
    // Signal the decoder to flush.
    kNotifyProcessFlush = 'npf ',
    // Used to process queued samples when there is new input.
    kNotifyProcessInput = 'npi ',
#ifdef DEBUG
    kNotifyFindLooperId = 'nfli',
#endif
  };

  MozPromiseHolder<InitPromise> mInitPromise;

  Mutex mMutex; // Protects mQueuedSamples.
  // A queue that stores the samples waiting to be sent to mDecoder.
  // Empty element means EOS and there shouldn't be any sample be queued after it.
  // Samples are queued in caller's thread and dequeued in mTaskLooper.
  nsTArray<RefPtr<MediaRawData>> mQueuedSamples;

  // The last decoded frame presentation time. Only accessed on mTaskLooper.
  int64_t mLastTime;

  Monitor mFlushMonitor; // Waits for flushing to complete.
  bool mIsFlushing; // Protected by mFlushMonitor.

  // Remembers the notification that is currently waiting for the decoder event
  // to avoid requesting more than one notification at the time, which is
  // forbidden by mDecoder.
  android::sp<android::AMessage> mToDo;

  // Stores sample info for output buffer processing later.
  struct WaitOutputInfo {
    WaitOutputInfo(int64_t aOffset, int64_t aTimestamp, bool aEOS)
      : mOffset(aOffset)
      , mTimestamp(aTimestamp)
      , mEOS(aEOS)
    {}
    const int64_t mOffset;
    const int64_t mTimestamp;
    const bool mEOS;
  };

  nsTArray<WaitOutputInfo> mWaitOutput;

  MediaDataDecoderCallback* mDecodeCallback; // Reports decoder output or error.

private:
  void UpdateWaitingList(int64_t aForgetUpTo);

#ifdef DEBUG
  typedef void* LooperId;

  bool OnTaskLooper();
  LooperId mTaskLooperId;
#endif
};

class AutoReleaseMediaBuffer
{
public:
  AutoReleaseMediaBuffer(android::MediaBuffer* aBuffer, android::MediaCodecProxy* aCodec)
    : mBuffer(aBuffer)
    , mCodec(aCodec)
  {}

  ~AutoReleaseMediaBuffer()
  {
    MOZ_ASSERT(mCodec.get());
    if (mBuffer) {
      mCodec->ReleaseMediaBuffer(mBuffer);
    }
  }

  android::MediaBuffer* forget()
  {
    android::MediaBuffer* tmp = mBuffer;
    mBuffer = nullptr;
    return tmp;
  }

private:
  android::MediaBuffer* mBuffer;
  android::sp<android::MediaCodecProxy> mCodec;
};

// Samples are decoded using the GonkDecoder (MediaCodec)
// created by the GonkDecoderManager. This class implements
// the higher-level logic that drives mapping the Gonk to the async
// MediaDataDecoder interface. The specifics of decoding the exact stream
// type are handled by GonkDecoderManager and the GonkDecoder it creates.
class GonkMediaDataDecoder : public MediaDataDecoder {
public:
  GonkMediaDataDecoder(GonkDecoderManager* aDecoderManager,
                       MediaDataDecoderCallback* aCallback);

  ~GonkMediaDataDecoder();

  RefPtr<InitPromise> Init() override;

  void Input(MediaRawData* aSample) override;

  void Flush() override;

  void Drain() override;

  void Shutdown() override;

  const char* GetDescriptionName() const override
  {
    return "gonk decoder";
  }

private:

  android::sp<GonkDecoderManager> mManager;
};

} // namespace mozilla

#endif // GonkMediaDataDecoder_h_