Beispiel #1
0
static void *
parse_client_set(void *buf, proto_base *pb)
{
	proto_client_set *s = g_slice_alloc(sizeof(proto_client_set));
	memcpy(s, pb, sizeof(proto_base));
	s->key_len = *((uint8_t *) buf);
	s->val_len = *((uint8_t *) buf + 1);
	s->key = g_slice_copy(s->key_len, buf + 3);
	s->val = g_slice_copy(s->val_len, buf + 3 + s->key_len);
	return s;
}
Beispiel #2
0
float *
cogl_vector3_copy (const float *vector)
{
  if (vector)
    return g_slice_copy (sizeof (float) * 3, vector);
  return NULL;
}
Beispiel #3
0
static void *
parse_client_del(void *buf, proto_base *pb)
{
	proto_server_del *s = g_slice_alloc(sizeof(proto_server_del));
	memcpy(s, pb, sizeof(proto_base));
	s->key_len = *((uint8_t *) buf);
	s->key = g_slice_copy(s->key_len, buf + 1);
	return s;
}
Beispiel #4
0
static void *
parse_server_get(void *buf, proto_base *pb)
{
	proto_server_get *s = g_slice_alloc(sizeof(proto_server_get));
	memcpy(s, pb, sizeof(proto_base));
	s->val_len = *((uint8_t *) buf);
	s->val = g_slice_copy(s->val_len, buf + 1);
	return s;
}
/**
 * gst_segment_copy:
 * @segment: (transfer none): a #GstSegment
 *
 * Create a copy of given @segment.
 *
 * Free-function: gst_segment_free
 *
 * Returns: (transfer full): a new #GstSegment, free with gst_segment_free().
 */
GstSegment *
gst_segment_copy (const GstSegment * segment)
{
  GstSegment *result = NULL;

  if (segment) {
    result = (GstSegment *) g_slice_copy (sizeof (GstSegment), segment);
  }
  return result;
}
Beispiel #6
0
static void
test_slice_copy (void)
{
  const gchar *block = "0123456789ABCDEF";
  gpointer p;

  p = g_slice_copy (12, block);
  g_assert (memcmp (p, block, 12) == 0);
  g_slice_free1 (12, p);
}
Beispiel #7
0
/**
 * gst_allocation_params_copy:
 * @params: (transfer none): a #GstAllocationParams
 *
 * Create a copy of @params.
 *
 * Free-function: gst_allocation_params_free
 *
 * Returns: (transfer full): a new ##GstAllocationParams, free with
 * gst_allocation_params_free().
 */
GstAllocationParams *
gst_allocation_params_copy (const GstAllocationParams * params)
{
  GstAllocationParams *result = NULL;

  if (params) {
    result =
        (GstAllocationParams *) g_slice_copy (sizeof (GstAllocationParams),
        params);
  }
  return result;
}
Beispiel #8
0
static gboolean
on_button_release (ClutterActor *stage, ClutterButtonEvent *event,
                   CallbackData *data)
{
  if (data->current_clip.type != CLIP_NONE)
    {
      data->clips = g_slist_prepend (data->clips,
                                     g_slice_copy (sizeof (Clip),
                                                   &data->current_clip));

      data->current_clip.type = CLIP_NONE;
    }

  clutter_actor_queue_redraw (stage);

  return FALSE;
}
Beispiel #9
0
struct qio_ev* qio_ev_new(
	const gchar *ev_path,
	const gchar *json,
	const gboolean internal)
{
	struct qio_ev ev = {
		.ev_path = g_strdup(ev_path),
		.json = g_strdup(json),
		.internal = internal,
		.subs = g_array_new(FALSE, FALSE, sizeof(struct qio_ev_sub)),
	};

	g_array_set_clear_func(ev.subs, _sub_clear);

	return g_slice_copy(sizeof(ev), &ev);
}

void qio_ev_free(struct qio_ev *ev)
{
	g_free(ev->ev_path);
	g_free(ev->json);
	g_array_free(ev->subs, TRUE);
	g_slice_free1(sizeof(*ev), ev);
}
Beispiel #10
0
Datei: output.c Projekt: worr/wsh
static void construct_out(struct collate* c, struct f_collate* f) {
	// If both are null, let's not bother with any of this
	if (*c->error == NULL && *c->output == NULL) return;

	gsize new_len = 0;
	gchar* host_list_str_stdout, * host_list_str_stderr, * host_list_str_base;
	host_list_str_base = host_list_str_stderr = host_list_str_stdout = NULL;

	// Calculate size of host list
	gsize host_list_len = 0;
	for (GSList* host_list = c->hosts; host_list != NULL;
	        host_list = host_list->next) {
		host_list_len += strlen(host_list->data) + 2;
	}

	// Build host list string
	host_list_str_base = g_slice_alloc0(host_list_len + WSHC_STDERR_TAIL_SIZE);
	for (GSList* host_list = c->hosts; host_list != NULL;
	        host_list = host_list->next) {
		g_strlcat(host_list_str_base, host_list->data, host_list_len);
		g_strlcat(host_list_str_base, " ", host_list_len);
	}

	// Copy the host list string
	if (*c->output && ! *c->error)
		host_list_str_stdout = host_list_str_base;
	else if (*c->error && ! *c->output)
		host_list_str_stderr = host_list_str_base;
	else {
		host_list_str_stdout = host_list_str_base;
		host_list_str_stderr = g_slice_copy(host_list_len + WSHC_STDOUT_TAIL_SIZE,
		                                    host_list_str_base);
	}

	// Add the tails of the host command to the host list
	if (host_list_str_stderr)
		strncat(host_list_str_stderr, WSHC_STDERR_TAIL, WSHC_STDERR_TAIL_SIZE);
	if(host_list_str_stdout)
		strncat(host_list_str_stdout, WSHC_STDOUT_TAIL, WSHC_STDOUT_TAIL_SIZE);

	// Calculate the size of the stderr and stdout
	gsize stderr_len = 0;
	for (gchar** p = c->error; *p != NULL; p++) stderr_len += (strlen(*p) + 1);

	gsize stdout_len = 0;
	for (gchar** p = c->output; *p != NULL; p++) stdout_len += (strlen(*p) + 1);

	// Allocate memory until requirement is satisfied
	new_len = 1 + stderr_len + stdout_len + host_list_len * 2 +
	          WSHC_STDOUT_TAIL_SIZE + WSHC_STDERR_TAIL_SIZE;
	while (*f->size == 0 || new_len + strlen(*f->out) > *f->size) {
		gchar* new_output = g_slice_alloc0(*f->size + WSHC_ALLOC_LEN);
		g_memmove(new_output, *f->out, *f->size);
		if (*f->size)
			g_slice_free1(*f->size, *f->out);
		*f->out = new_output;
		*f->size += WSHC_ALLOC_LEN;
	}

	// Start copying data into out
	if (*c->error && host_list_str_stderr) {
		strncat(*f->out, host_list_str_stderr, *f->size);

		for (gchar** p = c->error; *p != NULL; p++) {
			strncat(*f->out, *p, *f->size);
			strncat(*f->out, "\n", *f->size);
		}

		// Add separating newline
		if (*c->output != NULL)
			strncat(*f->out, "\n", *f->size);
	}

	if (*c->output && host_list_str_stdout) {
		strncat(*f->out, host_list_str_stdout, *f->size);

		for (gchar** p = c->output; *p != NULL; p++) {
			strncat(*f->out, *p, *f->size);
			strncat(*f->out, "\n", *f->size);
		}
	}

	strncat(*f->out, "\n", *f->size);

	if (host_list_str_stderr)
		g_slice_free1(host_list_len + WSHC_STDERR_TAIL_SIZE, host_list_str_stderr);
	if (host_list_str_stdout)
		g_slice_free1(host_list_len + WSHC_STDOUT_TAIL_SIZE, host_list_str_stdout);
}
/**
 * @brief Setup unicast UDP transport sockets for an RTP session
 */
gboolean rtp_udp_transport(RTSP_Client *rtsp,
                           RTP_session *rtp_s,
                           struct ParsedTransport *parsed)
{
    ev_io *io = &rtp_s->transport.udp.rtcp_reader;
    char *source = NULL;
    struct sockaddr_storage sa;
    socklen_t sa_len = rtsp->sa_len;
    struct sockaddr *sa_p = (struct sockaddr*) &sa;
    int firstsd;
    in_port_t firstport, rtp_port, rtcp_port;

    memcpy(sa_p, rtsp->local_sa, sa_len);

    /* The client will not provide ports for us, obviously, let's
     * just ask the kernel for one, and try it to use for RTP/RTCP
     * as needed, if it fails, just get another random one.
     *
     * RFC 3550 Section 11 describe the choice of port numbers for RTP
     * applications; since we're delievering RTP as part of an RTSP
     * stream, we fall in the latest case described. We thus *can*
     * avoid using the even-odd adjacent ports pair for RTP-RTCP.
     */

    neb_sa_set_port(sa_p, 0);

    if ( (firstsd = socket(sa_p->sa_family, SOCK_DGRAM, 0)) < 0 ) {
        xlog(LOG_ERR, "socket 1");
        goto error;
    }

    if ( bind(firstsd, sa_p, sa_len) < 0 ) {
        xlog(LOG_ERR, "bind 1");
        goto error;
    }

    if ( getsockname(firstsd, sa_p, &sa_len) < 0 ) {
        xlog(LOG_ERR, "getsockname 1");
        goto error;
    }

    firstport = neb_sa_get_port(sa_p);

    switch ( firstport % 2 ) {
    case 0:
        rtp_s->transport.udp.rtp_sd = firstsd; firstsd = -1;
        rtp_port = firstport; rtcp_port = firstport+1;
        if ( (rtp_s->transport.udp.rtcp_sd = socket(sa_p->sa_family, SOCK_DGRAM, 0)) < 0 ) {
            xlog(LOG_ERR, "socket 2");
            goto error;
        }

        neb_sa_set_port(sa_p, rtcp_port);

        if ( bind(rtp_s->transport.udp.rtcp_sd, sa_p, sa_len) < 0 ) {
            xlog(LOG_ERR, "bind 2");

            neb_sa_set_port(sa_p, 0);
            if ( bind(rtp_s->transport.udp.rtcp_sd, sa_p, sa_len) < 0 ) {
                xlog(LOG_ERR, "bind 3");
                goto error;
            }

            if ( getsockname(rtp_s->transport.udp.rtcp_sd, sa_p, &sa_len) < 0 ) {
                xlog(LOG_ERR, "getsockname 2");
                goto error;
            }

            rtcp_port = neb_sa_get_port(sa_p);
        }

        break;
    case 1:
        rtp_s->transport.udp.rtcp_sd = firstsd; firstsd = -1;
        rtcp_port = firstport; rtp_port = firstport-1;
        if ( (rtp_s->transport.udp.rtp_sd = socket(sa_p->sa_family, SOCK_DGRAM, 0)) < 0 ) {
            xlog(LOG_ERR, "socket 3");
            goto error;
        }

        neb_sa_set_port(sa_p, rtp_port);

        if ( bind(rtp_s->transport.udp.rtp_sd, sa_p, sa_len) < 0 ) {
            xlog(LOG_ERR, "bind 4");
            neb_sa_set_port(sa_p, 0);
            if ( bind(rtp_s->transport.udp.rtp_sd, sa_p, sa_len) < 0 ) {
                xlog(LOG_ERR, "bind 5");
                goto error;
            }

            if ( getsockname(rtp_s->transport.udp.rtp_sd, sa_p, &sa_len) < 0 ) {
                xlog(LOG_ERR, "getsockname 3");
                goto error;
            }

            rtp_port = neb_sa_get_port(sa_p);
        }

        break;
    }

    rtp_s->transport.udp.rtp_sa = g_slice_copy(sa_len, rtsp->peer_sa);
    neb_sa_set_port(rtp_s->transport.udp.rtp_sa, parsed->rtp_channel);

    if ( connect(rtp_s->transport.udp.rtp_sd,
                 rtp_s->transport.udp.rtp_sa,
                 sa_len) < 0 ) {
        xlog(LOG_ERR, "connect 1");
        goto error;
    }

    rtp_s->transport.udp.rtcp_sa = g_slice_copy(sa_len, rtsp->peer_sa);
    neb_sa_set_port(rtp_s->transport.udp.rtcp_sa, parsed->rtcp_channel);

    if ( connect(rtp_s->transport.udp.rtcp_sd,
                 rtp_s->transport.udp.rtcp_sa,
                 sa_len) < 0 ) {
        xlog(LOG_ERR, "connect 2");
        goto error;
    }

    io->data = rtp_s;
    ev_io_init(io, rtcp_udp_read_cb,
               rtp_s->transport.udp.rtcp_sd, EV_READ);

    rtp_s->send_rtp = rtp_udp_send_rtp;
    rtp_s->send_rtcp = rtp_udp_send_rtcp;
    rtp_s->close_transport = rtp_udp_close_transport;

    source = neb_sa_get_host((struct sockaddr*) &sa);

    rtp_s->transport_string = g_strdup_printf("RTP/AVP;unicast;source=%s;client_port=%d-%d;server_port=%d-%d;ssrc=%08X",
                                              rtsp->local_host,
                                              parsed->rtp_channel,
                                              parsed->rtcp_channel,
                                              rtp_port,
                                              rtcp_port,
                                              rtp_s->ssrc);

    free(source);

    return true;

 error:
    if ( rtp_s->transport.udp.rtp_sa != NULL )
        g_slice_free1(sa_len, rtp_s->transport.udp.rtp_sa);
    if ( rtp_s->transport.udp.rtcp_sa != NULL )
        g_slice_free1(sa_len, rtp_s->transport.udp.rtcp_sa);
    if ( firstsd >= 0 )
        close(firstsd);
    if ( rtp_s->transport.udp.rtp_sd >= 0 )
        close(rtp_s->transport.udp.rtp_sd);
    if ( rtp_s->transport.udp.rtcp_sd >= 0 )
        close(rtp_s->transport.udp.rtcp_sd);
    return false;
}
Beispiel #12
0
void _draw(JSValueRef canvas, double dest_width, double dest_height)
{
    static gboolean not_draw = FALSE;

    if (recognition_info.reco_state == RECOGNIZING) {
        /* g_debug("[%s] recognizing", __func__); */
        return;
    }

    if (!recognition_info.has_data) {
        g_debug("[%s] get no data from camera", __func__);
        return;
    }

    if (JSValueIsNull(get_global_context(), canvas)) {
        g_debug("[%s] draw with null canvas!", __func__);
        return;
    }

    if (recognition_info.source_data == NULL) {
        g_debug("[%s] source_data is null", __func__);
        return;
    }

    g_debug("[%s]", __func__);

    cairo_t* cr = fetch_cairo_from_html_canvas(get_global_context(), canvas);
    g_assert(cr != NULL);
    cairo_save(cr);

    gulong len = recognition_info.length;
    guchar* data = g_slice_copy(len, recognition_info.source_data);
    GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data(data,
                                                 GDK_COLORSPACE_RGB,  // color space
                                                 FALSE,  // has alpha
                                                 8,  // bits per sample
                                                 CAMERA_WIDTH,  // width
                                                 CAMERA_HEIGHT,  // height
                                                 3*CAMERA_WIDTH,  // row stride
                                                 destroy_pixels,  // destroy function
                                                 GINT_TO_POINTER(len)  // destroy function data
                                                );

    double scale = 0;
    if (CAMERA_WIDTH > CAMERA_HEIGHT) {
        scale = dest_height/CAMERA_HEIGHT;
        cairo_scale(cr, scale, scale);
        gdk_cairo_set_source_pixbuf(cr, pixbuf, 0.5 * (dest_width / scale -
                                                       CAMERA_WIDTH), 0);
    } else {
        scale = dest_width/CAMERA_WIDTH;
        cairo_scale(cr, scale, scale);
        gdk_cairo_set_source_pixbuf(cr, pixbuf, 0, 0.5 * (dest_height / scale -
                                                          CAMERA_HEIGHT));
    }

    cairo_paint(cr);
    cairo_restore(cr);

    canvas_custom_draw_did(cr, NULL);
    g_object_unref(pixbuf);

    recognition_info.has_data = FALSE;
}
Beispiel #13
0
/**
 * @brief Handle an incoming RTSP connection
 *
 * @param loop The event loop where the incoming connection was triggered
 * @param w The watcher that triggered the incoming connection
 * @param revents Unused
 *
 * This function takes care of all the handling of an incoming RTSP
 * client connection:
 *
 * @li accept the new socket;
 *
 * @li checks that there is space for new connections for the current
 *     fork;
 *
 * @li creates and sets up the @ref RTSP_Client object.
 *
 * The newly created instance is deleted by @ref
 * client_loop at the end of the processing
 *
 * @internal This function should be used as callback for an ev_io
 *           listener.
 */
void rtsp_client_incoming_cb(ATTR_UNUSED struct ev_loop *loop, ev_io *w,
                             ATTR_UNUSED int revents)
{
    feng_socket_listener *listen = w->data;
    int client_sd = -1, sock_proto;
    struct sockaddr_storage peer, bound;
    socklen_t peer_len = sizeof(struct sockaddr_storage),
        bound_len = sizeof(struct sockaddr_storage);

    RTSP_Client *rtsp;

    if ( (client_sd = accept(listen->fd, (struct sockaddr*)&peer, &peer_len)) < 0 ) {
        fnc_perror("accept failed");
        return;
    }

    if ( getsockname(client_sd, (struct sockaddr*)&bound, &bound_len) < 0 ) {
        fnc_perror("getsockname");
        goto error;
    }

    feng_assert_or_goto(peer_len == bound_len, error);

#if ENABLE_SCTP
    bound_len = sizeof(int);
    if ( getsockopt(client_sd, SOL_SOCKET, SO_PROTOCOL, &sock_proto, &bound_len) < 0 ) {
        fnc_perror("getsockopt");
        goto error;
    }
#else
    sock_proto = IPPROTO_TCP;
#endif

    fnc_log(FNC_LOG_INFO, "Incoming connection accepted on socket: %d",
            client_sd);

    rtsp = g_slice_new0(RTSP_Client);
    rtsp->input = g_byte_array_new();
    rtsp->sd = client_sd;

    rtsp->loop = ev_loop_new(EVFLAG_AUTO);

    switch (sock_proto) {
    case IPPROTO_TCP:
        rtsp->socktype = RTSP_TCP;
        rtsp->out_queue = g_queue_new();
        rtsp->write_data = rtsp_write_data_queue;
        break;
#if ENABLE_SCTP
    case IPPROTO_SCTP:
        rtsp->socktype = RTSP_SCTP;
        rtsp->write_data = rtsp_sctp_send_rtsp;
        break;
#endif
    default:
        fnc_log(FNC_LOG_ERR, "Invalid socket protocol: %d", sock_proto);
    }

    rtsp->vhost = feng_default_vhost;

    rtsp->local_host = neb_sa_get_host((struct sockaddr*)&bound);
    rtsp->remote_host = neb_sa_get_host((struct sockaddr*)&peer);

    rtsp->sa_len = peer_len;
    rtsp->peer_sa = g_slice_copy(peer_len, &peer);
    rtsp->local_sa = g_slice_copy(peer_len, &bound);

    rtsp->vhost->connection_count++;

    g_thread_pool_push(client_threads, rtsp, NULL);

    return;

 error:
    close(client_sd);
}