From af40a978c17343987b1f0fd9ececf9285cfdbcee Mon Sep 17 00:00:00 2001 From: Mike Brady Date: Tue, 27 May 2014 08:16:02 +0100 Subject: [PATCH] Begin adding 'device busy' status. --- rtsp.c | 122 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 73 insertions(+), 49 deletions(-) diff --git a/rtsp.c b/rtsp.c index c40a3d73..7c2398a3 100644 --- a/rtsp.c +++ b/rtsp.c @@ -52,6 +52,11 @@ #define INETx_ADDRSTRLEN INET_ADDRSTRLEN #endif +// Mike Brady's part... +static pthread_mutex_t play_lock = PTHREAD_MUTEX_INITIALIZER; +static int is_playing = 0; + + // only one thread is allowed to use the player at once. // it monitors the request variable (at least when interrupted) static pthread_mutex_t playing_mutex = PTHREAD_MUTEX_INITIALIZER; @@ -418,7 +423,7 @@ static void handle_setup(rtsp_conn_info *conn, if (config.userSuppliedLatency==0) { char * ua = msg_get_header(req,"User-Agent"); if (!ua) { - debug(1,"No User-Agent string found in the SETUP message and no user-supplied latency. Using AirP;ay latency of %d frames.",config.AirPlayLatency); + debug(1,"No User-Agent string found in the SETUP message and no user-supplied latency. Using AirPlay latency of %d frames.",config.AirPlayLatency); return; } if (strstr(ua,"iTunes")==ua) { @@ -469,6 +474,7 @@ static void handle_setup(rtsp_conn_info *conn, if (q++) strcat(hdr,q); // should unsplice the timing port entry } + player_play(&conn->stream); char *resphdr = malloc(200); @@ -515,60 +521,73 @@ static void handle_set_parameter(rtsp_conn_info *conn, static void handle_announce(rtsp_conn_info *conn, rtsp_message *req, rtsp_message *resp) { + if (pthread_mutex_trylock(&play_lock) == 0) { + char *paesiv = NULL; + char *prsaaeskey = NULL; + char *pfmtp = NULL; + char *cp = req->content; + int cp_left = req->contentlength; + char *next; + while (cp_left && cp) { + next = nextline(cp, cp_left); + cp_left -= next-cp; - char *paesiv = NULL; - char *prsaaeskey = NULL; - char *pfmtp = NULL; - char *cp = req->content; - int cp_left = req->contentlength; - char *next; - while (cp_left && cp) { - next = nextline(cp, cp_left); - cp_left -= next-cp; + if (!strncmp(cp, "a=fmtp:", 7)) + pfmtp = cp+7; - if (!strncmp(cp, "a=fmtp:", 7)) - pfmtp = cp+7; + if (!strncmp(cp, "a=aesiv:", 8)) + paesiv = cp+8; - if (!strncmp(cp, "a=aesiv:", 8)) - paesiv = cp+8; + if (!strncmp(cp, "a=rsaaeskey:", 12)) + prsaaeskey = cp+12; - if (!strncmp(cp, "a=rsaaeskey:", 12)) - prsaaeskey = cp+12; + cp = next; + } - cp = next; + if (!paesiv || !prsaaeskey || !pfmtp) { + warn("required params missing from announce"); + return; + } + + int len, keylen; + uint8_t *aesiv = base64_dec(paesiv, &len); + if (len != 16) { + warn("client announced aeskey of %d bytes, wanted 16", len); + free(aesiv); + return; + } + memcpy(conn->stream.aesiv, aesiv, 16); + free(aesiv); + + uint8_t *rsaaeskey = base64_dec(prsaaeskey, &len); + uint8_t *aeskey = rsa_apply(rsaaeskey, len, &keylen, RSA_MODE_KEY); + free(rsaaeskey); + if (keylen != 16) { + warn("client announced rsaaeskey of %d bytes, wanted 16", keylen); + free(aeskey); + return; + } + memcpy(conn->stream.aeskey, aeskey, 16); + free(aeskey); + + int i; + for (i=0; istream.fmtp)/sizeof(conn->stream.fmtp[0]); i++) + conn->stream.fmtp[i] = atoi(strsep(&pfmtp, " \t")); + + char *hdr = msg_get_header(req, "X-Apple-Client-Name"); + if (hdr) + debug(1,"Announcement from \"%s\".",hdr); + else { + hdr = msg_get_header(req, "User-Agent"); + if (hdr) + debug(1,"Announcement from \"%s\".",hdr); + } + + resp->respcode = 200; + } else { + resp->respcode = 453; + debug(1,"Already playing."); } - - if (!paesiv || !prsaaeskey || !pfmtp) { - warn("required params missing from announce"); - return; - } - - int len, keylen; - uint8_t *aesiv = base64_dec(paesiv, &len); - if (len != 16) { - warn("client announced aeskey of %d bytes, wanted 16", len); - free(aesiv); - return; - } - memcpy(conn->stream.aesiv, aesiv, 16); - free(aesiv); - - uint8_t *rsaaeskey = base64_dec(prsaaeskey, &len); - uint8_t *aeskey = rsa_apply(rsaaeskey, len, &keylen, RSA_MODE_KEY); - free(rsaaeskey); - if (keylen != 16) { - warn("client announced rsaaeskey of %d bytes, wanted 16", keylen); - free(aeskey); - return; - } - memcpy(conn->stream.aeskey, aeskey, 16); - free(aeskey); - - int i; - for (i=0; istream.fmtp)/sizeof(conn->stream.fmtp[0]); i++) - conn->stream.fmtp[i] = atoi(strsep(&pfmtp, " \t")); - - resp->respcode = 200; } @@ -769,7 +788,11 @@ static void *rtsp_conversation_thread_func(void *pconn) { struct method_handler *mh; for (mh=method_handlers; mh->method; mh++) { if (!strcmp(mh->method, req->method)) { + debug(1,"RTSP Packet received of type \"%s\":",mh->method), + msg_print_debug_headers(req); mh->handler(conn, req, resp); + debug(1,"RTSP Response:"); + msg_print_debug_headers(resp); break; } } @@ -786,6 +809,7 @@ respond: if (rtsp_playing()) { rtp_shutdown(); player_stop(); + pthread_mutex_unlock(&play_lock); please_shutdown = 0; pthread_mutex_unlock(&playing_mutex); }