1/*
2 * Copyright (C) 2011 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#ifndef __AAH_TX_PLAYER_H__
18#define __AAH_TX_PLAYER_H__
19
20#include <common_time/cc_helper.h>
21#include <libstagefright/include/HTTPBase.h>
22#include <libstagefright/include/NuCachedSource2.h>
23#include <libstagefright/include/TimedEventQueue.h>
24#include <media/MediaPlayerInterface.h>
25#include <media/stagefright/MediaExtractor.h>
26#include <media/stagefright/MediaSource.h>
27#include <utils/LinearTransform.h>
28#include <utils/String8.h>
29#include <utils/threads.h>
30
31#include "aah_tx_sender.h"
32
33namespace android {
34
35class AAH_TXPlayer : public MediaPlayerHWInterface {
36  public:
37    AAH_TXPlayer();
38
39    virtual status_t    initCheck();
40    virtual status_t    setDataSource(const char *url,
41                                      const KeyedVector<String8, String8>*
42                                      headers);
43    virtual status_t    setDataSource(int fd, int64_t offset, int64_t length);
44    virtual status_t    setVideoSurface(const sp<Surface>& surface);
45    virtual status_t    setVideoSurfaceTexture(const sp<ISurfaceTexture>&
46                                               surfaceTexture);
47    virtual status_t    prepare();
48    virtual status_t    prepareAsync();
49    virtual status_t    start();
50    virtual status_t    stop();
51    virtual status_t    pause();
52    virtual bool        isPlaying();
53    virtual status_t    seekTo(int msec);
54    virtual status_t    getCurrentPosition(int *msec);
55    virtual status_t    getDuration(int *msec);
56    virtual status_t    reset();
57    virtual status_t    setLooping(int loop);
58    virtual player_type playerType();
59    virtual status_t    setParameter(int key, const Parcel &request);
60    virtual status_t    getParameter(int key, Parcel *reply);
61    virtual status_t    invoke(const Parcel& request, Parcel *reply);
62    virtual status_t    getMetadata(const media::Metadata::Filter& ids,
63                                    Parcel* records);
64    virtual status_t    setVolume(float leftVolume, float rightVolume);
65    virtual status_t    setAudioStreamType(audio_stream_type_t streamType);
66    virtual status_t    setRetransmitEndpoint(
67                            const struct sockaddr_in* endpoint);
68
69    static const int64_t kAAHRetryKeepAroundTimeNs;
70
71  protected:
72    virtual ~AAH_TXPlayer();
73
74  private:
75    friend struct AwesomeEvent;
76
77    enum {
78        PLAYING             = 1,
79        PREPARING           = 8,
80        PREPARED            = 16,
81        PREPARE_CANCELLED   = 64,
82        CACHE_UNDERRUN      = 128,
83
84        // We are basically done preparing but are currently buffering
85        // sufficient data to begin playback and finish the preparation
86        // phase for good.
87        PREPARING_CONNECTED = 2048,
88
89        INCOGNITO           = 32768,
90    };
91
92    status_t setDataSource_l(const char *url,
93                             const KeyedVector<String8, String8> *headers);
94    status_t setDataSource_l(const sp<MediaExtractor>& extractor);
95    status_t finishSetDataSource_l();
96    status_t prepareAsync_l();
97    void onPrepareAsyncEvent();
98    void finishAsyncPrepare_l();
99    void abortPrepare(status_t err);
100    status_t play_l();
101    status_t pause_l(bool doClockUpdate = true);
102    status_t seekTo_l(int64_t timeUs);
103    void updateClockTransform_l(bool pause);
104    void sendEOS_l();
105    void cancelPlayerEvents(bool keepBufferingGoing = false);
106    void reset_l();
107    void notifyListener_l(int msg, int ext1 = 0, int ext2 = 0);
108    bool getBitrate_l(int64_t* bitrate);
109    status_t getDuration_l(int* msec);
110    bool getCachedDuration_l(int64_t* durationUs, bool* eos);
111    void ensureCacheIsFetching_l();
112    void postBufferingEvent_l();
113    void postPumpAudioEvent_l(int64_t delayUs);
114    void onBufferingUpdate();
115    void onPumpAudio();
116    void queuePacketToSender_l(const sp<TRTPPacket>& packet);
117
118    Mutex mLock;
119
120    TimedEventQueue mQueue;
121    bool mQueueStarted;
122
123    sp<TimedEventQueue::Event> mBufferingEvent;
124    bool mBufferingEventPending;
125
126    uint32_t mFlags;
127    uint32_t mExtractorFlags;
128
129    String8 mUri;
130    KeyedVector<String8, String8> mUriHeaders;
131
132    sp<DataSource> mFileSource;
133
134    sp<TimedEventQueue::Event> mAsyncPrepareEvent;
135    Condition mPreparedCondition;
136    status_t mPrepareResult;
137
138    bool mIsSeeking;
139    int64_t mSeekTimeUs;
140
141    sp<TimedEventQueue::Event> mPumpAudioEvent;
142    bool mPumpAudioEventPending;
143
144    sp<HTTPBase> mConnectingDataSource;
145    sp<NuCachedSource2> mCachedSource;
146
147    sp<MediaSource> mAudioSource;
148    TRTPAudioPacket::TRTPAudioCodecType mAudioCodec;
149    sp<MetaData> mAudioFormat;
150    uint8_t* mAudioCodecData;
151    size_t mAudioCodecDataSize;
152
153    int64_t mDurationUs;
154    int64_t mBitrate;
155
156    sp<AAH_TXSender> mAAH_Sender;
157    LinearTransform  mCurrentClockTransform;
158    bool             mCurrentClockTransformValid;
159    int64_t          mLastQueuedMediaTimePTS;
160    bool             mLastQueuedMediaTimePTSValid;
161    bool             mPlayRateIsPaused;
162    CCHelper         mCCHelper;
163
164    Mutex mEndpointLock;
165    AAH_TXSender::Endpoint mEndpoint;
166    bool mEndpointValid;
167    bool mEndpointRegistered;
168    uint16_t mProgramID;
169    uint8_t mTRTPVolume;
170
171    DISALLOW_EVIL_CONSTRUCTORS(AAH_TXPlayer);
172};
173
174}  // namespace android
175
176#endif  // __AAH_TX_PLAYER_H__
177