RTSPSource.h revision c5de09127e9e0d5df7aa587be317e1487d793245
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(
53            int64_t seekTimeUs,
54            MediaPlayerSeekMode mode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC) override;
55
56    void onMessageReceived(const sp<AMessage> &msg);
57
58protected:
59    virtual ~RTSPSource();
60
61    virtual sp<MetaData> getFormatMeta(bool audio);
62
63private:
64    enum {
65        kWhatNotify          = 'noti',
66        kWhatDisconnect      = 'disc',
67        kWhatPerformSeek     = 'seek',
68        kWhatPollBuffering   = 'poll',
69        kWhatSignalEOS       = 'eos ',
70    };
71
72    enum State {
73        DISCONNECTED,
74        CONNECTING,
75        CONNECTED,
76        SEEKING,
77    };
78
79    enum Flags {
80        // Don't log any URLs.
81        kFlagIncognito = 1,
82    };
83
84    // Buffer Prepare/Underflow/Overflow/Resume Marks
85    static const int64_t kPrepareMarkUs;
86    static const int64_t kUnderflowMarkUs;
87    static const int64_t kOverflowMarkUs;
88    static const int64_t kStartServerMarkUs;
89
90    struct TrackInfo {
91        sp<AnotherPacketSource> mSource;
92
93        int32_t mTimeScale;
94        uint32_t mRTPTime;
95        int64_t mNormalPlaytimeUs;
96        bool mNPTMappingValid;
97    };
98
99    sp<IMediaHTTPService> mHTTPService;
100    AString mURL;
101    KeyedVector<String8, String8> mExtraHeaders;
102    bool mUIDValid;
103    uid_t mUID;
104    uint32_t mFlags;
105    bool mIsSDP;
106    State mState;
107    status_t mFinalResult;
108    sp<AReplyToken> mDisconnectReplyID;
109    Mutex mBufferingLock;
110    bool mBuffering;
111    bool mInPreparationPhase;
112    bool mEOSPending;
113
114    sp<ALooper> mLooper;
115    sp<MyHandler> mHandler;
116    sp<SDPLoader> mSDPLoader;
117
118    Vector<TrackInfo> mTracks;
119    sp<AnotherPacketSource> mAudioTrack;
120    sp<AnotherPacketSource> mVideoTrack;
121
122    sp<ATSParser> mTSParser;
123
124    int32_t mSeekGeneration;
125
126    int64_t mEOSTimeoutAudio;
127    int64_t mEOSTimeoutVideo;
128
129    sp<AReplyToken> mSeekReplyID;
130
131    sp<AnotherPacketSource> getSource(bool audio);
132
133    void onConnected();
134    void onSDPLoaded(const sp<AMessage> &msg);
135    void onDisconnected(const sp<AMessage> &msg);
136    void finishDisconnectIfPossible();
137
138    void performSeek(int64_t seekTimeUs);
139    void schedulePollBuffering();
140    void checkBuffering(
141            bool *prepared,
142            bool *underflow,
143            bool *overflow,
144            bool *startServer,
145            bool *finished);
146    void onPollBuffering();
147
148    bool haveSufficientDataOnAllTracks();
149
150    void setEOSTimeout(bool audio, int64_t timeout);
151    void setError(status_t err);
152    void startBufferingIfNecessary();
153    bool stopBufferingIfNecessary();
154    void finishSeek(status_t err);
155
156    void postSourceEOSIfNecessary();
157    void signalSourceEOS(status_t result);
158    void onSignalEOS(const sp<AMessage> &msg);
159
160    bool sourceNearEOS(bool audio);
161    bool sourceReachedEOS(bool audio);
162
163    DISALLOW_EVIL_CONSTRUCTORS(RTSPSource);
164};
165
166}  // namespace android
167
168#endif  // RTSP_SOURCE_H_
169