summaryrefslogtreecommitdiffstats
path: root/mobile/android/geckoview/src/main/java/org/mozilla/gecko/media/GeckoHLSSample.java
blob: d2ab76a13d29e9e243097dfeb094aeef65d3de34 (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
/* 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.media.MediaCodec;
import android.media.MediaCodec.BufferInfo;
import android.media.MediaCodec.CryptoInfo;
import java.io.IOException;
import java.nio.ByteBuffer;
import org.mozilla.gecko.annotation.WrapForJNI;

public final class GeckoHLSSample {
  public static final GeckoHLSSample EOS;

  static {
    final BufferInfo eosInfo = new BufferInfo();
    eosInfo.set(0, 0, Long.MIN_VALUE, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
    EOS = new GeckoHLSSample(null, eosInfo, null, 0);
  }

  // Indicate the index of format which is used by this sample.
  @WrapForJNI public final int formatIndex;

  @WrapForJNI public long duration;

  @WrapForJNI public final BufferInfo info;

  @WrapForJNI public final CryptoInfo cryptoInfo;

  private ByteBuffer mBuffer = null;

  @WrapForJNI
  public void writeToByteBuffer(final ByteBuffer dest) throws IOException {
    if (mBuffer != null && dest != null && info.size > 0) {
      dest.put(mBuffer);
    }
  }

  @WrapForJNI
  public boolean isEOS() {
    return (info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0;
  }

  @WrapForJNI
  public boolean isKeyFrame() {
    return (info.flags & MediaCodec.BUFFER_FLAG_KEY_FRAME) != 0;
  }

  public static GeckoHLSSample create(
      final ByteBuffer src,
      final BufferInfo info,
      final CryptoInfo cryptoInfo,
      final int formatIndex) {
    return new GeckoHLSSample(src, info, cryptoInfo, formatIndex);
  }

  private GeckoHLSSample(
      final ByteBuffer buffer,
      final BufferInfo info,
      final CryptoInfo cryptoInfo,
      final int formatIndex) {
    this.formatIndex = formatIndex;
    duration = Long.MAX_VALUE;
    this.mBuffer = buffer;
    this.info = info;
    this.cryptoInfo = cryptoInfo;
  }

  @Override
  public String toString() {
    if (isEOS()) {
      return "EOS GeckoHLSSample";
    }

    final StringBuilder str = new StringBuilder();
    str.append("{ info=")
        .append("{ offset=")
        .append(info.offset)
        .append(", size=")
        .append(info.size)
        .append(", pts=")
        .append(info.presentationTimeUs)
        .append(", duration=")
        .append(duration)
        .append(", flags=")
        .append(Integer.toHexString(info.flags))
        .append(" }")
        .append(" }");
    return str.toString();
  }
}