gstnicesink.c 14.9 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
/*
 * This file is part of the Nice GLib ICE library.
 *
 * (C) 2006, 2007 Collabora Ltd.
 *  Contact: Dafydd Harries
 * (C) 2006, 2007 Nokia Corporation. All rights reserved.
 *  Contact: Kai Vehmanen
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the Nice GLib ICE library.
 *
 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
 * Corporation. All Rights Reserved.
 *
 * Contributors:
 *   Dafydd Harries, Collabora Ltd.
 *
 * Alternatively, the contents of this file may be used under the terms of the
 * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
 * case the provisions of LGPL are applicable instead of those above. If you
 * wish to allow use of your version of this file only under the terms of the
 * LGPL and not to allow others to use your version of this file under the
 * MPL, indicate your decision by deleting the provisions above and replace
 * them with the notice and other provisions required by the LGPL. If you do
 * not delete the provisions above, a recipient may use your version of this
 * file under either the MPL or the LGPL.
 */
38 39 40
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
Dafydd Harries's avatar
Dafydd Harries committed
41 42 43

#include "gstnicesink.h"

44 45 46 47

GST_DEBUG_CATEGORY_STATIC (nicesink_debug);
#define GST_CAT_DEFAULT nicesink_debug

Dafydd Harries's avatar
Dafydd Harries committed
48 49
static GstFlowReturn
gst_nice_sink_render (
Dafydd Harries's avatar
Dafydd Harries committed
50 51
  GstBaseSink *basesink,
  GstBuffer *buffer);
52 53 54 55 56 57
#if GST_CHECK_VERSION (1,0,0)
static GstFlowReturn
gst_nice_sink_render_list (
  GstBaseSink *basesink,
  GstBufferList *buffer_list);
#endif
Dafydd Harries's avatar
Dafydd Harries committed
58

59 60 61 62 63 64 65 66 67 68 69 70 71
static gboolean
gst_nice_sink_unlock (GstBaseSink *basesink);

static gboolean
gst_nice_sink_unlock_stop (GstBaseSink *basesink);

static void
_reliable_transport_writable (
    NiceAgent *agent,
    guint stream_id,
    guint component_id,
    GstNiceSink *sink);

Dafydd Harries's avatar
Dafydd Harries committed
72 73
static void
gst_nice_sink_set_property (
Dafydd Harries's avatar
Dafydd Harries committed
74 75 76 77
  GObject *object,
  guint prop_id,
  const GValue *value,
  GParamSpec *pspec);
Dafydd Harries's avatar
Dafydd Harries committed
78 79 80

static void
gst_nice_sink_get_property (
Dafydd Harries's avatar
Dafydd Harries committed
81 82 83 84
  GObject *object,
  guint prop_id,
  GValue *value,
  GParamSpec *pspec);
85 86 87

static void
gst_nice_sink_dispose (GObject *object);
88 89 90 91
#if GST_CHECK_VERSION (1,0,0)
static void
gst_nice_sink_finalize (GObject *object);
#endif
92

93 94 95 96
static GstStateChangeReturn
gst_nice_sink_change_state (
    GstElement * element,
    GstStateChange transition);
Dafydd Harries's avatar
Dafydd Harries committed
97 98 99 100 101 102 103 104

static GstStaticPadTemplate gst_nice_sink_sink_template =
GST_STATIC_PAD_TEMPLATE (
    "sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS_ANY);

105
G_DEFINE_TYPE (GstNiceSink, gst_nice_sink, GST_TYPE_BASE_SINK);
Dafydd Harries's avatar
Dafydd Harries committed
106 107 108 109 110 111 112 113 114 115 116 117

enum
{
  PROP_AGENT = 1,
  PROP_STREAM,
  PROP_COMPONENT
};

static void
gst_nice_sink_class_init (GstNiceSinkClass *klass)
{
  GstBaseSinkClass *gstbasesink_class;
118
  GstElementClass *gstelement_class;
Dafydd Harries's avatar
Dafydd Harries committed
119 120
  GObjectClass *gobject_class;

121 122 123
  GST_DEBUG_CATEGORY_INIT (nicesink_debug, "nicesink",
      0, "libnice sink");

Dafydd Harries's avatar
Dafydd Harries committed
124 125
  gstbasesink_class = (GstBaseSinkClass *) klass;
  gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_nice_sink_render);
126 127 128
#if GST_CHECK_VERSION (1,0,0)
  gstbasesink_class->render_list = GST_DEBUG_FUNCPTR (gst_nice_sink_render_list);
#endif
129 130
  gstbasesink_class->unlock = GST_DEBUG_FUNCPTR (gst_nice_sink_unlock);
  gstbasesink_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_nice_sink_unlock_stop);
Dafydd Harries's avatar
Dafydd Harries committed
131 132 133 134

  gobject_class = (GObjectClass *) klass;
  gobject_class->set_property = gst_nice_sink_set_property;
  gobject_class->get_property = gst_nice_sink_get_property;
135
  gobject_class->dispose = gst_nice_sink_dispose;
136 137 138
#if GST_CHECK_VERSION (1,0,0)
  gobject_class->finalize = gst_nice_sink_finalize;
#endif
Dafydd Harries's avatar
Dafydd Harries committed
139

140 141 142
  gstelement_class = (GstElementClass *) klass;
  gstelement_class->change_state = gst_nice_sink_change_state;

143 144
  gst_element_class_add_pad_template (gstelement_class,
      gst_static_pad_template_get (&gst_nice_sink_sink_template));
145 146 147 148 149 150
#if GST_CHECK_VERSION (1,0,0)
  gst_element_class_set_metadata (gstelement_class,
#else
  gst_element_class_set_details_simple (gstelement_class,
#endif
    "ICE sink",
151 152 153 154 155
    "Sink",
    "Interactive UDP connectivity establishment",
    "Dafydd Harries <dafydd.harries@collabora.co.uk>");


Dafydd Harries's avatar
Dafydd Harries committed
156
  g_object_class_install_property (gobject_class, PROP_AGENT,
157
      g_param_spec_object (
Dafydd Harries's avatar
Dafydd Harries committed
158 159 160
         "agent",
         "Agent",
         "The NiceAgent this source is bound to",
161
         NICE_TYPE_AGENT,
162
         G_PARAM_READWRITE));
Dafydd Harries's avatar
Dafydd Harries committed
163 164 165 166 167 168 169 170 171

  g_object_class_install_property (gobject_class, PROP_STREAM,
      g_param_spec_uint (
         "stream",
         "Stream ID",
         "The ID of the stream to read from",
         0,
         G_MAXUINT,
         0,
172
         G_PARAM_READWRITE));
Dafydd Harries's avatar
Dafydd Harries committed
173 174 175 176 177 178 179 180 181

  g_object_class_install_property (gobject_class, PROP_COMPONENT,
      g_param_spec_uint (
         "component",
         "Component ID",
         "The ID of the component to read from",
         0,
         G_MAXUINT,
         0,
182
         G_PARAM_READWRITE));
Dafydd Harries's avatar
Dafydd Harries committed
183 184 185
}

static void
186
gst_nice_sink_init (GstNiceSink *sink)
Dafydd Harries's avatar
Dafydd Harries committed
187
{
188 189 190 191
#if GST_CHECK_VERSION (1,0,0)
  guint max_mem;
#endif

192
  g_cond_init (&sink->writable_cond);
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207

#if GST_CHECK_VERSION (1,0,0)
  /* pre-allocate OutputVector, MapInfo and OutputMessage arrays
   * for use in the render and render_list functions */
  max_mem = gst_buffer_get_max_memory ();

  sink->n_vecs = max_mem;
  sink->vecs = g_new (GOutputVector, sink->n_vecs);

  sink->n_maps = max_mem;
  sink->maps = g_new (GstMapInfo, sink->n_maps);

  sink->n_messages = 1;
  sink->messages = g_new (NiceOutputMessage, sink->n_messages);
#endif
208 209 210 211 212 213 214 215 216 217 218
}

static void
_reliable_transport_writable (NiceAgent *agent, guint stream_id,
    guint component_id, GstNiceSink *sink)
{
  GST_OBJECT_LOCK (sink);
  if (stream_id == sink->stream_id && component_id == sink->component_id) {
    g_cond_broadcast (&sink->writable_cond);
  }
  GST_OBJECT_UNLOCK (sink);
Dafydd Harries's avatar
Dafydd Harries committed
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
#if GST_CHECK_VERSION (1,0,0)
static gsize
fill_vectors (GOutputVector * vecs, GstMapInfo * maps, guint n, GstBuffer * buf)
{
  GstMemory *mem;
  gsize size = 0;
  guint i;

  g_assert (gst_buffer_n_memory (buf) == n);

  for (i = 0; i < n; ++i) {
    mem = gst_buffer_peek_memory (buf, i);
    if (gst_memory_map (mem, &maps[i], GST_MAP_READ)) {
      vecs[i].buffer = maps[i].data;
      vecs[i].size = maps[i].size;
    } else {
      GST_WARNING ("Failed to map memory %p for reading", mem);
      vecs[i].buffer = "";
      vecs[i].size = 0;
    }
    size += vecs[i].size;
  }

  return size;
}

/* Buffer list code written by
 *   Tim-Philipp Müller <tim@centricular.com>
 * taken from
 *   gst-plugins-good/gst/udp/gstmultiudpsink.c
 */
Dafydd Harries's avatar
Dafydd Harries committed
252
static GstFlowReturn
253 254
gst_nice_sink_render_buffers (GstNiceSink * sink, GstBuffer ** buffers,
    guint num_buffers, guint8 * mem_nums, guint total_mem_num)
Dafydd Harries's avatar
Dafydd Harries committed
255
{
256 257 258 259
  NiceOutputMessage *msgs;
  GOutputVector *vecs;
  GstMapInfo *map_infos;
  guint i, mem;
260 261 262
  guint written = 0;
  gint ret;
  GstFlowReturn flow_ret = GST_FLOW_OK;
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
  GST_LOG_OBJECT (sink, "%u buffers, %u memories -> to be sent",
      num_buffers, total_mem_num);

  if (sink->n_vecs < total_mem_num) {
    sink->n_vecs = GST_ROUND_UP_16 (total_mem_num);
    g_free (sink->vecs);
    sink->vecs = g_new (GOutputVector, sink->n_vecs);
  }
  vecs = sink->vecs;

  if (sink->n_maps < total_mem_num) {
    sink->n_maps = GST_ROUND_UP_16 (total_mem_num);
    g_free (sink->maps);
    sink->maps = g_new (GstMapInfo, sink->n_maps);
  }
  map_infos = sink->maps;

  if (sink->n_messages < num_buffers) {
    sink->n_messages = GST_ROUND_UP_16 (num_buffers);
    g_free (sink->messages);
    sink->messages = g_new (NiceOutputMessage, sink->n_messages);
  }
  msgs = sink->messages;

  for (i = 0, mem = 0; i < num_buffers; ++i) {
    fill_vectors (&vecs[mem], &map_infos[mem], mem_nums[i], buffers[i]);
    msgs[i].buffers = &vecs[mem];
    msgs[i].n_buffers = mem_nums[i];
    mem += mem_nums[i];
  }

  GST_OBJECT_LOCK (sink);
  do {
    ret = nice_agent_send_messages_nonblocking(sink->agent, sink->stream_id,
        sink->component_id, msgs + written, num_buffers - written, NULL, NULL);

    if (ret > 0)
      written += ret;

    if (sink->reliable && written < num_buffers)
      g_cond_wait (&sink->writable_cond, GST_OBJECT_GET_LOCK (sink));

    if (sink->flushing) {
      flow_ret = GST_FLOW_FLUSHING;
      break;
    }
  } while (sink->reliable && written < num_buffers);
  GST_OBJECT_UNLOCK (sink);

  for (i = 0; i < mem; ++i)
    gst_memory_unmap (map_infos[i].memory, &map_infos[i]);

  return flow_ret;
}
#endif

static GstFlowReturn
gst_nice_sink_render (GstBaseSink *basesink, GstBuffer *buffer)
{
  GstNiceSink *nicesink = GST_NICE_SINK (basesink);
  GstFlowReturn flow_ret = GST_FLOW_OK;
325
#if GST_CHECK_VERSION (1,0,0)
326 327 328 329 330 331 332 333
  guint8 n_mem;

  n_mem = gst_buffer_n_memory (buffer);

  if (n_mem > 0) {
    flow_ret = gst_nice_sink_render_buffers (nicesink, &buffer, 1, &n_mem,
        n_mem);
  }
334

335
#else
336 337 338 339 340
  guint written = 0;
  gint ret;
  gchar *data = NULL;
  guint size = 0;

341 342
  data = (gchar *) GST_BUFFER_DATA (buffer);
  size = GST_BUFFER_SIZE (buffer);
Dafydd Harries's avatar
Dafydd Harries committed
343

344 345 346 347 348 349
  GST_OBJECT_LOCK (nicesink);
  do {
    ret = nice_agent_send (nicesink->agent, nicesink->stream_id,
        nicesink->component_id, size - written, data + written);
    if (ret > 0)
      written += ret;
350

351 352 353 354 355 356 357 358
    if (nicesink->reliable && written < size)
      g_cond_wait (&nicesink->writable_cond, GST_OBJECT_GET_LOCK (nicesink));
    if (nicesink->flushing) {
      flow_ret = GST_FLOW_WRONG_STATE;
      break;
    }
  } while (nicesink->reliable && written < size);
  GST_OBJECT_UNLOCK (nicesink);
Dafydd Harries's avatar
Dafydd Harries committed
359

360
#endif
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
  return flow_ret;
}

#if GST_CHECK_VERSION (1,0,0)
static GstFlowReturn
gst_nice_sink_render_list (GstBaseSink *basesink, GstBufferList *buffer_list)
{
  GstNiceSink *nicesink = GST_NICE_SINK (basesink);
  GstBuffer **buffers;
  GstFlowReturn flow_ret = GST_FLOW_OK;
  guint8 *mem_nums;
  guint total_mems;
  guint i, num_buffers;

  num_buffers = gst_buffer_list_length (buffer_list);
  if (num_buffers == 0)
    goto no_data;

  buffers = g_newa (GstBuffer *, num_buffers);
  mem_nums = g_newa (guint8, num_buffers);
  for (i = 0, total_mems = 0; i < num_buffers; ++i) {
    buffers[i] = gst_buffer_list_get (buffer_list, i);
    mem_nums[i] = gst_buffer_n_memory (buffers[i]);
    total_mems += mem_nums[i];
  }

  flow_ret = gst_nice_sink_render_buffers (nicesink, buffers, num_buffers,
      mem_nums, total_mems);

  return flow_ret;

no_data:
  {
    GST_LOG_OBJECT (nicesink, "empty buffer");
    return GST_FLOW_OK;
  }
397 398 399

  return flow_ret;
}
400
#endif
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422

static gboolean gst_nice_sink_unlock (GstBaseSink *basesink)
{
  GstNiceSink *nicesink = GST_NICE_SINK (basesink);

  GST_OBJECT_LOCK (nicesink);
  nicesink->flushing = TRUE;
  g_cond_broadcast (&nicesink->writable_cond);
  GST_OBJECT_UNLOCK (nicesink);

  return TRUE;
}

static gboolean gst_nice_sink_unlock_stop (GstBaseSink *basesink)
{
  GstNiceSink *nicesink = GST_NICE_SINK (basesink);

  GST_OBJECT_LOCK (nicesink);
  nicesink->flushing = FALSE;
  GST_OBJECT_UNLOCK (nicesink);

  return TRUE;
Dafydd Harries's avatar
Dafydd Harries committed
423 424
}

425 426 427 428 429
static void
gst_nice_sink_dispose (GObject *object)
{
  GstNiceSink *sink = GST_NICE_SINK (object);

430
  if (sink->agent && sink->writable_id)
431
    g_signal_handler_disconnect (sink->agent, sink->writable_id);
432 433
  sink->writable_id = 0;
  g_clear_object (&sink->agent);
434

435 436
  g_cond_clear (&sink->writable_cond);

437
  G_OBJECT_CLASS (gst_nice_sink_parent_class)->dispose (object);
438 439
}

440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
#if GST_CHECK_VERSION (1,0,0)
static void
gst_nice_sink_finalize (GObject *object)
{
  GstNiceSink *sink = GST_NICE_SINK (object);

  g_free (sink->vecs);
  sink->vecs = NULL;
  sink->n_vecs = 0;
  g_free (sink->maps);
  sink->maps = NULL;
  sink->n_maps = 0;
  g_free (sink->messages);
  sink->messages = NULL;
  sink->n_messages = 0;

  G_OBJECT_CLASS (gst_nice_sink_parent_class)->finalize (object);
}
#endif

Dafydd Harries's avatar
Dafydd Harries committed
460 461
static void
gst_nice_sink_set_property (
Dafydd Harries's avatar
Dafydd Harries committed
462 463 464 465
  GObject *object,
  guint prop_id,
  const GValue *value,
  GParamSpec *pspec)
Dafydd Harries's avatar
Dafydd Harries committed
466 467 468 469 470 471
{
  GstNiceSink *sink = GST_NICE_SINK (object);

  switch (prop_id)
    {
    case PROP_AGENT:
472
      if (sink->agent) {
473 474
        GST_ERROR_OBJECT (object,
            "Changing the agent on a nice sink not allowed");
475
      } else {
476
        sink->agent = g_value_dup_object (value);
477 478 479 480 481 482
        g_object_get (sink->agent, "reliable", &sink->reliable, NULL);
        if (sink->reliable)
          sink->writable_id = g_signal_connect (sink->agent,
              "reliable-transport-writable",
              (GCallback) _reliable_transport_writable, sink);
      }
Dafydd Harries's avatar
Dafydd Harries committed
483 484 485
      break;

    case PROP_STREAM:
486
      GST_OBJECT_LOCK (sink);
Dafydd Harries's avatar
Dafydd Harries committed
487
      sink->stream_id = g_value_get_uint (value);
488
      GST_OBJECT_UNLOCK (sink);
Dafydd Harries's avatar
Dafydd Harries committed
489 490 491
      break;

    case PROP_COMPONENT:
492 493 494 495 496 497 498 499 500
      {
        guint new_component_id = g_value_get_uint (value);
        GST_OBJECT_LOCK (sink);
        if (sink->component_id != new_component_id) {
          sink->component_id = new_component_id;
          g_cond_broadcast (&sink->writable_cond);
        }
        GST_OBJECT_UNLOCK (sink);
      }
Dafydd Harries's avatar
Dafydd Harries committed
501
      break;
502 503 504 505

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
Dafydd Harries's avatar
Dafydd Harries committed
506 507 508 509 510
    }
}

static void
gst_nice_sink_get_property (
Dafydd Harries's avatar
Dafydd Harries committed
511 512 513 514
  GObject *object,
  guint prop_id,
  GValue *value,
  GParamSpec *pspec)
Dafydd Harries's avatar
Dafydd Harries committed
515 516 517 518 519 520
{
  GstNiceSink *sink = GST_NICE_SINK (object);

  switch (prop_id)
    {
    case PROP_AGENT:
521
      g_value_set_object (value, sink->agent);
Dafydd Harries's avatar
Dafydd Harries committed
522 523 524
      break;

    case PROP_STREAM:
525
      GST_OBJECT_LOCK (sink);
Dafydd Harries's avatar
Dafydd Harries committed
526
      g_value_set_uint (value, sink->stream_id);
527
      GST_OBJECT_UNLOCK (sink);
Dafydd Harries's avatar
Dafydd Harries committed
528 529 530
      break;

    case PROP_COMPONENT:
531
      GST_OBJECT_LOCK (sink);
Dafydd Harries's avatar
Dafydd Harries committed
532
      g_value_set_uint (value, sink->component_id);
533
      GST_OBJECT_UNLOCK (sink);
Dafydd Harries's avatar
Dafydd Harries committed
534
      break;
535 536 537 538

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
Dafydd Harries's avatar
Dafydd Harries committed
539 540 541
    }
}

542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
static GstStateChangeReturn
gst_nice_sink_change_state (GstElement * element, GstStateChange transition)
{
  GstNiceSink *sink;
  GstStateChangeReturn ret;

  sink = GST_NICE_SINK (element);

  switch (transition) {
    case GST_STATE_CHANGE_NULL_TO_READY:
      if (sink->agent == NULL)
        {
          GST_ERROR_OBJECT (element,
              "Trying to start Nice sink without an agent set");
          return GST_STATE_CHANGE_FAILURE;
        }
558 559 560 561 562 563 564 565 566 567 568 569
      else if (sink->stream_id == 0)
          {
            GST_ERROR_OBJECT (element,
                "Trying to start Nice sink without a stream set");
            return GST_STATE_CHANGE_FAILURE;
          }
      else if (sink->component_id == 0)
          {
            GST_ERROR_OBJECT (element,
                "Trying to start Nice sink without a component set");
            return GST_STATE_CHANGE_FAILURE;
          }
570
      break;
571 572 573 574 575
    case GST_STATE_CHANGE_READY_TO_PAUSED:
    case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
    case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
    case GST_STATE_CHANGE_PAUSED_TO_READY:
    case GST_STATE_CHANGE_READY_TO_NULL:
576 577 578 579
    default:
      break;
  }

580 581
  ret = GST_ELEMENT_CLASS (gst_nice_sink_parent_class)->change_state (element,
      transition);
582 583 584

  return ret;
}