ijksdl_timer.c 7.1 KB

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