Home | History | Annotate | Download | only in nuplayer2
      1 /*
      2  * Copyright 2017 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_SOURCE2_H_
     18 
     19 #define RTSP_SOURCE2_H_
     20 
     21 #include "NuPlayer2Source.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 NuPlayer2::RTSPSource2 : public NuPlayer2::Source {
     34     RTSPSource2(
     35             const sp<AMessage> &notify,
     36             const sp<MediaHTTPService> &httpService,
     37             const char *url,
     38             const KeyedVector<String8, String8> *headers,
     39             uid_t uid = 0,
     40             bool isSDP = false);
     41 
     42     virtual status_t getBufferingSettings(
     43             BufferingSettings* buffering /* nonnull */) override;
     44     virtual status_t setBufferingSettings(const BufferingSettings& buffering) override;
     45 
     46     virtual void prepareAsync();
     47     virtual void start();
     48     virtual void stop();
     49 
     50     virtual status_t feedMoreTSData();
     51 
     52     virtual status_t dequeueAccessUnit(bool audio, sp<ABuffer> *accessUnit);
     53 
     54     virtual status_t getDuration(int64_t *durationUs);
     55     virtual status_t seekTo(
     56             int64_t seekTimeUs,
     57             MediaPlayer2SeekMode mode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC) override;
     58 
     59     void onMessageReceived(const sp<AMessage> &msg);
     60 
     61 protected:
     62     virtual ~RTSPSource2();
     63 
     64     virtual sp<MetaData> getFormatMeta(bool audio);
     65 
     66 private:
     67     enum {
     68         kWhatNotify          = 'noti',
     69         kWhatDisconnect      = 'disc',
     70         kWhatPerformSeek     = 'seek',
     71         kWhatPollBuffering   = 'poll',
     72         kWhatSignalEOS       = 'eos ',
     73     };
     74 
     75     enum State {
     76         DISCONNECTED,
     77         CONNECTING,
     78         CONNECTED,
     79         SEEKING,
     80     };
     81 
     82     enum Flags {
     83         // Don't log any URLs.
     84         kFlagIncognito = 1,
     85     };
     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<MediaHTTPService> mHTTPService;
     97     AString mURL;
     98     KeyedVector<String8, String8> mExtraHeaders;
     99     uid_t mUID;
    100     uint32_t mFlags;
    101     bool mIsSDP;
    102     State mState;
    103     status_t mFinalResult;
    104     sp<AReplyToken> mDisconnectReplyID;
    105     Mutex mBufferingLock;
    106     bool mBuffering;
    107     bool mInPreparationPhase;
    108     bool mEOSPending;
    109 
    110     Mutex mBufferingSettingsLock;
    111     BufferingSettings mBufferingSettings;
    112 
    113     sp<ALooper> mLooper;
    114     sp<MyHandler> mHandler;
    115     sp<SDPLoader> mSDPLoader;
    116 
    117     Vector<TrackInfo> mTracks;
    118     sp<AnotherPacketSource> mAudioTrack;
    119     sp<AnotherPacketSource> mVideoTrack;
    120 
    121     sp<ATSParser> mTSParser;
    122 
    123     int32_t mSeekGeneration;
    124 
    125     int64_t mEOSTimeoutAudio;
    126     int64_t mEOSTimeoutVideo;
    127 
    128     sp<AReplyToken> mSeekReplyID;
    129 
    130     sp<AnotherPacketSource> getSource(bool audio);
    131 
    132     void onConnected();
    133     void onSDPLoaded(const sp<AMessage> &msg);
    134     void onDisconnected(const sp<AMessage> &msg);
    135     void finishDisconnectIfPossible();
    136 
    137     void performSeek(int64_t seekTimeUs);
    138     void schedulePollBuffering();
    139     void checkBuffering(
    140             bool *prepared,
    141             bool *underflow,
    142             bool *overflow,
    143             bool *startServer,
    144             bool *finished);
    145     void onPollBuffering();
    146 
    147     bool haveSufficientDataOnAllTracks();
    148 
    149     void setEOSTimeout(bool audio, int64_t timeout);
    150     void setError(status_t err);
    151     void startBufferingIfNecessary();
    152     bool stopBufferingIfNecessary();
    153     void finishSeek(status_t err);
    154 
    155     void postSourceEOSIfNecessary();
    156     void signalSourceEOS(status_t result);
    157     void onSignalEOS(const sp<AMessage> &msg);
    158 
    159     bool sourceNearEOS(bool audio);
    160     bool sourceReachedEOS(bool audio);
    161 
    162     DISALLOW_EVIL_CONSTRUCTORS(RTSPSource2);
    163 };
    164 
    165 }  // namespace android
    166 
    167 #endif  // RTSP_SOURCE2_H_
    168