1/*
2 * Copyright (C) 2010 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 RTSP_SOURCE_H_
18
19#define RTSP_SOURCE_H_
20
21#include "NuPlayerSource.h"
22
23#include "ATSParser.h"
24
25namespace android {
26
27struct ALooper;
28struct AReplyToken;
29struct AnotherPacketSource;
30struct MyHandler;
31struct SDPLoader;
32
33struct NuPlayer::RTSPSource : public NuPlayer::Source {
34    RTSPSource(
35            const sp<AMessage> &notify,
36            const sp<IMediaHTTPService> &httpService,
37            const char *url,
38            const KeyedVector<String8, String8> *headers,
39            bool uidValid = false,
40            uid_t uid = 0,
41            bool isSDP = false);
42
43    virtual void prepareAsync();
44    virtual void start();
45    virtual void stop();
46
47    virtual status_t feedMoreTSData();
48
49    virtual status_t dequeueAccessUnit(bool audio, sp<ABuffer> *accessUnit);
50
51    virtual status_t getDuration(int64_t *durationUs);
52    virtual status_t seekTo(int64_t seekTimeUs);
53
54    void onMessageReceived(const sp<AMessage> &msg);
55
56protected:
57    virtual ~RTSPSource();
58
59    virtual sp<MetaData> getFormatMeta(bool audio);
60
61private:
62    enum {
63        kWhatNotify          = 'noti',
64        kWhatDisconnect      = 'disc',
65        kWhatPerformSeek     = 'seek',
66        kWhatPollBuffering   = 'poll',
67    };
68
69    enum State {
70        DISCONNECTED,
71        CONNECTING,
72        CONNECTED,
73        SEEKING,
74    };
75
76    enum Flags {
77        // Don't log any URLs.
78        kFlagIncognito = 1,
79    };
80
81    // Buffer Prepare/Underflow/Overflow/Resume Marks
82    static const int64_t kPrepareMarkUs;
83    static const int64_t kUnderflowMarkUs;
84    static const int64_t kOverflowMarkUs;
85    static const int64_t kStartServerMarkUs;
86
87    struct TrackInfo {
88        sp<AnotherPacketSource> mSource;
89
90        int32_t mTimeScale;
91        uint32_t mRTPTime;
92        int64_t mNormalPlaytimeUs;
93        bool mNPTMappingValid;
94    };
95
96    sp<IMediaHTTPService> mHTTPService;
97    AString mURL;
98    KeyedVector<String8, String8> mExtraHeaders;
99    bool mUIDValid;
100    uid_t mUID;
101    uint32_t mFlags;
102    bool mIsSDP;
103    State mState;
104    status_t mFinalResult;
105    sp<AReplyToken> mDisconnectReplyID;
106    Mutex mBufferingLock;
107    bool mBuffering;
108    bool mInPreparationPhase;
109
110    sp<ALooper> mLooper;
111    sp<MyHandler> mHandler;
112    sp<SDPLoader> mSDPLoader;
113
114    Vector<TrackInfo> mTracks;
115    sp<AnotherPacketSource> mAudioTrack;
116    sp<AnotherPacketSource> mVideoTrack;
117
118    sp<ATSParser> mTSParser;
119
120    int32_t mSeekGeneration;
121
122    int64_t mEOSTimeoutAudio;
123    int64_t mEOSTimeoutVideo;
124
125    sp<AReplyToken> mSeekReplyID;
126
127    sp<AnotherPacketSource> getSource(bool audio);
128
129    void onConnected();
130    void onSDPLoaded(const sp<AMessage> &msg);
131    void onDisconnected(const sp<AMessage> &msg);
132    void finishDisconnectIfPossible();
133
134    void performSeek(int64_t seekTimeUs);
135    void schedulePollBuffering();
136    void checkBuffering(bool *prepared, bool *underflow, bool *overflow, bool *startServer);
137    void onPollBuffering();
138
139    bool haveSufficientDataOnAllTracks();
140
141    void setEOSTimeout(bool audio, int64_t timeout);
142    void setError(status_t err);
143    void startBufferingIfNecessary();
144    bool stopBufferingIfNecessary();
145    void finishSeek(status_t err);
146
147    DISALLOW_EVIL_CONSTRUCTORS(RTSPSource);
148};
149
150}  // namespace android
151
152#endif  // RTSP_SOURCE_H_
153