summaryrefslogtreecommitdiff
path: root/ext/avtp/gstavtpbasedepayload.c
blob: 4ee2f61698f56aee77d3a3b428a65535b35d74db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
 * GStreamer AVTP Plugin
 * Copyright (C) 2019 Intel Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later
 * version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 */

#include "gstavtpbasedepayload.h"

GST_DEBUG_CATEGORY_STATIC (avtpbasedepayload_debug);
#define GST_CAT_DEFAULT (avtpbasedepayload_debug)

#define DEFAULT_STREAMID 0xAABBCCDDEEFF0000

enum
{
  PROP_0,
  PROP_STREAMID,
};

static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("application/x-avtp")
    );

static void gst_avtp_base_depayload_class_init (GstAvtpBaseDepayloadClass *
    klass);
static void gst_avtp_base_depayload_init (GstAvtpBaseDepayload *
    avtpbasedepayload, gpointer g_class);

static void gst_avtp_base_depayload_set_property (GObject * object,
    guint prop_id, const GValue * value, GParamSpec * pspec);
static void gst_avtp_base_depayload_get_property (GObject * object,
    guint prop_id, GValue * value, GParamSpec * pspec);

static gboolean gst_avtp_base_depayload_sink_event (GstPad * pad,
    GstObject * parent, GstEvent * event);

GType
gst_avtp_base_depayload_get_type (void)
{
  static GType avtpbasedepayload_type = 0;

  if (g_once_init_enter ((gsize *) & avtpbasedepayload_type)) {
    static const GTypeInfo avtpbasedepayload_info = {
      sizeof (GstAvtpBaseDepayloadClass),
      NULL,
      NULL,
      (GClassInitFunc) gst_avtp_base_depayload_class_init,
      NULL,
      NULL,
      sizeof (GstAvtpBaseDepayload),
      0,
      (GInstanceInitFunc) gst_avtp_base_depayload_init,
    };
    GType _type;

    _type = g_type_register_static (GST_TYPE_ELEMENT, "GstAvtpBaseDepayload",
        &avtpbasedepayload_info, G_TYPE_FLAG_ABSTRACT);

    g_once_init_leave ((gsize *) & avtpbasedepayload_type, _type);
  }
  return avtpbasedepayload_type;
}

static void
gst_avtp_base_depayload_class_init (GstAvtpBaseDepayloadClass * klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->set_property = gst_avtp_base_depayload_set_property;
  object_class->get_property = gst_avtp_base_depayload_get_property;

  g_object_class_install_property (object_class, PROP_STREAMID,
      g_param_spec_uint64 ("streamid", "Stream ID",
          "Stream ID associated with the AVTPDU", 0, G_MAXUINT64,
          DEFAULT_STREAMID, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
          GST_PARAM_MUTABLE_PAUSED));

  klass->chain = NULL;
  klass->sink_event = GST_DEBUG_FUNCPTR (gst_avtp_base_depayload_sink_event);

  GST_DEBUG_CATEGORY_INIT (avtpbasedepayload_debug, "avtpbasedepayload", 0,
      "Base class for AVTP depayloaders");

  gst_type_mark_as_plugin_api (GST_TYPE_AVTP_BASE_DEPAYLOAD, 0);
}

static void
gst_avtp_base_depayload_init (GstAvtpBaseDepayload * avtpbasedepayload,
    gpointer g_class)
{
  GstPadTemplate *templ;
  GstElement *element = GST_ELEMENT (avtpbasedepayload);
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
  GstAvtpBaseDepayloadClass *avtpbasedepayload_class =
      GST_AVTP_BASE_DEPAYLOAD_CLASS (g_class);

  g_assert (avtpbasedepayload_class->chain != NULL);

  templ = gst_element_class_get_pad_template (element_class, "src");
  g_assert (templ != NULL);
  avtpbasedepayload->srcpad = gst_pad_new_from_template (templ, "src");
  gst_pad_use_fixed_caps (avtpbasedepayload->srcpad);
  gst_element_add_pad (element, avtpbasedepayload->srcpad);

  avtpbasedepayload->sinkpad =
      gst_pad_new_from_static_template (&sink_template, "sink");
  gst_pad_set_chain_function (avtpbasedepayload->sinkpad,
      avtpbasedepayload_class->chain);
  gst_pad_set_event_function (avtpbasedepayload->sinkpad,
      avtpbasedepayload_class->sink_event);
  gst_element_add_pad (element, avtpbasedepayload->sinkpad);

  avtpbasedepayload->streamid = DEFAULT_STREAMID;

  avtpbasedepayload->prev_ptime = 0;
  avtpbasedepayload->seqnum = 0;
}

static void
gst_avtp_base_depayload_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstAvtpBaseDepayload *avtpbasedepayload = GST_AVTP_BASE_DEPAYLOAD (object);

  GST_DEBUG_OBJECT (avtpbasedepayload, "prop_id %u", prop_id);

  switch (prop_id) {
    case PROP_STREAMID:
      avtpbasedepayload->streamid = g_value_get_uint64 (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_avtp_base_depayload_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
{
  GstAvtpBaseDepayload *avtpbasedepayload = GST_AVTP_BASE_DEPAYLOAD (object);

  GST_DEBUG_OBJECT (avtpbasedepayload, "prop_id %u", prop_id);

  switch (prop_id) {
    case PROP_STREAMID:
      g_value_set_uint64 (value, avtpbasedepayload->streamid);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static gboolean
gst_avtp_base_depayload_sink_event (GstPad * pad, GstObject * parent,
    GstEvent * event)
{
  GstAvtpBaseDepayload *avtpbasedepayload = GST_AVTP_BASE_DEPAYLOAD (parent);

  GST_DEBUG_OBJECT (avtpbasedepayload, "event %s", GST_EVENT_TYPE_NAME (event));

  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_SEGMENT:
      /* Once the first AVTPDU is received, proper CAPS and SEGMENT events are
       * pushed downstream. These events are expected to be pushed in that
       * order by GStreamer. Since the default handling implemented by
       * gst_pad_event_default() pushes the SEGMENT event downstream right
       * away, it doesn't work for us and we have to handle it ourselves.
       *
       * Our handling is very straightforward: we discard this event and send
       * a proper segment event once the first AVTPDU is received. See
       * gst_avtp_base_depayload_push_segment_event() for more information.
       */
      gst_event_unref (event);
      return TRUE;
    default:
      return gst_pad_event_default (pad, parent, event);
  }
}

/* Helper function to convert AVTP timestamp to AVTP presentation time. Since
 * AVTP timestamp represents the lower 32-bit part from AVTP presentation time,
 * the helper requires a reference time ('ref' argument) to convert it properly.
 * The reference time must be in gstreamer clock-time coordinate.
 */
GstClockTime
gst_avtp_base_depayload_tstamp_to_ptime (GstAvtpBaseDepayload *
    avtpbasedepayload, guint32 tstamp, GstClockTime ref)
{
  GstClockTime ptime;

  ptime = (ref & 0xFFFFFFFF00000000ULL) | tstamp;

  /* If 'ptime' is less than the our reference time, it means the higher part
   * from 'ptime' needs to be incremented by 1 in order reflect the correct
   * presentation time.
   */
  if (ptime < ref)
    ptime += (1ULL << 32);

  GST_LOG_OBJECT (avtpbasedepayload, "AVTP presentation time %" GST_TIME_FORMAT,
      GST_TIME_ARGS (ptime));
  return ptime;
}

gboolean
gst_avtp_base_depayload_push_segment_event (GstAvtpBaseDepayload *
    avtpbasedepayload, guint32 avtp_tstamp)
{
  GstClock *clock;
  GstEvent *event;
  GstSegment segment;
  GstClockTime now, base_time, avtp_ptime;

  clock = GST_ELEMENT_CLOCK (avtpbasedepayload);

  now = gst_clock_get_time (clock);
  avtp_ptime =
      gst_avtp_base_depayload_tstamp_to_ptime (avtpbasedepayload, avtp_tstamp,
      now);
  base_time = gst_element_get_base_time (GST_ELEMENT (avtpbasedepayload));

  gst_segment_init (&segment, GST_FORMAT_TIME);
  segment.base = avtp_ptime - base_time;
  segment.start = avtp_ptime;
  segment.stop = -1;

  event = gst_event_new_segment (&segment);
  if (!event) {
    GST_ERROR_OBJECT (avtpbasedepayload, "Failed to create SEGMENT event");
    return FALSE;
  }

  if (!gst_pad_push_event (avtpbasedepayload->srcpad, event)) {
    GST_ERROR_OBJECT (avtpbasedepayload, "Failed to push SEGMENT event");
    return FALSE;
  }

  GST_DEBUG_OBJECT (avtpbasedepayload, "SEGMENT event pushed: %"
      GST_SEGMENT_FORMAT, &segment);

  avtpbasedepayload->prev_ptime = avtp_ptime;
  return TRUE;
}