summaryrefslogtreecommitdiffstats
path: root/tests/Camera2Tests/SmartCamera/SimpleCamera/src/androidx/media/filterfw/decoder/AudioTrackDecoder.java
blob: 3b3de9f97b61e13a2a1ff4d226083280e3266783 (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
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.media.filterfw.decoder;

import android.annotation.TargetApi;
import android.media.MediaCodec;
import android.media.MediaCodec.BufferInfo;
import android.media.MediaFormat;

import androidx.media.filterfw.FrameValue;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

/**
 * {@link TrackDecoder} for decoding audio tracks.
 *
 * TODO: find out if we always get 16 bits per channel and document.
 */
@TargetApi(16)
public class AudioTrackDecoder extends TrackDecoder {

    private final ByteArrayOutputStream mAudioByteStream; // Guarded by mAudioByteStreamLock.
    private final Object mAudioByteStreamLock;

    private int mAudioSampleRate;
    private int mAudioChannelCount;
    private long mAudioPresentationTimeUs;

    public AudioTrackDecoder(int trackIndex, MediaFormat format, Listener listener) {
        super(trackIndex, format, listener);

        if (!DecoderUtil.isAudioFormat(format)) {
            throw new IllegalArgumentException(
                    "AudioTrackDecoder can only be used with audio formats");
        }

        mAudioByteStream = new ByteArrayOutputStream();
        mAudioByteStreamLock = new Object();

        mAudioSampleRate = format.getInteger(MediaFormat.KEY_SAMPLE_RATE);
        mAudioChannelCount = format.getInteger(MediaFormat.KEY_CHANNEL_COUNT);
    }

    @Override
    protected MediaCodec initMediaCodec(MediaFormat format) {
        MediaCodec mediaCodec;
        try {
            mediaCodec = MediaCodec.createDecoderByType(
                    format.getString(MediaFormat.KEY_MIME));
        } catch (IOException e) {
            throw new RuntimeException(
                    "failed to create decoder for "
                    + format.getString(MediaFormat.KEY_MIME), e);
        }
        mediaCodec.configure(format, null, null, 0);
        return mediaCodec;
    }

    @Override
    protected boolean onDataAvailable(
            MediaCodec codec, ByteBuffer[] buffers, int bufferIndex, BufferInfo info) {
        ByteBuffer buffer = buffers[bufferIndex];
        byte[] data = new byte[info.size];
        buffer.position(info.offset);
        buffer.get(data, 0, info.size);

        synchronized (mAudioByteStreamLock) {
            try {
                if (mAudioByteStream.size() == 0 && data.length > 0) {
                    mAudioPresentationTimeUs = info.presentationTimeUs;
                }

                mAudioByteStream.write(data);
            } catch (IOException e) {
                // Just drop the audio sample.
            }
        }

        buffer.clear();
        codec.releaseOutputBuffer(bufferIndex, false);
        notifyListener();
        return true;
    }

    /**
     * Fills the argument {@link FrameValue} with an audio sample containing the audio that was
     * decoded since the last call of this method. The decoder's buffer is cleared as a result.
     */
    public void grabSample(FrameValue audioFrame) {
        synchronized (mAudioByteStreamLock) {
            if (audioFrame != null) {
                AudioSample sample = new AudioSample(
                        mAudioSampleRate, mAudioChannelCount, mAudioByteStream.toByteArray());
                audioFrame.setValue(sample);
                audioFrame.setTimestamp(mAudioPresentationTimeUs * 1000);
            }
            clearBuffer();
        }
    }

    /**
     * Clears the decoder's buffer.
     */
    public void clearBuffer() {
        synchronized (mAudioByteStreamLock) {
            mAudioByteStream.reset();
        }
    }

}