forked from zhuohengfeng/OpenCVFFmpegRtmp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXAudioRecord.cpp
More file actions
101 lines (86 loc) · 2.41 KB
/
XAudioRecord.cpp
File metadata and controls
101 lines (86 loc) · 2.41 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
//
// Created by hengfeng zhuo on 2019-08-05.
//
#include <QAudioInput>
#include "XAudioRecord.h"
#include <iostream>
class CXAudioRecord : public XAudioRecord {
public:
bool Init() override {
this->Stop();
// qt音频开始录制
QAudioFormat fmt;
fmt.setSampleRate(sampleRate);
fmt.setChannelCount(channels);
fmt.setSampleSize(sampleByte * 8);
fmt.setCodec("audio/pcm");
fmt.setByteOrder(QAudioFormat::LittleEndian);
fmt.setSampleType(QAudioFormat::UnSignedInt);
QAudioDeviceInfo info = QAudioDeviceInfo::defaultInputDevice();
if (!info.isFormatSupported(fmt)) {
std::cout << "Audio format not support" << std::endl;
fmt = info.nearestFormat(fmt);
}
input = new QAudioInput(fmt);
//开始录制音频
io = input->start();
if (!io)
return false;
return true;
}
void Stop() override {
// 停止线程
XDataThread::Stop();
if (input)
input->stop();
if (io)
io->close();
input = NULL;
io = NULL;
}
protected:
QAudioInput* input = nullptr;
QIODevice* io = nullptr;
/**
* 这个是QThread的线程执行体
*/
void run() override {
std::cout << "进入音频录制线程" << std::endl;
// 每次读取的字节数
int readSize = nbSamples * channels * sampleByte;
char* buf = new char[readSize];
while(!this->isExit) {
//读取已录制音频
//一次读取一帧音频
if (input->bytesReady() < readSize)
{
QThread::msleep(1);
continue;
}
int size = 0;
while (size != readSize)
{
int len = io->read(buf + size, readSize - size);
if (len < 0)break;
size += len;
}
if (size != readSize)
{
continue;
}
//已经读取一帧音频
Push(XData(buf, readSize));
}
delete[] buf;
std::cout << "退出音频录制线程" << std::endl;
}
};
XAudioRecord *XAudioRecord::Get(XAUDIOTYPE type, unsigned char index)
{
static CXAudioRecord record[255];
return &record[index];
}
XAudioRecord::XAudioRecord() {
}
XAudioRecord::~XAudioRecord() {
}