summaryrefslogtreecommitdiffstats
path: root/mobile/android/geckoview/src/main/java/org/mozilla/gecko/media/SampleBuffer.java
blob: e6b242708d9523a864d8ac0356de154450bc2d90 (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
/* 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.os.Parcel;
import android.os.Parcelable;
import java.io.IOException;
import java.nio.ByteBuffer;
import org.mozilla.gecko.annotation.WrapForJNI;
import org.mozilla.gecko.mozglue.SharedMemory;

public final class SampleBuffer implements Parcelable {
  private SharedMemory mSharedMem;

  /* package */
  public SampleBuffer(final SharedMemory sharedMem) {
    mSharedMem = sharedMem;
  }

  protected SampleBuffer(final Parcel in) {
    mSharedMem = in.readParcelable(SampleBuffer.class.getClassLoader());
  }

  @Override
  public void writeToParcel(final Parcel dest, final int flags) {
    dest.writeParcelable(mSharedMem, flags);
  }

  @Override
  public int describeContents() {
    return 0;
  }

  public static final Creator<SampleBuffer> CREATOR =
      new Creator<SampleBuffer>() {
        @Override
        public SampleBuffer createFromParcel(final Parcel in) {
          return new SampleBuffer(in);
        }

        @Override
        public SampleBuffer[] newArray(final int size) {
          return new SampleBuffer[size];
        }
      };

  public int capacity() {
    return mSharedMem != null ? mSharedMem.getSize() : 0;
  }

  public void readFromByteBuffer(final ByteBuffer src, final int offset, final int size)
      throws IOException {
    if (!src.isDirect()) {
      throw new IOException("SharedMemBuffer only support reading from direct byte buffer.");
    }
    try {
      nativeReadFromDirectBuffer(src, mSharedMem.getPointer(), offset, size);
      mSharedMem.flush();
    } catch (final NullPointerException e) {
      throw new IOException(e);
    }
  }

  private static native void nativeReadFromDirectBuffer(
      ByteBuffer src, long dest, int offset, int size);

  @WrapForJNI
  public void writeToByteBuffer(final ByteBuffer dest, final int offset, final int size)
      throws IOException {
    if (!dest.isDirect()) {
      throw new IOException("SharedMemBuffer only support writing to direct byte buffer.");
    }
    try {
      nativeWriteToDirectBuffer(mSharedMem.getPointer(), dest, offset, size);
    } catch (final NullPointerException e) {
      throw new IOException(e);
    }
  }

  private static native void nativeWriteToDirectBuffer(
      long src, ByteBuffer dest, int offset, int size);

  public void dispose() {
    if (mSharedMem != null) {
      mSharedMem.dispose();
      mSharedMem = null;
    }
  }

  @WrapForJNI
  public boolean isValid() {
    return mSharedMem != null;
  }

  @Override
  public String toString() {
    return "Buffer: " + mSharedMem;
  }
}