1a44153c1a57202fb538659eb50706e60454d6273Andreas Huber/*
2a44153c1a57202fb538659eb50706e60454d6273Andreas Huber * Copyright (C) 2010 The Android Open Source Project
3a44153c1a57202fb538659eb50706e60454d6273Andreas Huber *
4a44153c1a57202fb538659eb50706e60454d6273Andreas Huber * Licensed under the Apache License, Version 2.0 (the "License");
5a44153c1a57202fb538659eb50706e60454d6273Andreas Huber * you may not use this file except in compliance with the License.
6a44153c1a57202fb538659eb50706e60454d6273Andreas Huber * You may obtain a copy of the License at
7a44153c1a57202fb538659eb50706e60454d6273Andreas Huber *
8a44153c1a57202fb538659eb50706e60454d6273Andreas Huber *      http://www.apache.org/licenses/LICENSE-2.0
9a44153c1a57202fb538659eb50706e60454d6273Andreas Huber *
10a44153c1a57202fb538659eb50706e60454d6273Andreas Huber * Unless required by applicable law or agreed to in writing, software
11a44153c1a57202fb538659eb50706e60454d6273Andreas Huber * distributed under the License is distributed on an "AS IS" BASIS,
12a44153c1a57202fb538659eb50706e60454d6273Andreas Huber * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a44153c1a57202fb538659eb50706e60454d6273Andreas Huber * See the License for the specific language governing permissions and
14a44153c1a57202fb538659eb50706e60454d6273Andreas Huber * limitations under the License.
15a44153c1a57202fb538659eb50706e60454d6273Andreas Huber */
16a44153c1a57202fb538659eb50706e60454d6273Andreas Huber
17a44153c1a57202fb538659eb50706e60454d6273Andreas Huber#ifndef LIVE_SESSION_H_
18a44153c1a57202fb538659eb50706e60454d6273Andreas Huber
19a44153c1a57202fb538659eb50706e60454d6273Andreas Huber#define LIVE_SESSION_H_
20a44153c1a57202fb538659eb50706e60454d6273Andreas Huber
21a44153c1a57202fb538659eb50706e60454d6273Andreas Huber#include <media/stagefright/foundation/AHandler.h>
22a44153c1a57202fb538659eb50706e60454d6273Andreas Huber
23ad0d9c9c39a24b7fbd94e935a5855c9025341929Andreas Huber#include <utils/String8.h>
24ad0d9c9c39a24b7fbd94e935a5855c9025341929Andreas Huber
25a44153c1a57202fb538659eb50706e60454d6273Andreas Hubernamespace android {
26a44153c1a57202fb538659eb50706e60454d6273Andreas Huber
27a44153c1a57202fb538659eb50706e60454d6273Andreas Huberstruct ABuffer;
28a44153c1a57202fb538659eb50706e60454d6273Andreas Huberstruct DataSource;
29a44153c1a57202fb538659eb50706e60454d6273Andreas Huberstruct LiveDataSource;
30a44153c1a57202fb538659eb50706e60454d6273Andreas Huberstruct M3UParser;
311156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huberstruct HTTPBase;
32a44153c1a57202fb538659eb50706e60454d6273Andreas Huber
33a44153c1a57202fb538659eb50706e60454d6273Andreas Huberstruct LiveSession : public AHandler {
347314fa17093d514199fedcb55ac41136a1b31cb3Andreas Huber    enum Flags {
357314fa17093d514199fedcb55ac41136a1b31cb3Andreas Huber        // Don't log any URLs.
367314fa17093d514199fedcb55ac41136a1b31cb3Andreas Huber        kFlagIncognito = 1,
377314fa17093d514199fedcb55ac41136a1b31cb3Andreas Huber    };
389b80c2bdb205bc143104f54d0743b6eedd67b14eAndreas Huber    LiveSession(uint32_t flags = 0, bool uidValid = false, uid_t uid = 0);
39a44153c1a57202fb538659eb50706e60454d6273Andreas Huber
40a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    sp<DataSource> getDataSource();
41a44153c1a57202fb538659eb50706e60454d6273Andreas Huber
42ad0d9c9c39a24b7fbd94e935a5855c9025341929Andreas Huber    void connect(
43ad0d9c9c39a24b7fbd94e935a5855c9025341929Andreas Huber            const char *url,
44ad0d9c9c39a24b7fbd94e935a5855c9025341929Andreas Huber            const KeyedVector<String8, String8> *headers = NULL);
45ad0d9c9c39a24b7fbd94e935a5855c9025341929Andreas Huber
46a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    void disconnect();
47a44153c1a57202fb538659eb50706e60454d6273Andreas Huber
48a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    // Blocks until seek is complete.
49a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    void seekTo(int64_t timeUs);
50a44153c1a57202fb538659eb50706e60454d6273Andreas Huber
51a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    status_t getDuration(int64_t *durationUs);
52a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    bool isSeekable();
53a44153c1a57202fb538659eb50706e60454d6273Andreas Huber
54a44153c1a57202fb538659eb50706e60454d6273Andreas Huberprotected:
55a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    virtual ~LiveSession();
56a44153c1a57202fb538659eb50706e60454d6273Andreas Huber
57a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    virtual void onMessageReceived(const sp<AMessage> &msg);
58a44153c1a57202fb538659eb50706e60454d6273Andreas Huber
59a44153c1a57202fb538659eb50706e60454d6273Andreas Huberprivate:
60a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    enum {
6143c3e6ce02215ca99d506458f596cb1211639f29Andreas Huber        kMaxNumQueuedFragments = 3,
625bc087c573c70c84c6a39946457590b42d392a33Andreas Huber        kMaxNumRetries         = 5,
63a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    };
64a44153c1a57202fb538659eb50706e60454d6273Andreas Huber
65a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    enum {
66a44153c1a57202fb538659eb50706e60454d6273Andreas Huber        kWhatConnect        = 'conn',
67a44153c1a57202fb538659eb50706e60454d6273Andreas Huber        kWhatDisconnect     = 'disc',
68a44153c1a57202fb538659eb50706e60454d6273Andreas Huber        kWhatMonitorQueue   = 'moni',
69a44153c1a57202fb538659eb50706e60454d6273Andreas Huber        kWhatSeek           = 'seek',
70a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    };
71a44153c1a57202fb538659eb50706e60454d6273Andreas Huber
72a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    struct BandwidthItem {
73a44153c1a57202fb538659eb50706e60454d6273Andreas Huber        AString mURI;
74a44153c1a57202fb538659eb50706e60454d6273Andreas Huber        unsigned long mBandwidth;
75a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    };
76a44153c1a57202fb538659eb50706e60454d6273Andreas Huber
777314fa17093d514199fedcb55ac41136a1b31cb3Andreas Huber    uint32_t mFlags;
789b80c2bdb205bc143104f54d0743b6eedd67b14eAndreas Huber    bool mUIDValid;
799b80c2bdb205bc143104f54d0743b6eedd67b14eAndreas Huber    uid_t mUID;
807314fa17093d514199fedcb55ac41136a1b31cb3Andreas Huber
81a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    sp<LiveDataSource> mDataSource;
82a44153c1a57202fb538659eb50706e60454d6273Andreas Huber
831156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    sp<HTTPBase> mHTTPDataSource;
84a44153c1a57202fb538659eb50706e60454d6273Andreas Huber
85a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    AString mMasterURL;
86ad0d9c9c39a24b7fbd94e935a5855c9025341929Andreas Huber    KeyedVector<String8, String8> mExtraHeaders;
87ad0d9c9c39a24b7fbd94e935a5855c9025341929Andreas Huber
88a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    Vector<BandwidthItem> mBandwidthItems;
89a44153c1a57202fb538659eb50706e60454d6273Andreas Huber
90a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    KeyedVector<AString, sp<ABuffer> > mAESKeyForURI;
91a44153c1a57202fb538659eb50706e60454d6273Andreas Huber
92a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    ssize_t mPrevBandwidthIndex;
93a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    int64_t mLastPlaylistFetchTimeUs;
94a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    sp<M3UParser> mPlaylist;
95a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    int32_t mSeqNumber;
96a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    int64_t mSeekTimeUs;
97a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    int32_t mNumRetries;
98a44153c1a57202fb538659eb50706e60454d6273Andreas Huber
99a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    Mutex mLock;
100a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    Condition mCondition;
101a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    int64_t mDurationUs;
102a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    bool mSeekDone;
103ab8a0badb8fb1e294dacf2eb6a891439f348aff9Andreas Huber    bool mDisconnectPending;
104a44153c1a57202fb538659eb50706e60454d6273Andreas Huber
105a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    int32_t mMonitorQueueGeneration;
106a44153c1a57202fb538659eb50706e60454d6273Andreas Huber
1077e43a5a2dcfa9bc64ef477472a33c87a84695c09Andreas Huber    enum RefreshState {
1087e43a5a2dcfa9bc64ef477472a33c87a84695c09Andreas Huber        INITIAL_MINIMUM_RELOAD_DELAY,
1097e43a5a2dcfa9bc64ef477472a33c87a84695c09Andreas Huber        FIRST_UNCHANGED_RELOAD_ATTEMPT,
1107e43a5a2dcfa9bc64ef477472a33c87a84695c09Andreas Huber        SECOND_UNCHANGED_RELOAD_ATTEMPT,
1117e43a5a2dcfa9bc64ef477472a33c87a84695c09Andreas Huber        THIRD_UNCHANGED_RELOAD_ATTEMPT
1127e43a5a2dcfa9bc64ef477472a33c87a84695c09Andreas Huber    };
1137e43a5a2dcfa9bc64ef477472a33c87a84695c09Andreas Huber    RefreshState mRefreshState;
1147e43a5a2dcfa9bc64ef477472a33c87a84695c09Andreas Huber
1157e43a5a2dcfa9bc64ef477472a33c87a84695c09Andreas Huber    uint8_t mPlaylistHash[16];
1167e43a5a2dcfa9bc64ef477472a33c87a84695c09Andreas Huber
117a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    void onConnect(const sp<AMessage> &msg);
118a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    void onDisconnect();
119a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    void onDownloadNext();
120a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    void onMonitorQueue();
121a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    void onSeek(const sp<AMessage> &msg);
122a44153c1a57202fb538659eb50706e60454d6273Andreas Huber
1232aa4cc04154d82f6658fb86f394f13bb488b0468Andreas Huber    status_t fetchFile(
1242aa4cc04154d82f6658fb86f394f13bb488b0468Andreas Huber            const char *url, sp<ABuffer> *out,
1252aa4cc04154d82f6658fb86f394f13bb488b0468Andreas Huber            int64_t range_offset = 0, int64_t range_length = -1);
1262aa4cc04154d82f6658fb86f394f13bb488b0468Andreas Huber
1277e43a5a2dcfa9bc64ef477472a33c87a84695c09Andreas Huber    sp<M3UParser> fetchPlaylist(const char *url, bool *unchanged);
128a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    size_t getBandwidthIndex();
129a44153c1a57202fb538659eb50706e60454d6273Andreas Huber
130a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    status_t decryptBuffer(
131a44153c1a57202fb538659eb50706e60454d6273Andreas Huber            size_t playlistIndex, const sp<ABuffer> &buffer);
132a44153c1a57202fb538659eb50706e60454d6273Andreas Huber
133a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    void postMonitorQueue(int64_t delayUs = 0);
134a44153c1a57202fb538659eb50706e60454d6273Andreas Huber
1357e43a5a2dcfa9bc64ef477472a33c87a84695c09Andreas Huber    bool timeToRefreshPlaylist(int64_t nowUs) const;
1367e43a5a2dcfa9bc64ef477472a33c87a84695c09Andreas Huber
137a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    static int SortByBandwidth(const BandwidthItem *, const BandwidthItem *);
138a44153c1a57202fb538659eb50706e60454d6273Andreas Huber
139a44153c1a57202fb538659eb50706e60454d6273Andreas Huber    DISALLOW_EVIL_CONSTRUCTORS(LiveSession);
140a44153c1a57202fb538659eb50706e60454d6273Andreas Huber};
141a44153c1a57202fb538659eb50706e60454d6273Andreas Huber
142a44153c1a57202fb538659eb50706e60454d6273Andreas Huber}  // namespace android
143a44153c1a57202fb538659eb50706e60454d6273Andreas Huber
144a44153c1a57202fb538659eb50706e60454d6273Andreas Huber#endif  // LIVE_SESSION_H_
145