1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

Add utest or CRC32 IEEE, refine TABs of code

This commit is contained in:
winlin 2018-07-21 19:09:32 +08:00
parent 608c88b8f2
commit dea5e5bb48
2 changed files with 607 additions and 588 deletions

View file

@ -108,6 +108,7 @@ int64_t srs_get_system_time_ms()
return _srs_system_time_us_cache / 1000; return _srs_system_time_us_cache / 1000;
} }
int64_t srs_get_system_startup_time_ms() int64_t srs_get_system_startup_time_ms()
{ {
if (_srs_system_time_startup_time <= 0) { if (_srs_system_time_startup_time <= 0) {
@ -116,6 +117,7 @@ int64_t srs_get_system_startup_time_ms()
return _srs_system_time_startup_time / 1000; return _srs_system_time_startup_time / 1000;
} }
int64_t srs_update_system_time_ms() int64_t srs_update_system_time_ms()
{ {
timeval now; timeval now;
@ -515,13 +517,14 @@ int srs_do_create_dir_recursively(string dir)
srs_error("create dir %s failed. ret=%d", dir.c_str(), ret); srs_error("create dir %s failed. ret=%d", dir.c_str(), ret);
return ret; return ret;
} }
srs_info("create dir %s success.", dir.c_str()); srs_info("create dir %s success.", dir.c_str());
return ret; return ret;
} }
bool srs_bytes_equals(void* pa, void* pb, int size) bool srs_bytes_equals(void* pa, void* pb, int size)
{ {
uint8_t* a = (uint8_t*)pa; uint8_t* a = (uint8_t*)pa;
uint8_t* b = (uint8_t*)pb; uint8_t* b = (uint8_t*)pb;
@ -540,10 +543,10 @@ int srs_do_create_dir_recursively(string dir)
} }
return true; return true;
} }
srs_error_t srs_create_dir_recursively(string dir) srs_error_t srs_create_dir_recursively(string dir)
{ {
int ret = srs_do_create_dir_recursively(dir); int ret = srs_do_create_dir_recursively(dir);
if (ret == ERROR_SYSTEM_DIR_EXISTS || ret == ERROR_SUCCESS) { if (ret == ERROR_SYSTEM_DIR_EXISTS || ret == ERROR_SUCCESS) {
@ -551,10 +554,10 @@ int srs_do_create_dir_recursively(string dir)
} }
return srs_error_new(ret, "create dir %s", dir.c_str()); return srs_error_new(ret, "create dir %s", dir.c_str());
} }
bool srs_path_exists(std::string path) bool srs_path_exists(std::string path)
{ {
struct stat st; struct stat st;
// stat current dir, if exists, return error. // stat current dir, if exists, return error.
@ -563,10 +566,10 @@ int srs_do_create_dir_recursively(string dir)
} }
return false; return false;
} }
string srs_path_dirname(string path) string srs_path_dirname(string path)
{ {
std::string dirname = path; std::string dirname = path;
size_t pos = string::npos; size_t pos = string::npos;
@ -578,10 +581,10 @@ int srs_do_create_dir_recursively(string dir)
} }
return dirname; return dirname;
} }
string srs_path_basename(string path) string srs_path_basename(string path)
{ {
std::string dirname = path; std::string dirname = path;
size_t pos = string::npos; size_t pos = string::npos;
@ -594,10 +597,10 @@ int srs_do_create_dir_recursively(string dir)
} }
return dirname; return dirname;
} }
string srs_path_filename(string path) string srs_path_filename(string path)
{ {
std::string filename = path; std::string filename = path;
size_t pos = string::npos; size_t pos = string::npos;
@ -606,10 +609,10 @@ int srs_do_create_dir_recursively(string dir)
} }
return filename; return filename;
} }
string srs_path_filext(string path) string srs_path_filext(string path)
{ {
size_t pos = string::npos; size_t pos = string::npos;
if ((pos = path.rfind(".")) != string::npos) { if ((pos = path.rfind(".")) != string::npos) {
@ -617,10 +620,10 @@ int srs_do_create_dir_recursively(string dir)
} }
return ""; return "";
} }
bool srs_avc_startswith_annexb(SrsBuffer* stream, int* pnb_start_code) bool srs_avc_startswith_annexb(SrsBuffer* stream, int* pnb_start_code)
{ {
char* bytes = stream->data() + stream->pos(); char* bytes = stream->data() + stream->pos();
char* p = bytes; char* p = bytes;
@ -646,10 +649,10 @@ int srs_do_create_dir_recursively(string dir)
} }
return false; return false;
} }
bool srs_aac_startswith_adts(SrsBuffer* stream) bool srs_aac_startswith_adts(SrsBuffer* stream)
{ {
char* bytes = stream->data() + stream->pos(); char* bytes = stream->data() + stream->pos();
char* p = bytes; char* p = bytes;
@ -664,11 +667,11 @@ int srs_do_create_dir_recursively(string dir)
} }
return true; return true;
} }
// @see http://www.stmc.edu.hk/~vincent/ffmpeg_0.4.9-pre1/libavformat/mpegtsenc.c // @see http://www.stmc.edu.hk/~vincent/ffmpeg_0.4.9-pre1/libavformat/mpegtsenc.c
unsigned int __mpegts_crc32(const uint8_t *data, int len) unsigned int __mpegts_crc32(const uint8_t *data, int len)
{ {
/* /*
* MPEG2 transport stream (aka DVB) mux * MPEG2 transport stream (aka DVB) mux
* Copyright (c) 2003 Fabrice Bellard. * Copyright (c) 2003 Fabrice Bellard.
@ -740,11 +743,11 @@ int srs_do_create_dir_recursively(string dir)
} }
return crc; return crc;
} }
// @see https://github.com/ETrun/crc32/blob/master/crc32.c // @see https://github.com/ETrun/crc32/blob/master/crc32.c
uint32_t __crc32_ieee(uint32_t init, const uint8_t* buf, size_t nb_buf) uint32_t __crc32_ieee(uint32_t init, const uint8_t* buf, size_t nb_buf)
{ {
/*----------------------------------------------------------------------------*\ /*----------------------------------------------------------------------------*\
* CRC-32 version 2.0.0 by Craig Bruce, 2006-04-29. * CRC-32 version 2.0.0 by Craig Bruce, 2006-04-29.
* *
@ -813,19 +816,19 @@ int srs_do_create_dir_recursively(string dir)
} }
return crc^0xFFFFFFFF; return crc^0xFFFFFFFF;
} }
uint32_t srs_crc32_mpegts(const void* buf, int size) uint32_t srs_crc32_mpegts(const void* buf, int size)
{ {
return __mpegts_crc32((const uint8_t*)buf, size); return __mpegts_crc32((const uint8_t*)buf, size);
} }
uint32_t srs_crc32_ieee(const void* buf, int size, uint32_t previous) uint32_t srs_crc32_ieee(const void* buf, int size, uint32_t previous)
{ {
return __crc32_ieee(previous, (const uint8_t*)buf, size); return __crc32_ieee(previous, (const uint8_t*)buf, size);
} }
/* /*
* Copyright (c) 2006 Ryan Martell. (rdm4@martellventures.com) * Copyright (c) 2006 Ryan Martell. (rdm4@martellventures.com)
* *
* This file is part of FFmpeg. * This file is part of FFmpeg.
@ -844,7 +847,6 @@ int srs_do_create_dir_recursively(string dir)
* License along with FFmpeg; if not, write to the Free Software * License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
#ifndef UINT_MAX #ifndef UINT_MAX
#define UINT_MAX 0xffffffff #define UINT_MAX 0xffffffff
#endif #endif
@ -883,23 +885,22 @@ int srs_do_create_dir_recursively(string dir)
#define AV_BSWAP32C(x) (AV_BSWAP16C(x) << 16 | AV_BSWAP16C((x) >> 16)) #define AV_BSWAP32C(x) (AV_BSWAP16C(x) << 16 | AV_BSWAP16C((x) >> 16))
#ifndef av_bswap32 #ifndef av_bswap32
static const uint32_t av_bswap32(uint32_t x) static const uint32_t av_bswap32(uint32_t x)
{ {
return AV_BSWAP32C(x); return AV_BSWAP32C(x);
} }
#endif #endif
#define av_be2ne32(x) av_bswap32(x) #define av_be2ne32(x) av_bswap32(x)
/**
/**
* @file * @file
* @brief Base64 encode/decode * @brief Base64 encode/decode
* @author Ryan Martell <rdm4@martellventures.com> (with lots of Michael) * @author Ryan Martell <rdm4@martellventures.com> (with lots of Michael)
*/ */
/* ---------------- private code */ /* ---------------- private code */
static const uint8_t map2[256] = static const uint8_t map2[256] =
{ {
0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
@ -935,7 +936,7 @@ int srs_do_create_dir_recursively(string dir)
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
}; };
#define BASE64_DEC_STEP(i) do { \ #define BASE64_DEC_STEP(i) do { \
bits = map2[in[i]]; \ bits = map2[in[i]]; \
@ -944,8 +945,8 @@ int srs_do_create_dir_recursively(string dir)
v = i ? (v << 6) + bits : bits; \ v = i ? (v << 6) + bits : bits; \
} while(0) } while(0)
int srs_av_base64_decode(uint8_t* out, const char* in_str, int out_size) int srs_av_base64_decode(uint8_t* out, const char* in_str, int out_size)
{ {
uint8_t *dst = out; uint8_t *dst = out;
uint8_t *end = out + out_size; uint8_t *end = out + out_size;
// no sign extension // no sign extension
@ -987,24 +988,23 @@ int srs_do_create_dir_recursively(string dir)
in++; in++;
} }
out3: out3:
*dst++ = v >> 10; *dst++ = v >> 10;
v <<= 2; v <<= 2;
out2: out2:
*dst++ = v >> 4; *dst++ = v >> 4;
out1: out1:
out0: out0:
return bits & 1 ? -1 : (int)(dst - out); return bits & 1 ? -1 : (int)(dst - out);
} }
/***************************************************************************** /*****************************************************************************
* b64_encode: Stolen from VLC's http.c. * b64_encode: Stolen from VLC's http.c.
* Simplified by Michael. * Simplified by Michael.
* Fixed edge cases and made it work from data (vs. strings) by Ryan. * Fixed edge cases and made it work from data (vs. strings) by Ryan.
*****************************************************************************/ *****************************************************************************/
char* srs_av_base64_encode(char* out, int out_size, const uint8_t* in, int in_size)
char* srs_av_base64_encode(char* out, int out_size, const uint8_t* in, int in_size) {
{
static const char b64[] = static const char b64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
char *ret, *dst; char *ret, *dst;
@ -1039,20 +1039,20 @@ int srs_do_create_dir_recursively(string dir)
*dst = '\0'; *dst = '\0';
return ret; return ret;
} }
#define SPACE_CHARS " \t\r\n" #define SPACE_CHARS " \t\r\n"
int av_toupper(int c) int av_toupper(int c)
{ {
if (c >= 'a' && c <= 'z') { if (c >= 'a' && c <= 'z') {
c ^= 0x20; c ^= 0x20;
} }
return c; return c;
} }
int ff_hex_to_data(uint8_t* data, const char* p) int ff_hex_to_data(uint8_t* data, const char* p)
{ {
int c, len, v; int c, len, v;
len = 0; len = 0;
@ -1077,10 +1077,10 @@ int srs_do_create_dir_recursively(string dir)
} }
} }
return len; return len;
} }
int srs_chunk_header_c0(int perfer_cid, uint32_t timestamp, int32_t payload_length, int8_t message_type, int32_t stream_id, char* cache, int nb_cache) int srs_chunk_header_c0(int perfer_cid, uint32_t timestamp, int32_t payload_length, int8_t message_type, int32_t stream_id, char* cache, int nb_cache)
{ {
// to directly set the field. // to directly set the field.
char* pp = NULL; char* pp = NULL;
@ -1152,10 +1152,10 @@ int srs_do_create_dir_recursively(string dir)
// always has header // always has header
return (int)(p - cache); return (int)(p - cache);
} }
int srs_chunk_header_c3(int perfer_cid, uint32_t timestamp, char* cache, int nb_cache) int srs_chunk_header_c3(int perfer_cid, uint32_t timestamp, char* cache, int nb_cache)
{ {
// to directly set the field. // to directly set the field.
char* pp = NULL; char* pp = NULL;
@ -1200,5 +1200,5 @@ int srs_do_create_dir_recursively(string dir)
// always has header // always has header
return (int)(p - cache); return (int)(p - cache);
} }

View file

@ -1523,5 +1523,24 @@ VOID TEST(KernelUtility, AvcUev)
} }
} }
VOID TEST(KernelUtility, CRC32IEEE)
{
string datas[] = {
"123456789", "srs", "ossrs.net",
"SRS's a simplest, conceptual integrated, industrial-strength live streaming origin cluster."
};
uint32_t checksums[] = {
0xcbf43926, 0x7df334e9, 0x2f52242b,
0x7e8677bd
};
for (int i = 0; i < (int)(sizeof(datas)/sizeof(string)); i++) {
string data = datas[i];
uint32_t checksum = checksums[i];
EXPECT_EQ(checksum, srs_crc32_ieee(data.data(), data.length(), 0));
}
}
#endif #endif