gstrtpmp2tpay.c 6.99 KB
Newer Older
1
/* GStreamer
2
 * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
3 4 5 6 7 8 9 10 11 12 13 14 15
 *
 * 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
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
16 17
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
18 19 20 21 22 23 24 25 26 27 28
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include <stdlib.h>
#include <string.h>
#include <gst/rtp/gstrtpbuffer.h>

#include "gstrtpmp2tpay.h"
29
#include "gstrtputils.h"
30 31 32 33 34 35 36 37 38 39

static GstStaticPadTemplate gst_rtp_mp2t_pay_sink_template =
GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("video/mpegts,"
        "packetsize=(int)188," "systemstream=(boolean)true")
    );

static GstStaticPadTemplate gst_rtp_mp2t_pay_src_template =
40
    GST_STATIC_PAD_TEMPLATE ("src",
41 42 43
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("application/x-rtp, "
44 45 46 47
        "media = (string) \"video\", "
        "payload = (int) " GST_RTP_PAYLOAD_MP2T_STRING ", "
        "clock-rate = (int) 90000, " "encoding-name = (string) \"MP2T\" ; "
        "application/x-rtp, "
48 49
        "media = (string) \"video\", "
        "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
50
        "clock-rate = (int) 90000, " "encoding-name = (string) \"MP2T\"")
51 52
    );

53
static gboolean gst_rtp_mp2t_pay_setcaps (GstRTPBasePayload * payload,
54
    GstCaps * caps);
55
static GstFlowReturn gst_rtp_mp2t_pay_handle_buffer (GstRTPBasePayload *
56
    payload, GstBuffer * buffer);
57 58
static GstFlowReturn gst_rtp_mp2t_pay_flush (GstRTPMP2TPay * rtpmp2tpay);
static void gst_rtp_mp2t_pay_finalize (GObject * object);
59

60
#define gst_rtp_mp2t_pay_parent_class parent_class
61
G_DEFINE_TYPE (GstRTPMP2TPay, gst_rtp_mp2t_pay, GST_TYPE_RTP_BASE_PAYLOAD);
62 63 64 65 66

static void
gst_rtp_mp2t_pay_class_init (GstRTPMP2TPayClass * klass)
{
  GObjectClass *gobject_class;
67
  GstElementClass *gstelement_class;
68
  GstRTPBasePayloadClass *gstrtpbasepayload_class;
69 70

  gobject_class = (GObjectClass *) klass;
71
  gstelement_class = (GstElementClass *) klass;
72
  gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
73

74 75
  gobject_class->finalize = gst_rtp_mp2t_pay_finalize;

76 77
  gstrtpbasepayload_class->set_caps = gst_rtp_mp2t_pay_setcaps;
  gstrtpbasepayload_class->handle_buffer = gst_rtp_mp2t_pay_handle_buffer;
78

79 80 81 82
  gst_element_class_add_static_pad_template (gstelement_class,
      &gst_rtp_mp2t_pay_sink_template);
  gst_element_class_add_static_pad_template (gstelement_class,
      &gst_rtp_mp2t_pay_src_template);
83
  gst_element_class_set_static_metadata (gstelement_class,
84 85 86
      "RTP MPEG2 Transport Stream payloader", "Codec/Payloader/Network/RTP",
      "Payload-encodes MPEG2 TS into RTP packets (RFC 2250)",
      "Wim Taymans <wim.taymans@gmail.com>");
87 88 89
}

static void
90
gst_rtp_mp2t_pay_init (GstRTPMP2TPay * rtpmp2tpay)
91
{
92 93
  GST_RTP_BASE_PAYLOAD (rtpmp2tpay)->clock_rate = 90000;
  GST_RTP_BASE_PAYLOAD_PT (rtpmp2tpay) = GST_RTP_PAYLOAD_MP2T;
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

  rtpmp2tpay->adapter = gst_adapter_new ();
}

static void
gst_rtp_mp2t_pay_finalize (GObject * object)
{
  GstRTPMP2TPay *rtpmp2tpay;

  rtpmp2tpay = GST_RTP_MP2T_PAY (object);

  g_object_unref (rtpmp2tpay->adapter);
  rtpmp2tpay->adapter = NULL;

  G_OBJECT_CLASS (parent_class)->finalize (object);
109 110 111
}

static gboolean
112
gst_rtp_mp2t_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
113
{
114 115
  gboolean res;

116 117
  gst_rtp_base_payload_set_options (payload, "video",
      payload->pt != GST_RTP_PAYLOAD_MP2T, "MP2T", 90000);
118
  res = gst_rtp_base_payload_set_outcaps (payload, NULL);
119

120
  return res;
121 122
}

123 124 125
static GstFlowReturn
gst_rtp_mp2t_pay_flush (GstRTPMP2TPay * rtpmp2tpay)
{
126 127
  guint avail, mtu;
  GstFlowReturn ret = GST_FLOW_OK;
128 129 130 131
  GstBuffer *outbuf;

  avail = gst_adapter_available (rtpmp2tpay->adapter);

132 133 134 135 136 137
  mtu = GST_RTP_BASE_PAYLOAD_MTU (rtpmp2tpay);

  while (avail > 0 && (ret == GST_FLOW_OK)) {
    guint towrite;
    guint payload_len;
    guint packet_len;
138
    GstBuffer *paybuf;
139 140 141 142 143 144 145 146 147 148 149 150 151 152

    /* this will be the total length of the packet */
    packet_len = gst_rtp_buffer_calc_packet_len (avail, 0, 0);

    /* fill one MTU or all available bytes */
    towrite = MIN (packet_len, mtu);

    /* this is the payload length */
    payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
    payload_len -= payload_len % 188;

    /* need whole packets */
    if (!payload_len)
      break;
153

154
    /* create buffer to hold the payload */
155
    outbuf = gst_rtp_buffer_new_allocate (0, 0, 0);
156

157
    /* get payload */
158
    paybuf = gst_adapter_take_buffer_fast (rtpmp2tpay->adapter, payload_len);
159
    gst_rtp_copy_meta (GST_ELEMENT_CAST (rtpmp2tpay), outbuf, paybuf, 0);
160
    outbuf = gst_buffer_append (outbuf, paybuf);
161
    avail -= payload_len;
162

163
    GST_BUFFER_PTS (outbuf) = rtpmp2tpay->first_ts;
164
    GST_BUFFER_DURATION (outbuf) = rtpmp2tpay->duration;
165

166 167
    GST_DEBUG_OBJECT (rtpmp2tpay, "pushing buffer of size %u",
        (guint) gst_buffer_get_size (outbuf));
168 169 170

    ret = gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtpmp2tpay), outbuf);
  }
171 172 173 174

  return ret;
}

175
static GstFlowReturn
176
gst_rtp_mp2t_pay_handle_buffer (GstRTPBasePayload * basepayload,
177 178 179
    GstBuffer * buffer)
{
  GstRTPMP2TPay *rtpmp2tpay;
180
  guint size, avail, packet_len;
181
  GstClockTime timestamp, duration;
182 183 184 185
  GstFlowReturn ret;

  rtpmp2tpay = GST_RTP_MP2T_PAY (basepayload);

186
  size = gst_buffer_get_size (buffer);
187
  timestamp = GST_BUFFER_PTS (buffer);
188
  duration = GST_BUFFER_DURATION (buffer);
189

190
again:
191 192
  ret = GST_FLOW_OK;
  avail = gst_adapter_available (rtpmp2tpay->adapter);
193

194 195 196 197 198
  /* Initialize new RTP payload */
  if (avail == 0) {
    rtpmp2tpay->first_ts = timestamp;
    rtpmp2tpay->duration = duration;
  }
199

200 201
  /* get packet length of previous data and this new data */
  packet_len = gst_rtp_buffer_calc_packet_len (avail + size, 0, 0);
202

203 204 205
  /* if this buffer is going to overflow the packet, flush what we have,
   * or if upstream is handing us several packets, to keep latency low */
  if (!size || gst_rtp_base_payload_is_filled (basepayload,
206 207 208 209
          packet_len, rtpmp2tpay->duration + duration)) {
    ret = gst_rtp_mp2t_pay_flush (rtpmp2tpay);
    rtpmp2tpay->first_ts = timestamp;
    rtpmp2tpay->duration = duration;
210

211 212 213 214 215
    /* keep filling the payload */
  } else {
    if (GST_CLOCK_TIME_IS_VALID (duration))
      rtpmp2tpay->duration += duration;
  }
216

217
  /* copy buffer to adapter */
218 219 220 221 222 223 224 225 226
  if (buffer) {
    gst_adapter_push (rtpmp2tpay->adapter, buffer);
    buffer = NULL;
  }

  if (size >= (188 * 2)) {
    size = 0;
    goto again;
  }
227 228

  return ret;
229

230 231 232 233 234 235
}

gboolean
gst_rtp_mp2t_pay_plugin_init (GstPlugin * plugin)
{
  return gst_element_register (plugin, "rtpmp2tpay",
236
      GST_RANK_SECONDARY, GST_TYPE_RTP_MP2T_PAY);
237
}