static inline int rtsp_read_request(AVFormatContext *s, RTSPMessageHeader *request, const char *method) { RTSPState *rt = s->priv_data; char rbuf[1024]; int rbuflen, ret; do { ret = read_line(s, rbuf, sizeof(rbuf), &rbuflen); if (ret) return ret; if (rbuflen > 1) { av_dlog(s, "Parsing[%d]: %s\n", rbuflen, rbuf); ff_rtsp_parse_line(request, rbuf, rt, method); } } while (rbuflen > 0); if (request->seq != rt->seq + 1) { av_log(s, AV_LOG_ERROR, "Unexpected Sequence number %d\n", request->seq); return AVERROR(EINVAL); } if (rt->session_id[0] && strcmp(method, "OPTIONS")) { ret = check_sessionid(s, request); if (ret) return ret; } return 0; }
void* rtspserver(void *arg) { #ifdef WIN32 SOCKET s = *((SOCKET*) arg); int sinlen = sizeof(struct sockaddr_in); #else int s = *((int*) arg); socklen_t sinlen = sizeof(struct sockaddr_in); #endif const char *p; char buf[8192]; char cmd[32], url[1024], protocol[32]; int rlen; struct sockaddr_in sin; RTSPContext ctx; RTSPMessageHeader header1, *header = &header1; int thread_ret; // image info int iwidth = video_source_width(0); int iheight = video_source_height(0); // rtspconf = rtspconf_global(); sinlen = sizeof(sin); getpeername(s, (struct sockaddr*) &sin, &sinlen); // bzero(&ctx, sizeof(ctx)); if(per_client_init(&ctx) < 0) { ga_error("server initialization failed.\n"); return NULL; } ctx.state = SERVER_STATE_IDLE; // XXX: hasVideo is used to sync audio/video // This value is increased by 1 for each captured frame until it is gerater than zero // when this value is greater than zero, audio encoding then starts ... //ctx.hasVideo = -(rtspconf->video_fps>>1); // for slow encoders? ctx.hasVideo = 0; // with 'zerolatency' pthread_mutex_init(&ctx.rtsp_writer_mutex, NULL); #if 0 ctx.audioparam.channels = rtspconf->audio_channels; ctx.audioparam.samplerate = rtspconf->audio_samplerate; if(rtspconf->audio_device_format == AV_SAMPLE_FMT_S16) { #ifdef WIN32 #else ctx.audioparam.format = SND_PCM_FORMAT_S16_LE; #endif ctx.audioparam.bits_per_sample = 16; } // ga_error("INFO: image: %dx%d; audio: %d ch 16-bit pcm @ %dHz\n", iwidth, iheight, ctx.audioparam.channels, ctx.audioparam.samplerate); #endif // #if 0 #ifdef WIN32 if(ga_wasapi_init(&ctx.audioparam) < 0) { ga_error("cannot init wasapi.\n"); return NULL; } #else if((ctx.audioparam.handle = ga_alsa_init(&ctx.audioparam.sndlog)) == NULL) { ga_error("cannot init alsa.\n"); return NULL; } if(ga_alsa_set_param(&ctx.audioparam) < 0) { ga_error("cannot set alsa parameter\n"); return NULL; } #endif #endif // ga_error("[tid %ld] client connected from %s:%d\n", ga_gettid(), inet_ntoa(sin.sin_addr), htons(sin.sin_port)); // ctx.fd = s; // do { fd_set rfds; FD_ZERO(&rfds); FD_SET(ctx.fd, &rfds); if(select(ctx.fd+1, &rfds, NULL, NULL, NULL) <=0) { ga_error("select() failed: %s\n", strerror(errno)); goto quit; } // read commands if((rlen = rtsp_getnext(&ctx, buf, sizeof(buf))) < 0) { goto quit; } // Interleaved binary data? if(buf[0] == '$') { handle_rtcp(&ctx, buf, rlen); continue; } // REQUEST line ga_error("%s", buf); p = buf; get_word(cmd, sizeof(cmd), &p); get_word(url, sizeof(url), &p); get_word(protocol, sizeof(protocol), &p); // check protocol if(strcmp(protocol, "RTSP/1.0") != 0) { rtsp_reply_error(&ctx, RTSP_STATUS_VERSION); goto quit; } // read headers bzero(header, sizeof(*header)); do { int myseq = -1; char mysession[sizeof(header->session_id)] = ""; if((rlen = rtsp_getnext(&ctx, buf, sizeof(buf))) < 0) goto quit; if(buf[0]=='\n' || (buf[0]=='\r' && buf[1]=='\n')) break; #if 0 ga_error("HEADER: %s", buf); #endif // Special handling to CSeq & Session header // ff_rtsp_parse_line cannot handle CSeq & Session properly on Windows // any more? if(strncasecmp("CSeq: ", buf, 6) == 0) { myseq = strtol(buf+6, NULL, 10); } if(strncasecmp("Session: ", buf, 9) == 0) { strcpy(mysession, buf+9); } // ff_rtsp_parse_line(header, buf, NULL, NULL); // if(myseq > 0 && header->seq <= 0) { ga_error("WARNING: CSeq fixes applied (%d->%d).\n", header->seq, myseq); header->seq = myseq; } if(mysession[0] != '\0' && header->session_id[0]=='\0') { unsigned i; for(i = 0; i < sizeof(header->session_id)-1; i++) { if(mysession[i] == '\0' || isspace(mysession[i]) || mysession[i] == ';') break; header->session_id[i] = mysession[i]; } header->session_id[i+1] = '\0'; ga_error("WARNING: Session fixes applied (%s)\n", header->session_id); } } while(1); // special handle to session_id if(header->session_id != NULL) { char *p = header->session_id; while(*p != '\0') { if(*p == '\r' || *p == '\n') { *p = '\0'; break; } p++; } } // handle commands ctx.seq = header->seq; if (!strcmp(cmd, "DESCRIBE")) rtsp_cmd_describe(&ctx, url); else if (!strcmp(cmd, "OPTIONS")) rtsp_cmd_options(&ctx, url); else if (!strcmp(cmd, "SETUP")) rtsp_cmd_setup(&ctx, url, header); else if (!strcmp(cmd, "PLAY")) rtsp_cmd_play(&ctx, url, header); else if (!strcmp(cmd, "PAUSE")) rtsp_cmd_pause(&ctx, url, header); else if (!strcmp(cmd, "TEARDOWN")) rtsp_cmd_teardown(&ctx, url, header); else rtsp_reply_error(&ctx, RTSP_STATUS_METHOD); if(ctx.state == SERVER_STATE_TEARDOWN) { break; } } while(1); quit: ctx.state = SERVER_STATE_TEARDOWN; // close(ctx.fd); #ifdef SHARE_ENCODER encoder_unregister_client(&ctx); #else ga_error("connection closed, checking for worker threads...\n"); #if 0 // if(ctx.vthreadId != 0) { video_source_notify_one(ctx.vthreadId); } #endif pthread_join(ctx.vthread, (void**) &thread_ret); #ifdef ENABLE_AUDIO pthread_join(ctx.athread, (void**) &thread_ret); #endif /* ENABLE_AUDIO */ #endif /* SHARE_ENCODER */ // per_client_deinit(&ctx); //ga_error("RTSP client thread terminated (%d/%d clients left).\n", // video_source_client_count(), audio_source_client_count()); ga_error("RTSP client thread terminated.\n"); // return NULL; }
void* rtspserver(void *arg) { #ifdef WIN32 SOCKET s = *((SOCKET*) arg); int sinlen = sizeof(struct sockaddr_in); #else int s = *((int*) arg); socklen_t sinlen = sizeof(struct sockaddr_in); #endif const char *p; char buf[8192]; char cmd[32], url[1024], protocol[32]; int rlen; struct sockaddr_in sin; RTSPContext ctx; RTSPMessageHeader header1, *header = &header1; //int thread_ret; // image info //int iwidth = video_source_maxwidth(0); //int iheight = video_source_maxheight(0); // rtspconf = rtspconf_global(); sinlen = sizeof(sin); getpeername(s, (struct sockaddr*) &sin, &sinlen); // bzero(&ctx, sizeof(ctx)); if(per_client_init(&ctx) < 0) { ga_error("server initialization failed.\n"); return NULL; } bcopy(&sin, &ctx.client, sizeof(ctx.client)); ctx.state = SERVER_STATE_IDLE; // XXX: hasVideo is used to sync audio/video // This value is increased by 1 for each captured frame until it is gerater than zero // when this value is greater than zero, audio encoding then starts ... //ctx.hasVideo = -(rtspconf->video_fps>>1); // for slow encoders? ctx.hasVideo = 0; // with 'zerolatency' pthread_mutex_init(&ctx.rtsp_writer_mutex, NULL); // ga_error("[tid %ld] client connected from %s:%d\n", ga_gettid(), inet_ntoa(sin.sin_addr), htons(sin.sin_port)); // ctx.fd = s; // do { int i, fdmax, active; fd_set rfds; struct timeval to; FD_ZERO(&rfds); FD_SET(ctx.fd, &rfds); fdmax = ctx.fd; #ifdef HOLE_PUNCHING for(i = 0; i < 2*ctx.streamCount; i++) { FD_SET(ctx.rtpSocket[i], &rfds); if(ctx.rtpSocket[i] > fdmax) fdmax = ctx.rtpSocket[i]; } #endif to.tv_sec = 0; to.tv_usec = 500000; if((active = select(fdmax+1, &rfds, NULL, NULL, &to)) < 0) { ga_error("select() failed: %s\n", strerror(errno)); goto quit; } if(active == 0) { // try again! continue; } #ifdef HOLE_PUNCHING for(i = 0; i < 2*ctx.streamCount; i++) { struct sockaddr_in xsin; #ifdef WIN32 int xsinlen = sizeof(xsin); #else socklen_t xsinlen = sizeof(xsin); #endif if(FD_ISSET(ctx.rtpSocket[i], &rfds) == 0) continue; recvfrom(ctx.rtpSocket[i], buf, sizeof(buf), 0, (struct sockaddr*) &xsin, &xsinlen); if(ctx.rtpPortChecked[i] != 0) continue; // XXX: port should not flip-flop, so check only once if(xsin.sin_addr.s_addr != ctx.client.sin_addr.s_addr) { ga_error("RTP: client address mismatched? %u.%u.%u.%u != %u.%u.%u.%u\n", NIPQUAD(ctx.client.sin_addr.s_addr), NIPQUAD(xsin.sin_addr.s_addr)); continue; } if(xsin.sin_port != ctx.rtpPeerPort[i]) { ga_error("RTP: client port reconfigured: %u -> %u\n", (unsigned int) ntohs(ctx.rtpPeerPort[i]), (unsigned int) ntohs(xsin.sin_port)); ctx.rtpPeerPort[i] = xsin.sin_port; } else { ga_error("RTP: client is not under an NAT, port %d confirmed\n", (int) ntohs(ctx.rtpPeerPort[i])); } ctx.rtpPortChecked[i] = 1; } // is RTSP connection? if(FD_ISSET(ctx.fd, &rfds) == 0) continue; #endif // read commands if((rlen = rtsp_getnext(&ctx, buf, sizeof(buf))) < 0) { goto quit; } // Interleaved binary data? if(buf[0] == '$') { handle_rtcp(&ctx, buf, rlen); continue; } // REQUEST line ga_error("%s", buf); p = buf; get_word(cmd, sizeof(cmd), &p); get_word(url, sizeof(url), &p); get_word(protocol, sizeof(protocol), &p); // check protocol if(strcmp(protocol, "RTSP/1.0") != 0) { rtsp_reply_error(&ctx, RTSP_STATUS_VERSION); goto quit; } // read headers bzero(header, sizeof(*header)); do { int myseq = -1; char mysession[sizeof(header->session_id)] = ""; if((rlen = rtsp_getnext(&ctx, buf, sizeof(buf))) < 0) goto quit; if(buf[0]=='\n' || (buf[0]=='\r' && buf[1]=='\n')) break; #if 0 ga_error("HEADER: %s", buf); #endif // Special handling to CSeq & Session header // ff_rtsp_parse_line cannot handle CSeq & Session properly on Windows // any more? if(strncasecmp("CSeq: ", buf, 6) == 0) { myseq = strtol(buf+6, NULL, 10); } if(strncasecmp("Session: ", buf, 9) == 0) { strcpy(mysession, buf+9); } // ff_rtsp_parse_line(header, buf, NULL, NULL); // if(myseq > 0 && header->seq <= 0) { ga_error("WARNING: CSeq fixes applied (%d->%d).\n", header->seq, myseq); header->seq = myseq; } if(mysession[0] != '\0' && header->session_id[0]=='\0') { unsigned i; for(i = 0; i < sizeof(header->session_id)-1; i++) { if(mysession[i] == '\0' || isspace(mysession[i]) || mysession[i] == ';') break; header->session_id[i] = mysession[i]; } header->session_id[i+1] = '\0'; ga_error("WARNING: Session fixes applied (%s)\n", header->session_id); } } while(1); // special handle to session_id if(header->session_id != NULL) { char *p = header->session_id; while(*p != '\0') { if(*p == '\r' || *p == '\n') { *p = '\0'; break; } p++; } } // handle commands ctx.seq = header->seq; if (!strcmp(cmd, "DESCRIBE")) rtsp_cmd_describe(&ctx, url); else if (!strcmp(cmd, "OPTIONS")) rtsp_cmd_options(&ctx, url); else if (!strcmp(cmd, "SETUP")) rtsp_cmd_setup(&ctx, url, header); else if (!strcmp(cmd, "PLAY")) rtsp_cmd_play(&ctx, url, header); else if (!strcmp(cmd, "PAUSE")) rtsp_cmd_pause(&ctx, url, header); else if (!strcmp(cmd, "TEARDOWN")) rtsp_cmd_teardown(&ctx, url, header, 1); else rtsp_reply_error(&ctx, RTSP_STATUS_METHOD); if(ctx.state == SERVER_STATE_TEARDOWN) { break; } } while(1); quit: ctx.state = SERVER_STATE_TEARDOWN; // close(ctx.fd); // 2014-05-20: support only share-encoder model ff_server_unregister_client(&ctx); // per_client_deinit(&ctx); //ga_error("RTSP client thread terminated (%d/%d clients left).\n", // video_source_client_count(), audio_source_client_count()); ga_error("RTSP client thread terminated.\n"); // return NULL; }
static int rtsp_parse_request(RTSPContext *c) { const char *p, *p1, *p2; char cmd[32]; char url[1024]; char protocol[32]; char line[1024]; int len; RTSPMessageHeader header1 = { 0 }, *header = &header1; c->buffer_ptr[0] = '\0'; p = c->buffer; get_word(cmd, sizeof(cmd), &p); get_word(url, sizeof(url), &p); get_word(protocol, sizeof(protocol), &p); av_strlcpy(c->method, cmd, sizeof(c->method)); av_strlcpy(c->url, url, sizeof(c->url)); av_strlcpy(c->protocol, protocol, sizeof(c->protocol)); if (avio_open_dyn_buf(&c->pb) < 0) { // XXX: cannot do more c->pb = NULL;// safety return -1; } printf("cmd = %s \n", cmd); /* check version name */ if (strcmp(protocol, "RTSP/1.0") != 0) { rtsp_reply_error(c, RTSP_STATUS_VERSION); goto the_end; } /* parse each header line */ /* skip to next line */ while (*p != '\n' && *p != '\0') p++; if (*p == '\n') p++; while (*p != '\0') { p1 = memchr(p, '\n', (char *)c->buffer_ptr - p); if (!p1) break; p2 = p1; if (p2 > p && p2[-1] == '\r') p2--; /* skip empty line */ if (p2 == p) break; len = p2 - p; if (len > sizeof(line) - 1) len = sizeof(line) - 1; memcpy(line, p, len); line[len] = '\0'; //printf("rstp_parse_line = %s\n", line); // range parsing! if (!strncmp(line, "Range: npt=", strlen("Range: npt="))) { printf("range info contained! line = %s \n", line); char *end_pos = strrchr(line, '-'); char *start_pos = strrchr(line, '='); char *start_npt = malloc(end_pos - start_pos - 1); strncpy(start_npt, start_pos+1, end_pos - start_pos - 1); //printf("range start npt = %s\n", start_npt); double start_time = 0.0; sscanf(start_npt, "%lf", &start_time); //printf("range start time = %lfs\n", start_time); c->ntp_start_time = start_time; } // for trick if (!strncmp(line, "Scale:", strlen("Scale:"))) { printf("scale info contained! line = %s \n", line); char *end_pos = strrchr(line, '\n'); char *start_pos = strrchr(line, ' '); char *scale = malloc(end_pos - start_pos - 1); strncpy(scale, start_pos+1, end_pos - start_pos - 1); printf("scale = %s\n", scale); double speed = 0.0; sscanf(scale, "%lf", &speed); } ff_rtsp_parse_line(header, line, NULL, NULL); p = p1 + 1; } /* handle sequence number */ c->seq = header->seq; if (!strcmp(cmd, "DESCRIBE")) { printf("DESCRIBE!!!\n"); rtsp_cmd_describe(c, url); } else if (!strcmp(cmd, "OPTIONS")) { printf("Options! \n"); rtsp_cmd_options(c, url); } else if (!strcmp(cmd, "SETUP")) { printf("Setup!!!\n"); rtsp_cmd_setup(c, url, header); } else if (!strcmp(cmd, "PLAY")) { printf("Play!!! \n"); rtsp_cmd_play(c, url, header); } else if (!strcmp(cmd, "PAUSE")) { printf("PAUSE!!!\n"); rtsp_cmd_interrupt(c, url, header, 1); } else if (!strcmp(cmd, "TEARDOWN")) { rtsp_cmd_interrupt(c, url, header, 0); printf("TEARDOWN!!!\n"); } else { rtsp_reply_error(c, RTSP_STATUS_METHOD); printf("Error!!"); } the_end: // write rstp reply packet! text len = avio_close_dyn_buf(c->pb, &c->pb_buffer); c->pb = NULL; /* safety */ if (len < 0) { /* XXX: cannot do more */ printf("error! len < 0 when avio close dyn buf"); return -1; } c->buffer_ptr = c->pb_buffer; c->buffer_end = c->pb_buffer + len; c->state = RTSPSTATE_SEND_REPLY; return 0; }