Eddie James | 18e1e46 | 2018-12-11 13:24:36 -0600 | [diff] [blame] | 1 | #include "ikvm_manager.hpp" |
| 2 | |
| 3 | #include <thread> |
| 4 | |
| 5 | namespace ikvm |
| 6 | { |
Eddie James | 18e1e46 | 2018-12-11 13:24:36 -0600 | [diff] [blame] | 7 | Manager::Manager(const Args& args) : |
| 8 | continueExecuting(true), serverDone(false), videoDone(true), |
Marvin Lin | fe685fb | 2022-10-25 16:20:08 +0800 | [diff] [blame] | 9 | input(args.getKeyboardPath(), args.getPointerPath(), args.getUdcName()), |
George Liu | f79f6f5 | 2022-07-06 09:32:35 +0800 | [diff] [blame] | 10 | video(args.getVideoPath(), input, args.getFrameRate(), |
| 11 | args.getSubsampling()), |
Eddie James | 18e1e46 | 2018-12-11 13:24:36 -0600 | [diff] [blame] | 12 | server(args, input, video) |
George Liu | f79f6f5 | 2022-07-06 09:32:35 +0800 | [diff] [blame] | 13 | {} |
Eddie James | 18e1e46 | 2018-12-11 13:24:36 -0600 | [diff] [blame] | 14 | |
| 15 | void Manager::run() |
| 16 | { |
| 17 | std::thread run(serverThread, this); |
| 18 | |
| 19 | while (continueExecuting) |
| 20 | { |
| 21 | if (server.wantsFrame()) |
| 22 | { |
Jae Hyun Yoo | 0049bfa | 2019-03-06 15:39:58 -0800 | [diff] [blame] | 23 | video.start(); |
Eddie James | 18e1e46 | 2018-12-11 13:24:36 -0600 | [diff] [blame] | 24 | video.getFrame(); |
| 25 | server.sendFrame(); |
| 26 | } |
| 27 | else |
| 28 | { |
| 29 | video.stop(); |
| 30 | } |
| 31 | |
| 32 | if (video.needsResize()) |
| 33 | { |
| 34 | videoDone = false; |
| 35 | waitServer(); |
| 36 | video.resize(); |
| 37 | server.resize(); |
| 38 | setVideoDone(); |
| 39 | } |
| 40 | else |
| 41 | { |
| 42 | setVideoDone(); |
| 43 | waitServer(); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | run.join(); |
| 48 | } |
| 49 | |
| 50 | void Manager::serverThread(Manager* manager) |
| 51 | { |
| 52 | while (manager->continueExecuting) |
| 53 | { |
| 54 | manager->server.run(); |
| 55 | manager->setServerDone(); |
| 56 | manager->waitVideo(); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void Manager::setServerDone() |
| 61 | { |
| 62 | std::unique_lock<std::mutex> ulock(lock); |
| 63 | |
| 64 | serverDone = true; |
| 65 | sync.notify_all(); |
| 66 | } |
| 67 | |
| 68 | void Manager::setVideoDone() |
| 69 | { |
| 70 | std::unique_lock<std::mutex> ulock(lock); |
| 71 | |
| 72 | videoDone = true; |
| 73 | sync.notify_all(); |
| 74 | } |
| 75 | |
| 76 | void Manager::waitServer() |
| 77 | { |
| 78 | std::unique_lock<std::mutex> ulock(lock); |
| 79 | |
| 80 | while (!serverDone) |
| 81 | { |
| 82 | sync.wait(ulock); |
| 83 | } |
| 84 | |
| 85 | serverDone = false; |
| 86 | } |
| 87 | |
| 88 | void Manager::waitVideo() |
| 89 | { |
| 90 | std::unique_lock<std::mutex> ulock(lock); |
| 91 | |
| 92 | while (!videoDone) |
| 93 | { |
| 94 | sync.wait(ulock); |
| 95 | } |
| 96 | |
| 97 | // don't reset videoDone |
| 98 | } |
| 99 | |
| 100 | } // namespace ikvm |