2 * Copyright (C) 2011 Andoni Morales Alastruey <ylatuya@gmail.com>
3 * Copyright (C) 2012 Smart TV Alliance
4 * Author: Louis-Francis Ratté-Boulianne <lfrb@collabora.com>, Collabora Ltd.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
25 #include "gstdownloadrate.h"
28 _gst_download_rate_check_remove_rates (GstDownloadRate * rate)
30 if (rate->max_length == 0)
33 while (g_queue_get_length (&rate->queue) > rate->max_length) {
34 guint bitrate = GPOINTER_TO_UINT (g_queue_pop_head (&rate->queue));
36 rate->total -= bitrate;
41 gst_download_rate_init (GstDownloadRate * rate)
43 g_queue_init (&rate->queue);
44 g_mutex_init (&rate->mutex);
47 rate->aver_period = 0;
51 gst_download_rate_deinit (GstDownloadRate * rate)
53 gst_download_rate_clear (rate);
54 g_mutex_clear (&rate->mutex);
58 gst_download_rate_set_max_length (GstDownloadRate * rate, gint max_length)
60 g_mutex_lock (&rate->mutex);
61 rate->max_length = max_length;
62 _gst_download_rate_check_remove_rates (rate);
63 g_mutex_unlock (&rate->mutex);
67 gst_download_rate_get_max_length (GstDownloadRate * rate)
70 g_mutex_lock (&rate->mutex);
71 ret = rate->max_length;
72 g_mutex_unlock (&rate->mutex);
78 gst_download_rate_set_aver_period (GstDownloadRate * rate, guint64 aver_period)
80 g_mutex_lock (&rate->mutex);
81 rate->aver_period = aver_period;
82 _gst_download_rate_check_remove_rates (rate);
83 g_mutex_unlock (&rate->mutex);
87 gst_download_rate_get_aver_period (GstDownloadRate * rate)
90 g_mutex_lock (&rate->mutex);
91 ret = rate->aver_period;
92 g_mutex_unlock (&rate->mutex);
98 gst_download_rate_clear (GstDownloadRate * rate)
100 g_mutex_lock (&rate->mutex);
101 g_queue_clear (&rate->queue);
103 g_mutex_unlock (&rate->mutex);
107 gst_download_rate_add_rate (GstDownloadRate * rate, guint bytes, guint64 time, GstClockTime duration)
110 g_mutex_lock (&rate->mutex);
112 /* convert from bytes / nanoseconds to bits per second */
113 bitrate = G_GUINT64_CONSTANT (8000000000) * bytes / ((double) time);
114 g_queue_push_tail (&rate->queue, GUINT_TO_POINTER ((guint) bitrate));
115 rate->total += bitrate;
117 if ( duration * rate->max_length > rate->aver_period )
119 duration < rate->aver_period ? rate->aver_period / duration : 1;
121 _gst_download_rate_check_remove_rates (rate);
122 g_mutex_unlock (&rate->mutex);
126 gst_download_rate_get_current_rate (GstDownloadRate * rate)
129 g_mutex_lock (&rate->mutex);
130 if (g_queue_get_length (&rate->queue))
131 ret = rate->total / g_queue_get_length (&rate->queue);
134 g_mutex_unlock (&rate->mutex);