コード例 #1
0
void DesktopCaptureElement::readFrame()
{
    QScreen *screen = QGuiApplication::screens()[this->m_curScreenNumber];
    AkFrac fps(30000, 1001);

    AkVideoCaps caps;
    caps.isValid() = true;
    caps.format() = AkVideoCaps::Format_rgb24;
    caps.bpp() = AkVideoCaps::bitsPerPixel(caps.format());
    caps.width() = screen->size().width();
    caps.height() = screen->size().height();
    caps.fps() = fps;

    QPixmap frame = screen->grabWindow(QApplication::desktop()->winId());
    QImage frameImg= frame.toImage().convertToFormat(QImage::Format_RGB888);
    AkPacket packet = AkUtils::imageToPacket(frameImg, caps.toCaps());

    if (!packet)
        return;

    qint64 pts = qint64(QTime::currentTime().msecsSinceStartOfDay()
                        * fps.value() / 1e3);

    packet.setPts(pts);
    packet.setTimeBase(fps.invert());
    packet.setIndex(0);
    packet.setId(this->m_id);

    if (!this->m_threadedRead) {
        emit this->oStream(packet);

        return;
    }

    if (!this->m_threadStatus.isRunning()) {
        this->m_curPacket = packet;

        this->m_threadStatus = QtConcurrent::run(&this->m_threadPool,
                                                 this->sendPacket,
                                                 this,
                                                 this->m_curPacket);
    }
}
コード例 #2
0
AkPacket CaptureDShow::readFrame()
{
    IBaseFilter *source = NULL;
    this->m_graph->FindFilterByName(SOURCE_FILTER_NAME, &source);

    if (source) {
        this->m_controlsMutex.lock();
        QVariantMap imageControls = this->controlStatus(this->m_globalImageControls);
        this->m_controlsMutex.unlock();

        if (this->m_localImageControls != imageControls) {
            QVariantMap controls = this->mapDiff(this->m_localImageControls,
                                                 imageControls);
            this->setImageControls(source, controls);
            this->m_localImageControls = imageControls;
        }

        this->m_controlsMutex.lock();
        QVariantMap cameraControls = this->controlStatus(this->m_globalCameraControls);
        this->m_controlsMutex.unlock();

        if (this->m_localCameraControls != cameraControls) {
            QVariantMap controls = this->mapDiff(this->m_localCameraControls,
                                                 cameraControls);
            this->setCameraControls(source, controls);
            this->m_localCameraControls = cameraControls;
        }

        source->Release();
    }

    AM_MEDIA_TYPE mediaType;
    ZeroMemory(&mediaType, sizeof(AM_MEDIA_TYPE));
    this->m_grabber->GetConnectedMediaType(&mediaType);
    AkCaps caps = this->capsFromMediaType(&mediaType);

    AkPacket packet;

    timeval timestamp;
    gettimeofday(&timestamp, NULL);

    qint64 pts = qint64((timestamp.tv_sec
                         + 1e-6 * timestamp.tv_usec)
                        * this->m_timeBase.invert().value());

    if (this->m_ioMethod != IoMethodDirectRead) {
        this->m_mutex.lock();

        if (this->m_curBuffer.isEmpty())
            this->m_waitCondition.wait(&this->m_mutex, 1000);

        if (!this->m_curBuffer.isEmpty()) {
            int bufferSize = this->m_curBuffer.size();
            QByteArray oBuffer(bufferSize, Qt::Uninitialized);
            memcpy(oBuffer.data(),
                   this->m_curBuffer.constData(),
                   size_t(bufferSize));

            packet = AkPacket(caps, oBuffer);
            packet.setPts(pts);
            packet.setTimeBase(this->m_timeBase);
            packet.setIndex(0);
            packet.setId(this->m_id);
            this->m_curBuffer.clear();
        }

        this->m_mutex.unlock();
    } else {
        long bufferSize;

        HRESULT hr = this->m_grabber->GetCurrentBuffer(&bufferSize, NULL);

        if (FAILED(hr))
            return AkPacket();

        QByteArray oBuffer(bufferSize, Qt::Uninitialized);
        hr = this->m_grabber->GetCurrentBuffer(&bufferSize,
                                               reinterpret_cast<long *>(oBuffer.data()));

        if (FAILED(hr))
            return AkPacket();

        packet = AkPacket(caps, oBuffer);
        packet.setPts(pts);
        packet.setTimeBase(this->m_timeBase);
        packet.setIndex(0);
        packet.setId(this->m_id);
    }

    return packet;
}