void wxOSXTimerImpl::Stop() { if (m_info->m_timerRef) { CFRunLoopTimerInvalidate( m_info->m_timerRef ); CFRelease( m_info->m_timerRef ); } m_info->m_timerRef = kInvalidID; }
wxOSXTimerImpl::~wxOSXTimerImpl() { if (m_info->m_timerRef) { if ( CFRunLoopTimerIsValid(m_info->m_timerRef) ) CFRunLoopTimerInvalidate( m_info->m_timerRef ); CFRelease( m_info->m_timerRef ); } delete m_info; }
void stopSharedTimer() { WebThreadContext *threadContext = WebThreadCurrentContext(); CFRunLoopTimerRef sharedTimer = threadContext->sharedTimer; if (sharedTimer) { CFRunLoopTimerInvalidate(sharedTimer); CFRelease(sharedTimer); threadContext->sharedTimer = NULL; } }
/* extern */ void _CFTypeInvalidate(CFTypeRef obj) { CFTypeID t = CFGetTypeID(obj); /* Invalidate according to type of object. */ if (t == CFRunLoopSourceGetTypeID()) { CFRunLoopSourceInvalidate((CFRunLoopSourceRef)obj); } else if (t == CFMachPortGetTypeID()) { CFMachPortInvalidate((CFMachPortRef)obj); } else if (t == CFSocketGetTypeID()) { CFSocketInvalidate((CFSocketRef)obj); } /* For scheduled types of objects, it is invalidated by setting the client to NULL. */ else if (t == CFReadStreamGetTypeID()) { CFReadStreamSetClient((CFReadStreamRef)obj, kCFStreamEventNone, NULL, NULL); } else if (t == CFWriteStreamGetTypeID()) { CFWriteStreamSetClient((CFWriteStreamRef)obj, kCFStreamEventNone, NULL, NULL); } else if (t == CFHostGetTypeID()) { CFHostSetClient((CFHostRef)obj, NULL, NULL); } else if (t == SCNetworkReachabilityGetTypeID()) { SCNetworkReachabilitySetCallback((SCNetworkReachabilityRef)obj, NULL, NULL); } else if (t == CFRunLoopTimerGetTypeID()) { CFRunLoopTimerInvalidate((CFRunLoopTimerRef)obj); } else if (t == CFNetServiceGetTypeID()) { CFNetServiceSetClient((CFNetServiceRef)obj, NULL, NULL); } else if (t == CFNetServiceBrowserGetTypeID()) { CFNetServiceBrowserInvalidate((CFNetServiceBrowserRef)obj); } else if (t == CFNetServiceMonitorGetTypeID()) { CFNetServiceMonitorInvalidate((CFNetServiceMonitorRef)obj); } else if (t == SCNetworkReachabilityGetTypeID()) { SCNetworkConnectionStop((SCNetworkConnectionRef)obj, FALSE); } }
// recveive - receive a packet // Inputs: // num = device to receive from (zero based) // buf = buffer to receive packet // len = buffer's size // timeout = time to wait, in milliseconds // Output: // number of bytes received, or -1 on error // int pjrc_rawhid::receive(int num, void *buf, int len, int timeout) { hid_t *hid; buffer_t *b; CFRunLoopTimerRef timer=NULL; CFRunLoopTimerContext context; int ret=0, timeout_occurred=0; if (len < 1) return 0; hid = get_hid(num); if (!hid || !hid->open) return -1; if ((b = hid->first_buffer) != NULL) { if (len > b->len) len = b->len; memcpy(buf, b->buf, len); hid->first_buffer = b->next; free(b); return len; } memset(&context, 0, sizeof(context)); context.info = &timeout_occurred; timer = CFRunLoopTimerCreate(NULL, CFAbsoluteTimeGetCurrent() + (double)timeout / 1000.0, 0, 0, 0, timeout_callback, &context); CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopDefaultMode); the_correct_runloop = CFRunLoopGetCurrent(); //qDebug("--"); while (1) { //qDebug("."); CFRunLoopRun(); // Found the problem: somehow the input_callback does not // stop this CFRunLoopRun because it is hooked to a different run loop !!! // Hence the use of the "correct_runloop" variable above. //qDebug(" .."); if ((b = hid->first_buffer) != NULL) { if (len > b->len) len = b->len; memcpy(buf, b->buf, len); hid->first_buffer = b->next; free(b); ret = len; //qDebug("*************"); break; } if (!hid->open) { printf("pjrc_rawhid_recv, device not open\n"); ret = -1; break; } if (timeout_occurred) break; } CFRunLoopTimerInvalidate(timer); CFRelease(timer); return ret; }
void setSharedTimerFireTime(double fireTime) { ASSERT(sharedTimerFiredFunction); if (sharedTimer) { CFRunLoopTimerInvalidate(sharedTimer); CFRelease(sharedTimer); } CFAbsoluteTime fireDate = fireTime - kCFAbsoluteTimeIntervalSince1970; sharedTimer = CFRunLoopTimerCreate(0, fireDate, 0, 0, 0, timerFired, 0); CFRunLoopAddTimer(CFRunLoopGetCurrent(), sharedTimer, kCFRunLoopCommonModes); }
void Audio_Stream::close() { AS_TRACE("%s: enter\n", __PRETTY_FUNCTION__); if (m_watchdogTimer) { CFRunLoopTimerInvalidate(m_watchdogTimer); CFRelease(m_watchdogTimer), m_watchdogTimer = 0; } /* Close the HTTP stream first so that the audio stream parser isn't fed with more data to parse */ if (m_httpStreamRunning) { m_httpStream->close(); m_httpStreamRunning = false; } if (m_audioStreamParserRunning) { if (m_audioFileStream) { if (AudioFileStreamClose(m_audioFileStream) != 0) { AS_TRACE("%s: AudioFileStreamClose failed\n", __PRETTY_FUNCTION__); } m_audioFileStream = 0; } m_audioStreamParserRunning = false; } closeAudioQueue(); if (FAILED != state()) { /* * Set the stream state to stopped if the stream was stopped successfully. * We don't want to cause a spurious stopped state as the fail state should * be the final state in case the stream failed. */ setState(STOPPED); } /* * Free any remaining queud packets for encoding. */ queued_packet_t *cur = m_queuedHead; while (cur) { queued_packet_t *tmp = cur->next; free(cur); cur = tmp; } m_queuedHead = m_queuedTail = 0; AS_TRACE("%s: leave\n", __PRETTY_FUNCTION__); }
void Timer_cancel(TimerRef timer) { if (timer == NULL) { return; } timer->enabled = 0; timer->func = NULL; if (timer->rls) { CFRunLoopTimerInvalidate(timer->rls); my_CFRelease(&timer->rls); } return; }
int rawhid_recv(int num, void *buf, int len, int timeout) { //fprintf(stderr,"rawhid_recv start len: %d\n",len); //fprintf(stderr,"rawhid_recv start \n"); hid_t *hid; buffer_t *b; CFRunLoopTimerRef timer=NULL; CFRunLoopTimerContext context; int ret=0, timeout_occurred=0; if (len < 1) return 0; hid = get_hid(num); if (!hid || !hid->open) return -1; if ((b = hid->first_buffer) != NULL) { if (len > b->len) len = b->len; memcpy(buf, b->buf, len); hid->first_buffer = b->next; free(b); // fprintf(stderr,"rawhid_recv A len: %d\n\n",len); return len; } memset(&context, 0, sizeof(context)); context.info = &timeout_occurred; timer = CFRunLoopTimerCreate(NULL, CFAbsoluteTimeGetCurrent() + (double)timeout / 1000.0, 0, 0, 0, timeout_callback, &context); CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopDefaultMode); while (1) { CFRunLoopRun(); if ((b = hid->first_buffer) != NULL) { if (len > b->len) len = b->len; memcpy(buf, b->buf, len); hid->first_buffer = b->next; free(b); ret = len; break; } if (!hid->open) { //printf("rawhid_recv, device not open\n"); ret = -1; break; } if (timeout_occurred) break; } CFRunLoopTimerInvalidate(timer); CFRelease(timer); //fprintf(stderr,"rawhid_recv ret: %d\n",ret); return ret; }
/** * @brief Destroy a CFMIDIRunLoopSource instance. * Free all resources occupied by the runloop soruce and release all referenced objects. * @public @memberof CFMIDIRunLoopSource * @param source The runloop source. */ void CFMIDIRunLoopSourceDestroy( struct CFMIDIRunLoopSource * source ) { int i; MIDIPrecondReturn( source != NULL, EFAULT, (void)0 ); if( source->cfrlt != NULL ) { CFRunLoopTimerInvalidate( source->cfrlt ); CFRelease( source->cfrlt ); } for( i=0; i<source->length; i++ ) { if( source->cfrls[i] != NULL ) { CFRunLoopSourceInvalidate( source->cfrls[i] ); CFRelease( source->cfrls[i] ); } } free( source ); }
void setSharedTimerFireTime(double fireTime) { ASSERT(sharedTimerFiredFunction); WebThreadContext *threadContext = WebThreadCurrentContext(); CFRunLoopTimerRef sharedTimer = threadContext->sharedTimer; if (sharedTimer) { CFRunLoopTimerInvalidate(sharedTimer); CFRelease(sharedTimer); } CFAbsoluteTime fireDate = fireTime - kCFAbsoluteTimeIntervalSince1970; sharedTimer = CFRunLoopTimerCreate(0, fireDate, 0, 0, 0, timerFired, 0); threadContext->sharedTimer = sharedTimer; CFRunLoopAddTimer(threadContext->runLoop, sharedTimer, kCFRunLoopDefaultMode); }
void timer_cancel(timer_callout_t * callout) { if (callout == NULL) { return; } callout->enabled = FALSE; callout->func = NULL; callout->time_generation = S_time_generation; if (callout->timer_source) { my_log(LOG_DEBUG, "timer: freeing timer source"); CFRunLoopTimerInvalidate(callout->timer_source); CFRelease(callout->timer_source); callout->timer_source = 0; } return; }
/** * @brief receive - receive a packet * @param[in] num device to receive from (unused now) * @param[in] buf buffer to receive packet * @param[in] len buffer's size * @param[in] timeout = time to wait, in milliseconds * @returns number of bytes received, or -1 on error */ int pjrc_rawhid::receive(int, void *buf, int len, int timeout) { QMutexLocker locker(m_readMutex); Q_UNUSED(locker); if (!device_open) return -1; // Pass information to the callback to stop this run loop and signal if a timeout occurred struct timeout_info info; info.loopRef = CFRunLoopGetCurrent();; info.timed_out = false; CFRunLoopTimerContext context; memset(&context, 0, sizeof(context)); context.info = &info; // Set up the timer for the timeout CFRunLoopTimerRef timer; timer = CFRunLoopTimerCreate(NULL, CFAbsoluteTimeGetCurrent() + (double)timeout / 1000.0, 0, 0, 0, timeout_callback, &context); CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopDefaultMode); received_runloop = CFRunLoopGetCurrent(); // Run the CFRunLoop until either a timeout or data is available while(1) { if (buffer_count != 0) { if (len > buffer_count) len = buffer_count; memcpy(buf, buffer, len); buffer_count = 0; break; } else if (info.timed_out) { len = 0; break; } CFRunLoopRun(); // Wait for data } CFRunLoopTimerInvalidate(timer); CFRelease(timer); received_runloop = NULL; return len; }
static CFRunLoopTimerRef setupTimer (long int timeout, CFRunLoopTimerRef timer, CFRunLoopTimerCallBack callback) { if (timeout) { if (timer) CFRunLoopTimerSetNextFireDate (timer, CFAbsoluteTimeGetCurrent() + timeout * TIMER_RESOLUTION); else { timer = CFRunLoopTimerCreate(kCFAllocatorDefault, CFAbsoluteTimeGetCurrent() + timeout * TIMER_RESOLUTION, kCFAbsoluteTimeIntervalSince1904, 0, 0, callback, NULL); CFRunLoopAddTimer (CFRunLoopGetCurrent(), timer, kCFRunLoopDefaultMode); } } else { if (timer) { CFRunLoopTimerInvalidate (timer); CFRelease (timer); timer = NULL; } } return (timer); }
/* extern */ void HttpContextClose(HttpContextRef context) { CFRunLoopRef runLoop = CFRunLoopGetCurrent(); // Check if the read stream exists. if (context->_inStream) { // Unschedule, close, and release it. CFReadStreamSetClient(context->_inStream, 0, NULL, NULL); CFReadStreamUnscheduleFromRunLoop(context->_inStream, runLoop, kCFRunLoopCommonModes); CFReadStreamClose(context->_inStream); CFRelease(context->_inStream); // Remove the reference. context->_inStream = NULL; } // Check if the write stream exists. if (context->_outStream) { // Unschedule, close, and release it. CFWriteStreamSetClient(context->_outStream, 0, NULL, NULL); CFWriteStreamUnscheduleFromRunLoop(context->_outStream, runLoop, kCFRunLoopCommonModes); CFWriteStreamClose(context->_outStream); CFRelease(context->_outStream); // Remove the reference. context->_outStream = NULL; } // Get rid of the timer, if it still exists if (context->_timer != NULL) { CFRunLoopTimerInvalidate(context->_timer); CFRelease(context->_timer); context->_timer = NULL; } }
HeapTimer::~HeapTimer() { CFRunLoopRemoveTimer(m_runLoop.get(), m_timer.get(), kCFRunLoopCommonModes); CFRunLoopTimerInvalidate(m_timer.get()); }
static void smb_update_configuration(__unused CFRunLoopTimerRef _timer, void *info) { CFStringRef address = NULL; CFDictionaryRef dict; CFStringRef name; CFStringRef serviceID = NULL; SCDynamicStoreRef store = (SCDynamicStoreRef)info; // get network configuration dict = smb_copy_global_configuration(store); // use NetBIOS name from network configuration (if available) name = CFDictionaryGetValue(dict, kSCPropNetSMBNetBIOSName); if ((name != NULL) && _SC_CFStringIsValidNetBIOSName(name)) { my_log(LOG_INFO, "NetBIOS name (network configuration) = %@", name); goto set; } // get primary service ID serviceID = copy_primary_service(store); if (serviceID == NULL) { // if no primary service goto mDNS; } // get DNS name associated with primary IP, if available address = copy_primary_ip(store, serviceID); if (address != NULL) { Boolean ok; // start reverse DNS query using primary IP address ok = ptr_query_start(address); if (ok) { // if query started goto done; } } mDNS : // get local (multicast DNS) name, if available name = SCDynamicStoreCopyLocalHostName(store); if (name != NULL) { if (_SC_CFStringIsValidNetBIOSName(name)) { CFMutableDictionaryRef newDict; my_log(LOG_INFO, "NetBIOS name (multicast DNS) = %@", name); newDict = CFDictionaryCreateMutableCopy(NULL, 0, dict); CFDictionarySetValue(newDict, kSCPropNetSMBNetBIOSName, name); CFRelease(dict); dict = newDict; CFRelease(name); goto set; } CFRelease(name); } // get "default" name name = copy_default_name(); if (name != NULL) { CFMutableDictionaryRef newDict; my_log(LOG_INFO, "NetBIOS name (default) = %@", name); newDict = CFDictionaryCreateMutableCopy(NULL, 0, dict); CFDictionarySetValue(newDict, kSCPropNetSMBNetBIOSName, name); CFRelease(dict); dict = newDict; CFRelease(name); } set : // update SMB configuration smb_set_configuration(store, dict); done : if (address != NULL) CFRelease(address); if (dict != NULL) CFRelease(dict); if (serviceID != NULL) CFRelease(serviceID); if (timer != NULL) { CFRunLoopTimerInvalidate(timer); CFRelease(timer); timer = NULL; } return; }
void Audio_Stream::open() { if (m_httpStreamRunning) { AS_TRACE("%s: already running: return\n", __PRETTY_FUNCTION__); return; } if (m_needNewQueue && m_audioQueue) { m_needNewQueue = false; closeAudioQueue(); } m_contentLength = 0; m_seekTime = 0; m_bounceCount = 0; m_firstBufferingTime = 0; m_processedPacketsCount = 0; m_bitrateBufferIndex = 0; m_initializationError = noErr; if (m_watchdogTimer) { CFRunLoopTimerInvalidate(m_watchdogTimer); CFRelease(m_watchdogTimer), m_watchdogTimer = 0; } Stream_Configuration *config = Stream_Configuration::configuration(); if (m_contentType) { CFRelease(m_contentType), m_contentType = NULL; } if (m_httpStream->open()) { AS_TRACE("%s: HTTP stream opened, buffering...\n", __PRETTY_FUNCTION__); m_httpStreamRunning = true; setState(BUFFERING); if (config->startupWatchdogPeriod > 0) { /* * Start the WD if we have one requested. In this way we can track * that the stream doesn't stuck forever on the buffering state * (for instance some network error condition) */ CFRunLoopTimerContext ctx = {0, this, NULL, NULL, NULL}; m_watchdogTimer = CFRunLoopTimerCreate(NULL, CFAbsoluteTimeGetCurrent() + config->startupWatchdogPeriod, 0, 0, 0, watchdogTimerCallback, &ctx); AS_TRACE("Starting the startup watchdog, period %i seconds\n", config->startupWatchdogPeriod); CFRunLoopAddTimer(CFRunLoopGetCurrent(), m_watchdogTimer, kCFRunLoopCommonModes); } } else { AS_TRACE("%s: failed to open the HTTP stream\n", __PRETTY_FUNCTION__); closeAndSignalError(AS_ERR_OPEN); } }
CACFRunLoopTimer::~CACFRunLoopTimer() { CFRunLoopTimerInvalidate(mRunLoopTimer); CFRelease(mRunLoopTimer); }
/* This is called by audio file stream parser when it finds packets of audio */ void Audio_Stream::streamDataCallback(void *inClientData, UInt32 inNumberBytes, UInt32 inNumberPackets, const void *inInputData, AudioStreamPacketDescription *inPacketDescriptions) { AS_TRACE("%s: inNumberBytes %u, inNumberPackets %u\n", __FUNCTION__, inNumberBytes, inNumberPackets); Audio_Stream *THIS = static_cast<Audio_Stream*>(inClientData); if (!THIS->m_audioStreamParserRunning) { AS_TRACE("%s: stray callback detected!\n", __PRETTY_FUNCTION__); return; } for (int i = 0; i < inNumberPackets; i++) { /* Allocate the packet */ UInt32 size = inPacketDescriptions[i].mDataByteSize; queued_packet_t *packet = (queued_packet_t *)malloc(sizeof(queued_packet_t) + size); if (THIS->m_bitrateBufferIndex < kAudioStreamBitrateBufferSize) { // Only keep sampling for one buffer cycle; this is to keep the counters (for instance) duration // stable. THIS->m_bitrateBuffer[THIS->m_bitrateBufferIndex++] = 8 * inPacketDescriptions[i].mDataByteSize / THIS->m_packetDuration; } /* Prepare the packet */ packet->next = NULL; packet->desc = inPacketDescriptions[i]; packet->desc.mStartOffset = 0; memcpy(packet->data, (const char *)inInputData + inPacketDescriptions[i].mStartOffset, size); if (THIS->m_queuedHead == NULL) { THIS->m_queuedHead = THIS->m_queuedTail = packet; } else { THIS->m_queuedTail->next = packet; THIS->m_queuedTail = packet; } } int count = 0; queued_packet_t *cur = THIS->m_queuedHead; while (cur) { cur = cur->next; count++; } Stream_Configuration *config = Stream_Configuration::configuration(); if (count > config->decodeQueueSize) { AudioBufferList outputBufferList; outputBufferList.mNumberBuffers = 1; outputBufferList.mBuffers[0].mNumberChannels = THIS->m_dstFormat.mChannelsPerFrame; outputBufferList.mBuffers[0].mDataByteSize = THIS->m_outputBufferSize; outputBufferList.mBuffers[0].mData = THIS->m_outputBuffer; AudioStreamPacketDescription description; description.mStartOffset = 0; description.mDataByteSize = THIS->m_outputBufferSize; description.mVariableFramesInPacket = 0; UInt32 ioOutputDataPackets = THIS->m_outputBufferSize / THIS->m_dstFormat.mBytesPerPacket; AS_TRACE("calling AudioConverterFillComplexBuffer\n"); OSStatus err = AudioConverterFillComplexBuffer(THIS->m_audioConverter, &encoderDataCallback, THIS, &ioOutputDataPackets, &outputBufferList, NULL); if (err == noErr) { AS_TRACE("%i output bytes available for the audio queue\n", (unsigned int)ioOutputDataPackets); if (THIS->m_watchdogTimer) { AS_TRACE("The stream started to play, canceling the watchdog\n"); CFRunLoopTimerInvalidate(THIS->m_watchdogTimer); CFRelease(THIS->m_watchdogTimer), THIS->m_watchdogTimer = 0; } THIS->setState(PLAYING); THIS->audioQueue()->handleAudioPackets(outputBufferList.mBuffers[0].mDataByteSize, outputBufferList.mNumberBuffers, outputBufferList.mBuffers[0].mData, &description); if (THIS->m_delegate) { THIS->m_delegate->samplesAvailable(outputBufferList, description); } for(std::list<queued_packet_t*>::iterator iter = THIS->m_processedPackets.begin(); iter != THIS->m_processedPackets.end(); iter++) { queued_packet_t *cur = *iter; free(cur); } THIS->m_processedPackets.clear(); } else { AS_TRACE("AudioConverterFillComplexBuffer failed, error %i\n", err); } } else { AS_TRACE("Less than %i packets queued, returning...\n", config->decodeQueueSize); } }
void cfRunLoopStop() { CFRunLoopTimerInvalidate(timerRef); CFRunLoopRemoveTimer(CFRunLoopGetCurrent(), timerRef, kCFRunLoopCommonModes); CFRunLoopStop(CFRunLoopGetCurrent()); timerRef = NULL; }