1 /* GStreamer
2  * Copyright (C) <2012> Wim Taymans <wim.taymans@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 
20 /**
21  * SECTION:gstrtphdrext
22  * @title: GstRtphdrext
23  * @short_description: Helper methods for dealing with RTP header extensions
24  * @see_also: #GstRTPBasePayload, #GstRTPBaseDepayload, gstrtpbuffer
25  *
26  */
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30 
31 #include "gstrtphdrext.h"
32 
33 #include <stdlib.h>
34 #include <string.h>
35 
36 /**
37  * gst_rtp_hdrext_set_ntp_64:
38  * @data: the data to write to
39  * @size: the size of @data
40  * @ntptime: the NTP time
41  *
42  * Writes the NTP time in @ntptime to the format required for the NTP-64 header
43  * extension. @data must hold at least #GST_RTP_HDREXT_NTP_64_SIZE bytes.
44  *
45  * Returns: %TRUE on success.
46  */
47 gboolean
gst_rtp_hdrext_set_ntp_64(gpointer data,guint size,guint64 ntptime)48 gst_rtp_hdrext_set_ntp_64 (gpointer data, guint size, guint64 ntptime)
49 {
50   g_return_val_if_fail (data != NULL, FALSE);
51   g_return_val_if_fail (size >= GST_RTP_HDREXT_NTP_64_SIZE, FALSE);
52 
53   GST_WRITE_UINT64_BE (data, ntptime);
54 
55   return TRUE;
56 }
57 
58 /**
59  * gst_rtp_hdrext_get_ntp_64:
60  * @data: (array length=size) (element-type guint8): the data to read from
61  * @size: the size of @data
62  * @ntptime: (out): the result NTP time
63  *
64  * Reads the NTP time from the @size NTP-64 extension bytes in @data and store the
65  * result in @ntptime.
66  *
67  * Returns: %TRUE on success.
68  */
69 gboolean
gst_rtp_hdrext_get_ntp_64(gpointer data,guint size,guint64 * ntptime)70 gst_rtp_hdrext_get_ntp_64 (gpointer data, guint size, guint64 * ntptime)
71 {
72   g_return_val_if_fail (data != NULL, FALSE);
73   g_return_val_if_fail (size >= GST_RTP_HDREXT_NTP_64_SIZE, FALSE);
74 
75   if (ntptime)
76     *ntptime = GST_READ_UINT64_BE (data);
77 
78   return TRUE;
79 }
80 
81 /**
82  * gst_rtp_hdrext_set_ntp_56:
83  * @data: the data to write to
84  * @size: the size of @data
85  * @ntptime: the NTP time
86  *
87  * Writes the NTP time in @ntptime to the format required for the NTP-56 header
88  * extension. @data must hold at least #GST_RTP_HDREXT_NTP_56_SIZE bytes.
89  *
90  * Returns: %TRUE on success.
91  */
92 gboolean
gst_rtp_hdrext_set_ntp_56(gpointer data,guint size,guint64 ntptime)93 gst_rtp_hdrext_set_ntp_56 (gpointer data, guint size, guint64 ntptime)
94 {
95   guint8 *d = data;
96   gint i;
97 
98   g_return_val_if_fail (data != NULL, FALSE);
99   g_return_val_if_fail (size >= GST_RTP_HDREXT_NTP_56_SIZE, FALSE);
100 
101   for (i = 0; i < 7; i++) {
102     d[6 - i] = ntptime & 0xff;
103     ntptime >>= 8;
104   }
105   return TRUE;
106 }
107 
108 /**
109  * gst_rtp_hdrext_get_ntp_56:
110  * @data: (array length=size) (element-type guint8): the data to read from
111  * @size: the size of @data
112  * @ntptime: (out): the result NTP time
113  *
114  * Reads the NTP time from the @size NTP-56 extension bytes in @data and store the
115  * result in @ntptime.
116  *
117  * Returns: %TRUE on success.
118  */
119 gboolean
gst_rtp_hdrext_get_ntp_56(gpointer data,guint size,guint64 * ntptime)120 gst_rtp_hdrext_get_ntp_56 (gpointer data, guint size, guint64 * ntptime)
121 {
122   guint8 *d = data;
123 
124   g_return_val_if_fail (data != NULL, FALSE);
125   g_return_val_if_fail (size >= GST_RTP_HDREXT_NTP_56_SIZE, FALSE);
126 
127   if (ntptime) {
128     gint i;
129 
130     *ntptime = 0;
131     for (i = 0; i < 7; i++) {
132       *ntptime <<= 8;
133       *ntptime |= d[i];
134     }
135   }
136   return TRUE;
137 }
138