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
51a93fd2be99d21629bed504b9b7df035fc2f54562Leena Winterrowd    virtual void setBandwidthHistorySize(size_t numHistoryItems);
52a93fd2be99d21629bed504b9b7df035fc2f54562Leena Winterrowd
53a23456b306f35b9ecf973bf5818ca39295e9e029Ashish Sharma    static void RegisterSocketUserTag(int sockfd, uid_t uid, uint32_t kTag);
54a23456b306f35b9ecf973bf5818ca39295e9e029Ashish Sharma    static void UnRegisterSocketUserTag(int sockfd);
55dab718bba3945332dc75e268e1e7f0fe2eb91c4aAndreas Huber
565908f88a7e45380a9b0d71a3b1ea535d76c420b3Chad Brubaker    static void RegisterSocketUserMark(int sockfd, uid_t uid);
575908f88a7e45380a9b0d71a3b1ea535d76c420b3Chad Brubaker    static void UnRegisterSocketUserMark(int sockfd);
585908f88a7e45380a9b0d71a3b1ea535d76c420b3Chad Brubaker
5969d3d8a9540b0da787ea0beccad2517f057dd54dMarco Nelissen    virtual String8 toString() {
6069d3d8a9540b0da787ea0beccad2517f057dd54dMarco Nelissen        return mName;
6169d3d8a9540b0da787ea0beccad2517f057dd54dMarco Nelissen    }
6269d3d8a9540b0da787ea0beccad2517f057dd54dMarco Nelissen
635b1b8a93a07326f1cbc627f09e02988375189e0aJames Dongprotected:
64a93fd2be99d21629bed504b9b7df035fc2f54562Leena Winterrowd    virtual void addBandwidthMeasurement(size_t numBytes, int64_t delayUs);
6569d3d8a9540b0da787ea0beccad2517f057dd54dMarco Nelissen    String8 mName;
665b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong
671156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huberprivate:
685b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong    struct BandwidthEntry {
695b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong        int64_t mDelayUs;
705b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong        size_t mNumBytes;
715b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong    };
725b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong
735b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong    Mutex mLock;
745b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong
755b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong    List<BandwidthEntry> mBandwidthHistory;
765b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong    size_t mNumBandwidthHistoryItems;
775b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong    int64_t mTotalTransferTimeUs;
785b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong    size_t mTotalTransferBytes;
79a93fd2be99d21629bed504b9b7df035fc2f54562Leena Winterrowd    size_t mMaxBandwidthHistoryItems;
805b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong
815b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong    enum {
825b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong        kMinBandwidthCollectFreqMs = 1000,   // 1 second
835b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong        kMaxBandwidthCollectFreqMs = 60000,  // one minute
845b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong    };
855b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong
865b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong    int64_t mPrevBandwidthMeasureTimeUs;
875b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong    int32_t mPrevEstimatedBandWidthKbps;
885b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong    int32_t mBandWidthCollectFreqMs;
895b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong
901156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber    DISALLOW_EVIL_CONSTRUCTORS(HTTPBase);
911156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber};
921156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
931156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber}  // namespace android
941156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber
951156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber#endif  // HTTP_BASE_H_
96