1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <media/stagefright/foundation/AHandler.h>
18#include <media/stagefright/foundation/AString.h>
19#include <utils/KeyedVector.h>
20
21namespace android {
22
23struct ABuffer;
24struct ALooper;
25class AudioTrack;
26class IGraphicBufferProducer;
27struct MediaCodec;
28class MediaCodecBuffer;
29struct NuMediaExtractor;
30class Surface;
31
32struct SimplePlayer : public AHandler {
33    SimplePlayer();
34
35    status_t setDataSource(const char *path);
36    status_t setSurface(const sp<IGraphicBufferProducer> &bufferProducer);
37    status_t prepare();
38    status_t start();
39    status_t stop();
40    status_t reset();
41
42protected:
43    virtual ~SimplePlayer();
44
45    virtual void onMessageReceived(const sp<AMessage> &msg);
46
47private:
48    enum State {
49        UNINITIALIZED,
50        UNPREPARED,
51        STOPPED,
52        STARTED
53    };
54
55    enum {
56        kWhatSetDataSource,
57        kWhatSetSurface,
58        kWhatPrepare,
59        kWhatStart,
60        kWhatStop,
61        kWhatReset,
62        kWhatDoMoreStuff,
63    };
64
65    struct BufferInfo {
66        size_t mIndex;
67        size_t mOffset;
68        size_t mSize;
69        int64_t mPresentationTimeUs;
70        uint32_t mFlags;
71    };
72
73    struct CodecState
74    {
75        sp<MediaCodec> mCodec;
76        Vector<sp<ABuffer> > mCSD;
77        Vector<sp<MediaCodecBuffer> > mBuffers[2];
78
79        List<size_t> mAvailInputBufferIndices;
80        List<BufferInfo> mAvailOutputBufferInfos;
81
82        sp<AudioTrack> mAudioTrack;
83        uint32_t mNumFramesWritten;
84    };
85
86    State mState;
87    AString mPath;
88    sp<Surface> mSurface;
89
90    sp<NuMediaExtractor> mExtractor;
91    sp<ALooper> mCodecLooper;
92    KeyedVector<size_t, CodecState> mStateByTrackIndex;
93    int32_t mDoMoreStuffGeneration;
94
95    int64_t mStartTimeRealUs;
96
97    status_t onPrepare();
98    status_t onStart();
99    status_t onStop();
100    status_t onReset();
101    status_t onDoMoreStuff();
102    status_t onOutputFormatChanged(size_t trackIndex, CodecState *state);
103
104    void renderAudio(
105            CodecState *state, BufferInfo *info, const sp<MediaCodecBuffer> &buffer);
106
107    DISALLOW_EVIL_CONSTRUCTORS(SimplePlayer);
108};
109
110}  // namespace android
111