window-basic-auto-config.hpp 5.6 KB

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