-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstreampanel.cpp
152 lines (127 loc) · 4.32 KB
/
streampanel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "streampanel.h"
#include <QPushButton>
#include <QGridLayout>
#include <QThreadPool>
#include <iostream>
#include <tchar.h>
#include <windows.h>
using namespace std;
#define BUFSIZE 8192
HANDLE stdInRead;
HANDLE stdInWrite;
HANDLE stdOutRead;
HANDLE stdOutWrite;
HANDLE stdErrRead;
HANDLE stdErrWrite;
StreamPanel::StreamPanel(QMainWindow *parent) : Panel(parent)
{
text = new QTextEdit();
QPushButton *test = new QPushButton("Test");
test->setMaximumWidth(60);
connect(test, SIGNAL(clicked()), this, SLOT(test()));
QPushButton *clear = new QPushButton("Clear");
clear->setMaximumWidth(60);
connect(clear, SIGNAL(clicked()), this, SLOT(clear()));
QGridLayout *layout = new QGridLayout();
layout->addWidget(text, 0, 0, 1, 4);
layout->addWidget(test, 1, 2, 1, 1);
layout->addWidget(clear, 1, 3, 1, 1);
setLayout(layout);
}
void StreamPanel::test()
{
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(sa);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
if (!CreatePipe(&stdOutRead, &stdOutWrite, &sa, 0)) {
cout << "stdOut create pipe error: " << GetLastError() << endl;
}
if (!SetHandleInformation(stdOutRead, HANDLE_FLAG_INHERIT, 0)) {
cout << "stdOut set handle information: " << GetLastError() << endl;
}
if (!CreatePipe(&stdInRead, &stdInWrite, &sa, 0)) {
cout << "stdIn create pipe error: " << GetLastError() << endl;
}
if (!SetHandleInformation(stdInWrite, HANDLE_FLAG_INHERIT, 0)) {
cout << "stdIn set handle information: " << GetLastError() << endl;
}
if (!CreatePipe(&stdErrRead, &stdErrWrite, &sa, 0)) {
cout << "StdErr create pipe error: " << GetLastError() << endl;
}
if (!SetHandleInformation(stdErrRead, HANDLE_FLAG_INHERIT, 0)) {
cout << "stdErr set handle information: " << GetLastError() << endl;
}
STARTUPINFO si;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
si.dwFlags |= STARTF_USESTDHANDLES;
//si.wShowWindow = FALSE;
//si.dwFlags |= STARTF_USESHOWWINDOW;
PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(pi));
LPWSTR name = (LPWSTR)L"C:\\Users\\sr996\\Projects\\contrib\\bin\\youtube-dl.exe";
LPWSTR arg = (LPWSTR)L" https://www.youtube.com/watch?v=xkb7EQgIjMA -o -";
//if (!CreateProcess(name, arg, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi)) {
if (!CreateProcess(name, arg, NULL, NULL, FALSE, NULL, NULL, NULL, &si, &pi)) {
cout << "CreateProcess failed: " << GetLastError() << endl;
}
else {
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
/*
CloseHandle(stdOutRead);
CloseHandle(stdInWrite);
CloseHandle(stdErrRead);
*/
}
SetStdHandle(STD_ERROR_HANDLE, stdErrWrite);
SetStdHandle(STD_OUTPUT_HANDLE, stdOutWrite);
SetStdHandle(STD_INPUT_HANDLE, stdInRead);
emit play();
/*
Streamer *errStreamer = new Streamer(stdErrRead, this);
QThreadPool::globalInstance()->tryStart(errStreamer);
Streamer *outStreamer = new Streamer(stdOutRead, this);
QThreadPool::globalInstance()->tryStart(outStreamer);
*/
}
Streamer::Streamer(void *handle, Panel *panel)
{
this->handle = handle;
this->panel = panel;
}
void Streamer::run()
{
HANDLE std = (HANDLE)handle;
DWORD dwRead;
CHAR chBuf[BUFSIZE];
BOOL bSuccess = FALSE;
BOOL bFirstPass = TRUE;
for (;;) {
bSuccess = ReadFile(std, chBuf, BUFSIZE, &dwRead, NULL);
if (!bSuccess || dwRead == 0)
break;
if (std == stdErrRead) {
((StreamPanel*)panel)->text->append(QString(chBuf).mid(0, dwRead));
}
else if (std == stdOutRead) {
QByteArray array;
array.resize(dwRead);
memcpy(array.data(), chBuf, dwRead);
((StreamPanel*)panel)->data += array;
cout << "data size: " << ((StreamPanel*)panel)->data.size() << endl;
if (bFirstPass && ((StreamPanel*)panel)->data.size() > 1000000) {
((StreamPanel*)panel)->emit play();
bFirstPass = FALSE;
}
}
}
}
void StreamPanel::clear()
{
text->setText("");
}