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