1 /*
2  * Copyright (C) 2016 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 package com.google.android.exoplayer2.upstream;
17 
18 import android.os.Handler;
19 import androidx.annotation.Nullable;
20 
21 /**
22  * Provides estimates of the currently available bandwidth.
23  */
24 public interface BandwidthMeter {
25 
26   /**
27    * A listener of {@link BandwidthMeter} events.
28    */
29   interface EventListener {
30 
31     /**
32      * Called periodically to indicate that bytes have been transferred or the estimated bitrate has
33      * changed.
34      *
35      * <p>Note: The estimated bitrate is typically derived from more information than just {@code
36      * bytes} and {@code elapsedMs}.
37      *
38      * @param elapsedMs The time taken to transfer {@code bytesTransferred}, in milliseconds. This
39      *     is at most the elapsed time since the last callback, but may be less if there were
40      *     periods during which data was not being transferred.
41      * @param bytesTransferred The number of bytes transferred since the last callback.
42      * @param bitrateEstimate The estimated bitrate in bits/sec.
43      */
onBandwidthSample(int elapsedMs, long bytesTransferred, long bitrateEstimate)44     void onBandwidthSample(int elapsedMs, long bytesTransferred, long bitrateEstimate);
45   }
46 
47   /** Returns the estimated bitrate. */
getBitrateEstimate()48   long getBitrateEstimate();
49 
50   /**
51    * Returns the {@link TransferListener} that this instance uses to gather bandwidth information
52    * from data transfers. May be null if the implementation does not listen to data transfers.
53    */
54   @Nullable
getTransferListener()55   TransferListener getTransferListener();
56 
57   /**
58    * Adds an {@link EventListener}.
59    *
60    * @param eventHandler A handler for events.
61    * @param eventListener A listener of events.
62    */
addEventListener(Handler eventHandler, EventListener eventListener)63   void addEventListener(Handler eventHandler, EventListener eventListener);
64 
65   /**
66    * Removes an {@link EventListener}.
67    *
68    * @param eventListener The listener to be removed.
69    */
removeEventListener(EventListener eventListener)70   void removeEventListener(EventListener eventListener);
71 }
72