예제 #1
0
static gboolean
gst_mim_enc_setcaps (GstMimEnc * mimenc, GstCaps * caps)
{
  GstStructure *structure;
  int height, width;
  gboolean ret = FALSE;

  structure = gst_caps_get_structure (caps, 0);
  if (!gst_structure_get_int (structure, "width", &width)) {
    GST_DEBUG_OBJECT (mimenc, "No width set");
    return FALSE;
  }
  if (!gst_structure_get_int (structure, "height", &height)) {
    GST_DEBUG_OBJECT (mimenc, "No height set");
    return FALSE;
  }

  GST_OBJECT_LOCK (mimenc);

  if (mimenc->width == width && mimenc->height == height) {
    ret = TRUE;
    goto out;
  }


  if (width == 320 && height == 240)
    mimenc->res = MIMIC_RES_HIGH;
  else if (width == 160 && height == 120)
    mimenc->res = MIMIC_RES_LOW;
  else {
    GST_WARNING_OBJECT (mimenc, "Invalid resolution %dx%d", width, height);
    goto out;
  }

  gst_mim_enc_reset_locked (mimenc);

  mimenc->width = (guint16) width;
  mimenc->height = (guint16) height;

  GST_DEBUG_OBJECT (mimenc, "Got info from caps w : %d, h : %d",
      mimenc->width, mimenc->height);

  mimenc->enc = mimic_open ();
  if (!mimenc->enc) {
    GST_ERROR_OBJECT (mimenc, "mimic_open failed");
    goto out;
  }

  if (!mimic_encoder_init (mimenc->enc, mimenc->res)) {
    GST_ERROR_OBJECT (mimenc, "mimic_encoder_init error");
    goto out;
  }

  if (!mimic_get_property (mimenc->enc, "buffer_size", &mimenc->buffer_size)) {
    GST_ERROR_OBJECT (mimenc, "mimic_get_property(buffer_size) error");
  }

  ret = TRUE;

out:
  GST_OBJECT_UNLOCK (mimenc);
  return ret;
}
예제 #2
0
static GstFlowReturn
gst_mimenc_chain (GstPad * pad, GstBuffer * in)
{
  GstMimEnc *mimenc;
  GstBuffer *out_buf = NULL, *buf = NULL;
  guchar *data;
  gint buffer_size;
  GstBuffer *header = NULL;
  GstFlowReturn res = GST_FLOW_OK;
  GstEvent *event = NULL;
  gboolean keyframe;

  g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
  mimenc = GST_MIMENC (gst_pad_get_parent (pad));

  g_return_val_if_fail (GST_IS_MIMENC (mimenc), GST_FLOW_ERROR);

  GST_OBJECT_LOCK (mimenc);

  if (mimenc->segment.format == GST_FORMAT_UNDEFINED) {
    GST_WARNING_OBJECT (mimenc, "No new-segment received,"
        " initializing segment with time 0..-1");
    gst_segment_init (&mimenc->segment, GST_FORMAT_TIME);
    gst_segment_set_newsegment (&mimenc->segment,
        FALSE, 1.0, GST_FORMAT_TIME, 0, -1, 0);
  }

  if (mimenc->enc == NULL) {
    mimenc->enc = mimic_open ();
    if (mimenc->enc == NULL) {
      GST_WARNING_OBJECT (mimenc, "mimic_open error\n");
      res = GST_FLOW_ERROR;
      goto out_unlock;
    }

    if (!mimic_encoder_init (mimenc->enc, mimenc->res)) {
      GST_WARNING_OBJECT (mimenc, "mimic_encoder_init error\n");
      mimic_close (mimenc->enc);
      mimenc->enc = NULL;
      res = GST_FLOW_ERROR;
      goto out_unlock;
    }

    if (!mimic_get_property (mimenc->enc, "buffer_size", &mimenc->buffer_size)) {
      GST_WARNING_OBJECT (mimenc, "mimic_get_property('buffer_size') error\n");
      mimic_close (mimenc->enc);
      mimenc->enc = NULL;
      res = GST_FLOW_ERROR;
      goto out_unlock;
    }
  }

  buf = in;
  data = GST_BUFFER_DATA (buf);

  out_buf = gst_buffer_new_and_alloc (mimenc->buffer_size);
  GST_BUFFER_TIMESTAMP (out_buf) =
      gst_segment_to_running_time (&mimenc->segment, GST_FORMAT_TIME,
      GST_BUFFER_TIMESTAMP (buf));
  mimenc->last_buffer = GST_BUFFER_TIMESTAMP (out_buf);
  buffer_size = mimenc->buffer_size;
  keyframe = (mimenc->frames % MAX_INTERFRAMES) == 0 ? TRUE : FALSE;
  if (!mimic_encode_frame (mimenc->enc, data, GST_BUFFER_DATA (out_buf),
          &buffer_size, keyframe)) {
    GST_WARNING_OBJECT (mimenc, "mimic_encode_frame error\n");
    gst_buffer_unref (out_buf);
    gst_buffer_unref (buf);
    res = GST_FLOW_ERROR;
    goto out_unlock;
  }
  GST_BUFFER_SIZE (out_buf) = buffer_size;

  GST_DEBUG_OBJECT (mimenc, "incoming buf size %d, encoded size %d",
      GST_BUFFER_SIZE (buf), GST_BUFFER_SIZE (out_buf));
  ++mimenc->frames;

  // now let's create that tcp header
  header = gst_mimenc_create_tcp_header (mimenc, buffer_size,
      GST_BUFFER_TIMESTAMP (out_buf), keyframe, FALSE);

  if (!header) {
    gst_buffer_unref (out_buf);
    GST_DEBUG_OBJECT (mimenc, "header not created succesfully");
    res = GST_FLOW_ERROR;
    goto out_unlock;
  }

  if (mimenc->need_newsegment) {
    event = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME, 0, -1, 0);
    mimenc->need_newsegment = FALSE;
  }

  GST_OBJECT_UNLOCK (mimenc);

  if (event) {
    if (!gst_pad_push_event (mimenc->srcpad, event))
      GST_WARNING_OBJECT (mimenc, "Failed to push NEWSEGMENT event");
  }

  res = gst_pad_push (mimenc->srcpad, header);
  if (res != GST_FLOW_OK) {
    gst_buffer_unref (out_buf);
    goto out;
  }

  res = gst_pad_push (mimenc->srcpad, out_buf);

out:
  if (buf)
    gst_buffer_unref (buf);
  gst_object_unref (mimenc);

  return res;

out_unlock:
  GST_OBJECT_UNLOCK (mimenc);
  goto out;

}