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
25namespace android {
26
27struct ABuffer;
28struct DataSource;
29struct LiveDataSource;
30struct M3UParser;
31struct HTTPBase;
32
33struct LiveSession : public AHandler {
34    enum Flags {
35        // Don't log any URLs.
36        kFlagIncognito = 1,
37    };
38    LiveSession(uint32_t flags = 0, bool uidValid = false, uid_t uid = 0);
39
40    sp<DataSource> getDataSource();
41
42    void connect(
43            const char *url,
44            const KeyedVector<String8, String8> *headers = NULL);
45
46    void disconnect();
47
48    // Blocks until seek is complete.
49    void seekTo(int64_t timeUs);
50
51    status_t getDuration(int64_t *durationUs);
52    bool isSeekable();
53
54protected:
55    virtual ~LiveSession();
56
57    virtual void onMessageReceived(const sp<AMessage> &msg);
58
59private:
60    enum {
61        kMaxNumQueuedFragments = 3,
62        kMaxNumRetries         = 5,
63    };
64
65    enum {
66        kWhatConnect        = 'conn',
67        kWhatDisconnect     = 'disc',
68        kWhatMonitorQueue   = 'moni',
69        kWhatSeek           = 'seek',
70    };
71
72    struct BandwidthItem {
73        AString mURI;
74        unsigned long mBandwidth;
75    };
76
77    uint32_t mFlags;
78    bool mUIDValid;
79    uid_t mUID;
80
81    sp<LiveDataSource> mDataSource;
82
83    sp<HTTPBase> mHTTPDataSource;
84
85    AString mMasterURL;
86    KeyedVector<String8, String8> mExtraHeaders;
87
88    Vector<BandwidthItem> mBandwidthItems;
89
90    KeyedVector<AString, sp<ABuffer> > mAESKeyForURI;
91
92    ssize_t mPrevBandwidthIndex;
93    int64_t mLastPlaylistFetchTimeUs;
94    sp<M3UParser> mPlaylist;
95    int32_t mSeqNumber;
96    int64_t mSeekTimeUs;
97    int32_t mNumRetries;
98
99    Mutex mLock;
100    Condition mCondition;
101    int64_t mDurationUs;
102    bool mSeekDone;
103    bool mDisconnectPending;
104
105    int32_t mMonitorQueueGeneration;
106
107    enum RefreshState {
108        INITIAL_MINIMUM_RELOAD_DELAY,
109        FIRST_UNCHANGED_RELOAD_ATTEMPT,
110        SECOND_UNCHANGED_RELOAD_ATTEMPT,
111        THIRD_UNCHANGED_RELOAD_ATTEMPT
112    };
113    RefreshState mRefreshState;
114
115    uint8_t mPlaylistHash[16];
116
117    void onConnect(const sp<AMessage> &msg);
118    void onDisconnect();
119    void onDownloadNext();
120    void onMonitorQueue();
121    void onSeek(const sp<AMessage> &msg);
122
123    status_t fetchFile(
124            const char *url, sp<ABuffer> *out,
125            int64_t range_offset = 0, int64_t range_length = -1);
126
127    sp<M3UParser> fetchPlaylist(const char *url, bool *unchanged);
128    size_t getBandwidthIndex();
129
130    status_t decryptBuffer(
131            size_t playlistIndex, const sp<ABuffer> &buffer);
132
133    void postMonitorQueue(int64_t delayUs = 0);
134
135    bool timeToRefreshPlaylist(int64_t nowUs) const;
136
137    static int SortByBandwidth(const BandwidthItem *, const BandwidthItem *);
138
139    DISALLOW_EVIL_CONSTRUCTORS(LiveSession);
140};
141
142}  // namespace android
143
144#endif  // LIVE_SESSION_H_
145