23
23
#include "clapper-app-utils.h"
24
24
#include "clapper-app-media-item-box.h"
25
25
26
- /* Useful only on Windows */
27
26
#ifdef G_OS_WIN32
27
+ #include <windows.h>
28
+ #ifdef HAVE_WIN_PROCESS_THREADS_API
29
+ #include <processthreadsapi.h>
30
+ #endif
31
+ #ifdef HAVE_WIN_TIME_API
32
+ #include <timeapi.h>
33
+ #endif
34
+ #endif
35
+
36
+ #define GST_CAT_DEFAULT clapper_app_utils_debug
37
+ GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT );
38
+
39
+ void
40
+ clapper_app_utils_debug_init (void )
41
+ {
42
+ GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT , "clapperapputils" , 0 ,
43
+ "Clapper App Utils" );
44
+ }
45
+
46
+ /* Windows specific functions */
47
+ #ifdef G_OS_WIN32
48
+
49
+ /*
50
+ * clapper_app_utils_win_enforce_hi_res_clock:
51
+ *
52
+ * Enforce high resolution clock by explicitly disabling Windows
53
+ * timer resolution power throttling. When disabled, system remembers
54
+ * and honors any previous timer resolution request by the process.
55
+ *
56
+ * By default, Windows 11 may automatically ignore the timer
57
+ * resolution requests in certain scenarios.
58
+ */
59
+ void
60
+ clapper_app_utils_win_enforce_hi_res_clock (void )
61
+ {
62
+ #ifdef HAVE_WIN_PROCESS_THREADS_API
63
+ PROCESS_POWER_THROTTLING_STATE PowerThrottling ;
64
+ RtlZeroMemory (& PowerThrottling , sizeof (PowerThrottling ));
65
+ gboolean success ;
66
+
67
+ PowerThrottling .Version = PROCESS_POWER_THROTTLING_CURRENT_VERSION ;
68
+ PowerThrottling .ControlMask = PROCESS_POWER_THROTTLING_IGNORE_TIMER_RESOLUTION ;
69
+ PowerThrottling .StateMask = 0 ; // Always honor timer resolution requests
70
+
71
+ success = (gboolean ) SetProcessInformation (
72
+ GetCurrentProcess (),
73
+ ProcessPowerThrottling ,
74
+ & PowerThrottling ,
75
+ sizeof (PowerThrottling ));
76
+
77
+ /* Not an error. Older Windows does not have this functionality, but
78
+ * also honor hi-res clock by default anyway, so do not print then. */
79
+ GST_INFO ("Windows hi-res clock support is %senforced" ,
80
+ (success ) ? "" : "NOT " );
81
+ #endif
82
+ }
83
+
84
+ /*
85
+ * clapper_app_utils_win_hi_res_clock_start:
86
+ *
87
+ * Start Windows high resolution clock which will improve
88
+ * accuracy of various Windows timer APIs and precision
89
+ * of #GstSystemClock during playback.
90
+ *
91
+ * On Windows 10 version 2004 (and older), this function affects
92
+ * a global Windows setting. On any other (newer) version this
93
+ * will only affect a single process.
94
+ *
95
+ * Returns: Timer resolution period value.
96
+ */
97
+ guint
98
+ clapper_app_utils_win_hi_res_clock_start (void )
99
+ {
100
+ guint resolution = 0 ;
101
+
102
+ #ifdef HAVE_WIN_TIME_API
103
+ TIMECAPS time_caps ;
104
+ MMRESULT res ;
105
+
106
+ if ((res = timeGetDevCaps (& time_caps , sizeof (TIMECAPS ))) != TIMERR_NOERROR ) {
107
+ GST_WARNING ("Could not query timer resolution, code: %u" , res );
108
+ return 0 ;
109
+ }
110
+
111
+ resolution = MIN (MAX (time_caps .wPeriodMin , 1 ), time_caps .wPeriodMax );
112
+
113
+ if ((res = timeBeginPeriod (resolution )) != TIMERR_NOERROR ) {
114
+ GST_WARNING ("Could not request timer resolution, code: %u" , res );
115
+ return 0 ;
116
+ }
117
+
118
+ GST_INFO ("Started Windows hi-res clock, precision: %ums" , resolution );
119
+ #endif
120
+
121
+ return resolution ;
122
+ }
123
+
124
+ /*
125
+ * clapper_app_utils_win_hi_res_clock_stop:
126
+ * @resolution: started resolution value (non-zero)
127
+ *
128
+ * Stop previously started Microsoft Windows high resolution clock.
129
+ */
130
+ void
131
+ clapper_app_utils_win_hi_res_clock_stop (guint resolution )
132
+ {
133
+ #ifdef HAVE_WIN_TIME_API
134
+ MMRESULT res ;
135
+
136
+ if ((res = timeEndPeriod (resolution )) == TIMERR_NOERROR )
137
+ GST_INFO ("Stopped Windows hi-res clock" );
138
+ else
139
+ GST_ERROR ("Could not stop hi-res clock, code: %u" , res );
140
+ #endif
141
+ }
142
+
143
+ /* Extensions are used only on Windows */
28
144
const gchar * const *
29
145
clapper_app_utils_get_extensions (void )
30
146
{
@@ -45,7 +161,7 @@ clapper_app_utils_get_subtitles_extensions (void)
45
161
46
162
return subs_extensions ;
47
163
}
48
- #endif
164
+ #endif // G_OS_WIN32
49
165
50
166
const gchar * const *
51
167
clapper_app_utils_get_mime_types (void )
0 commit comments