Start thread
| 42 | |
| 43 | // Start thread |
| 44 | void PlayerPrivate::run() |
| 45 | { |
| 46 | // bail if no reader set |
| 47 | if (!reader) |
| 48 | return; |
| 49 | |
| 50 | // Start the threads |
| 51 | if (reader->info.has_audio) |
| 52 | audioPlayback->startThread(Priority::high); |
| 53 | if (reader->info.has_video) { |
| 54 | videoCache->startThread(Priority::high); |
| 55 | videoPlayback->startThread(Priority::high); |
| 56 | } |
| 57 | |
| 58 | using std::chrono::duration_cast; |
| 59 | |
| 60 | // Types for storing time durations in whole and fractional microseconds |
| 61 | using micro_sec = std::chrono::microseconds; |
| 62 | using double_micro_sec = std::chrono::duration<double, micro_sec::period>; |
| 63 | |
| 64 | // Init start_time of playback |
| 65 | std::chrono::time_point<std::chrono::system_clock, std::chrono::microseconds> start_time; |
| 66 | start_time = std::chrono::time_point_cast<micro_sec>(std::chrono::system_clock::now()); ///< timestamp playback starts |
| 67 | |
| 68 | while (!threadShouldExit()) { |
| 69 | // Calculate on-screen time for a single frame |
| 70 | int frame_speed = std::max(abs(speed), 1); |
| 71 | const auto frame_duration = double_micro_sec(1000000.0 / (reader->info.fps.ToDouble() * frame_speed)); |
| 72 | const auto max_sleep = frame_duration * 4; ///< Don't sleep longer than X times a frame duration |
| 73 | |
| 74 | // Pausing Code (which re-syncs audio/video times) |
| 75 | // - If speed is zero or speed changes |
| 76 | // - If pre-roll is not ready (This should allow scrubbing of the timeline without waiting on pre-roll) |
| 77 | bool wait_paused_hold = (speed == 0 && video_position == last_video_position); |
| 78 | bool wait_speed_change = (speed != 0 && last_speed != speed); |
| 79 | bool cache_ready = videoCache->isReady(); |
| 80 | bool wait_preroll = (speed != 0 && !is_dirty && !cache_ready); |
| 81 | bool should_wait = (wait_paused_hold || wait_speed_change || wait_preroll); |
| 82 | |
| 83 | if (should_wait) |
| 84 | { |
| 85 | // Sleep for a fraction of frame duration |
| 86 | std::this_thread::sleep_for(frame_duration / 4); |
| 87 | |
| 88 | // Reset current playback start time |
| 89 | start_time = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now()); |
| 90 | playback_frames = 0; |
| 91 | last_speed = speed; |
| 92 | |
| 93 | // Seek audio thread (since audio is also paused) |
| 94 | audioPlayback->Seek(video_position); |
| 95 | |
| 96 | continue; |
| 97 | } |
| 98 | |
| 99 | // Get the current video frame |
| 100 | frame = getFrame(); |
| 101 |