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 
21 namespace android {
22 
23 struct ABuffer;
24 struct ALooper;
25 struct AudioTrack;
26 class IGraphicBufferProducer;
27 struct MediaCodec;
28 struct NuMediaExtractor;
29 class Surface;
30 
31 struct SimplePlayer : public AHandler {
32     SimplePlayer();
33 
34     status_t setDataSource(const char *path);
35     status_t setSurface(const sp<IGraphicBufferProducer> &bufferProducer);
36     status_t prepare();
37     status_t start();
38     status_t stop();
39     status_t reset();
40 
41 protected:
42     virtual ~SimplePlayer();
43 
44     virtual void onMessageReceived(const sp<AMessage> &msg);
45 
46 private:
47     enum State {
48         UNINITIALIZED,
49         UNPREPARED,
50         STOPPED,
51         STARTED
52     };
53 
54     enum {
55         kWhatSetDataSource,
56         kWhatSetSurface,
57         kWhatPrepare,
58         kWhatStart,
59         kWhatStop,
60         kWhatReset,
61         kWhatDoMoreStuff,
62     };
63 
64     struct BufferInfo {
65         size_t mIndex;
66         size_t mOffset;
67         size_t mSize;
68         int64_t mPresentationTimeUs;
69         uint32_t mFlags;
70     };
71 
72     struct CodecState
73     {
74         sp<MediaCodec> mCodec;
75         Vector<sp<ABuffer> > mCSD;
76         Vector<sp<ABuffer> > mBuffers[2];
77 
78         List<size_t> mAvailInputBufferIndices;
79         List<BufferInfo> mAvailOutputBufferInfos;
80 
81         sp<AudioTrack> mAudioTrack;
82         uint32_t mNumFramesWritten;
83     };
84 
85     State mState;
86     AString mPath;
87     sp<Surface> mSurface;
88 
89     sp<NuMediaExtractor> mExtractor;
90     sp<ALooper> mCodecLooper;
91     KeyedVector<size_t, CodecState> mStateByTrackIndex;
92     int32_t mDoMoreStuffGeneration;
93 
94     int64_t mStartTimeRealUs;
95 
96     status_t onPrepare();
97     status_t onStart();
98     status_t onStop();
99     status_t onReset();
100     status_t onDoMoreStuff();
101     status_t onOutputFormatChanged(size_t trackIndex, CodecState *state);
102 
103     void renderAudio(
104             CodecState *state, BufferInfo *info, const sp<ABuffer> &buffer);
105 
106     DISALLOW_EVIL_CONSTRUCTORS(SimplePlayer);
107 };
108 
109 }  // namespace android
110