winlin

fix isse #35: the gettimeofday jitter, log warn message

@@ -338,7 +338,7 @@ CORE_OBJS="${MODULE_OBJS[@]}" @@ -338,7 +338,7 @@ CORE_OBJS="${MODULE_OBJS[@]}"
338 MODULE_ID="KERNEL" 338 MODULE_ID="KERNEL"
339 MODULE_DEPENDS=("CORE") 339 MODULE_DEPENDS=("CORE")
340 ModuleLibIncs=(${SRS_OBJS}) 340 ModuleLibIncs=(${SRS_OBJS})
341 -MODULE_FILES=("srs_kernel_error" "srs_kernel_log" "srs_kernel_stream" "srs_kernel_buffer") 341 +MODULE_FILES=("srs_kernel_error" "srs_kernel_log" "srs_kernel_stream" "srs_kernel_buffer" "srs_kernel_utility")
342 KERNEL_INCS="src/kernel"; MODULE_DIR=${KERNEL_INCS} . auto/modules.sh 342 KERNEL_INCS="src/kernel"; MODULE_DIR=${KERNEL_INCS} . auto/modules.sh
343 KERNEL_OBJS="${MODULE_OBJS[@]}" 343 KERNEL_OBJS="${MODULE_OBJS[@]}"
344 # 344 #
@@ -34,6 +34,7 @@ using namespace std; @@ -34,6 +34,7 @@ using namespace std;
34 #include <srs_protocol_rtmp_stack.hpp> 34 #include <srs_protocol_rtmp_stack.hpp>
35 #include <srs_app_config.hpp> 35 #include <srs_app_config.hpp>
36 #include <srs_core_autofree.hpp> 36 #include <srs_core_autofree.hpp>
  37 +#include <srs_kernel_utility.hpp>
37 38
38 SrsBandwidth::SrsBandwidth() 39 SrsBandwidth::SrsBandwidth()
39 { 40 {
@@ -37,6 +37,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -37,6 +37,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 #include <srs_kernel_error.hpp> 37 #include <srs_kernel_error.hpp>
38 #include <srs_app_client.hpp> 38 #include <srs_app_client.hpp>
39 #include <srs_app_config.hpp> 39 #include <srs_app_config.hpp>
  40 +#include <srs_kernel_utility.hpp>
40 41
41 #define SERVER_LISTEN_BACKLOG 512 42 #define SERVER_LISTEN_BACKLOG 512
42 #define SRS_TIME_RESOLUTION_MS 500 43 #define SRS_TIME_RESOLUTION_MS 500
@@ -24,6 +24,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -24,6 +24,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 #include <srs_app_socket.hpp> 24 #include <srs_app_socket.hpp>
25 25
26 #include <srs_kernel_error.hpp> 26 #include <srs_kernel_error.hpp>
  27 +#include <srs_kernel_utility.hpp>
27 28
28 SrsSocket::SrsSocket(st_netfd_t client_stfd) 29 SrsSocket::SrsSocket(st_netfd_t client_stfd)
29 { 30 {
@@ -24,30 +24,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -24,30 +24,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 #include <srs_core.hpp> 24 #include <srs_core.hpp>
25 25
26 #include <string.h> 26 #include <string.h>
27 -#include <sys/time.h>  
28 #include <netdb.h> 27 #include <netdb.h>
29 #include <arpa/inet.h> 28 #include <arpa/inet.h>
30 29
31 -static int64_t _srs_system_time_us_cache = 0;  
32 -  
33 -int64_t srs_get_system_time_ms()  
34 -{  
35 - return _srs_system_time_us_cache / 1000;  
36 -}  
37 -  
38 -void srs_update_system_time_ms()  
39 -{  
40 - timeval now;  
41 -  
42 - gettimeofday(&now, NULL);  
43 -  
44 - // @see: https://github.com/winlinvip/simple-rtmp-server/issues/35  
45 - // we must convert the tv_sec/tv_usec to int64_t.  
46 - _srs_system_time_us_cache = ((int64_t)now.tv_sec) * 1000 * 1000 + (int64_t)now.tv_usec;  
47 -  
48 - _srs_system_time_us_cache = srs_max(0, _srs_system_time_us_cache);  
49 -}  
50 -  
51 std::string srs_replace(std::string str, std::string old_str, std::string new_str) 30 std::string srs_replace(std::string str, std::string old_str, std::string new_str)
52 { 31 {
53 std::string ret = str; 32 std::string ret = str;
@@ -86,11 +86,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -86,11 +86,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
86 #define srs_min(a, b) (((a) < (b))? (a) : (b)) 86 #define srs_min(a, b) (((a) < (b))? (a) : (b))
87 #define srs_max(a, b) (((a) < (b))? (b) : (a)) 87 #define srs_max(a, b) (((a) < (b))? (b) : (a))
88 88
89 -// get current system time in ms, use cache to avoid performance problem  
90 -extern int64_t srs_get_system_time_ms();  
91 -// the deamon st-thread will update it.  
92 -extern void srs_update_system_time_ms();  
93 -  
94 // signal defines. 89 // signal defines.
95 #define SIGNAL_RELOAD SIGHUP 90 #define SIGNAL_RELOAD SIGHUP
96 91
  1 +/*
  2 +The MIT License (MIT)
  3 +
  4 +Copyright (c) 2013-2014 winlin
  5 +
  6 +Permission is hereby granted, free of charge, to any person obtaining a copy of
  7 +this software and associated documentation files (the "Software"), to deal in
  8 +the Software without restriction, including without limitation the rights to
  9 +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  10 +the Software, and to permit persons to whom the Software is furnished to do so,
  11 +subject to the following conditions:
  12 +
  13 +The above copyright notice and this permission notice shall be included in all
  14 +copies or substantial portions of the Software.
  15 +
  16 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18 +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19 +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20 +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21 +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22 +*/
  23 +
  24 +#include <srs_kernel_utility.hpp>
  25 +
  26 +#include <sys/time.h>
  27 +
  28 +#include <srs_kernel_log.hpp>
  29 +
  30 +static int64_t _srs_system_time_us_cache = 0;
  31 +
  32 +int64_t srs_get_system_time_ms()
  33 +{
  34 + return _srs_system_time_us_cache / 1000;
  35 +}
  36 +
  37 +void srs_update_system_time_ms()
  38 +{
  39 + timeval now;
  40 +
  41 + if (gettimeofday(&now, NULL) < 0) {
  42 + srs_warn("gettimeofday failed, ignore");
  43 + return;
  44 + }
  45 +
  46 + // @see: https://github.com/winlinvip/simple-rtmp-server/issues/35
  47 + // we must convert the tv_sec/tv_usec to int64_t.
  48 + int64_t now_us = ((int64_t)now.tv_sec) * 1000 * 1000 + (int64_t)now.tv_usec;
  49 + if (now_us < _srs_system_time_us_cache) {
  50 + srs_warn("system time negative, "
  51 + "history=%"PRId64"us, now=%"PRId64"", _srs_system_time_us_cache, now_us);
  52 + }
  53 +
  54 + _srs_system_time_us_cache = now_us;
  55 +}
  1 +/*
  2 +The MIT License (MIT)
  3 +
  4 +Copyright (c) 2013-2014 winlin
  5 +
  6 +Permission is hereby granted, free of charge, to any person obtaining a copy of
  7 +this software and associated documentation files (the "Software"), to deal in
  8 +the Software without restriction, including without limitation the rights to
  9 +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  10 +the Software, and to permit persons to whom the Software is furnished to do so,
  11 +subject to the following conditions:
  12 +
  13 +The above copyright notice and this permission notice shall be included in all
  14 +copies or substantial portions of the Software.
  15 +
  16 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18 +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19 +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20 +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21 +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22 +*/
  23 +
  24 +#ifndef SRS_KERNEL_UTILITY_HPP
  25 +#define SRS_KERNEL_UTILITY_HPP
  26 +
  27 +/*
  28 +#include <srs_kernel_utility.hpp>
  29 +*/
  30 +
  31 +#include <srs_core.hpp>
  32 +
  33 +// get current system time in ms, use cache to avoid performance problem
  34 +extern int64_t srs_get_system_time_ms();
  35 +// the deamon st-thread will update it.
  36 +extern void srs_update_system_time_ms();
  37 +
  38 +#endif
@@ -23,6 +23,8 @@ file @@ -23,6 +23,8 @@ file
23 ..\kernel\srs_kernel_log.cpp, 23 ..\kernel\srs_kernel_log.cpp,
24 ..\kernel\srs_kernel_stream.hpp, 24 ..\kernel\srs_kernel_stream.hpp,
25 ..\kernel\srs_kernel_stream.cpp, 25 ..\kernel\srs_kernel_stream.cpp,
  26 + ..\kernel\srs_kernel_utility.hpp,
  27 + ..\kernel\srs_kernel_utility.cpp,
26 rtmp-protocol readonly separator, 28 rtmp-protocol readonly separator,
27 ..\rtmp\srs_protocol_amf0.hpp, 29 ..\rtmp\srs_protocol_amf0.hpp,
28 ..\rtmp\srs_protocol_amf0.cpp, 30 ..\rtmp\srs_protocol_amf0.cpp,