1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 package org.mozilla.gecko.media;
6 
7 import android.media.MediaCodec;
8 import android.media.MediaCodec.BufferInfo;
9 import android.media.MediaCodec.CryptoInfo;
10 
11 import org.mozilla.gecko.annotation.WrapForJNI;
12 
13 import java.io.IOException;
14 import java.nio.ByteBuffer;
15 
16 public final class GeckoHLSSample {
17     public static final GeckoHLSSample EOS;
18     static {
19         BufferInfo eosInfo = new BufferInfo();
20         eosInfo.set(0, 0, Long.MIN_VALUE, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
21         EOS = new GeckoHLSSample(null, eosInfo, null, 0);
22     }
23 
24     // Indicate the index of format which is used by this sample.
25     @WrapForJNI
26     final public int formatIndex;
27 
28     @WrapForJNI
29     public long duration;
30 
31     @WrapForJNI
32     final public BufferInfo info;
33 
34     @WrapForJNI
35     final public CryptoInfo cryptoInfo;
36 
37     private ByteBuffer mBuffer = null;
38 
39     @WrapForJNI
writeToByteBuffer(final ByteBuffer dest)40     public void writeToByteBuffer(final ByteBuffer dest) throws IOException {
41         if (mBuffer != null && dest != null && info.size > 0) {
42             dest.put(mBuffer);
43         }
44     }
45 
46     @WrapForJNI
isEOS()47     public boolean isEOS() {
48         return (info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0;
49     }
50 
51     @WrapForJNI
isKeyFrame()52     public boolean isKeyFrame() {
53         return (info.flags & MediaCodec.BUFFER_FLAG_KEY_FRAME) != 0;
54     }
55 
create(final ByteBuffer src, final BufferInfo info, final CryptoInfo cryptoInfo, final int formatIndex)56     public static GeckoHLSSample create(final ByteBuffer src, final BufferInfo info,
57                                         final CryptoInfo cryptoInfo, final int formatIndex) {
58         return new GeckoHLSSample(src, info, cryptoInfo, formatIndex);
59     }
60 
GeckoHLSSample(final ByteBuffer buffer, final BufferInfo info, final CryptoInfo cryptoInfo, final int formatIndex)61     private GeckoHLSSample(final ByteBuffer buffer, final BufferInfo info,
62                            final CryptoInfo cryptoInfo, final int formatIndex) {
63         this.formatIndex = formatIndex;
64         duration = Long.MAX_VALUE;
65         this.mBuffer = buffer;
66         this.info = info;
67         this.cryptoInfo = cryptoInfo;
68     }
69 
70     @Override
toString()71     public String toString() {
72         if (isEOS()) {
73             return "EOS GeckoHLSSample";
74         }
75 
76         StringBuilder str = new StringBuilder();
77         str.append("{ info=").
78                 append("{ offset=").append(info.offset).
79                 append(", size=").append(info.size).
80                 append(", pts=").append(info.presentationTimeUs).
81                 append(", duration=").append(duration).
82                 append(", flags=").append(Integer.toHexString(info.flags)).append(" }").
83                 append(" }");
84         return str.toString();
85     }
86 }
87