forked from muleimulei/CloudMeeting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudioOutput.cpp
172 lines (158 loc) · 3.75 KB
/
AudioOutput.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "AudioOutput.h"
#include <QMutexLocker>
#include "netheader.h"
#include <QDebug>
#include <QHostAddress>
#ifndef FRAME_LEN_125MS
#define FRAME_LEN_125MS 1900
#endif
extern QUEUE_DATA<MESG> audio_recv; //音频接收队列
AudioOutput::AudioOutput(QObject *parent)
: QThread(parent)
{
QAudioFormat format;
format.setSampleRate(8000);
format.setChannelCount(1);
format.setSampleSize(16);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::UnSignedInt);
QAudioDeviceInfo info = QAudioDeviceInfo::defaultOutputDevice();
if (!info.isFormatSupported(format))
{
qWarning() << "Raw audio format not supported by backend, cannot play audio.";
return;
}
audio = new QAudioOutput(format, this);
connect(audio, SIGNAL(stateChanged(QAudio::State)), this, SLOT(handleStateChanged(QAudio::State) ));
outputdevice = nullptr;
}
AudioOutput::~AudioOutput()
{
delete audio;
}
QString AudioOutput::errorString()
{
if (audio->error() == QAudio::OpenError)
{
return QString("AudioOutput An error occurred opening the audio device").toUtf8();
}
else if (audio->error() == QAudio::IOError)
{
return QString("AudioOutput An error occurred during read/write of audio device").toUtf8();
}
else if (audio->error() == QAudio::UnderrunError)
{
return QString("AudioOutput Audio data is not being fed to the audio device at a fast enough rate").toUtf8();
}
else if (audio->error() == QAudio::FatalError)
{
return QString("AudioOutput A non-recoverable error has occurred, the audio device is not usable at this time.");
}
else
{
return QString("AudioOutput No errors have occurred").toUtf8();
}
}
void AudioOutput::handleStateChanged(QAudio::State state)
{
switch (state)
{
case QAudio::ActiveState:
break;
case QAudio::SuspendedState:
break;
case QAudio::StoppedState:
if (audio->error() != QAudio::NoError)
{
audio->stop();
emit audiooutputerror(errorString());
qDebug() << "out audio error" << audio->error();
}
break;
case QAudio::IdleState:
break;
case QAudio::InterruptedState:
break;
default:
break;
}
}
void AudioOutput::startPlay()
{
if (audio->state() == QAudio::ActiveState) return;
WRITE_LOG("start playing audio");
outputdevice = audio->start();
}
void AudioOutput::stopPlay()
{
if (audio->state() == QAudio::StoppedState) return;
{
QMutexLocker lock(&device_lock);
outputdevice = nullptr;
}
audio->stop();
WRITE_LOG("stop playing audio");
}
void AudioOutput::run()
{
is_canRun = true;
QByteArray m_pcmDataBuffer;
WRITE_LOG("start playing audio thread 0x%p", QThread::currentThreadId());
for (;;)
{
{
QMutexLocker lock(&m_lock);
if (is_canRun == false)
{
stopPlay();
WRITE_LOG("stop playing audio thread 0x%p", QThread::currentThreadId());
return;
}
}
MESG* msg = audio_recv.pop_msg();
if (msg == NULL) continue;
{
QMutexLocker lock(&device_lock);
if (outputdevice != nullptr)
{
m_pcmDataBuffer.append((char*)msg->data, msg->len);
if (m_pcmDataBuffer.size() >= FRAME_LEN_125MS)
{
//写入音频数据
qint64 ret = outputdevice->write(m_pcmDataBuffer.data(), FRAME_LEN_125MS);
if (ret < 0)
{
qDebug() << outputdevice->errorString();
return;
}
else
{
emit speaker(QHostAddress(msg->ip).toString());
m_pcmDataBuffer = m_pcmDataBuffer.right(m_pcmDataBuffer.size() - ret);
}
}
}
else
{
m_pcmDataBuffer.clear();
}
}
if (msg->data) free(msg->data);
if (msg) free(msg);
}
}
void AudioOutput::stopImmediately()
{
QMutexLocker lock(&m_lock);
is_canRun = false;
}
void AudioOutput::setVolumn(int val)
{
audio->setVolume(val / 100.0);
}
void AudioOutput::clearQueue()
{
qDebug() << "audio recv clear";
audio_recv.clear();
}