Esempio n. 1
0
void Engine::startRecording()
{
    if (m_audioInput) {
        if (QAudio::AudioInput == m_mode &&
            QAudio::SuspendedState == m_state) {
            m_audioInput->resume();
        } else {
            m_spectrumAnalyser.cancelCalculation();
            spectrumChanged(0, 0, FrequencySpectrum());

            m_buffer.fill(0);
            setRecordPosition(0, true);
            stopPlayback();
            m_mode = QAudio::AudioInput;
            CHECKED_CONNECT(m_audioInput, SIGNAL(stateChanged(QAudio::State)),
                            this, SLOT(audioStateChanged(QAudio::State)));
            CHECKED_CONNECT(m_audioInput, SIGNAL(notify()),
                            this, SLOT(audioNotify()));
            m_count = 0;
            m_dataLength = 0;
            emit dataLengthChanged(0);
            m_audioInputIODevice = m_audioInput->start();
            CHECKED_CONNECT(m_audioInputIODevice, SIGNAL(readyRead()),
                            this, SLOT(audioDataReady()));
        }
    }
}
Esempio n. 2
0
void DataPool::readyRead()
{
    QTcpSocket *socket;

    socket = qobject_cast<QTcpSocket *>(sender());
    if(socket == NULL)
        return;
    if(!accountMap.contains(socket))
        return;

    Connection &conn = accountMap[socket];
    if(conn.tcpSocket != socket)
    {
        qWarning() << "DataPool::readyRead() tcpSocket not the same";
        return;
    }

    while(socket->state() == QAbstractSocket::ConnectedState)
    {
        switch(conn.state)
        {
        case Connection::state_recv_len:
            if(conn.tcpSocket->bytesAvailable() < sizeof(quint32))
                return;
            do
            {
                QDataStream in(socket);
                in.setVersion(QDataStream::Qt_4_6);
                in >> conn.len;
            }while(0);
            conn.state = Connection::state_recv_data;
            break;

        case Connection::state_recv_data:
            if(conn.tcpSocket->bytesAvailable() < conn.len)
                return;
            conn.state = Connection::state_loop;
            if(conn.connectionType == Connection::unknown_connection)
            {
                quint8 cmd;
                QString fileName;
                quint64 fileSize;
                QDataStream in(socket);
                in.setVersion(QDataStream::Qt_4_6);
                in >> cmd;
                switch(cmd)
                {
                case 0x08:
                    conn.connectionType = Connection::message_connection;
                    in >> conn.peerUID;
                    break;

                case 0x0A:
                    in >> conn.peerUID >> fileName >> fileSize;
                    conn.connectionType = Connection::file_connection;
                    conn.state = Connection::state_wait;
                    emit fileRequest(conn.peerUID, fileName, fileSize);
                    return;

                case 0x0D:
                    in >> conn.peerUID;
                    conn.connectionType = Connection::audio_connection;
                    conn.state = Connection::state_wait;
                    emit audioRequest(conn.peerUID);
                    return;

                default:
                    qDebug() << "#1:" << cmd;
                    conn.state = Connection::state_close;
                    conn.tcpSocket->close();
                    return;
                }
            }
            else
            {
                bool accepted;
                QDataStream in(socket);
                in.setVersion(QDataStream::Qt_4_6);
                in >> accepted;
                if(conn.connectionType == Connection::file_connection)
                    emit fileRequestResult(conn.peerUID, accepted);
                else if(conn.connectionType == Connection::audio_connection)
                    emit audioRequestResult(conn.peerUID, accepted);
            }
            break;

        case Connection::state_wait:
            qDebug() << "#2";
            conn.state = Connection::state_close;
            conn.tcpSocket->close();
            return;

        case Connection::state_loop:
            if(conn.tcpSocket->bytesAvailable() < sizeof(quint32))
                return;
            do
            {
                QDataStream in(socket);
                in.setVersion(QDataStream::Qt_4_6);
                in >> conn.len;
            }while(0);
            conn.state = Connection::state_loop_data;
            break;

        case Connection::state_loop_data:
            if(conn.tcpSocket->bytesAvailable() < conn.len)
                return;
            do
            {
                quint8 cmd;
                QString msg;
                QByteArray data;
                QDataStream in(socket);
                in.setVersion(QDataStream::Qt_4_6);
                in >> cmd;
                conn.state = Connection::state_loop;
                switch(conn.connectionType)
                {
                case Connection::message_connection:
                    in >> msg;
                    emit newMessage(conn.peerUID, msg);
                    break;

                case Connection::file_connection:
                    if(cmd == 0x0B)
                    {
                        in >> data;
                        emit fileDataReady(conn.peerUID, data);
                    }
                    else if(cmd == 0x0C)
                    {
                        conn.state = Connection::state_done;
                        conn.tcpSocket->close();
                        emit fileReceived(conn.peerUID);
                        return;
                    }
                    break;

                case Connection::audio_connection:
                    in >> data;
                    emit audioDataReady(conn.peerUID, data);
                    break;

                default:
                    return;
                }