Example #1
0
//process all that shit in vectors.... very gay
void fill_vectors(QNode* qb, ANNpoint* center, double side) {
  if (qb->children == 0) {
    Globals::qt_nodes.push_back(qb);
    Globals::qt_centers.push_back(center);
    Globals::qt_sides.push_back(side);
    //std::cout << side << std::endl;
  } else {
    int it;
    ANNpointArray child_centers = annAllocPts(Globals::pow2dim, Globals::dim);
    for (int i=0; i<Globals::pow2dim; i++) {
      it = 1;
      for (int j=0; j<Globals::dim; j++) {
        if ((i&it) != 0) {
          child_centers[i][j] = (*center)[j]+(side/4.0);
        } else {
          child_centers[i][j] = (*center)[j]-(side/4.0);
        }
        it = it << 1;
      }
      fill_vectors(qb->children[0]+i,&child_centers[i],side/2.0);
    }
    annDeallocPts(child_centers);
  }
}
GstFlowReturn
gst_writev_buffers (GstObject * sink, gint fd, GstPoll * fdset,
    GstBuffer ** buffers, guint num_buffers, guint8 * mem_nums,
    guint total_mem_num, guint64 * total_written, guint64 * cur_pos)
{
  struct iovec *vecs;
  GstMapInfo *map_infos;
  GstFlowReturn flow_ret;
  gsize size = 0;
  guint i, j;

  GST_LOG_OBJECT (sink, "%u buffers, %u memories", num_buffers, total_mem_num);

  vecs = g_newa (struct iovec, total_mem_num);
  map_infos = g_newa (GstMapInfo, total_mem_num);

  /* populate output vectors */
  for (i = 0, j = 0; i < num_buffers; ++i) {
    size += fill_vectors (&vecs[j], &map_infos[j], mem_nums[i], buffers[i]);
    j += mem_nums[i];
  }

  /* now write it all out! */
  {
    gssize ret, left;
    guint n_vecs = total_mem_num;

    left = size;
    do {
#ifndef HAVE_WIN32
      if (fdset != NULL) {
        do {
          GST_DEBUG_OBJECT (sink, "going into select, have %" G_GSSIZE_FORMAT
              " bytes to write", left);
          ret = gst_poll_wait (fdset, GST_CLOCK_TIME_NONE);
        } while (ret == -1 && (errno == EINTR || errno == EAGAIN));

        if (ret == -1) {
          if (errno == EBUSY)
            goto stopped;
          else
            goto select_error;
        }
      }
#endif

      ret = gst_writev (fd, vecs, n_vecs, left);

      if (ret > 0) {
        if (total_written)
          *total_written += ret;
        if (cur_pos)
          *cur_pos += ret;
      }

      if (ret == left)
        break;

      if (ret < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
        /* do nothing, try again */
      } else if (ret < 0) {
        goto write_error;
      } else if (ret < left) {
        /* skip vectors that have been written in full */
        while (ret >= vecs[0].iov_len) {
          ret -= vecs[0].iov_len;
          left -= vecs[0].iov_len;
          ++vecs;
          --n_vecs;
        }
        g_assert (n_vecs > 0);
        /* skip partially written vector data */
        if (ret > 0) {
          vecs[0].iov_len -= ret;
          vecs[0].iov_base = ((guint8 *) vecs[0].iov_base) + ret;
          left -= ret;
        }
      }
#ifdef HAVE_WIN32
      /* do short sleep on windows where we don't use gst_poll(),
       * to avoid excessive busy looping */
      if (fdset != NULL)
        g_usleep (1000);
#endif

    }
    while (left > 0);
  }

  flow_ret = GST_FLOW_OK;

out:

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

  return flow_ret;

/* ERRORS */
#ifndef HAVE_WIN32
select_error:
  {
    GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
        ("select on file descriptor: %s", g_strerror (errno)));
    GST_DEBUG_OBJECT (sink, "Error during select: %s", g_strerror (errno));
    flow_ret = GST_FLOW_ERROR;
    goto out;
  }
stopped:
  {
    GST_DEBUG_OBJECT (sink, "Select stopped");
    flow_ret = GST_FLOW_FLUSHING;
    goto out;
  }
#endif
write_error:
  {
    switch (errno) {
      case ENOSPC:
        GST_ELEMENT_ERROR (sink, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
        break;
      default:{
        GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
            ("Error while writing to file descriptor %d: %s",
                fd, g_strerror (errno)));
      }
    }
    flow_ret = GST_FLOW_ERROR;
    goto out;
  }
}