summaryrefslogtreecommitdiffstats
path: root/mobile/android/geckoview/src/main/java/org/mozilla/gecko/media/GeckoHlsRendererBase.java
blob: ecb7b93d6116ec00c63bf2173e27f346fb3bab22 (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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/* 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/. */

package org.mozilla.gecko.media;

import android.util.Log;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.mozilla.geckoview.BuildConfig;
import org.mozilla.thirdparty.com.google.android.exoplayer2.BaseRenderer;
import org.mozilla.thirdparty.com.google.android.exoplayer2.C;
import org.mozilla.thirdparty.com.google.android.exoplayer2.ExoPlaybackException;
import org.mozilla.thirdparty.com.google.android.exoplayer2.Format;
import org.mozilla.thirdparty.com.google.android.exoplayer2.FormatHolder;
import org.mozilla.thirdparty.com.google.android.exoplayer2.RendererCapabilities;
import org.mozilla.thirdparty.com.google.android.exoplayer2.decoder.DecoderInputBuffer;

public abstract class GeckoHlsRendererBase extends BaseRenderer {
  protected static final int QUEUED_INPUT_SAMPLE_DURATION_THRESHOLD = 1000000; // 1sec
  protected final FormatHolder mFormatHolder = new FormatHolder();
  /*
   *  DEBUG/LOGTAG will be set in the 2 subclass GeckoHlsAudioRenderer and
   *  GeckoHlsVideoRenderer, and we still wants to log message in the base class
   *  GeckoHlsRendererBase, so neither 'static' nor 'final' are applied to them.
   */
  protected boolean DEBUG;
  protected String LOGTAG;
  // Notify GeckoHlsPlayer about renderer's status, i.e. data has arrived.
  protected GeckoHlsPlayer.ComponentEventDispatcher mPlayerEventDispatcher;

  protected ConcurrentLinkedQueue<GeckoHLSSample> mDemuxedInputSamples =
      new ConcurrentLinkedQueue<>();

  protected ByteBuffer mInputBuffer = null;
  protected ArrayList<Format> mFormats = new ArrayList<Format>();
  protected boolean mInitialized = false;
  protected boolean mWaitingForData = true;
  protected boolean mInputStreamEnded = false;
  protected long mFirstSampleStartTime = Long.MIN_VALUE;

  protected abstract void createInputBuffer() throws ExoPlaybackException;

  protected abstract void handleReconfiguration(DecoderInputBuffer bufferForRead);

  protected abstract void handleFormatRead(DecoderInputBuffer bufferForRead)
      throws ExoPlaybackException;

  protected abstract void handleEndOfStream(DecoderInputBuffer bufferForRead);

  protected abstract void handleSamplePreparation(DecoderInputBuffer bufferForRead);

  protected abstract void resetRenderer();

  protected abstract boolean clearInputSamplesQueue();

  protected abstract void notifyPlayerInputFormatChanged(Format newFormat);

  private DecoderInputBuffer mBufferForRead =
      new DecoderInputBuffer(DecoderInputBuffer.BUFFER_REPLACEMENT_MODE_NORMAL);
  private final DecoderInputBuffer mFlagsOnlyBuffer = DecoderInputBuffer.newFlagsOnlyInstance();

  protected void assertTrue(final boolean condition) {
    if (DEBUG && !condition) {
      throw new AssertionError("Expected condition to be true");
    }
  }

  public GeckoHlsRendererBase(
      final int trackType, final GeckoHlsPlayer.ComponentEventDispatcher eventDispatcher) {
    super(trackType);
    mPlayerEventDispatcher = eventDispatcher;
  }

  private boolean isQueuedEnoughData() {
    if (mDemuxedInputSamples.isEmpty()) {
      return false;
    }

    final Iterator<GeckoHLSSample> iter = mDemuxedInputSamples.iterator();
    long firstPTS = 0;
    if (iter.hasNext()) {
      final GeckoHLSSample sample = iter.next();
      firstPTS = sample.info.presentationTimeUs;
    }
    long lastPTS = firstPTS;
    while (iter.hasNext()) {
      final GeckoHLSSample sample = iter.next();
      lastPTS = sample.info.presentationTimeUs;
    }
    return Math.abs(lastPTS - firstPTS) > QUEUED_INPUT_SAMPLE_DURATION_THRESHOLD;
  }

  public Format getFormat(final int index) {
    assertTrue(index >= 0);
    final Format fmt = index < mFormats.size() ? mFormats.get(index) : null;
    if (DEBUG) {
      Log.d(LOGTAG, "getFormat : index = " + index + ", format : " + fmt);
    }
    return fmt;
  }

  public synchronized long getFirstSamplePTS() {
    return mFirstSampleStartTime;
  }

  public synchronized ConcurrentLinkedQueue<GeckoHLSSample> getQueuedSamples(final int number) {
    final ConcurrentLinkedQueue<GeckoHLSSample> samples =
        new ConcurrentLinkedQueue<GeckoHLSSample>();

    GeckoHLSSample sample = null;
    final int queuedSize = mDemuxedInputSamples.size();
    for (int i = 0; i < queuedSize; i++) {
      if (i >= number) {
        break;
      }
      sample = mDemuxedInputSamples.poll();
      samples.offer(sample);
    }

    sample = samples.isEmpty() ? null : samples.peek();
    if (sample == null) {
      if (DEBUG) {
        Log.d(LOGTAG, "getQueuedSamples isEmpty, mWaitingForData = true !");
      }
      mWaitingForData = true;
    } else if (mFirstSampleStartTime == Long.MIN_VALUE) {
      mFirstSampleStartTime = sample.info.presentationTimeUs;
      if (DEBUG) {
        Log.d(LOGTAG, "mFirstSampleStartTime = " + mFirstSampleStartTime);
      }
    }
    return samples;
  }

  protected void handleDrmInitChanged(final Format oldFormat, final Format newFormat) {
    final Object oldDrmInit = oldFormat == null ? null : oldFormat.drmInitData;
    final Object newDrnInit = newFormat.drmInitData;

    // TODO: Notify MFR if the content is encrypted or not.
    if (newDrnInit != oldDrmInit) {
      if (newDrnInit != null) {
      } else {
      }
    }
  }

  protected boolean canReconfigure(final Format oldFormat, final Format newFormat) {
    // Referring to ExoPlayer's MediaCodecBaseRenderer, the default is set
    // to false. Only override it in video renderer subclass.
    return false;
  }

  protected void prepareReconfiguration() {
    // Referring to ExoPlayer's MediaCodec related renderers, only video
    // renderer handles this.
  }

  protected void updateCSDInfo(final Format format) {
    // do nothing.
  }

  protected void onInputFormatChanged(final Format newFormat) throws ExoPlaybackException {
    Format oldFormat;
    try {
      oldFormat = mFormats.get(mFormats.size() - 1);
    } catch (final IndexOutOfBoundsException e) {
      oldFormat = null;
    }
    if (DEBUG) {
      Log.d(LOGTAG, "[onInputFormatChanged] old : " + oldFormat + " => new : " + newFormat);
    }
    mFormats.add(newFormat);
    handleDrmInitChanged(oldFormat, newFormat);

    if (mInitialized && canReconfigure(oldFormat, newFormat)) {
      prepareReconfiguration();
    } else {
      resetRenderer();
      maybeInitRenderer();
    }

    updateCSDInfo(newFormat);
    notifyPlayerInputFormatChanged(newFormat);
  }

  protected void maybeInitRenderer() throws ExoPlaybackException {
    if (mInitialized || mFormats.size() == 0) {
      return;
    }
    if (DEBUG) {
      Log.d(LOGTAG, "Initializing ... ");
    }
    try {
      createInputBuffer();
      mInitialized = true;
    } catch (final OutOfMemoryError e) {
      throw ExoPlaybackException.createForRenderer(
          new RuntimeException(e),
          getIndex(),
          mFormats.isEmpty() ? null : getFormat(mFormats.size() - 1),
          RendererCapabilities.FORMAT_HANDLED);
    }
  }

  /*
   * The place we get demuxed data from HlsMediaSource(ExoPlayer).
   * The data will then be converted to GeckoHLSSample and deliver to
   * GeckoHlsDemuxerWrapper for further use.
   * If the return value is ture, that means a GeckoHLSSample is queued
   * successfully. We can try to feed more samples into queue.
   * If the return value is false, that means we might encounter following
   * situation 1) not initialized 2) input stream is ended 3) queue is full.
   * 4) format changed. 5) exception happened.
   */
  protected synchronized boolean feedInputBuffersQueue() throws ExoPlaybackException {
    if (!mInitialized || mInputStreamEnded || isQueuedEnoughData()) {
      // Need to reinitialize the renderer or the input stream has ended
      // or we just reached the maximum queue size.
      return false;
    }

    mBufferForRead.data = mInputBuffer;
    if (mBufferForRead.data != null) {
      mBufferForRead.clear();
    }

    handleReconfiguration(mBufferForRead);

    // Read data from HlsMediaSource
    int result = C.RESULT_NOTHING_READ;
    try {
      result = readSource(mFormatHolder, mBufferForRead, false);
    } catch (final Exception e) {
      Log.e(LOGTAG, "[feedInput] Exception when readSource :", e);
      return false;
    }

    if (result == C.RESULT_NOTHING_READ) {
      return false;
    }

    if (result == C.RESULT_FORMAT_READ) {
      handleFormatRead(mBufferForRead);
      return true;
    }

    // We've read a buffer.
    if (mBufferForRead.isEndOfStream()) {
      if (DEBUG) {
        Log.d(LOGTAG, "Now we're at the End Of Stream.");
      }
      handleEndOfStream(mBufferForRead);
      return false;
    }

    mBufferForRead.flip();

    handleSamplePreparation(mBufferForRead);

    maybeNotifyDataArrived();
    return true;
  }

  private void maybeNotifyDataArrived() {
    if (mWaitingForData && isQueuedEnoughData()) {
      if (DEBUG) {
        Log.d(LOGTAG, "onDataArrived");
      }
      mPlayerEventDispatcher.onDataArrived(getTrackType());
      mWaitingForData = false;
    }
  }

  private void readFormat() throws ExoPlaybackException {
    mFlagsOnlyBuffer.clear();
    final int result = readSource(mFormatHolder, mFlagsOnlyBuffer, true);
    if (result == C.RESULT_FORMAT_READ) {
      onInputFormatChanged(mFormatHolder.format);
    }
  }

  @Override
  protected void onEnabled(final boolean joining) {
    // Do nothing.
  }

  @Override
  protected void onDisabled() {
    mFormats.clear();
    resetRenderer();
  }

  @Override
  public boolean isReady() {
    return mFormats.size() != 0;
  }

  @Override
  public boolean isEnded() {
    return mInputStreamEnded;
  }

  @Override
  protected synchronized void onPositionReset(final long positionUs, final boolean joining) {
    if (DEBUG) {
      Log.d(LOGTAG, "onPositionReset : positionUs = " + positionUs);
    }
    mInputStreamEnded = false;
    if (mInitialized) {
      clearInputSamplesQueue();
    }
  }

  /*
   * This is called by ExoPlayerImplInternal.java.
   * ExoPlayer checks the status of renderer, i.e. isReady() / isEnded(), and
   * calls renderer.render by passing its wall clock time.
   */
  @Override
  public void render(final long positionUs, final long elapsedRealtimeUs)
      throws ExoPlaybackException {
    if (BuildConfig.DEBUG_BUILD) {
      Log.d(LOGTAG, "positionUs = " + positionUs + ", mInputStreamEnded = " + mInputStreamEnded);
    }
    if (mInputStreamEnded) {
      return;
    }
    if (mFormats.size() == 0) {
      readFormat();
    }

    maybeInitRenderer();
    while (feedInputBuffersQueue()) {
      // Do nothing
    }
  }
}