summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Lucina <mato@kotelna.sk>2010-01-29 18:52:22 +0100
committerMartin Lucina <mato@kotelna.sk>2010-01-29 18:52:22 +0100
commit0693a17f258ce8f9ca5ac6fc3fcf9b632c8e9489 (patch)
tree552bf4cf83560e11b2bd687c0c5aeb8d7f33101e
parente7c4bbd6127f63a75edf6379f57d1db5dc66d427 (diff)
Change the inproc:// connection logic
An inproc:// endpoint must exist before we can connect to it. This change removes the silly sleep(1) hack to wait for the sender thread to come online and instead does the zmq_bind() in the main thread and the zmq_connect() in the sender thread.
-rw-r--r--zmq-camera.c66
1 files changed, 46 insertions, 20 deletions
diff --git a/zmq-camera.c b/zmq-camera.c
index a18026d..3be17c6 100644
--- a/zmq-camera.c
+++ b/zmq-camera.c
@@ -34,12 +34,20 @@
#define FOURCC(a,b,c,d) (unsigned int)((((unsigned int)d)<<24)+\
(((unsigned int)c)<<16)+(((unsigned int)b)<<8)+a)
+/* inproc endpoint used to provide the "local loopback" view of the camera */
+const char local_camera[] = "inproc://local-camera";
+
+/* Sender thread initialiser */
struct sender_args_t
{
const char *endpoint;
void *ctx;
};
+/*
+ * Sender thread
+ */
+
void *sender_thread (void *arg)
{
struct sender_args_t *sender_args;
@@ -55,8 +63,8 @@ void *sender_thread (void *arg)
sender_args = (struct sender_args_t *)arg;
- /* Create a ZMQ_PUB socket for sending video data and bind it to both
- an inproc endpoint for the local loopback view and the endpoint
+ /* Create a ZMQ_PUB socket for sending video data and connect it to both
+ an inproc endpoint for the local loopback view and bind to the endpoint
specified by the user. */
s = zmq_socket (sender_args->ctx, ZMQ_PUB);
assert (s);
@@ -66,9 +74,9 @@ void *sender_thread (void *arg)
zmq_strerror (errno));
exit (1);
}
- rc = zmq_bind (s, "inproc://local-camera");
+ rc = zmq_connect (s, local_camera);
if (rc != 0) {
- fprintf (stderr, "zmq_bind (\"inproc://local-camera\"): %s\n",
+ fprintf (stderr, "zmq_connect (\"%s\"): %s\n", local_camera,
zmq_strerror (errno));
exit (1);
}
@@ -191,6 +199,10 @@ void *sender_thread (void *arg)
return NULL;
}
+/*
+ * Print usage and exit.
+ */
+
void err_usage(void)
{
const char usage[] = \
@@ -211,6 +223,10 @@ void err_usage(void)
exit (1);
}
+/*
+ * Main thread
+ */
+
int main (int argc, char *argv [])
{
void *ctx, *s;
@@ -239,29 +255,36 @@ int main (int argc, char *argv [])
ctx = zmq_init (2, 1, 0);
assert (ctx);
- /* If sending video, start the sender thread */
+ /* Create a ZMQ_SUB socket to receive video data. If we're sending video,
+ bind to an inproc: endpoint that the sender thread will connect to,
+ otherwise connect to the endpoint the user specified. */
+ s = zmq_socket (ctx, ZMQ_SUB);
+ assert (s);
if (is_sender) {
+ rc = zmq_bind (s, local_camera);
+ if (rc != 0) {
+ fprintf (stderr, "zmq_bind (\"%s\"): %s\n", local_camera,
+ zmq_strerror (errno));
+ exit (1);
+ }
+
+ /* Start the sender thread after binding to the inproc: endpoint
+ since this must exist for it to connect. */
struct sender_args_t sender_args = {endpoint, ctx};
pthread_t sender;
rc = pthread_create (&sender, NULL, sender_thread,
(void*) &sender_args);
assert (rc == 0);
- /* XXX */ sleep (1);
+ }
+ else {
+ rc = zmq_connect (s, endpoint);
+ if (rc != 0) {
+ fprintf (stderr, "zmq_connect (\"%s\"): %s\n", endpoint,
+ zmq_strerror (errno));
+ exit (1);
+ }
}
- /* Create a ZMQ_SUB socket to receive video data. If we're also sending,
- bind to an inproc: endpoint to get the video feed, otherwise bind to
- the endpoint the user specified. */
- s = zmq_socket (ctx, ZMQ_SUB);
- assert (s);
- if (is_sender)
- endpoint = "inproc://local-camera";
- rc = zmq_connect (s, endpoint);
- if (rc != 0) {
- fprintf (stderr, "zmq_connect (\"%s\"): %s\n", endpoint,
- zmq_strerror (errno));
- exit (1);
- }
/* Subscribe to all messages on socket. */
rc = zmq_setsockopt (s, ZMQ_SUBSCRIBE, "", 0);
assert (rc == 0);
@@ -317,7 +340,10 @@ int main (int argc, char *argv [])
exit (1);
}
/* Set window title to endpoint we are receiving from */
- SDL_WM_SetCaption (endpoint, endpoint);
+ if (is_sender)
+ SDL_WM_SetCaption (local_camera, local_camera);
+ else
+ SDL_WM_SetCaption (endpoint, endpoint);
sdl_initialised = 1;
}