Changeset 5865


Ignore:
Timestamp:
Aug 23, 2018 4:42:29 AM (6 years ago)
Author:
ming
Message:

Fixed #2140: Timestamp clock issue when device is asleep in iOS

File:
1 edited

Legend:

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

    r5574 r5865  
    122122 
    123123#elif defined(PJ_DARWINOS) && PJ_DARWINOS != 0 
    124 #include <mach/mach.h> 
    125 #include <mach/clock.h> 
    126 #include <errno.h> 
     124 
     125/* SYSTEM_CLOCK will stop when the device is in deep sleep, so we use 
     126 * KERN_BOOTTIME instead.  
     127 * See ticket #2140 for more details. 
     128 */ 
     129#define USE_KERN_BOOTTIME 1 
     130 
     131#if USE_KERN_BOOTTIME 
     132#   include <sys/sysctl.h> 
     133#else 
     134#   include <mach/mach.h> 
     135#   include <mach/clock.h> 
     136#   include <errno.h> 
     137#endif 
    127138 
    128139#ifndef NSEC_PER_SEC 
     
    130141#endif 
    131142 
    132 PJ_DEF(pj_status_t) pj_get_timestamp(pj_timestamp *ts) 
    133 { 
     143#if USE_KERN_BOOTTIME 
     144static int64_t get_boottime() 
     145{ 
     146    struct timeval boottime; 
     147    int mib[2] = {CTL_KERN, KERN_BOOTTIME}; 
     148    size_t size = sizeof(boottime); 
     149    int rc; 
     150 
     151    rc = sysctl(mib, 2, &boottime, &size, NULL, 0); 
     152    if (rc != 0) 
     153      return 0; 
     154 
     155    return (int64_t)boottime.tv_sec * 1000000 + (int64_t)boottime.tv_usec; 
     156} 
     157#endif 
     158 
     159PJ_DEF(pj_status_t) pj_get_timestamp(pj_timestamp *ts) 
     160{ 
     161#if USE_KERN_BOOTTIME 
     162    int64_t before_now, after_now; 
     163    struct timeval now; 
     164 
     165    after_now = get_boottime(); 
     166    do { 
     167        before_now = after_now; 
     168        gettimeofday(&now, NULL); 
     169        after_now = get_boottime(); 
     170    } while (after_now != before_now); 
     171 
     172    ts->u64 = (int64_t)now.tv_sec * 1000000 + (int64_t)now.tv_usec; 
     173    ts->u64 -= before_now; 
     174    ts->u64 *= 1000; 
     175#else 
    134176    mach_timespec_t tp; 
    135177    int ret; 
     
    149191    ts->u64 *= NSEC_PER_SEC; 
    150192    ts->u64 += tp.tv_nsec; 
     193#endif 
    151194 
    152195    return PJ_SUCCESS; 
Note: See TracChangeset for help on using the changeset viewer.