Home | History | Annotate | Download | only in httplive
      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 LIVE_SESSION_H_
     18 
     19 #define LIVE_SESSION_H_
     20 
     21 #include <media/stagefright/foundation/AHandler.h>
     22 
     23 #include <utils/String8.h>
     24 
     25 namespace android {
     26 
     27 struct ABuffer;
     28 struct AnotherPacketSource;
     29 struct DataSource;
     30 struct HTTPBase;
     31 struct LiveDataSource;
     32 struct M3UParser;
     33 struct PlaylistFetcher;
     34 struct Parcel;
     35 
     36 struct LiveSession : public AHandler {
     37     enum Flags {
     38         // Don't log any URLs.
     39         kFlagIncognito = 1,
     40     };
     41     LiveSession(
     42             const sp<AMessage> &notify,
     43             uint32_t flags = 0, bool uidValid = false, uid_t uid = 0);
     44 
     45     enum StreamType {
     46         STREAMTYPE_AUDIO        = 1,
     47         STREAMTYPE_VIDEO        = 2,
     48         STREAMTYPE_SUBTITLES    = 4,
     49     };
     50     status_t dequeueAccessUnit(StreamType stream, sp<ABuffer> *accessUnit);
     51 
     52     status_t getStreamFormat(StreamType stream, sp<AMessage> *format);
     53 
     54     void connectAsync(
     55             const char *url,
     56             const KeyedVector<String8, String8> *headers = NULL);
     57 
     58     status_t disconnect();
     59 
     60     // Blocks until seek is complete.
     61     status_t seekTo(int64_t timeUs);
     62 
     63     status_t getDuration(int64_t *durationUs) const;
     64     status_t getTrackInfo(Parcel *reply) const;
     65     status_t selectTrack(size_t index, bool select);
     66 
     67     bool isSeekable() const;
     68     bool hasDynamicDuration() const;
     69 
     70     enum {
     71         kWhatStreamsChanged,
     72         kWhatError,
     73         kWhatPrepared,
     74         kWhatPreparationFailed,
     75     };
     76 
     77 protected:
     78     virtual ~LiveSession();
     79 
     80     virtual void onMessageReceived(const sp<AMessage> &msg);
     81 
     82 private:
     83     friend struct PlaylistFetcher;
     84 
     85     enum {
     86         kWhatConnect                    = 'conn',
     87         kWhatDisconnect                 = 'disc',
     88         kWhatSeek                       = 'seek',
     89         kWhatFetcherNotify              = 'notf',
     90         kWhatCheckBandwidth             = 'bndw',
     91         kWhatChangeConfiguration        = 'chC0',
     92         kWhatChangeConfiguration2       = 'chC2',
     93         kWhatChangeConfiguration3       = 'chC3',
     94         kWhatFinishDisconnect2          = 'fin2',
     95     };
     96 
     97     struct BandwidthItem {
     98         size_t mPlaylistIndex;
     99         unsigned long mBandwidth;
    100     };
    101 
    102     struct FetcherInfo {
    103         sp<PlaylistFetcher> mFetcher;
    104         int64_t mDurationUs;
    105         bool mIsPrepared;
    106     };
    107 
    108     sp<AMessage> mNotify;
    109     uint32_t mFlags;
    110     bool mUIDValid;
    111     uid_t mUID;
    112 
    113     bool mInPreparationPhase;
    114 
    115     sp<HTTPBase> mHTTPDataSource;
    116     KeyedVector<String8, String8> mExtraHeaders;
    117 
    118     AString mMasterURL;
    119 
    120     Vector<BandwidthItem> mBandwidthItems;
    121     ssize_t mPrevBandwidthIndex;
    122 
    123     sp<M3UParser> mPlaylist;
    124 
    125     KeyedVector<AString, FetcherInfo> mFetcherInfos;
    126     AString mAudioURI, mVideoURI, mSubtitleURI;
    127     uint32_t mStreamMask;
    128 
    129     KeyedVector<StreamType, sp<AnotherPacketSource> > mPacketSources;
    130 
    131     int32_t mCheckBandwidthGeneration;
    132 
    133     size_t mContinuationCounter;
    134     sp<AMessage> mContinuation;
    135 
    136     int64_t mLastDequeuedTimeUs;
    137     int64_t mRealTimeBaseUs;
    138 
    139     bool mReconfigurationInProgress;
    140     uint32_t mDisconnectReplyID;
    141 
    142     sp<PlaylistFetcher> addFetcher(const char *uri);
    143 
    144     void onConnect(const sp<AMessage> &msg);
    145     status_t onSeek(const sp<AMessage> &msg);
    146     void onFinishDisconnect2();
    147 
    148     status_t fetchFile(
    149             const char *url, sp<ABuffer> *out,
    150             int64_t range_offset = 0, int64_t range_length = -1);
    151 
    152     sp<M3UParser> fetchPlaylist(
    153             const char *url, uint8_t *curPlaylistHash, bool *unchanged);
    154 
    155     size_t getBandwidthIndex();
    156 
    157     static int SortByBandwidth(const BandwidthItem *, const BandwidthItem *);
    158 
    159     void changeConfiguration(
    160             int64_t timeUs, size_t bandwidthIndex, bool pickTrack = false);
    161     void onChangeConfiguration(const sp<AMessage> &msg);
    162     void onChangeConfiguration2(const sp<AMessage> &msg);
    163     void onChangeConfiguration3(const sp<AMessage> &msg);
    164 
    165     void scheduleCheckBandwidthEvent();
    166     void cancelCheckBandwidthEvent();
    167 
    168     void onCheckBandwidth();
    169 
    170     void finishDisconnect();
    171 
    172     void postPrepared(status_t err);
    173 
    174     DISALLOW_EVIL_CONSTRUCTORS(LiveSession);
    175 };
    176 
    177 }  // namespace android
    178 
    179 #endif  // LIVE_SESSION_H_
    180