void run() { running = true; preRun(); while (running) { if (selector.wait(waitFor())) { receive(); } else { timeout(); } } postRun(); }
void QLuaApplication::Private::Thread::run() { QMutexLocker locker(&mutex); preRun(); QEventLoop theloop; loop = &theloop; locker.unlock(); loop->exec(); locker.relock(); loop = 0; postRun(); }
/** @brief Launch program made up of tokens in parameter TokenContainer and terminate when done @param tc TokenContainer struct with both the tokens and their count @return true to continue execution of the shell */ bool launchCommands(TokenContainer *tc, char *input) { pid_t child; struct rusage start; bool bg = checkBackgroundJob(tc); preRun(tc); getrusage(RUSAGE_CHILDREN, &start); addToHistory(input); if (strcmp(tc->tokens[0], "last10") == 0) printLastTen(); else if (strcmp(tc->tokens[0], "cd") == 0) changeDirectory(tc); else { child = fork(); if(!child) { if(bg) { int out = open("/dev/null", O_WRONLY); dup2(out, 0); dup2(out, 1); dup2(out, 2); close(out); } } // Make sure pid is child process if (child == 0) { if (execvp(tc->tokens[0], tc->tokens) == -1 && !bg) { perror("Failed on child process in launchCommands"); } exit(EXIT_FAILURE); } else if (child < 0) { perror("Error forking in launchCommands"); } // Parent processes code else { if (bg) { // Add the child to the backgroundJobs container bgJobs.backgroundJobs[bgJobs.bgJobCounter] = child; // Collect command for postrun output strcpy(bgJobs.bgCommands[bgJobs.bgJobCounter],tc->tokens[0]); // Associate starting time with background process bgJobs.starts[bgJobs.bgJobCounter++] = start; printf("[%d] %d : Running\n", bgJobs.bgJobCounter, child); } else { int returnStatus; waitpid(child, &returnStatus, 0); postRun(tc, &start, child); } } } return true; }
void FFmpegLibAvStreamImpl::run() { Mutex lockMutex; ScopedLock lock (lockMutex); preRun(); // Minimal samples for time-period passed by one frame multiplied by two(speed of filling audio buffer should be faster than audio playback), // limited by 32767 as restriction of ffmpeg-wrapper const unsigned short samplesPart = std::min((double)32767, (double)(m_audioFormat.m_bytePerSample * m_audioFormat.m_channelsNb * m_audioFormat.m_sampleRate) / m_frame_rate * 2); const unsigned int minBlockSize = samplesPart * m_audioFormat.m_bytePerSample * m_audioFormat.m_channelsNb; unsigned char * pAudioData = minBlockSize > 0 ? new unsigned char[minBlockSize * 2] : NULL; // ... * 2], because it could read more than minBlockSize try { // bool videoGrabbingInProcess; bool audioGrabbingInProcess; // bool isPlaybackStarted = false; size_t drop_frame_nb = 0; // while (m_shadowThreadStop == false) { unsigned int videoWriteFlag = 0; audioGrabbingInProcess = false; videoGrabbingInProcess = false; // // Grab Audio Buffer // if (isHasAudio() && pAudioData != NULL) { const unsigned int space_audio_size = m_audio_buffer.freeSpaceSize(); if (space_audio_size > minBlockSize && m_audio_buffering_finished == false) { double max_avail_time_micros = -1.0; // // Limit audio-grabbing by time to avoid video artifacts // if (isPlaybackStarted) { // // 1. To avoid audio-artifacts, we should guaranty that audio-buffer filled. // So, as less audio-buffer filled, as more available time for audio grabbing. // Even if it will become to appear the video artifacts. // // 2. To avoid video-artifacts, we should TRY spend as less as possible time for // audio grabbing. // const double audio_bufferedTimeSec = (double)(m_audio_buffer.size() - space_audio_size) / (double)(m_audioFormat.m_bytePerSample * m_audioFormat.m_channelsNb * m_audioFormat.m_sampleRate); // const double audio_bufferedRatio = (double)(m_audio_buffer.size() - space_audio_size) / (double)m_audio_buffer.size(); if (audio_bufferedRatio > 0.25) { const double video_frameTimeSec = 1.0 / m_frame_rate; // audio_bufferedRatio: 0.25 ... 0.5 ... 1.0 double coeff = 0.25 / (audio_bufferedRatio - 0.25 + std::numeric_limits<double>::min()) - 0.333; // +INF ... 0.7 ... 0.0 max_avail_time_micros = video_frameTimeSec * coeff * 1000000.0; // +INF ... frame*0.7 ... 0.0 } else { if (m_useRibbonTimeStrategy == false) videoWriteFlag |= 1; // disable decoding during searching } } const int bytesread = FFmpegWrapper::getAudioSamples(m_audioIndex, 123456789, m_audioFormat.m_channelsNb, m_audioFormat.m_avSampleFormat, m_audioFormat.m_sampleRate, samplesPart, pAudioData, max_avail_time_micros) * m_audioFormat.m_bytePerSample * m_audioFormat.m_channelsNb; if (bytesread > 0) { m_audio_buffer.write (pAudioData, bytesread); audioGrabbingInProcess = true; } else { m_audio_buffering_finished = true; if (bytesread < 0) throw std::runtime_error("Audio failed"); } } } // // Grab Video Buffer // if (isHasVideo()) { if (m_video_buffer.isStreamFinished() == false) { if (m_video_buffer.isBufferFull() == false) { if (m_useRibbonTimeStrategy == true) { drop_frame_nb = 0; } else { // // Provide little acceleration for video grabbing // Test shows, that 20-frame buffer accelerated enough by 1-frame dropping in half of the buffer-size. // if (isPlaybackStarted && m_video_buffer.isStreamFinished() == false) { double aspect = (double)m_video_buffer.freeSpaceSize() / (double)m_video_buffer.size(); drop_frame_nb = aspect * 2; } } m_video_buffer.writeFrame (videoWriteFlag, drop_frame_nb); videoGrabbingInProcess = true; } } else { m_renderer.quit(false); } } if (isPlaybackStarted && isPlaybackFinished()) { break; } // // If grabbings do not work(waste a time): // // 1. If it is first wasting of the time and playback did not started before, we may start playback. // 2. It may couse to get too many empty loops, so locks this thread till some buffer opens for writing, or interrupt for exit // if (audioGrabbingInProcess == false && videoGrabbingInProcess == false) { if (isPlaybackStarted == false) { isPlaybackStarted = true; startPlayback(); } if (m_shadowThreadStop == false) m_threadLocker.wait (& lockMutex); } } } catch (const std::exception & error) { //OSG_WARN << "FFmpegLibAvStreamImpl::run : " << error.what() << std::endl; av_log(NULL, AV_LOG_WARNING, "FFmpegLibAvStreamImpl::run : %s", error.what()); } catch (...) { //OSG_WARN << "FFmpegLibAvStreamImpl::run : unhandled exception" << std::endl; av_log(NULL, AV_LOG_WARNING, "FFmpegLibAvStreamImpl::run : unhandled exception"); } if (pAudioData) delete []pAudioData; m_shadowThreadStop = true; postRun(); }