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_static_mutex_init (&rate->mutex);
50 gst_download_rate_deinit (GstDownloadRate * rate)
52 gst_download_rate_clear (rate);
56 gst_download_rate_set_max_length (GstDownloadRate * rate, gint max_length)
58 g_static_mutex_lock (&rate->mutex);
59 rate->max_length = max_length;
60 _gst_download_rate_check_remove_rates (rate);
61 g_static_mutex_unlock (&rate->mutex);
65 gst_download_rate_get_max_length (GstDownloadRate * rate)
68 g_static_mutex_lock (&rate->mutex);
69 ret = rate->max_length;
70 g_static_mutex_unlock (&rate->mutex);
76 gst_download_rate_clear (GstDownloadRate * rate)
78 g_static_mutex_lock (&rate->mutex);
79 g_queue_clear (&rate->queue);
81 g_static_mutex_unlock (&rate->mutex);
85 gst_download_rate_add_rate (GstDownloadRate * rate, guint bytes, guint64 time)
88 g_static_mutex_lock (&rate->mutex);
90 /* convert from bytes / nanoseconds to bits per second */
91 bitrate = G_GUINT64_CONSTANT (8000000000) * bytes / time;
93 g_queue_push_tail (&rate->queue, GUINT_TO_POINTER ((guint) bitrate));
94 rate->total += bitrate;
96 _gst_download_rate_check_remove_rates (rate);
97 g_static_mutex_unlock (&rate->mutex);
101 gst_download_rate_get_current_rate (GstDownloadRate * rate)
104 g_static_mutex_lock (&rate->mutex);
105 if (g_queue_get_length (&rate->queue))
106 ret = rate->total / g_queue_get_length (&rate->queue);
109 g_static_mutex_unlock (&rate->mutex);