tizen 2.3.1 release
[framework/multimedia/gst-plugins-ext0.10.git] / dashdemux / src / gstdownloadrate.c
1 /* GStreamer
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.
5  *
6  * gstfragment.c:
7  *
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.
12  *
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.
17  *
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.
22  */
23
24 #include <glib.h>
25 #include "gstdownloadrate.h"
26
27 static void
28 _gst_download_rate_check_remove_rates (GstDownloadRate * rate)
29 {
30   if (rate->max_length == 0)
31     return;
32
33   while (g_queue_get_length (&rate->queue) > rate->max_length) {
34     guint bitrate = GPOINTER_TO_UINT (g_queue_pop_head (&rate->queue));
35
36     rate->total -= bitrate;
37   }
38 }
39
40 void
41 gst_download_rate_init (GstDownloadRate * rate)
42 {
43   g_queue_init (&rate->queue);
44   g_mutex_init (&rate->mutex);
45   rate->total = 0;
46   rate->max_length = 0;
47   rate->aver_period = 0;
48 }
49
50 void
51 gst_download_rate_deinit (GstDownloadRate * rate)
52 {
53   gst_download_rate_clear (rate);
54   g_mutex_clear (&rate->mutex);
55 }
56
57 void
58 gst_download_rate_set_max_length (GstDownloadRate * rate, gint max_length)
59 {
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);
64 }
65
66 gint
67 gst_download_rate_get_max_length (GstDownloadRate * rate)
68 {
69   guint ret;
70   g_mutex_lock (&rate->mutex);
71   ret = rate->max_length;
72   g_mutex_unlock (&rate->mutex);
73
74   return ret;
75 }
76
77 void
78   gst_download_rate_set_aver_period (GstDownloadRate * rate, guint64 aver_period)
79 {
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);
84 }
85
86 GstClockTime
87 gst_download_rate_get_aver_period (GstDownloadRate * rate)
88 {
89   guint64 ret;
90   g_mutex_lock (&rate->mutex);
91   ret = rate->aver_period;
92   g_mutex_unlock (&rate->mutex);
93
94   return ret;
95 }
96
97 void
98 gst_download_rate_clear (GstDownloadRate * rate)
99 {
100   g_mutex_lock (&rate->mutex);
101   g_queue_clear (&rate->queue);
102   rate->total = 0;
103   g_mutex_unlock (&rate->mutex);
104 }
105
106 void
107 gst_download_rate_add_rate (GstDownloadRate * rate, guint bytes, guint64 time, GstClockTime duration)
108 {
109   guint64 bitrate;
110   g_mutex_lock (&rate->mutex);
111
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;
116
117   if ( duration * rate->max_length > rate->aver_period )
118     rate->max_length = 
119       duration < rate->aver_period ? rate->aver_period / duration : 1;
120
121   _gst_download_rate_check_remove_rates (rate);
122   g_mutex_unlock (&rate->mutex);
123 }
124
125 guint64
126 gst_download_rate_get_current_rate (GstDownloadRate * rate)
127 {
128   guint64 ret;
129   g_mutex_lock (&rate->mutex);
130   if (g_queue_get_length (&rate->queue)) 
131     ret = rate->total / g_queue_get_length (&rate->queue);
132   else
133     ret = 0;
134   g_mutex_unlock (&rate->mutex);
135
136   return ret;
137 }