window-basic-auto-config.hpp 5.2 KB

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