• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 use std::time::Duration;
6 
7 use libc;
8 
9 use crate::RateEstimator;
10 
11 /// # Safety
12 ///
13 /// To use this function safely, `window_size` must be a valid pointer to a
14 /// timespec.
15 #[no_mangle]
rate_estimator_create( rate: libc::c_uint, window_size: *const libc::timespec, smooth_factor: libc::c_double, ) -> *mut RateEstimator16 pub unsafe extern "C" fn rate_estimator_create(
17     rate: libc::c_uint,
18     window_size: *const libc::timespec,
19     smooth_factor: libc::c_double,
20 ) -> *mut RateEstimator {
21     if window_size.is_null() {
22         return std::ptr::null_mut::<RateEstimator>();
23     }
24 
25     let ts = &*window_size;
26     let window = Duration::new(ts.tv_sec as u64, ts.tv_nsec as u32);
27 
28     match RateEstimator::try_new(rate, window, smooth_factor) {
29         Ok(re) => Box::into_raw(Box::new(re)),
30         Err(_) => std::ptr::null_mut::<RateEstimator>(),
31     }
32 }
33 
34 /// # Safety
35 ///
36 /// To use this function safely, `re` must be a pointer returned from
37 /// rate_estimator_create, or null.
38 #[no_mangle]
rate_estimator_destroy(re: *mut RateEstimator)39 pub unsafe extern "C" fn rate_estimator_destroy(re: *mut RateEstimator) {
40     if re.is_null() {
41         return;
42     }
43 
44     drop(Box::from_raw(re));
45 }
46 
47 /// # Safety
48 ///
49 /// To use this function safely, `re` must be a pointer returned from
50 /// rate_estimator_create, or null.
51 #[no_mangle]
rate_estimator_add_frames(re: *mut RateEstimator, frames: libc::c_int)52 pub unsafe extern "C" fn rate_estimator_add_frames(re: *mut RateEstimator, frames: libc::c_int) {
53     if re.is_null() {
54         return;
55     }
56 
57     (*re).add_frames(frames)
58 }
59 
60 /// # Safety
61 ///
62 /// To use this function safely, `re` must be a pointer returned from
63 /// rate_estimator_create, or null, and `now` must be a valid pointer to a
64 /// timespec.
65 #[no_mangle]
rate_estimator_check( re: *mut RateEstimator, level: libc::c_int, now: *const libc::timespec, ) -> i3266 pub unsafe extern "C" fn rate_estimator_check(
67     re: *mut RateEstimator,
68     level: libc::c_int,
69     now: *const libc::timespec,
70 ) -> i32 {
71     if re.is_null() || now.is_null() {
72         return 0;
73     }
74 
75     let ts = &*now;
76     if ts.tv_sec < 0 || ts.tv_nsec < 0 {
77         return 0;
78     }
79     let secs = ts.tv_sec as u64 + (ts.tv_nsec / 1_000_000_000) as u64;
80     let nsecs = (ts.tv_nsec % 1_000_000_000) as u32;
81     let now = Duration::new(secs, nsecs);
82 
83     (*re).update_estimated_rate(level, now) as i32
84 }
85 
86 /// # Safety
87 ///
88 /// To use this function safely, `re` must be a pointer returned from
89 /// rate_estimator_create, or null.
90 #[no_mangle]
rate_estimator_get_rate(re: *const RateEstimator) -> libc::c_double91 pub unsafe extern "C" fn rate_estimator_get_rate(re: *const RateEstimator) -> libc::c_double {
92     if re.is_null() {
93         return 0.0;
94     }
95 
96     (*re).get_estimated_rate()
97 }
98 
99 /// # Safety
100 ///
101 /// To use this function safely, `re` must be a pointer returned from
102 /// rate_estimator_create, or null.
103 #[no_mangle]
rate_estimator_reset_rate(re: *mut RateEstimator, rate: libc::c_uint)104 pub unsafe extern "C" fn rate_estimator_reset_rate(re: *mut RateEstimator, rate: libc::c_uint) {
105     if re.is_null() {
106         return;
107     }
108 
109     (*re).reset_rate(rate)
110 }
111