-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathVideoStreamWidget.cpp
More file actions
429 lines (366 loc) · 11.9 KB
/
VideoStreamWidget.cpp
File metadata and controls
429 lines (366 loc) · 11.9 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#include "VideoStreamWidget.h"
#include "CustomMessageBox.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QApplication>
#include <QDebug>
#include <QThread>
#include <QUrl>
#include <QMessageBox>
#include <QPushButton>
/**
* @brief VideoStreamWidget 생성자
* @details RTSP 비디오 스트림 위젯을 생성하고 UI, 미디어플레이어, 타이머를 초기화합니다.
* @param parent 부모 위젯
*/
VideoStreamWidget::VideoStreamWidget(QWidget *parent)
: QWidget(parent)
, m_videoWidget(nullptr)
, m_statusLabel(nullptr)
, m_liveIndicator(nullptr)
, m_layout(nullptr)
, m_mediaPlayer(nullptr)
, m_connectionTimer(nullptr)
, m_liveBlinkTimer(nullptr)
, m_statusUpdateTimer(nullptr)
, m_isStreaming(false)
, m_reconnectAttempts(0)
{
setupUI();
setupMediaPlayer();
setupTimers();
}
/**
* @brief VideoStreamWidget 소멸자
*/
VideoStreamWidget::~VideoStreamWidget()
{
stopStream();
if (m_mediaPlayer) {
delete m_mediaPlayer;
}
}
/**
* @brief UI 설정 함수
*/
void VideoStreamWidget::setupUI()
{
m_layout = new QVBoxLayout(this);
m_layout->setSpacing(0);
m_layout->setContentsMargins(0, 0, 0, 0);
// 상태 표시 영역 (상단 바)
QWidget *statusWidget = new QWidget();
statusWidget->setFixedHeight(50); // 고정 높이
QHBoxLayout *statusLayout = new QHBoxLayout(statusWidget);
statusLayout->setContentsMargins(10, 4, 10, 4);
statusLayout->setSpacing(10);
// LIVE Indicator
m_liveIndicator = new QLabel("●");
m_liveIndicator->setStyleSheet("color: red; font-size: 14px; font-weight: bold;");
m_liveIndicator->setVisible(false);
m_liveIndicator->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
statusLayout->addWidget(m_liveIndicator);
// 상태 텍스트
m_statusLabel = new QLabel("스트림 대기 중...");
m_statusLabel->setStyleSheet("color: #cccccc; font-size: 13px;");
m_statusLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
statusLayout->addWidget(m_statusLabel);
statusLayout->addStretch();
// draw 버튼 추가
QPushButton *drawButton = new QPushButton();
drawButton->setIcon(QIcon(":/icons/draw.png")); // 아이콘 경로 확인
drawButton->setIconSize(QSize(22, 22));
drawButton->setFixedSize(36, 36);
drawButton->setCursor(Qt::PointingHandCursor);
drawButton->setStyleSheet(
"QPushButton { background-color: #3b3e52; border: none; border-radius: 6px; }"
"QPushButton:hover { background-color: #4b4f68; }"
);
connect(drawButton, &QPushButton::clicked, this, [=]() {
emit drawButtonClicked(); // MainWindow 연결 필요
});
statusLayout->addWidget(drawButton);
m_layout->addWidget(statusWidget);
// 비디오 표시 영역
m_videoWidget = new QVideoWidget();
m_videoWidget->setMinimumSize(640, 480);
m_videoWidget->setStyleSheet("border: 2px solid #ddd; background-color: #000000;");
m_layout->addWidget(m_videoWidget);
setLayout(m_layout);
}
/**
* @brief 미디어플레이어 설정 함수
*/
void VideoStreamWidget::setupMediaPlayer()
{
m_mediaPlayer = new QMediaPlayer(this);
m_mediaPlayer->setVideoOutput(m_videoWidget);
// 미디어 플레이어 시그널 연결
connect(m_mediaPlayer, &QMediaPlayer::mediaStatusChanged,
this, &VideoStreamWidget::onMediaStatusChanged);
connect(m_mediaPlayer, &QMediaPlayer::playbackStateChanged,
this, &VideoStreamWidget::onPlaybackStateChanged);
connect(m_mediaPlayer, &QMediaPlayer::errorOccurred,
this, &VideoStreamWidget::onErrorOccurred);
}
/**
* @brief 타이머 설정 함수
*/
void VideoStreamWidget::setupTimers()
{
// 연결 타임아웃 타이머
m_connectionTimer = new QTimer(this);
m_connectionTimer->setSingleShot(true);
m_connectionTimer->setInterval(30000);
connect(m_connectionTimer, &QTimer::timeout, this, &VideoStreamWidget::onConnectionTimeout);
// LIVE 표시 깜빡임 타이머
m_liveBlinkTimer = new QTimer(this);
m_liveBlinkTimer->setInterval(1000); // 1초
connect(m_liveBlinkTimer, &QTimer::timeout, [this]() {
if (m_liveIndicator->isVisible()) {
QString currentColor = m_liveIndicator->styleSheet().contains("#4caf50") ? "#4caf50" : "#4caf50";
m_liveIndicator->setStyleSheet(QString("color: %1; font-size: 16px; font-weight: bold;").arg(currentColor));
}
});
// 상태 업데이트 타이머
m_statusUpdateTimer = new QTimer(this);
m_statusUpdateTimer->setInterval(3000); // 5초마다
connect(m_statusUpdateTimer, &QTimer::timeout, this, &VideoStreamWidget::updateConnectionStatus);
}
/**
* @brief 스트림 시작
* @param rtspUrl RTSP 주소
*/
void VideoStreamWidget::startStream(const QString &rtspUrl)
{
if (m_isStreaming) {
stopStream();
}
m_rtspUrl = rtspUrl;
m_reconnectAttempts = 0;
qDebug() << "스트림 시작 시도:" << rtspUrl;
showConnectionStatus("연결 중...", "#ff9800");
m_connectionTimer->start();
m_statusUpdateTimer->start();
// RTSP URL 설정 및 재생 시작
QUrl url(rtspUrl);
m_mediaPlayer->setSource(url);
m_mediaPlayer->play();
m_isStreaming = true;
}
/**
* @brief 스트림 중지
*/
void VideoStreamWidget::stopStream()
{
if (!m_isStreaming) return;
m_isStreaming = false;
// 타이머 중지
m_connectionTimer->stop();
m_liveBlinkTimer->stop();
m_statusUpdateTimer->stop();
// 미디어 플레이어 중지
if (m_mediaPlayer) {
m_mediaPlayer->stop();
}
m_liveIndicator->setVisible(false);
showConnectionStatus("스트림 중지됨", "#666");
qDebug() << "스트림 중지됨";
}
/**
* @brief 스트리밍 여부 반환
* @return 스트리밍 중이면 true
*/
bool VideoStreamWidget::isStreaming() const
{
return m_isStreaming;
}
/**
* @brief 스트림 URL 설정
* @param url RTSP 주소
*/
void VideoStreamWidget::setStreamUrl(const QString &url)
{
m_rtspUrl = url;
}
/**
* @brief 마우스 클릭 이벤트 처리
* @param event 마우스 이벤트
*/
void VideoStreamWidget::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
emit clicked();
}
QWidget::mousePressEvent(event);
}
/**
* @brief 미디어 상태 변경 슬롯
* @param status 미디어 상태
*/
void VideoStreamWidget::onMediaStatusChanged(QMediaPlayer::MediaStatus status)
{
qDebug() << "미디어 상태 변경:" << status;
switch (status) {
case QMediaPlayer::LoadingMedia:
showConnectionStatus("미디어 로딩 중...", "#ff9800");
break;
case QMediaPlayer::LoadedMedia:
showConnectionStatus("미디어 로드 완료", "#4caf50");
m_connectionTimer->stop();
break;
case QMediaPlayer::BufferingMedia:{
showConnectionStatus("연결됨", "#4caf50");
m_connectionTimer->stop();
m_liveIndicator->setVisible(true);
m_liveBlinkTimer->start();
m_reconnectAttempts = 0;
CustomMessageBox msgBox(nullptr, "RTSP 연결", "RTSP 연결에 성공했습니다!");
msgBox.exec();
break;
}
case QMediaPlayer::BufferedMedia:
showConnectionStatus("연결됨", "#4caf50");
m_connectionTimer->stop();
m_liveIndicator->setVisible(true);
m_liveBlinkTimer->start();
m_reconnectAttempts = 0;
qDebug() << "버퍼링 완료 - 스트림 재생 시작";
break;
case QMediaPlayer::EndOfMedia:
qDebug() << "스트림 종료됨";
if (m_isStreaming) {
QTimer::singleShot(5000, this, &VideoStreamWidget::attemptReconnection);
}
break;
case QMediaPlayer::InvalidMedia:
qDebug() << "잘못된 미디어";
emit streamError("잘못된 미디어 형식입니다");
attemptReconnection();
break;
default:
break;
}
}
/**
* @brief 재생 상태 변경 슬롯
* @param state 재생 상태
*/
void VideoStreamWidget::onPlaybackStateChanged(QMediaPlayer::PlaybackState state)
{
qDebug() << "재생 상태 변경:" << state;
switch (state) {
case QMediaPlayer::PlayingState:
showConnectionStatus("재생 중", "#4caf50");
break;
case QMediaPlayer::PausedState:
showConnectionStatus("일시정지", "#ff9800");
break;
case QMediaPlayer::StoppedState:
showConnectionStatus("중지됨", "#666");
m_liveIndicator->setVisible(false);
m_liveBlinkTimer->stop();
break;
}
}
/**
* @brief 에러 발생 슬롯
* @param error 에러 코드
* @param errorString 에러 메시지
*/
void VideoStreamWidget::onErrorOccurred(QMediaPlayer::Error error, const QString &errorString)
{
qDebug() << "미디어 플레이어 에러:" << error << errorString;
QString errorMsg;
switch (error) {
case QMediaPlayer::NoError:
return;
case QMediaPlayer::ResourceError:
errorMsg = "리소스 에러: " + errorString;
break;
case QMediaPlayer::FormatError:
errorMsg = "포맷 에러: " + errorString;
break;
case QMediaPlayer::NetworkError:
errorMsg = "네트워크 에러: " + errorString;
break;
case QMediaPlayer::AccessDeniedError:
errorMsg = "접근 거부: " + errorString;
break;
default:
errorMsg = "알 수 없는 에러: " + errorString;
break;
}
showConnectionStatus("에러 발생", "#f44336");
emit streamError(errorMsg);
CustomMessageBox msgBox(nullptr, "RTSP 연결 실패", errorMsg);
msgBox.exec();
if (m_isStreaming) {
attemptReconnection();
}
}
/**
* @brief 연결 타임아웃 슬롯
*/
void VideoStreamWidget::onConnectionTimeout()
{
qDebug() << "연결 타임아웃";
showConnectionStatus("연결 타임아웃", "#f44336");
emit streamError("연결 타임아웃: 스트림에 연결할 수 없습니다");
attemptReconnection();
}
/**
* @brief 재연결 시도 슬롯
*/
void VideoStreamWidget::attemptReconnection()
{
if (m_reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) {
qDebug() << "최대 재연결 시도 횟수 초과";
stopStream();
emit streamError("최대 재연결 시도 횟수를 초과했습니다");
return;
}
m_reconnectAttempts++;
showConnectionStatus(QString("재연결 시도 중... (%1/%2)").arg(m_reconnectAttempts).arg(MAX_RECONNECT_ATTEMPTS), "#ff9800");
// 현재 재생 중지
if (m_mediaPlayer) {
m_mediaPlayer->stop();
}
// 잠시 대기 후 재연결 시도
QTimer::singleShot(3000, this, [this]() {
if (!m_rtspUrl.isEmpty() && m_isStreaming) {
qDebug() << "재연결 시도:" << m_reconnectAttempts;
m_connectionTimer->start();
QUrl url(m_rtspUrl);
m_mediaPlayer->setSource(url);
m_mediaPlayer->play();
}
});
}
/**
* @brief 연결 상태 업데이트 슬롯
*/
void VideoStreamWidget::updateConnectionStatus()
{
if (!m_isStreaming) {
return;
}
// 미디어 플레이어 상태 확인
if (m_mediaPlayer->playbackState() == QMediaPlayer::PlayingState &&
m_mediaPlayer->mediaStatus() == QMediaPlayer::BufferedMedia) {
if (m_reconnectAttempts > 0) {
m_reconnectAttempts = 0;
showConnectionStatus("연결 복구됨", "#4caf50");
}
}
}
/**
* @brief 연결 상태 표시
* @param status 상태 문자열
* @param color 색상
*/
void VideoStreamWidget::showConnectionStatus(const QString &status, const QString &color)
{
m_statusLabel->setText(status);
m_statusLabel->setStyleSheet(QString("color: %1; font-size: 12px; padding: 5px;").arg(color));
}