Rabbit Remote Control 0.0.30
Loading...
Searching...
No Matches
RecordVideo.cpp
1#include <QLoggingCategory>
2#include <QUrl>
3#include <QVideoFrame>
4#include "RecordVideo.h"
5
6static Q_LOGGING_CATEGORY(log, "Record.Video")
7static Q_LOGGING_CATEGORY(logThread, "Record.Video.Thread")
8
15 : QThread()
16{
17 bool check = connect(this, SIGNAL(finished()),
18 this, SLOT(deleteLater()));
19 Q_ASSERT(check);
20}
21
22int CRecordVideoThread::SetFile(const QString &szFile)
23{
24 m_szFile = szFile;
25 return 0;
26}
27
28void CRecordVideoThread::run()
29{
30 bool check = false;
31 qDebug(logThread) << "Record video thread start ...";
32 CRecordVideo record;
33 int nRet = record.Start(m_szFile);
34 if(nRet) {
35 qCritical(logThread) << "Start record fail:" << nRet;
36 emit sigStatusChanged(CFrmViewer::RecordVideoStatus::Error);
37 return;
38 }
39 check = connect(&record, SIGNAL(sigStatusChanged(CFrmViewer::RecordVideoStatus)),
40 this, SIGNAL(sigStatusChanged(CFrmViewer::RecordVideoStatus)));
41 Q_ASSERT(check);
42 check = connect(this, SIGNAL(sigUpdate(QImage)),
43 &record, SLOT(slotUpdate(QImage)));
44 Q_ASSERT(check);
45 check = connect(&record, SIGNAL(sigError(int,QString)),
46 this, SIGNAL(sigError(int,QString)));
47 Q_ASSERT(check);
48 emit sigStatusChanged(CFrmViewer::RecordVideoStatus::Recording);
49
50 exec();
51
52 nRet = record.Stop();
53 if(nRet) {
54 qCritical(logThread) << "Stop record fail:" << nRet;
55 emit sigStatusChanged(CFrmViewer::RecordVideoStatus::Error);
56 }
57 else
58 emit sigStatusChanged(CFrmViewer::RecordVideoStatus::Stop);
59 qDebug(logThread) << "Record video thread end";
60}
61
62CRecordVideo::CRecordVideo(QObject *parent)
63 : QObject{parent}
64{
65#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
66 bool check = connect(
67 &m_Recorder, &QMediaRecorder::errorOccurred,
68 this, [&](QMediaRecorder::Error error, const QString &errorString) {
69 qDebug(log) << "Recorder error occurred:" << error << errorString;
70 Stop();
71 emit sigError(error, errorString);
72 });
73 Q_ASSERT(check);
74#endif
75}
76
77int CRecordVideo::Start(const QString &szFile)
78{
79 int nRet = 0;
80 qDebug(log) << "Start";
81#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
82 m_Parameter.SetEnable(true);
83 m_Parameter >> m_Recorder;
84 m_CaptureSession.setVideoFrameInput(&m_VideoFrameInput);
85 //m_CaptureSession.setAudioBufferInput(&m_AudioBufferInput);
86 m_CaptureSession.setRecorder(&m_Recorder);
87 m_Recorder.setOutputLocation(QUrl::fromLocalFile(m_Parameter.GetFile(true)));
88 m_Recorder.record();
89#endif
90 return nRet;
91}
92
93int CRecordVideo::Stop()
94{
95 int nRet = 0;
96 qDebug(log) << "Stop";
97#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
98 m_Recorder.stop();
99 m_CaptureSession.setVideoFrameInput(nullptr);
100 m_CaptureSession.setAudioBufferInput(nullptr);
101 m_CaptureSession.setRecorder(nullptr);
102#endif
103
104 return nRet;
105}
106
107void CRecordVideo::slotUpdate(QImage img)
108{
109 qDebug(log) << "Update image";
110#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
111 QVideoFrame frame(img);
112 bool bRet = m_VideoFrameInput.sendVideoFrame(frame);
113 if(!bRet) {
114 //TODO: 放入未成功发送队列,
115 // 当 QVideoFrameInput::readyToSendVideoFrame() 时,再发送
116 qDebug(log) << "m_VideoFrameInput.sendVideoFrame fail";
117 }
118#endif
119}
const QString GetFile(bool bAuto=false)
Record video thread.
Definition RecordVideo.h:29
Record video to file.
Definition RecordVideo.h:51