-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathrecordingthread.cpp
More file actions
543 lines (471 loc) · 14.3 KB
/
recordingthread.cpp
File metadata and controls
543 lines (471 loc) · 14.3 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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
#include "recordingthread.h"
#include <QStandardPaths>
#include <QDir>
#include <QtCore/QDebug>
#include "kanmusumemory_global.h"
RecordingThread::RecordingThread(QObject *parent) :
QThread(parent)
, m_webView(NULL)
, m_timer(NULL)
, m_recordingCounter(0)
, m_process()
, m_audio()
, m_recordThread(this)
, m_stop(true)
{
//録音
QAudioEncoderSettings audioSettings;
audioSettings.setCodec("audio/pcm");
audioSettings.setQuality(QMultimedia::HighQuality);
m_audio.setEncodingSettings(audioSettings);
m_audio.moveToThread(&m_recordThread);
//テンポラリ
setTempPath(QString("%1/%2/%3")
.arg(QStandardPaths::writableLocation(QStandardPaths::TempLocation))
.arg(KANMEMO_PROJECT)
.arg("recording"));
//タイマー
m_timer = new QTimer(this);
connect(m_timer, &QTimer::timeout, [this](){
static bool f = true;
if(f){
qDebug() << "timer first " << m_et.elapsed();
f = false;
}
//キャプチャ
unsigned long counter = getRecordingCounter();
capture(counter);
//保存スレッド開始
if(!isRunning()){
start();
}
//経過時間
if(m_start_offset_time > 0){
qint64 n = m_et.elapsed() - m_start_offset_time;
if((n - duration()) > 500){
setDuration(m_et.elapsed() - m_start_offset_time);
}
}
});
//録画スレッド終了からの変換
connect(this, &RecordingThread::finished, this, &RecordingThread::recordFinished, Qt::QueuedConnection);
//プロセスが終了した
connect(&m_process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(processFinished(int, QProcess::ExitStatus)) );
//プロセスがエラー
connect(&m_process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)) );
//録音開始
connect(this, &RecordingThread::audioRecord, &m_audio, &AudioRecorder::record);
//録音がエラー
connect(&m_audio, SIGNAL(error(QMediaRecorder::Error)), this, SLOT(audioRecordingError(QMediaRecorder::Error)));
//録音の状態変化
connect(&m_audio, &QAudioRecorder::statusChanged, [this](QMediaRecorder::Status status){
qDebug() << "QAudioRecorder::statusChanged " << status << "," << QString::number(m_et.elapsed()) << "," << m_audio.duration();
});
//録音スレッド開始
m_recordThread.start(QThread::TimeCriticalPriority);
// m_recordThread.setPriority(QThread::TimeCriticalPriority);
//状態変更
setStatus(Ready);
}
RecordingThread::~RecordingThread()
{
//録音スレッド停止
m_recordThread.exit();
m_recordThread.wait();
}
//録画開始
void RecordingThread::startRecording()
{
qDebug() << "start recording timer " << m_et.elapsed();
//状態変更
setStatus(Recording);
//クリア
m_recordingCounter = 0;
m_SaveDataList.clear();
m_et.start();
m_start_offset_time = -1;
setDuration(0);
m_soundOffsetCount = 0;
//録音開始
QString audio_path = getTempAudioPath();
emit audioRecord(QUrl::fromLocalFile(audio_path), audioInputName());
//タイマー開始
m_stop = false;
m_timer->start(static_cast<int>(1000.0/fps()));
}
//録画終了
void RecordingThread::stopRecording()
{
//状態変更
setStatus(Saving);
//録音停止
m_audio.stop();
//タイマー停止
m_timer->stop();
m_stop = true;
qDebug() << "stop recording timer:" << m_recordingCounter;
}
//保存フォルダ(テンポラリの中身をクリア)
void RecordingThread::clearCaptureFiles()
{
QDir dir(getTempPath());
QStringList nameFilters;
nameFilters << "*.jpg";
dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks | QDir::NoDotAndDotDot);
dir.setNameFilters(nameFilters);
QFileInfoList list = dir.entryInfoList();
//ファイルを消す
for(int i=0; i<list.length(); i++){
QFile::remove(list[i].filePath());
}
}
//準備OKか
bool RecordingThread::isSettingValid()
{
QFileInfo save(savePath());
if(!QFile::exists(save.dir().absolutePath())){
return false;
}else if(!QFile::exists(tempPath())){
return false;
}else if(!QFile::exists(toolPath())){
return false;
}else if(fps() < 10 || 30 < fps()){
return false;
}else{
QStringList s = audioInputNames();
bool ok = false;
foreach (QString s1, s) {
if(s1.compare(audioInputName()) == 0){
ok = true;
break;
}
}
if(!ok){
return false;
}
}
return true;
}
WebView *RecordingThread::webView() const
{
return m_webView;
}
void RecordingThread::setWebView(WebView *webview)
{
m_webView = webview;
}
//保存パス
QString RecordingThread::savePath() const
{
return m_savePath;
}
void RecordingThread::setSavePath(const QString &savePath)
{
m_savePath = savePath;
}
//テンポラリパス(強制変更)
QString RecordingThread::tempPath() const
{
return m_tempPath;
}
void RecordingThread::setTempPath(const QString &tempPath)
{
m_tempPath = tempPath;
}
//ツールのパス
QString RecordingThread::toolPath() const
{
return m_toolPath;
}
void RecordingThread::setToolPath(const QString &toolPath)
{
m_toolPath = toolPath;
}
//ツールのパラメータ
QString RecordingThread::toolParam() const
{
return m_toolParam;
}
void RecordingThread::setToolParam(const QString &toolParam)
{
m_toolParam = toolParam;
}
//録画終了
void RecordingThread::recordFinished()
{
qDebug() << "finish thread ";
//状態変更
setStatus(Convert);
//残骸チェック
foreach (SaveData data, m_SaveDataList) {
qDebug() << "remnant:" << data.image.isNull() << "," << data.path;
}
//変換
convert();
//デバッグ計測
qint64 pt = 0;
foreach (qint64 t, m_interval) {
qDebug() << t << "(" << (t-pt) << ")";
pt = t;
}
}
//プロセス終了
void RecordingThread::processFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
qDebug() << "RecordingThread::processFinished , exitCode=" << exitCode << ", exitStatus=" << exitStatus;
QByteArray output = m_process.readAllStandardError();
QString str = QString::fromLocal8Bit( output );
qDebug() << ">" << str;
//状態変更
setStatus(Ready);
}
//プロセスエラー
void RecordingThread::processError(QProcess::ProcessError error)
{
qDebug() << "RecordingThread::processError " << error;
//状態変更
setStatus(Ready);
}
//録音エラー
void RecordingThread::audioRecordingError(QMediaRecorder::Error error)
{
qDebug() << "Recording Audio Error:" << error;
}
//音ズレ調整用
qint32 RecordingThread::soundOffset() const
{
return m_soundOffset;
}
void RecordingThread::setSoundOffset(const qint32 &soundOffset)
{
qDebug() << "set Sound offset:" << soundOffset;
m_soundOffset = soundOffset;
}
qint64 RecordingThread::duration() const
{
return m_duration;
}
void RecordingThread::setDuration(const qint64 &duration)
{
if(m_duration == duration) return;
m_duration = duration;
emit durationChanged(m_duration);
}
RecordingThread::RecordingStatus RecordingThread::status() const
{
return m_status;
}
void RecordingThread::setStatus(const RecordingStatus &status)
{
if(m_status == status) return;
m_status = status;
emit statusChanged(status);
}
//オーディオ録音するデバイス名
QString RecordingThread::audioInputName() const
{
if(m_audioInputName.isEmpty()){
return m_audio.defaultAudioInput();
}else{
return m_audioInputName;
}
}
void RecordingThread::setAudioInputName(const QString &audioInputName)
{
m_audioInputName = audioInputName;
}
//オーディオデバイスの一覧
QStringList RecordingThread::audioInputNames() const
{
return m_audio.audioInputs();
}
//フレームレート
qreal RecordingThread::fps() const
{
return m_fps;
}
void RecordingThread::setFps(const qreal &fps)
{
m_fps = fps;
}
//キャプチャしてデータをリストへ突っ込む
void RecordingThread::capture(unsigned long count)
{
QImage img = webView()->capture();
if(img.isNull()){
qDebug() << "don't capture";
return;
}
qint64 elapsed = m_et.elapsed();
qint64 duration = m_audio.duration();
SaveData data(img
, QString("%1/kanmemo_%2.jpg").arg(getTempPath()).arg(count, 6, 10, QChar('0'))
, elapsed, duration);
m_mutex.lock();
m_SaveDataList.append(data);
m_mutex.unlock();
}
//静止画を動画に変換する
void RecordingThread::convert()
{
QString image_path = QString("%1/kanmemo_%6d.jpg").arg(getTempPath());
QString audio_path = getTempAudioPath();
#if defined(Q_OS_WIN)
image_path = image_path.replace(QStringLiteral("/"), QStringLiteral("\\"));
#endif
QFileInfo fi(audio_path);
QStringList args;
args.append("-r");
args.append(QString::number(fps()));
args.append("-i");
args.append(image_path);
if(fi.exists()){
args.append("-i");
args.append(audio_path);
}
args.append("-vcodec");
args.append("libx264");
args.append("-qscale:v");
args.append("0");
args.append("-y");
args.append(savePath());
QString args_str;
foreach (QString arg, args) {
args_str += arg + " ";
}
qDebug() << args_str;
if(m_process.state() == QProcess::NotRunning){
m_process.start(toolPath(), args);
}else{
qDebug() << "not start convert process.(ffmpeg)";
}
}
//最新のカウンタを取得
unsigned long RecordingThread::getRecordingCounter()
{
return m_recordingCounter++;
}
//連続データの保存先の取得
QString RecordingThread::getTempPath()
{
QString path;
path = tempPath();
//なければつくる
QDir dir(path);
if(!dir.exists())
dir.mkpath(path);
return path;
}
//録音ファイルのパスを取得
QString RecordingThread::getTempAudioPath()
{
QString audio_path = QString("%1/kanmemo.wav").arg(getTempPath());
#if defined(Q_OS_WIN)
audio_path = audio_path.replace(QStringLiteral("/"), QStringLiteral("\\"));
#endif
return audio_path;
}
void RecordingThread::run()
{
qDebug() << "start recoding thread " << QString::number(m_et.elapsed());
//前のデータを消す
qDebug() << "clear data in previous";
clearCaptureFiles();
//録画
bool empty;
qreal frame_time = 1000.0 / fps();
qint64 elapse;
//フレームのカウント
unsigned long count = 0;
unsigned long timer_time = 0;
m_start_offset_time = m_SaveDataList.first().elapse;
qDebug() << "start_offset_time:" << m_start_offset_time;
m_mutex.lock();
empty = m_SaveDataList.isEmpty();
m_mutex.unlock();
while(!empty){
//保存
SaveData data = m_SaveDataList.first();
save(data, count++);
timer_time = (count-1)*1000/fps();
elapse = data.elapse - m_start_offset_time;
// qDebug() << count << " , " << timer_time << "-" << elapse << "=" << (timer_time - elapse)
// << " : " << data.duration << "-" << elapse << "=" << (data.duration - elapse);
//次があるか確認
m_mutex.lock();
m_SaveDataList.removeFirst();
empty = m_SaveDataList.isEmpty();
m_mutex.unlock();
//停止指示がかかってない時は待つ
while(empty && m_stop == false){
// qDebug() << "wait empty " << m_stop << "," << data.path;
msleep(static_cast<unsigned long>(2000.0/fps()));
empty = m_SaveDataList.isEmpty();
}
//設定フレームあったか確認
if(!empty){
//保存しようとしているフレームの理論時間
timer_time = (count-1)*1000/fps();
elapse = data.elapse - m_start_offset_time;
//理論時間とのズレが1フレームより大きい間補間する
while(abs(timer_time - elapse) > frame_time){
//保存
save(data, count++);
//保存しようとしているフレームの理論時間
timer_time = (count-1)*1000/fps();
elapse = data.elapse - m_start_offset_time;
//保存した時間
// qDebug() << count << " , " << timer_time << "-" << elapse << "=" << (timer_time - elapse)
// << " : " << data.duration << "-" << elapse << "=" << (data.duration - elapse)
// << " *";
}
}
}
qDebug() << "stop recoding thread " << QString::number(m_et.elapsed());
}
void RecordingThread::save(SaveData &data, unsigned long count)
{
QString path;
//音ズレ調整で画像を作ったり、消したり
if(soundOffset() > 0){
//音を後ろにずらす(画像を消す)
if(m_soundOffsetCount < soundOffset()){
//消す
qDebug() << "sound offset(plus):" << m_soundOffsetCount << "/" << soundOffset();
m_soundOffsetCount++;
///////////////////////
/// returnしてるから注意!
///////////////////////
return;
}else{
count -= soundOffset();
}
}else if(soundOffset() < 0){
//音を前にずらす(画像を水増し)
if(count == 0){
qDebug() << "sound offset(minus):" + soundOffset();
for(int i=0; i<(-1 * soundOffset()); i++){
path = QString("%1/kanmemo_%2.jpg").arg(getTempPath()).arg(i, 6, 10, QChar('0'));
if(data.image.isNull()){
qDebug() << "list data is null(offset)";
}else if(data.image.save(path, "jpg")){
//ok
}else{
//ng
qDebug() << "failed save(offset)";
}
}
}
count += -1 * soundOffset(); //値がマイナスなので
}else{
}
path = QString("%1/kanmemo_%2.jpg").arg(getTempPath()).arg(count, 6, 10, QChar('0'));
if(data.image.isNull()){
qDebug() << "list data is null";
}else if(data.image.save(path, "jpg")){
//ok
}else{
//ng
qDebug() << "failed save";
}
}