diff --git a/README.md b/README.md index 1168e5c..e510070 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,8 @@ A simple webcam recorder for Omarchy. Choose your camera and microphone once, then press **Space** to record. Press again to pause or resume the same take. Stop the take and it opens right away in a built-in editor: trim either end, cut ranges out of the middle, then **Save**. - Remembers camera and microphone by device ID; missing inputs never silently switch. -- Automatically selects the camera's maximum advertised video resolution, preferring 30 fps at that resolution. Preview preserves the whole frame. +- Automatically selects the camera's maximum advertised video resolution, preferring 30 fps at that resolution. Preview preserves the whole frame. A resolution menu next to the camera lets slower machines choose a smaller size, which is remembered. +- MJPEG cameras are decoded off the GUI thread and encoded as ordinary 4:2:0 H.264, so high resolutions stay playable and affordable on modest CPUs. - Live microphone meter with peak hold and clipping indication, including while paused. No microphone playback through your speakers. - Explicit No audio option for silent recordings. - Live Omarchy accent syncing, including theme symlink switches. Controls and dialogs follow Hyprland's active corner rounding, including personal overrides. Dark chrome and a yellow fallback follow Omacut; button foregrounds adapt for contrast. diff --git a/src/Main.qml b/src/Main.qml index 972c8b5..9fc4403 100644 --- a/src/Main.qml +++ b/src/Main.qml @@ -160,6 +160,7 @@ ApplicationWindow { readonly property var icons: ({ camera: "M4.5 6h9a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2h-9a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2z M15.5 10.5l6-3.5v10l-6-3.5z", microphone: "M12 3a3 3 0 0 1 3 3v5a3 3 0 0 1-6 0V6a3 3 0 0 1 3-3z M5.5 11a6.5 6.5 0 0 0 13 0 M12 17.5V21", + resolution: "M4 9V5h4 M20 9V5h-4 M4 15v4h4 M20 15v4h-4", chevron: "M6 9l6 6 6-6", back: "M4 12a8 8 0 1 0 2.4-5.7 M4 4v4.5h4.5", play: "M8 4.5v15l12.5-7.5z", @@ -418,6 +419,14 @@ ApplicationWindow { onActivated: index => { backend.selectCamera(index); liveVideo.forceActiveFocus() } Accessible.name: "Camera" } + SourceChoice { + id: resolutionChoice; iconPath: win.icons.resolution + maximumTextWidth: Math.min(260, sources.width - 44) + model: backend.resolutions; currentIndex: backend.resolutionIndex + enabled: !backend.takeActive && !win.busy && backend.resolutions.length > 1 + onActivated: index => { backend.selectResolution(index); liveVideo.forceActiveFocus() } + Accessible.name: "Resolution" + } SourceChoice { id: microphoneChoice; iconPath: win.icons.microphone maximumTextWidth: Math.min(260, sources.width - 44) diff --git a/src/backend.cpp b/src/backend.cpp index 9c50603..8cc2256 100644 --- a/src/backend.cpp +++ b/src/backend.cpp @@ -27,6 +27,10 @@ Backend::Backend(FilePicker *picker, bool activateHardware, QObject *parent) m_root = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + "/recordings"; m_cameraId=m_settings.value("camera/id").toString(); m_audioId=m_settings.value("microphone/id").toString(); + { + const auto parts=m_settings.value("camera/resolution").toString().split('x'); + if(parts.size()==2) m_chosenResolution=QSize(parts[0].toInt(),parts[1].toInt()); + } m_capture.setVideoSink(&m_sink); connect(&m_sink,&QVideoSink::videoFrameChanged,this,&Backend::receiveVideo); if(m_activateHardware) { @@ -54,6 +58,10 @@ Backend::~Backend() { if(m_export) { m_export->disconnect(this); m_export->kill(); m_export->waitForFinished(); QFile::remove(m_exportPartial); } } int Backend::cameraIndex() const { return indexOf(m_cameras,m_cameraId); } +int Backend::resolutionIndex() const { + for(int i=0;i=m_cameras.size() || m_state=="saving" || m_probing) return; m_cameraId=m_cameras[index].toMap()["id"].toString(); activateSources(); } +void Backend::selectResolution(int index) { + if(m_writer || index<0 || index>=m_resolutions.size() || m_state=="saving" || m_probing) return; + const auto choice=m_resolutions[index].toMap(); + // The first entry is the maximum, which is stored as "no preference". + m_chosenResolution=index==0 ? QSize() : QSize(choice["width"].toInt(),choice["height"].toInt()); + if(index==0) m_settings.remove("camera/resolution"); + else m_settings.setValue("camera/resolution",QString("%1x%2").arg(m_chosenResolution.width()).arg(m_chosenResolution.height())); + activateSources(); +} void Backend::selectMicrophone(int index) { if(m_writer || index<0 || index>=m_microphones.size() || m_state=="saving" || m_probing) return; m_audioId=m_microphones[index].toMap()["id"].toString(); activateSources(); diff --git a/src/backend.h b/src/backend.h index e8eaceb..4e1a548 100644 --- a/src/backend.h +++ b/src/backend.h @@ -1,4 +1,5 @@ #pragma once +#include #include #include #include @@ -26,6 +27,8 @@ class Backend : public QObject { Q_PROPERTY(QVariantList cameras READ cameras NOTIFY devicesChanged) Q_PROPERTY(QVariantList microphones READ microphones NOTIFY devicesChanged) Q_PROPERTY(int cameraIndex READ cameraIndex NOTIFY devicesChanged) + Q_PROPERTY(QVariantList resolutions READ resolutions NOTIFY resolutionsChanged) + Q_PROPERTY(int resolutionIndex READ resolutionIndex NOTIFY resolutionsChanged) Q_PROPERTY(int microphoneIndex READ microphoneIndex NOTIFY devicesChanged) Q_PROPERTY(bool ready READ ready NOTIFY changed) Q_PROPERTY(bool audioEnabled READ audioEnabled NOTIFY changed) @@ -54,6 +57,8 @@ class Backend : public QObject { QString message() const { return m_message; } QString formatLabel() const { return m_formatLabel; } QVariantList cameras() const { return m_cameras; } + QVariantList resolutions() const { return m_resolutions; } + int resolutionIndex() const; QVariantList microphones() const { return m_microphones; } int cameraIndex() const; int microphoneIndex() const; @@ -78,6 +83,7 @@ class Backend : public QObject { edit::Clips currentEdit() const { return m_edit; } Q_INVOKABLE void setPreview(QObject *sink); Q_INVOKABLE void selectCamera(int index); + Q_INVOKABLE void selectResolution(int index); Q_INVOKABLE void selectMicrophone(int index); Q_INVOKABLE void retry(); Q_INVOKABLE void toggleRecording(); @@ -102,6 +108,7 @@ class Backend : public QObject { signals: void changed(); void devicesChanged(); + void resolutionsChanged(); void meterChanged(); void editChanged(); void safeToClose(); @@ -150,7 +157,8 @@ class Backend : public QObject { bool m_activateHardware; QString m_pendingDestination; Writer *m_writer = nullptr; - QVariantList m_cameras, m_microphones; + QVariantList m_cameras, m_microphones, m_resolutions; + QSize m_chosenResolution; // empty means the camera's maximum QString m_cameraId, m_audioId, m_state = "starting", m_message, m_formatLabel; QString m_root, m_takeId, m_clipPath, m_clipFileName; QString m_interruption; diff --git a/src/common.pri b/src/common.pri index f7936b5..b10b4e8 100644 --- a/src/common.pri +++ b/src/common.pri @@ -1,5 +1,5 @@ CONFIG += link_pkgconfig -PKGCONFIG += libpulse +PKGCONFIG += libpulse libswscale libavcodec libavutil HEADERS += $$PWD/theme.h $$PWD/mediautils.h $$PWD/writer.h $$PWD/backend.h \ $$PWD/filepicker.h $$PWD/portalfilepicker.h $$PWD/audiocapture.h \ $$PWD/edit.h $$PWD/thumbnails.h diff --git a/src/mediautils.cpp b/src/mediautils.cpp index 190212f..964fff0 100644 --- a/src/mediautils.cpp +++ b/src/mediautils.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -28,12 +29,29 @@ int media::bestFormat(const QList &formats) { } return best; } -QCameraFormat media::bestCameraFormat(const QCameraDevice &device) { +QCameraFormat media::bestCameraFormat(const QCameraDevice &device, const QSize &preferred) { const auto formats = device.videoFormats(); QList choices; - for (const auto &f : formats) choices.append({f.resolution(), f.minFrameRate(), f.maxFrameRate(), int(f.pixelFormat())}); + QList origin; + for (int i = 0; i < formats.size(); ++i) { + const auto &f = formats[i]; + if (!preferred.isEmpty() && f.resolution() != preferred) continue; + choices.append({f.resolution(), f.minFrameRate(), f.maxFrameRate(), int(f.pixelFormat())}); + origin.append(i); + } + // An explicit resolution that this camera lacks falls back to the maximum. + if (choices.isEmpty() && !preferred.isEmpty()) return bestCameraFormat(device); const int index = bestFormat(choices); - return index < 0 ? QCameraFormat() : formats[index]; + return index < 0 ? QCameraFormat() : formats[origin[index]]; +} +QList media::cameraResolutions(const QCameraDevice &device) { + QList sizes; + for (const auto &f : device.videoFormats()) + if (!f.resolution().isEmpty() && f.maxFrameRate() > 0 && !sizes.contains(f.resolution())) sizes.append(f.resolution()); + std::sort(sizes.begin(), sizes.end(), [](const QSize &a, const QSize &b) { + return qint64(a.width()) * a.height() > qint64(b.width()) * b.height(); + }); + return sizes; } double media::peak(const QByteArray &data, const QAudioFormat &format) { double result = 0; diff --git a/src/mediautils.h b/src/mediautils.h index 17645d3..d6bbae7 100644 --- a/src/mediautils.h +++ b/src/mediautils.h @@ -10,7 +10,8 @@ namespace media { struct Format { QSize size; double minFps; double maxFps; int pixelFormat; }; int bestFormat(const QList &formats); -QCameraFormat bestCameraFormat(const QCameraDevice &device); +QCameraFormat bestCameraFormat(const QCameraDevice &device, const QSize &preferred = {}); +QList cameraResolutions(const QCameraDevice &device); double targetFps(double minimum, double maximum); double peak(const QByteArray &data, const QAudioFormat &format); QString copyAtomically(const QString &source, const QString &destination); diff --git a/src/writer.cpp b/src/writer.cpp index 3a29664..29750d0 100644 --- a/src/writer.cpp +++ b/src/writer.cpp @@ -1,4 +1,12 @@ #include "writer.h" +#include +#include +#include +extern "C" { +#include +#include +#include +} #include #include #include @@ -30,6 +38,54 @@ class FrameBuffer : public QAbstractVideoBuffer { QVideoFrame m_source; bool m_mapped=false; }; + +// Webcams deliver MJPEG, which Qt's encoder decodes to 4:4:4 that hardware H.264 +// encoders reject and software x264 struggles with at 1080p. Hand the encoder +// plain 8-bit 4:2:0 NV12 instead, which also plays everywhere. Runs on a worker. +QVideoFrame jpegToNv12(const QByteArray &jpeg, const QSize &size) { + const AVCodec *codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG); + if (!codec) return {}; + AVCodecContext *decoder = avcodec_alloc_context3(codec); + AVPacket *packet = av_packet_alloc(); + AVFrame *decoded = av_frame_alloc(); + QVideoFrame output; + if (decoder && packet && decoded && avcodec_open2(decoder, codec, nullptr) == 0 + && av_new_packet(packet, jpeg.size()) == 0) { + memcpy(packet->data, jpeg.constData(), jpeg.size()); + if (avcodec_send_packet(decoder, packet) == 0 && avcodec_receive_frame(decoder, decoded) == 0 + && decoded->width == size.width() && decoded->height == size.height()) { + QVideoFrameFormat format(size, QVideoFrameFormat::Format_NV12); + format.setColorSpace(QVideoFrameFormat::ColorSpace_BT601); + format.setColorRange(QVideoFrameFormat::ColorRange_Video); + QVideoFrame frame(format); + if (frame.map(QVideoFrame::WriteOnly)) { + thread_local SwsContext *context = nullptr; + const bool fullRange = decoded->color_range == AVCOL_RANGE_JPEG || decoded->format == AV_PIX_FMT_YUVJ420P + || decoded->format == AV_PIX_FMT_YUVJ422P || decoded->format == AV_PIX_FMT_YUVJ444P; + AVPixelFormat source = AVPixelFormat(decoded->format); + if (source == AV_PIX_FMT_YUVJ420P) source = AV_PIX_FMT_YUV420P; + else if (source == AV_PIX_FMT_YUVJ422P) source = AV_PIX_FMT_YUV422P; + else if (source == AV_PIX_FMT_YUVJ444P) source = AV_PIX_FMT_YUV444P; + context = sws_getCachedContext(context, size.width(), size.height(), source, + size.width(), size.height(), AV_PIX_FMT_NV12, SWS_BILINEAR, nullptr, nullptr, nullptr); + uint8_t *planes[2] = {frame.bits(0), frame.bits(1)}; + const int strides[2] = {frame.bytesPerLine(0), frame.bytesPerLine(1)}; + int rows = 0; + if (context) { + const int *coefficients = sws_getCoefficients(SWS_CS_ITU601); + sws_setColorspaceDetails(context, coefficients, fullRange ? 1 : 0, coefficients, 0, 0, 1 << 16, 1 << 16); + rows = sws_scale(context, decoded->data, decoded->linesize, 0, size.height(), planes, strides); + } + frame.unmap(); + if (rows == size.height()) output = frame; + } + } + } + av_frame_free(&decoded); + av_packet_free(&packet); + avcodec_free_context(&decoder); + return output; +} } void TakeClock::start(qint64 now) { @@ -75,6 +131,7 @@ QList TakeClock::joins() const { } Writer::Writer(QObject *parent) : QObject(parent) { + m_convertPool.setMaxThreadCount(3); m_session.setRecorder(&m_recorder); m_flushTimeout.setSingleShot(true); m_flushTimeout.setInterval(10000); @@ -93,6 +150,7 @@ static QMediaFormat recordingFormat(bool audio) { if (audio) f.setAudioCodec(QMediaFormat::AudioCodec::AAC); return f; } +Writer::~Writer() { m_convertPool.waitForDone(); } bool Writer::supported(bool audio) { return recordingFormat(audio).isSupported(QMediaFormat::Encode); } bool Writer::start(const QString &path, const QVideoFrameFormat &format, double fps, const QAudioFormat &audioFormat, qint64 now) { @@ -137,16 +195,32 @@ void Writer::videoAt(QVideoFrame frame, qint64 position) { const qint64 frameIndex = qRound64(position * m_fps / 1000000.0); const qint64 timestamp = qRound64(frameIndex * 1000000.0 / m_fps); if (timestamp <= m_lastVideo) return; - m_tailFrame = frame; - frame = QVideoFrame(std::make_unique(frame)); - frame.setStartTime(timestamp); - frame.setEndTime(qRound64((frameIndex + 1) * 1000000.0 / m_fps)); - frame.setStreamFrameRate(m_fps); - m_lastVideo = timestamp; // Bounded native-frame references; never build an unbounded 4K frame queue. if (m_frames.size() >= 6) { fail("Video encoding cannot keep up at this camera's maximum resolution, so the take was stopped."); return; } - m_frames.enqueue(frame); - drain(); + m_tailFrame = frame; + m_lastVideo = timestamp; + auto job = std::make_shared(); + bool queued = false; + if (frame.pixelFormat() == QVideoFrameFormat::Format_Jpeg) { + QByteArray jpeg; + if (frame.map(QVideoFrame::ReadOnly)) { + jpeg = QByteArray(reinterpret_cast(frame.bits(0)), frame.mappedBytes(0)); + frame.unmap(); + } + if (jpeg.isEmpty()) return; + const QSize size = frame.size(); + queued = true; + m_convertPool.start([this, job, jpeg, size] { + job->frame = jpegToNv12(jpeg, size); + job->ready.store(true, std::memory_order_release); + QMetaObject::invokeMethod(this, &Writer::drain, Qt::QueuedConnection); + }); + } else { + job->frame = QVideoFrame(std::make_unique(frame)); + job->ready.store(true, std::memory_order_release); + } + m_frames.enqueue({job, timestamp, qRound64((frameIndex + 1) * 1000000.0 / m_fps), {}}); + if (!queued) drain(); } void Writer::audio(QByteArray data, qint64 capturedAt) { if (m_stopping || m_failed || !m_audio || data.isEmpty()) return; @@ -194,7 +268,18 @@ void Writer::appendAudio(const QByteArray &data) { void Writer::drain() { if (m_draining || m_failed) return; m_draining = true; - while (!m_frames.isEmpty() && m_video->sendVideoFrame(m_frames.head())) m_frames.dequeue(); + while (!m_frames.isEmpty() && m_frames.head().job->ready.load(std::memory_order_acquire)) { + auto &head = m_frames.head(); + if (!head.job->frame.isValid()) { m_frames.dequeue(); continue; } // undecodable camera frame + if (!head.frame.isValid()) { + head.frame = QVideoFrame(std::make_unique(head.job->frame)); + head.frame.setStartTime(head.start); + head.frame.setEndTime(head.end); + head.frame.setStreamFrameRate(m_fps); + } + if (!m_video->sendVideoFrame(head.frame)) break; + m_frames.dequeue(); + } while (!m_buffers.isEmpty() && m_audio->sendAudioBuffer(m_buffers.head())) { m_queuedAudioUs -= m_buffers.head().duration(); m_buffers.dequeue(); } diff --git a/src/writer.h b/src/writer.h index 9838276..cb8bd53 100644 --- a/src/writer.h +++ b/src/writer.h @@ -6,6 +6,9 @@ #include #include #include +#include +#include +#include #include #include @@ -34,6 +37,7 @@ class Writer : public QObject { Q_OBJECT public: explicit Writer(QObject *parent = nullptr); + ~Writer() override; bool start(const QString &path, const QVideoFrameFormat &videoFormat, double fps, const QAudioFormat &audioFormat, qint64 now); void video(QVideoFrame frame, qint64 capturedAt); @@ -58,7 +62,11 @@ class Writer : public QObject { QVideoFrameInput *m_video = nullptr; QAudioBufferInput *m_audio = nullptr; QAudioFormat m_audioFormat; - QQueue m_frames; + // A video frame waiting for its (possibly off-thread) conversion to finish. + struct Job { std::atomic ready{false}; QVideoFrame frame; }; + struct Pending { std::shared_ptr job; qint64 start = 0, end = 0; QVideoFrame frame; }; + QQueue m_frames; + QThreadPool m_convertPool; QVideoFrame m_tailFrame; QQueue m_buffers; QTimer m_flushTimeout; diff --git a/tests/ui_tests.cpp b/tests/ui_tests.cpp index 85371be..9b6e46c 100644 --- a/tests/ui_tests.cpp +++ b/tests/ui_tests.cpp @@ -28,6 +28,8 @@ QtObject { property var cameras: [{label: "Test camera"}] property var microphones: [{label: "Test microphone"}] property int cameraIndex: 0 + property var resolutions: [{label: "1920 × 1080 maximum"}, {label: "1280 × 720"}] + property int resolutionIndex: 0 property int microphoneIndex: 0 property bool ready: state === "ready" property bool audioEnabled: true @@ -63,6 +65,7 @@ QtObject { function toggleRecording() { toggles++; state = state === "ready" || state === "paused" ? "recording" : "paused"; changed() } function finish() { state = "finished"; changed() } function selectCamera(i) {} + function selectResolution(i) {} function selectMicrophone(i) {} function newRecording() { newRecordings++; state = "ready"; changed() } function closeTake() {}