summaryrefslogtreecommitdiffstats
path: root/mobile/android/base/java/org/mozilla/gecko/media/SamplePool.java
blob: 9041e37561eee39a271935247b828d99ea2c4155 (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
/* 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 org.mozilla.gecko.mozglue.SharedMemory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

final class SamplePool {
    private final class Impl {
        private final String mName;
        private int mNextId = 0;
        private int mDefaultBufferSize = 4096;
        private final List<Sample> mRecycledSamples = new ArrayList<>();

        private Impl(String name) {
            mName = name;
        }

        private void setDefaultBufferSize(int size) {
            mDefaultBufferSize = size;
        }

        private synchronized Sample allocate(int size)  {
            Sample sample;
            if (!mRecycledSamples.isEmpty()) {
                sample = mRecycledSamples.remove(0);
                sample.info.set(0, 0, 0, 0);
            } else {
                SharedMemory shm = null;
                try {
                    shm = new SharedMemory(mNextId++, Math.max(size, mDefaultBufferSize));
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (shm != null) {
                    sample = Sample.create(shm);
                } else {
                    sample = Sample.create();
                }
            }

            return sample;
        }

        private synchronized void recycle(Sample recycled) {
            if (recycled.buffer.capacity() >= mDefaultBufferSize) {
                mRecycledSamples.add(recycled);
            } else {
                recycled.dispose();
            }
        }

        private synchronized void clear() {
            for (Sample s : mRecycledSamples) {
                s.dispose();
            }

            mRecycledSamples.clear();
        }

        @Override
        protected void finalize() {
            clear();
        }
    }

    private final Impl mInputs;
    private final Impl mOutputs;

    /* package */ SamplePool(String name) {
        mInputs = new Impl(name + " input buffer pool");
        mOutputs = new Impl(name + " output buffer pool");
    }

    /* package */ void setInputBufferSize(int size) {
        mInputs.setDefaultBufferSize(size);
    }

    /* package */ void setOutputBufferSize(int size) {
        mOutputs.setDefaultBufferSize(size);
    }

    /* package */ Sample obtainInput(int size) {
        return mInputs.allocate(size);
    }

    /* package */ Sample obtainOutput(MediaCodec.BufferInfo info) {
        Sample output = mOutputs.allocate(info.size);
        output.info.set(0, info.size, info.presentationTimeUs, info.flags);
        return output;
    }

    /* package */ void recycleInput(Sample sample) {
        sample.cryptoInfo = null;
        mInputs.recycle(sample);
    }

    /* package */ void recycleOutput(Sample sample) {
        mOutputs.recycle(sample);
    }

    /* package */ void reset() {
        mInputs.clear();
        mOutputs.clear();
    }
}