summaryrefslogtreecommitdiff
path: root/ext/avtp/gstavtpcrfsync.c
blob: 4b4d08318977c8cf54b6e7975e773d1b13522768 (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
/*
 * 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 Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

/**
 * SECTION:element-avtpcrfsync
 * @see_also: avtpcrfcheck
 *
 * Adjust the Presentation Time from AVTPDUs to align with the reference clock
 * provided by the CRF stream. For detailed information see chapter 10 in
 * https://standards.ieee.org/standard/1722-2016.html. A helpful aid for
 * visualizing CRF and it's advantages can be found at
 * http://grouper.ieee.org/groups/1722/contributions/2014/1722a-rsilfvast-Diagrams%20for%20Common%20Timing%20Grid%20and%20Presentation%20Time%20(for%20review%20and%20discussion).pdf
 * (Look at page 1).
 *
 * <refsect2>
 * <title>Example pipeline</title>
 * |[
 * gst-launch-1.0 audiotestsrc ! audioconvert ! avtpaafpay ! avtpcrfsync ! avtpsink
 * ]| This example pipeline will adjust the timestamps for rawaudio payload.
 * Refer to the avtpcrfcheck example to validate the adjusted timestamp.
 * </refsect2>
 */

#include <avtp.h>
#include <avtp_aaf.h>
#include <avtp_crf.h>
#include <avtp_cvf.h>
#include <glib.h>
#include <math.h>

#include "gstavtpcrfbase.h"
#include "gstavtpcrfsync.h"
#include "gstavtpcrfutil.h"

GST_DEBUG_CATEGORY_STATIC (avtpcrfsync_debug);
#define GST_CAT_DEFAULT (avtpcrfsync_debug)

#define gst_avtp_crf_sync_parent_class parent_class
G_DEFINE_TYPE (GstAvtpCrfSync, gst_avtp_crf_sync, GST_TYPE_AVTP_CRF_BASE);
GST_ELEMENT_REGISTER_DEFINE (avtpcrfsync, "avtpcrfsync", GST_RANK_NONE,
    GST_TYPE_AVTP_CRF_SYNC);
static GstFlowReturn gst_avtp_crf_sync_transform_ip (GstBaseTransform * parent,
    GstBuffer * buffer);

static void
gst_avtp_crf_sync_class_init (GstAvtpCrfSyncClass * klass)
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);

  gst_element_class_set_static_metadata (element_class,
      "Clock Reference Format (CRF) Synchronizer",
      "Filter/Network/AVTP",
      "Synchronize Presentation Time from AVTPDUs so they are phase-locked with clock provided by CRF stream",
      "Vedang Patel <vedang.patel@intel.com>");

  GST_BASE_TRANSFORM_CLASS (klass)->transform_ip =
      GST_DEBUG_FUNCPTR (gst_avtp_crf_sync_transform_ip);

  GST_DEBUG_CATEGORY_INIT (avtpcrfsync_debug, "avtpcrfsync", 0,
      "CRF Synchronizer");
}

static void
gst_avtp_crf_sync_init (GstAvtpCrfSync * avtpcrfsync)
{
  /* Nothing to do here. */
}

static void
set_avtp_tstamp (GstAvtpCrfSync * avtpcrfsync, struct avtp_stream_pdu *pdu,
    GstClockTime tstamp)
{
  int res;
  guint32 type;

  res =
      avtp_pdu_get ((struct avtp_common_pdu *) pdu, AVTP_FIELD_SUBTYPE, &type);
  g_assert (res == 0);

  switch (type) {
    case AVTP_SUBTYPE_AAF:
      res = avtp_aaf_pdu_set (pdu, AVTP_AAF_FIELD_TIMESTAMP, tstamp);
      g_assert (res == 0);
      break;
    case AVTP_SUBTYPE_CVF:
      res = avtp_cvf_pdu_set (pdu, AVTP_CVF_FIELD_TIMESTAMP, tstamp);
      g_assert (res == 0);
      break;
    default:
      GST_ERROR_OBJECT (avtpcrfsync, "type 0x%x not supported.\n", type);
      break;
  }
}

static void
set_avtp_mr_bit (GstAvtpCrfSync * avtpcrfsync, struct avtp_stream_pdu *pdu,
    guint64 mr)
{
  int res;
  guint32 type;

  res =
      avtp_pdu_get ((struct avtp_common_pdu *) pdu, AVTP_FIELD_SUBTYPE, &type);
  g_assert (res == 0);

  switch (type) {
    case AVTP_SUBTYPE_AAF:
      res = avtp_aaf_pdu_set (pdu, AVTP_AAF_FIELD_MR, mr);
      g_assert (res == 0);
      break;
    case AVTP_SUBTYPE_CVF:
      res = avtp_cvf_pdu_set (pdu, AVTP_CVF_FIELD_MR, mr);
      g_assert (res == 0);
      break;
    default:
      GST_ERROR_OBJECT (avtpcrfsync, "type 0x%x not supported.\n", type);
      break;
  }
}

static GstFlowReturn
gst_avtp_crf_sync_transform_ip (GstBaseTransform * parent, GstBuffer * buffer)
{
  GstClockTime tstamp, h264_time = 0, adjusted_tstamp, adjusted_h264_time = 0;
  GstAvtpCrfBase *avtpcrfbase = GST_AVTP_CRF_BASE (parent);
  GstAvtpCrfSync *avtpcrfsync = GST_AVTP_CRF_SYNC (avtpcrfbase);
  GstAvtpCrfThreadData *thread_data = &avtpcrfbase->thread_data;
  GstClockTime current_ts = thread_data->current_ts;
  gdouble avg_period = thread_data->average_period;
  struct avtp_stream_pdu *pdu;
  gboolean h264_packet;
  GstMapInfo info;
  gboolean res;

  if (!avg_period || !current_ts)
    return GST_FLOW_OK;

  res = gst_buffer_map (buffer, &info, GST_MAP_READWRITE);
  if (!res) {
    GST_ELEMENT_ERROR (avtpcrfsync, RESOURCE, OPEN_WRITE,
        ("cannot access buffer"), (NULL));
    return GST_FLOW_ERROR;
  }

  if (!buffer_size_valid (&info)) {
    GST_DEBUG_OBJECT (avtpcrfsync, "Malformed AVTPDU, discarding it");
    goto exit;
  }

  pdu = (struct avtp_stream_pdu *) info.data;

  h264_packet = h264_tstamp_valid (pdu);

  if (h264_packet) {
    res = avtp_cvf_pdu_get (pdu, AVTP_CVF_FIELD_H264_TIMESTAMP, &h264_time);
    g_assert (res == 0);

    /*
     * Extrapolate H264 tstamp to 64 bit and assume it's greater than CRF
     * timestamp.
     */
    h264_time |= current_ts & 0xFFFFFFFF00000000;
    if (h264_time < current_ts)
      h264_time += (1ULL << 32);

    /*
     * float typecasted to guint64 truncates the decimal part. So, round() it
     * before casting.
     */
    adjusted_h264_time =
        (GstClockTime) roundl (current_ts + ceill (((gdouble) h264_time -
                current_ts) / avg_period) * avg_period);
    res =
        avtp_cvf_pdu_set (pdu, AVTP_CVF_FIELD_H264_TIMESTAMP,
        adjusted_h264_time);
    g_assert (res == 0);

    GST_LOG_OBJECT (avtpcrfsync,
        "Adjust H264 timestamp in CVF packet. tstamp: %" G_GUINT64_FORMAT
        " adjusted_tstamp: %" G_GUINT64_FORMAT,
        h264_time & 0xFFFFFFFF, adjusted_h264_time & 0xFFFFFFFF);
  }

  tstamp = get_avtp_tstamp (avtpcrfbase, pdu);
  if (tstamp == GST_CLOCK_TIME_NONE)
    goto exit;

  /*
   * Extrapolate the 32-bit AVTP Timestamp to 64-bit and assume it's greater
   * than the 64-bit CRF timestamp.
   */
  tstamp |= current_ts & 0xFFFFFFFF00000000;
  if (tstamp < current_ts)
    tstamp += (1ULL << 32);

  /*
   * float typecasted to guint64 truncates the decimal part. So, round() it
   * before casting.
   */
  adjusted_tstamp =
      (GstClockTime) roundl (current_ts + ceill ((tstamp -
              current_ts) / avg_period) * avg_period);
  set_avtp_tstamp (avtpcrfsync, pdu, adjusted_tstamp);
  set_avtp_mr_bit (avtpcrfsync, pdu, thread_data->mr);
  GST_LOG_OBJECT (avtpcrfsync,
      "Adjust AVTP timestamp. tstamp: %" G_GUINT64_FORMAT
      " Adjusted tstamp: %" G_GUINT64_FORMAT,
      tstamp & 0xFFFFFFFF, adjusted_tstamp & 0xFFFFFFFF);

  /*
   * Since we adjusted the AVTP/H264 presentation times in the AVTPDU, we also
   * need to adjust buffer times by the same amount so that the buffer is
   * transmitted at the right time.
   */
  if (h264_packet) {
    if (GST_BUFFER_DTS (buffer) != GST_CLOCK_TIME_NONE)
      GST_BUFFER_DTS (buffer) += adjusted_tstamp - tstamp;
    GST_BUFFER_PTS (buffer) += adjusted_h264_time - h264_time;
  } else {
    GST_BUFFER_PTS (buffer) += adjusted_tstamp - tstamp;
  }

exit:
  gst_buffer_unmap (buffer, &info);
  return GST_FLOW_OK;
}