blob: 08129919b341e80f126adc6cc22ae5fc6499f801 [file] [log] [blame]
Andreas Huber2bfdd422011-10-11 15:24:07 -07001/*
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
Andreas Hubercfaeeec2012-08-31 10:27:46 -070023#include "ATSParser.h"
24
Andreas Huber2bfdd422011-10-11 15:24:07 -070025namespace android {
26
27struct ALooper;
Lajos Molnar3f274362015-03-05 14:35:41 -080028struct AReplyToken;
Andreas Huber2bfdd422011-10-11 15:24:07 -070029struct AnotherPacketSource;
30struct MyHandler;
Oscar Rydhé7a33b772012-02-20 10:15:48 +010031struct SDPLoader;
Andreas Huber2bfdd422011-10-11 15:24:07 -070032
33struct NuPlayer::RTSPSource : public NuPlayer::Source {
34 RTSPSource(
Andreas Huberb5f25f02013-02-05 10:14:26 -080035 const sp<AMessage> &notify,
Andreas Huber1b86fe02014-01-29 11:13:26 -080036 const sp<IMediaHTTPService> &httpService,
Andreas Huber2bfdd422011-10-11 15:24:07 -070037 const char *url,
38 const KeyedVector<String8, String8> *headers,
39 bool uidValid = false,
Oscar Rydhé7a33b772012-02-20 10:15:48 +010040 uid_t uid = 0,
41 bool isSDP = false);
Andreas Huber2bfdd422011-10-11 15:24:07 -070042
Wei Jia48fa06d2016-12-20 15:30:49 -080043 virtual status_t getDefaultBufferingSettings(
44 BufferingSettings* buffering /* nonnull */) override;
45 virtual status_t setBufferingSettings(const BufferingSettings& buffering) override;
46
Andreas Huber9575c962013-02-05 13:59:56 -080047 virtual void prepareAsync();
Andreas Huber2bfdd422011-10-11 15:24:07 -070048 virtual void start();
49 virtual void stop();
50
51 virtual status_t feedMoreTSData();
52
Andreas Huber2bfdd422011-10-11 15:24:07 -070053 virtual status_t dequeueAccessUnit(bool audio, sp<ABuffer> *accessUnit);
54
55 virtual status_t getDuration(int64_t *durationUs);
Wei Jiac5de0912016-11-18 10:22:14 -080056 virtual status_t seekTo(
57 int64_t seekTimeUs,
58 MediaPlayerSeekMode mode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC) override;
Andreas Huberb7c8e912012-11-27 15:02:53 -080059
Andreas Huber2bfdd422011-10-11 15:24:07 -070060 void onMessageReceived(const sp<AMessage> &msg);
61
62protected:
63 virtual ~RTSPSource();
64
Andreas Huber84066782011-08-16 09:34:26 -070065 virtual sp<MetaData> getFormatMeta(bool audio);
66
Andreas Huber2bfdd422011-10-11 15:24:07 -070067private:
68 enum {
69 kWhatNotify = 'noti',
70 kWhatDisconnect = 'disc',
Andreas Huberee736e92011-12-08 13:04:50 -080071 kWhatPerformSeek = 'seek',
Robert Shih641e0c72016-02-22 11:37:20 -080072 kWhatPollBuffering = 'poll',
Robert Shihf1d261f2016-07-29 16:44:39 -070073 kWhatSignalEOS = 'eos ',
Wei Jia48fa06d2016-12-20 15:30:49 -080074 kWhatSetBufferingSettings = 'sBuS',
Andreas Huber2bfdd422011-10-11 15:24:07 -070075 };
76
77 enum State {
78 DISCONNECTED,
79 CONNECTING,
80 CONNECTED,
81 SEEKING,
82 };
83
84 enum Flags {
85 // Don't log any URLs.
86 kFlagIncognito = 1,
87 };
88
89 struct TrackInfo {
90 sp<AnotherPacketSource> mSource;
91
92 int32_t mTimeScale;
93 uint32_t mRTPTime;
94 int64_t mNormalPlaytimeUs;
Andreas Huber1906e5c2011-12-08 12:27:47 -080095 bool mNPTMappingValid;
Andreas Huber2bfdd422011-10-11 15:24:07 -070096 };
97
Andreas Huber1b86fe02014-01-29 11:13:26 -080098 sp<IMediaHTTPService> mHTTPService;
Andreas Huber2bfdd422011-10-11 15:24:07 -070099 AString mURL;
100 KeyedVector<String8, String8> mExtraHeaders;
101 bool mUIDValid;
102 uid_t mUID;
103 uint32_t mFlags;
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100104 bool mIsSDP;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700105 State mState;
106 status_t mFinalResult;
Lajos Molnar3f274362015-03-05 14:35:41 -0800107 sp<AReplyToken> mDisconnectReplyID;
Chong Zhang180d1b92014-12-02 18:35:35 -0800108 Mutex mBufferingLock;
Roger Jönssonb50e83e2013-01-21 16:26:41 +0100109 bool mBuffering;
Robert Shih641e0c72016-02-22 11:37:20 -0800110 bool mInPreparationPhase;
Robert Shihf1d261f2016-07-29 16:44:39 -0700111 bool mEOSPending;
Wei Jia48fa06d2016-12-20 15:30:49 -0800112 BufferingSettings mBufferingSettings;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700113
114 sp<ALooper> mLooper;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700115 sp<MyHandler> mHandler;
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100116 sp<SDPLoader> mSDPLoader;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700117
118 Vector<TrackInfo> mTracks;
119 sp<AnotherPacketSource> mAudioTrack;
120 sp<AnotherPacketSource> mVideoTrack;
121
Andreas Hubercfaeeec2012-08-31 10:27:46 -0700122 sp<ATSParser> mTSParser;
123
Andreas Huberee736e92011-12-08 13:04:50 -0800124 int32_t mSeekGeneration;
125
Roger Jönssonb50e83e2013-01-21 16:26:41 +0100126 int64_t mEOSTimeoutAudio;
127 int64_t mEOSTimeoutVideo;
128
Robert Shih8d237a52015-07-13 17:59:36 -0700129 sp<AReplyToken> mSeekReplyID;
130
Andreas Huber2bfdd422011-10-11 15:24:07 -0700131 sp<AnotherPacketSource> getSource(bool audio);
132
133 void onConnected();
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100134 void onSDPLoaded(const sp<AMessage> &msg);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700135 void onDisconnected(const sp<AMessage> &msg);
136 void finishDisconnectIfPossible();
137
Andreas Huberee736e92011-12-08 13:04:50 -0800138 void performSeek(int64_t seekTimeUs);
Robert Shih641e0c72016-02-22 11:37:20 -0800139 void schedulePollBuffering();
Robert Shihf1d261f2016-07-29 16:44:39 -0700140 void checkBuffering(
141 bool *prepared,
142 bool *underflow,
143 bool *overflow,
144 bool *startServer,
145 bool *finished);
Robert Shih641e0c72016-02-22 11:37:20 -0800146 void onPollBuffering();
Andreas Huberee736e92011-12-08 13:04:50 -0800147
Andreas Huberbfd4d0d2012-05-17 14:18:50 -0700148 bool haveSufficientDataOnAllTracks();
149
Roger Jönssonb50e83e2013-01-21 16:26:41 +0100150 void setEOSTimeout(bool audio, int64_t timeout);
Chong Zhang180d1b92014-12-02 18:35:35 -0800151 void setError(status_t err);
152 void startBufferingIfNecessary();
153 bool stopBufferingIfNecessary();
Robert Shih8d237a52015-07-13 17:59:36 -0700154 void finishSeek(status_t err);
Roger Jönssonb50e83e2013-01-21 16:26:41 +0100155
Robert Shihf1d261f2016-07-29 16:44:39 -0700156 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
Andreas Huber2bfdd422011-10-11 15:24:07 -0700163 DISALLOW_EVIL_CONSTRUCTORS(RTSPSource);
164};
165
166} // namespace android
167
168#endif // RTSP_SOURCE_H_