Changeset 5457 for pjproject


Ignore:
Timestamp:
Oct 10, 2016 8:20:54 AM (7 years ago)
Author:
nanang
Message:

Re #1961: Updated Android pj_gettimestamp() implementation to use ANDROID_ALARM_ELAPSED_REALTIME.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjlib/src/pj/os_timestamp_posix.c

    r5447 r5457  
    162162 
    163163#elif defined(__ANDROID__) 
    164 #include <errno.h> 
    165 #include <time.h> 
    166  
    167 #if !defined(CLOCK_BOOTTIME) 
    168 #  include <linux/android_alarm.h> 
    169 #  include <fcntl.h> 
    170 #endif 
     164 
     165#include <errno.h> 
     166#include <linux/android_alarm.h> 
     167#include <fcntl.h> 
    171168 
    172169#define NSEC_PER_SEC    1000000000 
    173170 
     171static int s_alarm_fd = -1; 
     172 
     173void close_alarm_fd() 
     174{ 
     175    if (s_alarm_fd != -1) 
     176        close(s_alarm_fd); 
     177    s_alarm_fd = -1; 
     178} 
     179 
    174180PJ_DEF(pj_status_t) pj_get_timestamp(pj_timestamp *ts) 
    175181{ 
    176182    struct timespec tp; 
    177183 
    178 #if defined(CLOCK_BOOTTIME) 
    179     /* Use CLOCK_BOOTTIME if supported */ 
    180     if (clock_gettime(CLOCK_BOOTTIME, &tp) != 0) { 
    181         return PJ_RETURN_OS_ERROR(pj_get_native_os_error()); 
    182     } 
    183 #else 
    184     /* For older NDK version, use ANDROID_ALARM_ELAPSED_REALTIME */ 
    185     static int s_fd = -1; 
    186  
    187     if (s_fd == -1) { 
     184    if (s_alarm_fd == -1) { 
    188185        int fd = open("/dev/alarm", O_RDONLY); 
    189186        if (fd >= 0) { 
    190             s_fd = fd; 
    191             //close(fd); 
     187            s_alarm_fd = fd; 
     188            pj_atexit(&close_alarm_fd); 
    192189        } else { 
    193190            return PJ_RETURN_OS_ERROR(pj_get_native_os_error());                 
    194191        } 
    195192    } 
    196     int err = ioctl(s_fd, 
     193    int err = ioctl(s_alarm_fd, 
    197194            ANDROID_ALARM_GET_TIME(ANDROID_ALARM_ELAPSED_REALTIME), &tp); 
    198195 
     
    200197        return PJ_RETURN_OS_ERROR(pj_get_native_os_error()); 
    201198    } 
    202 #endif 
    203199 
    204200    ts->u64 = tp.tv_sec; 
Note: See TracChangeset for help on using the changeset viewer.