1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
| #pragma comment(lib, "ws2_32.lib") #pragma pack(1)
typedef struct RTP_FIXED_HEADER{ unsigned char csrc_len:4; unsigned char extension:1; unsigned char padding:1; unsigned char version:2; unsigned char payload:7; unsigned char marker:1; unsigned short seq_no; unsigned long timestamp; unsigned long ssrc; } RTP_FIXED_HEADER; typedef struct MPEGTS_FIXED_HEADER { unsigned sync_byte: 8; unsigned transport_error_indicator: 1; unsigned payload_unit_start_indicator: 1; unsigned transport_priority: 1; unsigned PID: 13; unsigned scrambling_control: 2; unsigned adaptation_field_exist: 2; unsigned continuity_counter: 4; } MPEGTS_FIXED_HEADER; int simplest_udp_parser(int port) { WSADATA wsaData; WORD sockVersion = MAKEWORD(2,2); int cnt=0; FILE *myout=stdout; FILE *fp1=fopen("output_dump.ts","wb+"); if(WSAStartup(sockVersion, &wsaData) != 0){ return 0; } SOCKET serSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if(serSocket == INVALID_SOCKET){ printf("socket error !"); return 0; } sockaddr_in serAddr; serAddr.sin_family = AF_INET; serAddr.sin_port = htons(port); serAddr.sin_addr.S_un.S_addr = INADDR_ANY; if(bind(serSocket, (sockaddr *)&serAddr, sizeof(serAddr)) == SOCKET_ERROR){ printf("bind error !"); closesocket(serSocket); return 0; } sockaddr_in remoteAddr; int nAddrLen = sizeof(remoteAddr); int parse_rtp=1; int parse_mpegts=1; printf("Listening on port %d\n",port); char recvData[10000]; while (1){ int pktsize = recvfrom(serSocket, recvData, 10000, 0, (sockaddr *)&remoteAddr, &nAddrLen); if (pktsize > 0){ if(parse_rtp!=0){ char payload_str[10]={0}; RTP_FIXED_HEADER rtp_header; int rtp_header_size=sizeof(RTP_FIXED_HEADER); memcpy((void *)&rtp_header,recvData,rtp_header_size); char payload=rtp_header.payload; switch(payload){ case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17: case 18: sprintf(payload_str,"Audio");break; case 31: sprintf(payload_str,"H.261");break; case 32: sprintf(payload_str,"MPV");break; case 33: sprintf(payload_str,"MP2T");break; case 34: sprintf(payload_str,"H.263");break; case 96: sprintf(payload_str,"H.264");break; default:sprintf(payload_str,"other");break; } unsigned int timestamp=ntohl(rtp_header.timestamp); unsigned int seq_no=ntohs(rtp_header.seq_no); fprintf(myout,"[RTP Pkt] %5d| %5s| %10u| %5d| %5d|\n",cnt,payload_str,timestamp,seq_no,pktsize); char *rtp_data=recvData+rtp_header_size; int rtp_data_size=pktsize-rtp_header_size; fwrite(rtp_data,rtp_data_size,1,fp1); if(parse_mpegts!=0&&payload==33){ MPEGTS_FIXED_HEADER mpegts_header; for(int i=0;i<rtp_data_size;i=i+188){ if(rtp_data[i]!=0x47) break; fprintf(myout," [MPEGTS Pkt]\n"); } } }else{ fprintf(myout,"[UDP Pkt] %5d| %5d|\n",cnt,pktsize); fwrite(recvData,pktsize,1,fp1); } cnt++; } } closesocket(serSocket); WSACleanup(); fclose(fp1); return 0; }
|