HTTPBase.h revision 5b1b8a93a07326f1cbc627f09e02988375189e0a
11156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber/*
21156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber * Copyright (C) 2011 The Android Open Source Project
31156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber *
41156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber * Licensed under the Apache License, Version 2.0 (the "License");
51156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber * you may not use this file except in compliance with the License.
61156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber * You may obtain a copy of the License at
71156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber *
81156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber *      http://www.apache.org/licenses/LICENSE-2.0
91156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber *
101156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber * Unless required by applicable law or agreed to in writing, software
111156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber * distributed under the License is distributed on an "AS IS" BASIS,
121156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber * See the License for the specific language governing permissions and
141156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber * limitations under the License.
151156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber */
161156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
171156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber#ifndef HTTP_BASE_H_
181156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
191156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber#define HTTP_BASE_H_
201156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
211156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber#include <media/stagefright/foundation/ABase.h>
221156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber#include <media/stagefright/DataSource.h>
235b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong#include <media/stagefright/MediaErrors.h>
245b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong#include <utils/threads.h>
251156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
261156dc913a5ba7b2bc86489468d4914430f03d14Andreas Hubernamespace android {
271156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
281156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huberstruct HTTPBase : public DataSource {
291156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    enum Flags {
301156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber        // Don't log any URLs.
311156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber        kFlagIncognito = 1
321156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    };
331156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
341156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    HTTPBase();
351156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
361156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    virtual status_t connect(
371156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber            const char *uri,
381156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber            const KeyedVector<String8, String8> *headers = NULL,
391156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber            off64_t offset = 0) = 0;
401156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
411156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    virtual void disconnect() = 0;
421156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
431156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    // Returns true if bandwidth could successfully be estimated,
441156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    // false otherwise.
455b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong    virtual bool estimateBandwidth(int32_t *bandwidth_bps);
465b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong
475b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong    virtual status_t getEstimatedBandwidthKbps(int32_t *kbps);
485b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong
495b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong    virtual status_t setBandwidthStatCollectFreq(int32_t freqMs);
501156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
511156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    static sp<HTTPBase> Create(uint32_t flags = 0);
521156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
535b1b8a93a07326f1cbc627f09e02988375189e0aJames Dongprotected:
545b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong    void addBandwidthMeasurement(size_t numBytes, int64_t delayUs);
555b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong
561156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huberprivate:
575b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong
585b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong    struct BandwidthEntry {
595b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong        int64_t mDelayUs;
605b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong        size_t mNumBytes;
615b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong    };
625b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong
635b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong    Mutex mLock;
645b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong
655b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong    List<BandwidthEntry> mBandwidthHistory;
665b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong    size_t mNumBandwidthHistoryItems;
675b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong    int64_t mTotalTransferTimeUs;
685b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong    size_t mTotalTransferBytes;
695b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong
705b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong    enum {
715b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong        kMinBandwidthCollectFreqMs = 1000,   // 1 second
725b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong        kMaxBandwidthCollectFreqMs = 60000,  // one minute
735b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong    };
745b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong
755b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong    int64_t mPrevBandwidthMeasureTimeUs;
765b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong    int32_t mPrevEstimatedBandWidthKbps;
775b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong    int32_t mBandWidthCollectFreqMs;
785b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong
795b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong
801156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    DISALLOW_EVIL_CONSTRUCTORS(HTTPBase);
811156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber};
821156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
831156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber}  // namespace android
841156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
851156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber#endif  // HTTP_BASE_H_
86