winlin

refine code, add human readable section

@@ -701,23 +701,6 @@ int srs_publish_stream(srs_rtmp_t rtmp) @@ -701,23 +701,6 @@ int srs_publish_stream(srs_rtmp_t rtmp)
701 return ret; 701 return ret;
702 } 702 }
703 703
704 -const char* srs_type2string(char type)  
705 -{  
706 - static const char* audio = "Audio";  
707 - static const char* video = "Video";  
708 - static const char* data = "Data";  
709 - static const char* unknown = "Unknown";  
710 -  
711 - switch (type) {  
712 - case SRS_RTMP_TYPE_AUDIO: return audio;  
713 - case SRS_RTMP_TYPE_VIDEO: return video;  
714 - case SRS_RTMP_TYPE_SCRIPT: return data;  
715 - default: return unknown;  
716 - }  
717 -  
718 - return unknown;  
719 -}  
720 -  
721 int srs_bandwidth_check(srs_rtmp_t rtmp, 704 int srs_bandwidth_check(srs_rtmp_t rtmp,
722 int64_t* start_time, int64_t* end_time, 705 int64_t* start_time, int64_t* end_time,
723 int* play_kbps, int* publish_kbps, 706 int* play_kbps, int* publish_kbps,
@@ -948,29 +931,6 @@ char srs_get_codec_id(char* data, int size) @@ -948,29 +931,6 @@ char srs_get_codec_id(char* data, int size)
948 return codec_id; 931 return codec_id;
949 } 932 }
950 933
951 -const char* srs_code_id2string(char codec_id)  
952 -{  
953 - static const char* h263 = "H.263";  
954 - static const char* screen = "Screen";  
955 - static const char* vp6 = "VP6";  
956 - static const char* vp6_alpha = "VP6Alpha";  
957 - static const char* screen2 = "Screen2";  
958 - static const char* h264 = "H.264";  
959 - static const char* unknown = "Unknown";  
960 -  
961 - switch (codec_id) {  
962 - case 2: return h263;  
963 - case 3: return screen;  
964 - case 4: return vp6;  
965 - case 5: return vp6_alpha;  
966 - case 6: return screen2;  
967 - case 7: return h264;  
968 - default: return unknown;  
969 - }  
970 -  
971 - return unknown;  
972 -}  
973 -  
974 char srs_get_avc_packet_type(char* data, int size) 934 char srs_get_avc_packet_type(char* data, int size)
975 { 935 {
976 if (size < 2) { 936 if (size < 2) {
@@ -990,23 +950,6 @@ char srs_get_avc_packet_type(char* data, int size) @@ -990,23 +950,6 @@ char srs_get_avc_packet_type(char* data, int size)
990 return avc_packet_type; 950 return avc_packet_type;
991 } 951 }
992 952
993 -const char* srs_avc_packet2string(char avc_packet_type)  
994 -{  
995 - static const char* sps_pps = "SpsPps";  
996 - static const char* nalu = "Nalu";  
997 - static const char* sps_pps_end = "SpsPpsEnd";  
998 - static const char* unknown = "Unknown";  
999 -  
1000 - switch (avc_packet_type) {  
1001 - case 0: return sps_pps;  
1002 - case 1: return nalu;  
1003 - case 2: return sps_pps_end;  
1004 - default: return unknown;  
1005 - }  
1006 -  
1007 - return unknown;  
1008 -}  
1009 -  
1010 char srs_get_frame_type(char* data, int size) 953 char srs_get_frame_type(char* data, int size)
1011 { 954 {
1012 if (size < 1) { 955 if (size < 1) {
@@ -1026,101 +969,6 @@ char srs_get_frame_type(char* data, int size) @@ -1026,101 +969,6 @@ char srs_get_frame_type(char* data, int size)
1026 return frame_type; 969 return frame_type;
1027 } 970 }
1028 971
1029 -const char* srs_frame_type2string(char frame_type)  
1030 -{  
1031 - static const char* keyframe = "I";  
1032 - static const char* interframe = "P/B";  
1033 - static const char* disposable_interframe = "DI";  
1034 - static const char* generated_keyframe = "GI";  
1035 - static const char* video_infoframe = "VI";  
1036 - static const char* unknown = "Unknown";  
1037 -  
1038 - switch (frame_type) {  
1039 - case 1: return keyframe;  
1040 - case 2: return interframe;  
1041 - case 3: return disposable_interframe;  
1042 - case 4: return generated_keyframe;  
1043 - case 5: return video_infoframe;  
1044 - default: return unknown;  
1045 - }  
1046 -  
1047 - return unknown;  
1048 -}  
1049 -  
1050 -int srs_print_rtmp_packet(char type, u_int32_t timestamp, char* data, int size)  
1051 -{  
1052 - int ret = ERROR_SUCCESS;  
1053 -  
1054 - u_int32_t pts;  
1055 - if (srs_parse_timestamp(timestamp, type, data, size, &pts) != 0) {  
1056 - return ret;  
1057 - }  
1058 -  
1059 - if (type == SRS_RTMP_TYPE_VIDEO) {  
1060 - srs_lib_trace("Video packet type=%s, dts=%d, pts=%d, size=%d, %s(%s,%s)",  
1061 - srs_type2string(type), timestamp, pts, size,  
1062 - srs_code_id2string(srs_get_codec_id(data, size)),  
1063 - srs_avc_packet2string(srs_get_avc_packet_type(data, size)),  
1064 - srs_frame_type2string(srs_get_frame_type(data, size))  
1065 - );  
1066 - } else if (type == SRS_RTMP_TYPE_AUDIO) {  
1067 - srs_lib_trace("Audio packet type=%s, dts=%d, pts=%d, size=%d",  
1068 - srs_type2string(type), timestamp, pts, size);  
1069 - } else if (type == SRS_RTMP_TYPE_SCRIPT) {  
1070 - srs_lib_verbose("Data packet type=%s, time=%d, size=%d",  
1071 - srs_type2string(type), timestamp, size);  
1072 - int nparsed = 0;  
1073 - while (nparsed < size) {  
1074 - int nb_parsed_this = 0;  
1075 - srs_amf0_t amf0 = srs_amf0_parse(data + nparsed, size - nparsed, &nb_parsed_this);  
1076 - if (amf0 == NULL) {  
1077 - break;  
1078 - }  
1079 -  
1080 - nparsed += nb_parsed_this;  
1081 -  
1082 - char* amf0_str = NULL;  
1083 - srs_raw_trace("%s", srs_amf0_human_print(amf0, &amf0_str, NULL));  
1084 - srs_amf0_free_bytes(amf0_str);  
1085 - }  
1086 - } else {  
1087 - srs_lib_trace("Unknown packet type=%s, dts=%d, pts=%d, size=%d",  
1088 - srs_type2string(type), timestamp, pts, size);  
1089 - }  
1090 -  
1091 - return ret;  
1092 -}  
1093 -  
1094 -const char* srs_format_time()  
1095 -{  
1096 - struct timeval tv;  
1097 - static char buf[23];  
1098 -  
1099 - memset(buf, 0, sizeof(buf));  
1100 -  
1101 - // clock time  
1102 - if (gettimeofday(&tv, NULL) == -1) {  
1103 - return buf;  
1104 - }  
1105 -  
1106 - // to calendar time  
1107 - struct tm* tm;  
1108 - if ((tm = localtime((const time_t*)&tv.tv_sec)) == NULL) {  
1109 - return buf;  
1110 - }  
1111 -  
1112 - snprintf(buf, sizeof(buf),  
1113 - "%d-%02d-%02d %02d:%02d:%02d.%03d",  
1114 - 1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday,  
1115 - tm->tm_hour, tm->tm_min, tm->tm_sec,  
1116 - (int)(tv.tv_usec / 1000));  
1117 -  
1118 - // for srs-librtmp, @see https://github.com/winlinvip/simple-rtmp-server/issues/213  
1119 - buf[sizeof(buf) - 1] = 0;  
1120 -  
1121 - return buf;  
1122 -}  
1123 -  
1124 struct FlvContext 972 struct FlvContext
1125 { 973 {
1126 SrsFileReader reader; 974 SrsFileReader reader;
@@ -1595,6 +1443,158 @@ char* srs_amf0_human_print(srs_amf0_t amf0, char** pdata, int* psize) @@ -1595,6 +1443,158 @@ char* srs_amf0_human_print(srs_amf0_t amf0, char** pdata, int* psize)
1595 return any->human_print(pdata, psize); 1443 return any->human_print(pdata, psize);
1596 } 1444 }
1597 1445
  1446 +const char* srs_type2string(char type)
  1447 +{
  1448 + static const char* audio = "Audio";
  1449 + static const char* video = "Video";
  1450 + static const char* data = "Data";
  1451 + static const char* unknown = "Unknown";
  1452 +
  1453 + switch (type) {
  1454 + case SRS_RTMP_TYPE_AUDIO: return audio;
  1455 + case SRS_RTMP_TYPE_VIDEO: return video;
  1456 + case SRS_RTMP_TYPE_SCRIPT: return data;
  1457 + default: return unknown;
  1458 + }
  1459 +
  1460 + return unknown;
  1461 +}
  1462 +
  1463 +const char* srs_code_id2string(char codec_id)
  1464 +{
  1465 + static const char* h263 = "H.263";
  1466 + static const char* screen = "Screen";
  1467 + static const char* vp6 = "VP6";
  1468 + static const char* vp6_alpha = "VP6Alpha";
  1469 + static const char* screen2 = "Screen2";
  1470 + static const char* h264 = "H.264";
  1471 + static const char* unknown = "Unknown";
  1472 +
  1473 + switch (codec_id) {
  1474 + case 2: return h263;
  1475 + case 3: return screen;
  1476 + case 4: return vp6;
  1477 + case 5: return vp6_alpha;
  1478 + case 6: return screen2;
  1479 + case 7: return h264;
  1480 + default: return unknown;
  1481 + }
  1482 +
  1483 + return unknown;
  1484 +}
  1485 +
  1486 +const char* srs_avc_packet2string(char avc_packet_type)
  1487 +{
  1488 + static const char* sps_pps = "SpsPps";
  1489 + static const char* nalu = "Nalu";
  1490 + static const char* sps_pps_end = "SpsPpsEnd";
  1491 + static const char* unknown = "Unknown";
  1492 +
  1493 + switch (avc_packet_type) {
  1494 + case 0: return sps_pps;
  1495 + case 1: return nalu;
  1496 + case 2: return sps_pps_end;
  1497 + default: return unknown;
  1498 + }
  1499 +
  1500 + return unknown;
  1501 +}
  1502 +
  1503 +const char* srs_frame_type2string(char frame_type)
  1504 +{
  1505 + static const char* keyframe = "I";
  1506 + static const char* interframe = "P/B";
  1507 + static const char* disposable_interframe = "DI";
  1508 + static const char* generated_keyframe = "GI";
  1509 + static const char* video_infoframe = "VI";
  1510 + static const char* unknown = "Unknown";
  1511 +
  1512 + switch (frame_type) {
  1513 + case 1: return keyframe;
  1514 + case 2: return interframe;
  1515 + case 3: return disposable_interframe;
  1516 + case 4: return generated_keyframe;
  1517 + case 5: return video_infoframe;
  1518 + default: return unknown;
  1519 + }
  1520 +
  1521 + return unknown;
  1522 +}
  1523 +
  1524 +int srs_print_rtmp_packet(char type, u_int32_t timestamp, char* data, int size)
  1525 +{
  1526 + int ret = ERROR_SUCCESS;
  1527 +
  1528 + u_int32_t pts;
  1529 + if (srs_parse_timestamp(timestamp, type, data, size, &pts) != 0) {
  1530 + return ret;
  1531 + }
  1532 +
  1533 + if (type == SRS_RTMP_TYPE_VIDEO) {
  1534 + srs_lib_trace("Video packet type=%s, dts=%d, pts=%d, size=%d, %s(%s,%s)",
  1535 + srs_type2string(type), timestamp, pts, size,
  1536 + srs_code_id2string(srs_get_codec_id(data, size)),
  1537 + srs_avc_packet2string(srs_get_avc_packet_type(data, size)),
  1538 + srs_frame_type2string(srs_get_frame_type(data, size))
  1539 + );
  1540 + } else if (type == SRS_RTMP_TYPE_AUDIO) {
  1541 + srs_lib_trace("Audio packet type=%s, dts=%d, pts=%d, size=%d",
  1542 + srs_type2string(type), timestamp, pts, size);
  1543 + } else if (type == SRS_RTMP_TYPE_SCRIPT) {
  1544 + srs_lib_verbose("Data packet type=%s, time=%d, size=%d",
  1545 + srs_type2string(type), timestamp, size);
  1546 + int nparsed = 0;
  1547 + while (nparsed < size) {
  1548 + int nb_parsed_this = 0;
  1549 + srs_amf0_t amf0 = srs_amf0_parse(data + nparsed, size - nparsed, &nb_parsed_this);
  1550 + if (amf0 == NULL) {
  1551 + break;
  1552 + }
  1553 +
  1554 + nparsed += nb_parsed_this;
  1555 +
  1556 + char* amf0_str = NULL;
  1557 + srs_raw_trace("%s", srs_amf0_human_print(amf0, &amf0_str, NULL));
  1558 + srs_amf0_free_bytes(amf0_str);
  1559 + }
  1560 + } else {
  1561 + srs_lib_trace("Unknown packet type=%s, dts=%d, pts=%d, size=%d",
  1562 + srs_type2string(type), timestamp, pts, size);
  1563 + }
  1564 +
  1565 + return ret;
  1566 +}
  1567 +
  1568 +const char* srs_format_time()
  1569 +{
  1570 + struct timeval tv;
  1571 + static char buf[23];
  1572 +
  1573 + memset(buf, 0, sizeof(buf));
  1574 +
  1575 + // clock time
  1576 + if (gettimeofday(&tv, NULL) == -1) {
  1577 + return buf;
  1578 + }
  1579 +
  1580 + // to calendar time
  1581 + struct tm* tm;
  1582 + if ((tm = localtime((const time_t*)&tv.tv_sec)) == NULL) {
  1583 + return buf;
  1584 + }
  1585 +
  1586 + snprintf(buf, sizeof(buf),
  1587 + "%d-%02d-%02d %02d:%02d:%02d.%03d",
  1588 + 1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday,
  1589 + tm->tm_hour, tm->tm_min, tm->tm_sec,
  1590 + (int)(tv.tv_usec / 1000));
  1591 +
  1592 + // for srs-librtmp, @see https://github.com/winlinvip/simple-rtmp-server/issues/213
  1593 + buf[sizeof(buf) - 1] = 0;
  1594 +
  1595 + return buf;
  1596 +}
  1597 +
1598 /** 1598 /**
1599 * write audio raw frame to SRS. 1599 * write audio raw frame to SRS.
1600 */ 1600 */
@@ -226,16 +226,6 @@ extern int srs_bandwidth_check(srs_rtmp_t rtmp, @@ -226,16 +226,6 @@ extern int srs_bandwidth_check(srs_rtmp_t rtmp,
226 // 18 = script data 226 // 18 = script data
227 #define SRS_RTMP_TYPE_SCRIPT 18 227 #define SRS_RTMP_TYPE_SCRIPT 18
228 /** 228 /**
229 -* convert the flv tag type to string.  
230 -* SRS_RTMP_TYPE_AUDIO to "Audio"  
231 -* SRS_RTMP_TYPE_VIDEO to "Video"  
232 -* SRS_RTMP_TYPE_SCRIPT to "Data"  
233 -* otherwise, "Unknown"  
234 -* @remark user never free the return char*,  
235 -* it's static shared const string.  
236 -*/  
237 -extern const char* srs_type2string(char type);  
238 -/**  
239 * read a audio/video/script-data packet from rtmp stream. 229 * read a audio/video/script-data packet from rtmp stream.
240 * @param type, output the packet type, macros: 230 * @param type, output the packet type, macros:
241 * SRS_RTMP_TYPE_AUDIO, FlvTagAudio 231 * SRS_RTMP_TYPE_AUDIO, FlvTagAudio
@@ -323,20 +313,6 @@ extern int srs_parse_timestamp( @@ -323,20 +313,6 @@ extern int srs_parse_timestamp(
323 extern char srs_get_codec_id(char* data, int size); 313 extern char srs_get_codec_id(char* data, int size);
324 314
325 /** 315 /**
326 -* get the codec id string.  
327 -* H.263 = Sorenson H.263  
328 -* Screen = Screen video  
329 -* VP6 = On2 VP6  
330 -* VP6Alpha = On2 VP6 with alpha channel  
331 -* Screen2 = Screen video version 2  
332 -* H.264 = AVC  
333 -* otherwise, "Unknown"  
334 -* @remark user never free the return char*,  
335 -* it's static shared const string.  
336 -*/  
337 -extern const char* srs_code_id2string(char codec_id);  
338 -  
339 -/**  
340 * get the AVCPacketType of video tag. 316 * get the AVCPacketType of video tag.
341 * The following values are defined: 317 * The following values are defined:
342 * 0 = AVC sequence header 318 * 0 = AVC sequence header
@@ -348,17 +324,6 @@ extern const char* srs_code_id2string(char codec_id); @@ -348,17 +324,6 @@ extern const char* srs_code_id2string(char codec_id);
348 extern char srs_get_avc_packet_type(char* data, int size); 324 extern char srs_get_avc_packet_type(char* data, int size);
349 325
350 /** 326 /**
351 -* get the avc packet type string.  
352 -* SpsPps = AVC sequence header  
353 -* Nalu = AVC NALU  
354 -* SpsPpsEnd = AVC end of sequence  
355 -* otherwise, "Unknown"  
356 -* @remark user never free the return char*,  
357 -* it's static shared const string.  
358 -*/  
359 -extern const char* srs_avc_packet2string(char avc_packet_type);  
360 -  
361 -/**  
362 * get the FrameType of video tag. 327 * get the FrameType of video tag.
363 * Type of video frame. The following values are defined: 328 * Type of video frame. The following values are defined:
364 * 1 = key frame (for AVC, a seekable frame) 329 * 1 = key frame (for AVC, a seekable frame)
@@ -370,32 +335,6 @@ extern const char* srs_avc_packet2string(char avc_packet_type); @@ -370,32 +335,6 @@ extern const char* srs_avc_packet2string(char avc_packet_type);
370 */ 335 */
371 extern char srs_get_frame_type(char* data, int size); 336 extern char srs_get_frame_type(char* data, int size);
372 337
373 -/**  
374 -* get the frame type string.  
375 -* I = key frame (for AVC, a seekable frame)  
376 -* P/B = inter frame (for AVC, a non-seekable frame)  
377 -* DI = disposable inter frame (H.263 only)  
378 -* GI = generated key frame (reserved for server use only)  
379 -* VI = video info/command frame  
380 -* otherwise, "Unknown"  
381 -* @remark user never free the return char*,  
382 -* it's static shared const string.  
383 -*/  
384 -extern const char* srs_frame_type2string(char frame_type);  
385 -  
386 -/**  
387 -* print the rtmp packet, use srs_lib_trace/srs_lib_verbose for packet,  
388 -* and use srs_raw_trace for script data body.  
389 -* @return an error code for parse the timetstamp to dts and pts.  
390 -*/  
391 -extern int srs_print_rtmp_packet(char type, u_int32_t timestamp, char* data, int size);  
392 -  
393 -// log to console, for use srs-librtmp application.  
394 -extern const char* srs_format_time();  
395 -#define srs_lib_trace(msg, ...) printf("[%s] ", srs_format_time());printf(msg, ##__VA_ARGS__);printf("\n")  
396 -#define srs_lib_verbose(msg, ...) printf("[%s] ", srs_format_time());printf(msg, ##__VA_ARGS__);printf("\n")  
397 -#define srs_raw_trace(msg, ...) printf(msg, ##__VA_ARGS__)  
398 -  
399 /************************************************************* 338 /*************************************************************
400 ************************************************************** 339 **************************************************************
401 * flv codec 340 * flv codec
@@ -553,6 +492,12 @@ extern void srs_amf0_ecma_array_property_set(srs_amf0_t amf0, const char* name, @@ -553,6 +492,12 @@ extern void srs_amf0_ecma_array_property_set(srs_amf0_t amf0, const char* name,
553 extern int srs_amf0_strict_array_property_count(srs_amf0_t amf0); 492 extern int srs_amf0_strict_array_property_count(srs_amf0_t amf0);
554 extern srs_amf0_t srs_amf0_strict_array_property_at(srs_amf0_t amf0, int index); 493 extern srs_amf0_t srs_amf0_strict_array_property_at(srs_amf0_t amf0, int index);
555 extern void srs_amf0_strict_array_append(srs_amf0_t amf0, srs_amf0_t value); 494 extern void srs_amf0_strict_array_append(srs_amf0_t amf0, srs_amf0_t value);
  495 +
  496 +/*************************************************************
  497 +**************************************************************
  498 +* human readable print.
  499 +**************************************************************
  500 +*************************************************************/
556 /** 501 /**
557 * human readable print 502 * human readable print
558 * @param pdata, output the heap data, NULL to ignore. 503 * @param pdata, output the heap data, NULL to ignore.
@@ -560,6 +505,67 @@ extern void srs_amf0_strict_array_append(srs_amf0_t amf0, srs_amf0_t value); @@ -560,6 +505,67 @@ extern void srs_amf0_strict_array_append(srs_amf0_t amf0, srs_amf0_t value);
560 * @return return the *pdata for print. NULL to ignore. 505 * @return return the *pdata for print. NULL to ignore.
561 */ 506 */
562 extern char* srs_amf0_human_print(srs_amf0_t amf0, char** pdata, int* psize); 507 extern char* srs_amf0_human_print(srs_amf0_t amf0, char** pdata, int* psize);
  508 +/**
  509 +* convert the flv tag type to string.
  510 +* SRS_RTMP_TYPE_AUDIO to "Audio"
  511 +* SRS_RTMP_TYPE_VIDEO to "Video"
  512 +* SRS_RTMP_TYPE_SCRIPT to "Data"
  513 +* otherwise, "Unknown"
  514 +* @remark user never free the return char*,
  515 +* it's static shared const string.
  516 +*/
  517 +extern const char* srs_type2string(char type);
  518 +
  519 +/**
  520 +* get the codec id string.
  521 +* H.263 = Sorenson H.263
  522 +* Screen = Screen video
  523 +* VP6 = On2 VP6
  524 +* VP6Alpha = On2 VP6 with alpha channel
  525 +* Screen2 = Screen video version 2
  526 +* H.264 = AVC
  527 +* otherwise, "Unknown"
  528 +* @remark user never free the return char*,
  529 +* it's static shared const string.
  530 +*/
  531 +extern const char* srs_code_id2string(char codec_id);
  532 +
  533 +/**
  534 +* get the avc packet type string.
  535 +* SpsPps = AVC sequence header
  536 +* Nalu = AVC NALU
  537 +* SpsPpsEnd = AVC end of sequence
  538 +* otherwise, "Unknown"
  539 +* @remark user never free the return char*,
  540 +* it's static shared const string.
  541 +*/
  542 +extern const char* srs_avc_packet2string(char avc_packet_type);
  543 +
  544 +/**
  545 +* get the frame type string.
  546 +* I = key frame (for AVC, a seekable frame)
  547 +* P/B = inter frame (for AVC, a non-seekable frame)
  548 +* DI = disposable inter frame (H.263 only)
  549 +* GI = generated key frame (reserved for server use only)
  550 +* VI = video info/command frame
  551 +* otherwise, "Unknown"
  552 +* @remark user never free the return char*,
  553 +* it's static shared const string.
  554 +*/
  555 +extern const char* srs_frame_type2string(char frame_type);
  556 +
  557 +/**
  558 +* print the rtmp packet, use srs_lib_trace/srs_lib_verbose for packet,
  559 +* and use srs_raw_trace for script data body.
  560 +* @return an error code for parse the timetstamp to dts and pts.
  561 +*/
  562 +extern int srs_print_rtmp_packet(char type, u_int32_t timestamp, char* data, int size);
  563 +
  564 +// log to console, for use srs-librtmp application.
  565 +extern const char* srs_format_time();
  566 +#define srs_lib_trace(msg, ...) printf("[%s] ", srs_format_time());printf(msg, ##__VA_ARGS__);printf("\n")
  567 +#define srs_lib_verbose(msg, ...) printf("[%s] ", srs_format_time());printf(msg, ##__VA_ARGS__);printf("\n")
  568 +#define srs_raw_trace(msg, ...) printf(msg, ##__VA_ARGS__)
563 569
564 /************************************************************* 570 /*************************************************************
565 ************************************************************** 571 **************************************************************