window-basic-auto-config.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #pragma once
  2. #include <QWizard>
  3. #include <QPointer>
  4. #include <QFormLayout>
  5. #include <QWizardPage>
  6. #include <condition_variable>
  7. #include <utility>
  8. #include <thread>
  9. #include <memory>
  10. #include <vector>
  11. #include <string>
  12. #include <mutex>
  13. class Ui_AutoConfigStartPage;
  14. class Ui_AutoConfigVideoPage;
  15. class Ui_AutoConfigStreamPage;
  16. class Ui_AutoConfigTestPage;
  17. class AutoConfigStreamPage;
  18. class Auth;
  19. class AutoConfig : public QWizard {
  20. Q_OBJECT
  21. friend class AutoConfigStartPage;
  22. friend class AutoConfigVideoPage;
  23. friend class AutoConfigStreamPage;
  24. friend class AutoConfigTestPage;
  25. enum class Type {
  26. Invalid,
  27. Streaming,
  28. Recording,
  29. VirtualCam,
  30. };
  31. enum class Service {
  32. Twitch,
  33. YouTube,
  34. Other,
  35. };
  36. enum class Encoder {
  37. x264,
  38. NVENC,
  39. QSV,
  40. AMD,
  41. Apple,
  42. Stream,
  43. };
  44. enum class Quality {
  45. Stream,
  46. High,
  47. };
  48. enum class FPSType : int {
  49. PreferHighFPS,
  50. PreferHighRes,
  51. UseCurrent,
  52. fps30,
  53. fps60,
  54. };
  55. static inline const char *GetEncoderId(Encoder enc);
  56. AutoConfigStreamPage *streamPage = nullptr;
  57. Service service = Service::Other;
  58. Quality recordingQuality = Quality::Stream;
  59. Encoder recordingEncoder = Encoder::Stream;
  60. Encoder streamingEncoder = Encoder::x264;
  61. Type type = Type::Streaming;
  62. FPSType fpsType = FPSType::PreferHighFPS;
  63. int idealBitrate = 2500;
  64. int baseResolutionCX = 1920;
  65. int baseResolutionCY = 1080;
  66. int idealResolutionCX = 1280;
  67. int idealResolutionCY = 720;
  68. int idealFPSNum = 60;
  69. int idealFPSDen = 1;
  70. std::string serviceName;
  71. std::string serverName;
  72. std::string server;
  73. std::string key;
  74. bool hardwareEncodingAvailable = false;
  75. bool nvencAvailable = false;
  76. bool qsvAvailable = false;
  77. bool vceAvailable = false;
  78. bool appleAvailable = false;
  79. int startingBitrate = 2500;
  80. bool customServer = false;
  81. bool bandwidthTest = false;
  82. bool testRegions = true;
  83. bool twitchAuto = false;
  84. bool regionUS = true;
  85. bool regionEU = true;
  86. bool regionAsia = true;
  87. bool regionOther = true;
  88. bool preferHighFPS = false;
  89. bool preferHardware = false;
  90. int specificFPSNum = 0;
  91. int specificFPSDen = 0;
  92. void TestHardwareEncoding();
  93. bool CanTestServer(const char *server);
  94. virtual void done(int result) override;
  95. void SaveStreamSettings();
  96. void SaveSettings();
  97. public:
  98. AutoConfig(QWidget *parent);
  99. ~AutoConfig();
  100. enum Page {
  101. StartPage,
  102. VideoPage,
  103. StreamPage,
  104. TestPage,
  105. };
  106. };
  107. class AutoConfigStartPage : public QWizardPage {
  108. Q_OBJECT
  109. friend class AutoConfig;
  110. std::unique_ptr<Ui_AutoConfigStartPage> ui;
  111. public:
  112. AutoConfigStartPage(QWidget *parent = nullptr);
  113. ~AutoConfigStartPage();
  114. virtual int nextId() const override;
  115. public slots:
  116. void on_prioritizeStreaming_clicked();
  117. void on_prioritizeRecording_clicked();
  118. void PrioritizeVCam();
  119. };
  120. class AutoConfigVideoPage : public QWizardPage {
  121. Q_OBJECT
  122. friend class AutoConfig;
  123. std::unique_ptr<Ui_AutoConfigVideoPage> ui;
  124. public:
  125. AutoConfigVideoPage(QWidget *parent = nullptr);
  126. ~AutoConfigVideoPage();
  127. virtual int nextId() const override;
  128. virtual bool validatePage() override;
  129. };
  130. class AutoConfigStreamPage : public QWizardPage {
  131. Q_OBJECT
  132. friend class AutoConfig;
  133. enum class Section : int {
  134. Connect,
  135. StreamKey,
  136. };
  137. std::shared_ptr<Auth> auth;
  138. std::unique_ptr<Ui_AutoConfigStreamPage> ui;
  139. QString lastService;
  140. bool ready = false;
  141. void LoadServices(bool showAll);
  142. inline bool IsCustomService() const;
  143. public:
  144. AutoConfigStreamPage(QWidget *parent = nullptr);
  145. ~AutoConfigStreamPage();
  146. virtual bool isComplete() const override;
  147. virtual int nextId() const override;
  148. virtual bool validatePage() override;
  149. void OnAuthConnected();
  150. void OnOAuthStreamKeyConnected();
  151. public slots:
  152. void on_show_clicked();
  153. void on_connectAccount_clicked();
  154. void on_disconnectAccount_clicked();
  155. void on_useStreamKey_clicked();
  156. void ServiceChanged();
  157. void UpdateKeyLink();
  158. void UpdateMoreInfoLink();
  159. void UpdateServerList();
  160. void UpdateCompleted();
  161. void reset_service_ui_fields(std::string &service);
  162. };
  163. class AutoConfigTestPage : public QWizardPage {
  164. Q_OBJECT
  165. friend class AutoConfig;
  166. QPointer<QFormLayout> results;
  167. std::unique_ptr<Ui_AutoConfigTestPage> ui;
  168. std::thread testThread;
  169. std::condition_variable cv;
  170. std::mutex m;
  171. bool cancel = false;
  172. bool started = false;
  173. enum class Stage {
  174. Starting,
  175. BandwidthTest,
  176. StreamEncoder,
  177. RecordingEncoder,
  178. Finished,
  179. };
  180. Stage stage = Stage::Starting;
  181. bool softwareTested = false;
  182. void StartBandwidthStage();
  183. void StartStreamEncoderStage();
  184. void StartRecordingEncoderStage();
  185. void FindIdealHardwareResolution();
  186. bool TestSoftwareEncoding();
  187. void TestBandwidthThread();
  188. void TestStreamEncoderThread();
  189. void TestRecordingEncoderThread();
  190. void FinalizeResults();
  191. struct ServerInfo {
  192. std::string name;
  193. std::string address;
  194. int bitrate = 0;
  195. int ms = -1;
  196. inline ServerInfo() {}
  197. inline ServerInfo(const char *name_, const char *address_)
  198. : name(name_),
  199. address(address_)
  200. {
  201. }
  202. };
  203. void GetServers(std::vector<ServerInfo> &servers);
  204. public:
  205. AutoConfigTestPage(QWidget *parent = nullptr);
  206. ~AutoConfigTestPage();
  207. virtual void initializePage() override;
  208. virtual void cleanupPage() override;
  209. virtual bool isComplete() const override;
  210. virtual int nextId() const override;
  211. public slots:
  212. void NextStage();
  213. void UpdateMessage(QString message);
  214. void Failure(QString message);
  215. void Progress(int percentage);
  216. };