ijksdl_timer.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*****************************************************************************
  2. * ijksdl_thread.c
  3. *****************************************************************************
  4. *
  5. * copyright (c) 2013 Zhang Rui <bbcallen@gmail.com>
  6. *
  7. * This file is part of ijkPlayer.
  8. *
  9. * ijkPlayer is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * ijkPlayer is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with ijkPlayer; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include "ijksdl_timer.h"
  24. #include <unistd.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <time.h>
  28. #include <sys/time.h>
  29. #if defined(__APPLE__)
  30. #include <mach/mach_time.h>
  31. static int g_is_mach_base_info_inited = 0;
  32. static kern_return_t g_mach_base_info_ret = 0;
  33. static mach_timebase_info_data_t g_mach_base_info;
  34. /* nanosleep is not included in c99, just a workaround for CocoaPods */
  35. int nanosleep(const struct timespec *, struct timespec *) __DARWIN_ALIAS_C(nanosleep);
  36. #endif
  37. #include "ijksdl_log.h"
  38. #ifdef NTESIJKSDLTimer
  39. void SDL_Delay(Uint32 ms)
  40. {
  41. int was_error;
  42. struct timespec elapsed, tv;
  43. /* Set the timeout interval */
  44. elapsed.tv_sec = ms / 1000;
  45. elapsed.tv_nsec = (ms % 1000) * 1000000;
  46. do {
  47. tv.tv_sec = elapsed.tv_sec;
  48. tv.tv_nsec = elapsed.tv_nsec;
  49. was_error = nanosleep(&tv, &elapsed);
  50. } while (was_error);
  51. }
  52. #endif
  53. Uint64 NTES_SDL_GetTickHR(void)
  54. {
  55. Uint64 clock;
  56. #if defined(__ANDROID__)
  57. struct timespec now;
  58. #ifdef CLOCK_MONOTONIC_COARSE
  59. clock_gettime(CLOCK_MONOTONIC_COARSE, &now);
  60. #else
  61. clock_gettime(CLOCK_MONOTONIC_HR, &now);
  62. #endif
  63. clock = now.tv_sec * 1000 + now.tv_nsec / 1000000;
  64. #elif defined(__APPLE__)
  65. if (!g_is_mach_base_info_inited) {
  66. g_mach_base_info_ret = mach_timebase_info(&g_mach_base_info);
  67. g_is_mach_base_info_inited = 1;
  68. }
  69. if (g_mach_base_info_ret == 0) {
  70. uint64_t now = mach_absolute_time();
  71. clock = now * g_mach_base_info.numer / g_mach_base_info.denom / 1000000;
  72. } else {
  73. struct timeval now;
  74. gettimeofday(&now, NULL);
  75. clock = now.tv_sec * 1000 + now.tv_usec / 1000;
  76. }
  77. #endif
  78. return (clock);
  79. }
  80. #ifdef NTESIJKSDLTimer
  81. void SDL_ProfilerReset(SDL_Profiler* profiler, int max_sample)
  82. {
  83. memset(profiler, 0, sizeof(SDL_Profiler));
  84. if (max_sample < 0)
  85. profiler->max_sample = 3;
  86. else
  87. profiler->max_sample = max_sample;
  88. }
  89. void SDL_ProfilerBegin(SDL_Profiler* profiler)
  90. {
  91. profiler->begin_time = NTES_SDL_GetTickHR();
  92. }
  93. int64_t SDL_ProfilerEnd(SDL_Profiler* profiler)
  94. {
  95. int64_t delta = NTES_SDL_GetTickHR() - profiler->begin_time;
  96. if (profiler->max_sample > 0) {
  97. profiler->total_elapsed += delta;
  98. profiler->total_counter += 1;
  99. profiler->sample_elapsed += delta;
  100. profiler->sample_counter += 1;
  101. if (profiler->sample_counter > profiler->max_sample) {
  102. profiler->sample_elapsed -= profiler->average_elapsed;
  103. profiler->sample_counter -= 1;
  104. }
  105. if (profiler->sample_counter > 0) {
  106. profiler->average_elapsed = profiler->sample_elapsed / profiler->sample_counter;
  107. }
  108. if (profiler->sample_elapsed > 0) {
  109. profiler->sample_per_seconds = profiler->sample_counter * 1000.f / profiler->sample_elapsed;
  110. }
  111. }
  112. return delta;
  113. }
  114. void SDL_SpeedSamplerReset(SDL_SpeedSampler *sampler)
  115. {
  116. memset(sampler, 0, sizeof(SDL_SpeedSampler));
  117. sampler->capacity = sizeof(sampler->samples) / sizeof(Uint64);
  118. }
  119. float SDL_SpeedSamplerAdd(SDL_SpeedSampler *sampler, int enable_log, const char *log_tag)
  120. {
  121. Uint64 current = NTES_SDL_GetTickHR();
  122. sampler->samples[sampler->next_index] = current;
  123. sampler->next_index++;
  124. sampler->next_index %= sampler->capacity;
  125. if (sampler->count + 1 >= sampler->capacity) {
  126. sampler->first_index++;
  127. sampler->first_index %= sampler->capacity;
  128. } else {
  129. sampler->count++;
  130. }
  131. if (sampler->count < 2)
  132. return 0;
  133. float samples_per_second = 1000.0f * (sampler->count - 1) / (current - sampler->samples[sampler->first_index]);
  134. if (enable_log && (sampler->last_log_time + 1000 < current || sampler->last_log_time > current)) {
  135. sampler->last_log_time = current;
  136. ALOGW("%s: %.2f\n", log_tag ? log_tag : "N/A", samples_per_second);
  137. }
  138. return samples_per_second;
  139. }
  140. void SDL_SpeedSampler2Reset(SDL_SpeedSampler2 *sampler, int sample_range)
  141. {
  142. memset(sampler, 0, sizeof(SDL_SpeedSampler2));
  143. sampler->sample_range = sample_range;
  144. sampler->last_profile_tick = (int64_t)NTES_SDL_GetTickHR();
  145. }
  146. int64_t SDL_SpeedSampler2Add(SDL_SpeedSampler2 *sampler, int quantity)
  147. {
  148. if (quantity < 0)
  149. return 0;
  150. int64_t sample_range = sampler->sample_range;
  151. int64_t last_tick = sampler->last_profile_tick;
  152. int64_t last_duration = sampler->last_profile_duration;
  153. int64_t last_quantity = sampler->last_profile_quantity;
  154. int64_t now = (int64_t)NTES_SDL_GetTickHR();
  155. int64_t elapsed = (int64_t)llabs(now - last_tick);
  156. if (elapsed < 0 || elapsed >= sample_range) {
  157. // overflow, reset to initialized state
  158. sampler->last_profile_tick = now;
  159. sampler->last_profile_duration = sample_range;
  160. sampler->last_profile_quantity = quantity;
  161. sampler->last_profile_speed = quantity * 1000 / sample_range;
  162. return sampler->last_profile_speed;
  163. }
  164. int64_t new_quantity = last_quantity + quantity;
  165. int64_t new_duration = last_duration + elapsed;
  166. if (new_duration > sample_range) {
  167. new_quantity = new_quantity * sample_range / new_duration;
  168. new_duration = sample_range;
  169. }
  170. sampler->last_profile_tick = now;
  171. sampler->last_profile_duration = new_duration;
  172. sampler->last_profile_quantity = new_quantity;
  173. if (new_duration > 0)
  174. sampler->last_profile_speed = new_quantity * 1000 / new_duration;
  175. return sampler->last_profile_speed;
  176. }
  177. int64_t SDL_SpeedSampler2GetSpeed(SDL_SpeedSampler2 *sampler)
  178. {
  179. int64_t sample_range = sampler->sample_range;
  180. int64_t last_tick = sampler->last_profile_tick;
  181. int64_t last_quantity = sampler->last_profile_quantity;
  182. int64_t last_duration = sampler->last_profile_duration;
  183. int64_t now = (int64_t)NTES_SDL_GetTickHR();
  184. int64_t elapsed = (int64_t)llabs(now - last_tick);
  185. if (elapsed < 0 || elapsed >= sample_range)
  186. return 0;
  187. int64_t new_quantity = last_quantity;
  188. int64_t new_duration = last_duration + elapsed;
  189. if (new_duration > sample_range) {
  190. new_quantity = new_quantity * sample_range / new_duration;
  191. new_duration = sample_range;
  192. }
  193. if (new_duration <= 0)
  194. return 0;
  195. return new_quantity * 1000 / new_duration;
  196. }
  197. #endif