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