gstavtpaafpay.c 12 KB
Newer Older
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
/*
 * 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
 */

/**
 * SECTION:element-avtpaafpay
 * @see_also: avtpaafdepay
 *
 * Payload raw audio into AVTPDUs according to IEEE 1722-2016. For detailed
 * information see https://standards.ieee.org/standard/1722-2016.html.
 *
 * <refsect2>
 * <title>Example pipeline</title>
 * |[
 * gst-launch-1.0 audiotestsrc ! audioconvert ! avtpaafpay ! avtpsink
 * ]| This example pipeline will payload raw audio. Refer to the avtpaafdepay
 * example to depayload and play the AVTP stream.
 * </refsect2>
 */

#include <avtp.h>
#include <avtp_aaf.h>
#include <gst/audio/audio-format.h>

#include "gstavtpaafpay.h"

GST_DEBUG_CATEGORY_STATIC (avtpaafpay_debug);
#define GST_CAT_DEFAULT (avtpaafpay_debug)

47
#define DEFAULT_TIMESTAMP_MODE GST_AVTP_AAF_TIMESTAMP_MODE_NORMAL
48 49 50 51

enum
{
  PROP_0,
52
  PROP_TIMESTAMP_MODE,
53 54 55 56 57 58 59 60 61 62 63 64
};

static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("audio/x-raw, "
        "format = (string) { S16BE, S24BE, S32BE, F32BE }, "
        "rate = (int) { 8000, 16000, 24000, 32000, 44100, 48000, 88200, 96000, 176400, 192000 }, "
        "channels = " GST_AUDIO_CHANNELS_RANGE ", "
        "layout = (string) interleaved")
    );

65
#define GST_TYPE_AVTP_AAF_TIMESTAMP_MODE (gst_avtp_aaf_timestamp_mode_get_type())
66
static GType
67
gst_avtp_aaf_timestamp_mode_get_type (void)
68
{
69 70 71
  static const GEnumValue timestamp_mode_types[] = {
    {GST_AVTP_AAF_TIMESTAMP_MODE_NORMAL, "Normal timestamping mode", "normal"},
    {GST_AVTP_AAF_TIMESTAMP_MODE_SPARSE, "Sparse timestamping mode", "sparse"},
72 73
    {0, NULL, NULL},
  };
74
  static gsize id = 0;
75

76 77
  if (g_once_init_enter (&id)) {
    GType new_type;
78

79
    new_type =
80 81
        g_enum_register_static ("GstAvtpAafTimestampMode",
        timestamp_mode_types);
82 83 84 85 86

    g_once_init_leave (&id, (gsize) new_type);
  }

  return (GType) id;
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
}

#define gst_avtp_aaf_pay_parent_class parent_class
G_DEFINE_TYPE (GstAvtpAafPay, gst_avtp_aaf_pay, GST_TYPE_AVTP_BASE_PAYLOAD);

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

static GstStateChangeReturn gst_avtp_aaf_pay_change_state (GstElement * element,
    GstStateChange transition);

static GstFlowReturn gst_avtp_aaf_pay_chain (GstPad * pad, GstObject * parent,
    GstBuffer * buffer);
static gboolean gst_avtp_aaf_pay_sink_event (GstPad * pad, GstObject * parent,
    GstEvent * event);

static void
gst_avtp_aaf_pay_class_init (GstAvtpAafPayClass * klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
  GstAvtpBasePayloadClass *avtpbasepayload_class =
      GST_AVTP_BASE_PAYLOAD_CLASS (klass);

  object_class->set_property = gst_avtp_aaf_pay_set_property;
  object_class->get_property = gst_avtp_aaf_pay_get_property;

116 117 118 119
  g_object_class_install_property (object_class, PROP_TIMESTAMP_MODE,
      g_param_spec_enum ("timestamp-mode", "Timestamping Mode",
          "AAF timestamping mode", GST_TYPE_AVTP_AAF_TIMESTAMP_MODE,
          DEFAULT_TIMESTAMP_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
          GST_PARAM_MUTABLE_PAUSED));

  element_class->change_state =
      GST_DEBUG_FUNCPTR (gst_avtp_aaf_pay_change_state);

  gst_element_class_add_static_pad_template (element_class, &sink_template);

  gst_element_class_set_static_metadata (element_class,
      "AVTP Audio Format (AAF) payloader",
      "Codec/Payloader/Network/AVTP",
      "Payload-encode Raw audio into AAF AVTPDU (IEEE 1722)",
      "Andre Guedes <andre.guedes@intel.com>");

  avtpbasepayload_class->chain = GST_DEBUG_FUNCPTR (gst_avtp_aaf_pay_chain);
  avtpbasepayload_class->sink_event =
      GST_DEBUG_FUNCPTR (gst_avtp_aaf_pay_sink_event);

  GST_DEBUG_CATEGORY_INIT (avtpaafpay_debug, "avtpaafpay", 0,
      "AAF AVTP Payloader");
139

140
  gst_type_mark_as_plugin_api (GST_TYPE_AVTP_AAF_TIMESTAMP_MODE, 0);
141 142 143 144 145
}

static void
gst_avtp_aaf_pay_init (GstAvtpAafPay * avtpaafpay)
{
146
  avtpaafpay->timestamp_mode = DEFAULT_TIMESTAMP_MODE;
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

  avtpaafpay->header = NULL;
  avtpaafpay->channels = 0;
  avtpaafpay->depth = 0;
  avtpaafpay->rate = 0;
  avtpaafpay->format = 0;
}

static void
gst_avtp_aaf_pay_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstAvtpAafPay *avtpaafpay = GST_AVTP_AAF_PAY (object);

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

  switch (prop_id) {
164 165
    case PROP_TIMESTAMP_MODE:
      avtpaafpay->timestamp_mode = g_value_get_enum (value);
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_avtp_aaf_pay_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
{
  GstAvtpAafPay *avtpaafpay = GST_AVTP_AAF_PAY (object);

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

  switch (prop_id) {
182 183
    case PROP_TIMESTAMP_MODE:
      g_value_set_enum (value, avtpaafpay->timestamp_mode);
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static GstStateChangeReturn
gst_avtp_aaf_pay_change_state (GstElement * element, GstStateChange transition)
{
  GstStateChangeReturn ret;
  GstAvtpAafPay *avtpaafpay = GST_AVTP_AAF_PAY (element);

  GST_DEBUG_OBJECT (avtpaafpay, "transition %d", transition);

  switch (transition) {
    case GST_STATE_CHANGE_NULL_TO_READY:{
      GstMemory *mem;

      mem = gst_allocator_alloc (NULL, sizeof (struct avtp_stream_pdu), NULL);
      if (!mem) {
        GST_ERROR_OBJECT (avtpaafpay, "Failed to allocate GstMemory");
        return GST_STATE_CHANGE_FAILURE;
      }
Ederson de Souza's avatar
Ederson de Souza committed
208
      avtpaafpay->header = mem;
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
      break;
    }
    case GST_STATE_CHANGE_READY_TO_PAUSED:{
      int res;
      GstMapInfo info;
      struct avtp_stream_pdu *pdu;
      GstMemory *mem = avtpaafpay->header;
      GstAvtpBasePayload *avtpbasepayload = GST_AVTP_BASE_PAYLOAD (element);

      if (!gst_memory_map (mem, &info, GST_MAP_WRITE)) {
        GST_ERROR_OBJECT (avtpaafpay, "Failed to map GstMemory");
        return GST_STATE_CHANGE_FAILURE;
      }
      pdu = (struct avtp_stream_pdu *) info.data;
      res = avtp_aaf_pdu_init (pdu);
      g_assert (res == 0);
      res = avtp_aaf_pdu_set (pdu, AVTP_AAF_FIELD_MR, 0);
      g_assert (res == 0);
      res = avtp_aaf_pdu_set (pdu, AVTP_AAF_FIELD_TV, 1);
      g_assert (res == 0);
      res = avtp_aaf_pdu_set (pdu, AVTP_AAF_FIELD_TU, 0);
      g_assert (res == 0);
      res = avtp_aaf_pdu_set (pdu, AVTP_AAF_FIELD_STREAM_ID,
          avtpbasepayload->streamid);
      g_assert (res == 0);
234 235
      res =
          avtp_aaf_pdu_set (pdu, AVTP_AAF_FIELD_SP, avtpaafpay->timestamp_mode);
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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
      g_assert (res == 0);
      gst_memory_unmap (mem, &info);
      break;
    }
    default:
      break;
  }

  ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
  if (ret == GST_STATE_CHANGE_FAILURE) {
    GST_ERROR_OBJECT (avtpaafpay, "Parent failed to handle state transition");
    return ret;
  }

  switch (transition) {
    case GST_STATE_CHANGE_READY_TO_NULL:
      gst_memory_unref (avtpaafpay->header);
      break;
    default:
      break;
  }

  return ret;
}

static GstFlowReturn
gst_avtp_aaf_pay_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
{
  int res;
  GstMemory *mem;
  GstMapInfo info;
  gsize data_len;
  GstClockTime ptime;
  struct avtp_stream_pdu *pdu;
  GstAvtpAafPay *avtpaafpay = GST_AVTP_AAF_PAY (parent);
  GstAvtpBasePayload *avtpbasepayload = GST_AVTP_BASE_PAYLOAD (parent);

  ptime = gst_avtp_base_payload_calc_ptime (avtpbasepayload, buffer);
  data_len = gst_buffer_get_size (buffer);

  mem = gst_memory_copy (avtpaafpay->header, 0, -1);
  if (!gst_memory_map (mem, &info, GST_MAP_WRITE)) {
    GST_ELEMENT_ERROR (avtpaafpay, RESOURCE, WRITE, ("Failed to map memory"),
        (NULL));
    gst_buffer_unref (buffer);
    return GST_FLOW_ERROR;
  }
  pdu = (struct avtp_stream_pdu *) info.data;
  res = avtp_aaf_pdu_set (pdu, AVTP_AAF_FIELD_TIMESTAMP, ptime);
  g_assert (res == 0);
  res = avtp_aaf_pdu_set (pdu, AVTP_AAF_FIELD_NSR, avtpaafpay->rate);
  g_assert (res == 0);
  res = avtp_aaf_pdu_set (pdu, AVTP_AAF_FIELD_FORMAT, avtpaafpay->format);
  g_assert (res == 0);
  res = avtp_aaf_pdu_set (pdu, AVTP_AAF_FIELD_BIT_DEPTH, avtpaafpay->depth);
  g_assert (res == 0);
  res = avtp_aaf_pdu_set (pdu, AVTP_AAF_FIELD_STREAM_DATA_LEN, data_len);
  g_assert (res == 0);
  res = avtp_aaf_pdu_set (pdu, AVTP_AAF_FIELD_CHAN_PER_FRAME,
      avtpaafpay->channels);
  g_assert (res == 0);
  res = avtp_aaf_pdu_set (pdu, AVTP_AAF_FIELD_SEQ_NUM,
      avtpbasepayload->seqnum++);
  g_assert (res == 0);
  gst_memory_unmap (mem, &info);

  gst_buffer_prepend_memory (buffer, mem);
  return gst_pad_push (avtpbasepayload->srcpad, buffer);
}

static int
gst_to_avtp_rate (gint rate)
{
  switch (rate) {
    case 8000:
      return AVTP_AAF_PCM_NSR_8KHZ;
    case 16000:
      return AVTP_AAF_PCM_NSR_16KHZ;
    case 24000:
      return AVTP_AAF_PCM_NSR_24KHZ;
    case 32000:
      return AVTP_AAF_PCM_NSR_32KHZ;
    case 44100:
      return AVTP_AAF_PCM_NSR_44_1KHZ;
    case 48000:
      return AVTP_AAF_PCM_NSR_48KHZ;
    case 88200:
      return AVTP_AAF_PCM_NSR_88_2KHZ;
    case 96000:
      return AVTP_AAF_PCM_NSR_96KHZ;
    case 176400:
      return AVTP_AAF_PCM_NSR_176_4KHZ;
    case 192000:
      return AVTP_AAF_PCM_NSR_192KHZ;
    default:
      return AVTP_AAF_PCM_NSR_USER;
  }
}

static int
gst_to_avtp_format (GstAudioFormat format)
{
  switch (format) {
    case GST_AUDIO_FORMAT_S16BE:
      return AVTP_AAF_FORMAT_INT_16BIT;
    case GST_AUDIO_FORMAT_S24BE:
      return AVTP_AAF_FORMAT_INT_24BIT;
    case GST_AUDIO_FORMAT_S32BE:
      return AVTP_AAF_FORMAT_INT_32BIT;
    case GST_AUDIO_FORMAT_F32BE:
      return AVTP_AAF_FORMAT_FLOAT_32BIT;
    default:
      return AVTP_AAF_FORMAT_USER;
  }
}

static gboolean
gst_avtp_aaf_pay_new_caps (GstAvtpAafPay * avtpaafpay, GstCaps * caps)
{
  GstAudioInfo info;

  gst_audio_info_init (&info);
  if (!gst_audio_info_from_caps (&info, caps)) {
    GST_ERROR_OBJECT (avtpaafpay, "Failed to get info from caps");
    return FALSE;
  }

  avtpaafpay->channels = info.channels;
  avtpaafpay->depth = info.finfo->depth;
  avtpaafpay->rate = gst_to_avtp_rate (info.rate);
  avtpaafpay->format = gst_to_avtp_format (info.finfo->format);

  GST_DEBUG_OBJECT (avtpaafpay, "channels %d, depth %d, rate %d, format %s",
      info.channels, info.finfo->depth, info.rate,
      gst_audio_format_to_string (info.finfo->format));
  return TRUE;
}

static gboolean
gst_avtp_aaf_pay_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
{
  GstCaps *caps;
  GstAvtpAafPay *avtpaafpay = GST_AVTP_AAF_PAY (parent);
Ederson de Souza's avatar
Ederson de Souza committed
379
  gboolean ret;
380 381 382 383 384 385

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

  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_CAPS:
      gst_event_parse_caps (event, &caps);
Ederson de Souza's avatar
Ederson de Souza committed
386 387 388
      ret = gst_avtp_aaf_pay_new_caps (avtpaafpay, caps);
      gst_event_unref (event);
      return ret;
389 390 391 392 393 394 395 396 397 398 399 400
    default:
      return GST_AVTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (pad,
          parent, event);
  }
}

gboolean
gst_avtp_aaf_pay_plugin_init (GstPlugin * plugin)
{
  return gst_element_register (plugin, "avtpaafpay", GST_RANK_NONE,
      GST_TYPE_AVTP_AAF_PAY);
}