diff --git a/MULTITRACK-AUDIO.md b/MULTITRACK-AUDIO.md new file mode 100644 index 0000000..8f7077a --- /dev/null +++ b/MULTITRACK-AUDIO.md @@ -0,0 +1,134 @@ +# Separate audio tracks on Ubuntu 24.04 + +This combined patch targets Ubuntu noble's `simplescreenrecorder 0.4.4-3build4` +source AFTER its four distribution patches have been applied. `dpkg-source -x` +and `apt source` apply those patches automatically. Apply it to fresh distro +source, without either of the earlier feature patches. It includes separate +PulseAudio tracks, checkbox-only source selection, and the optional recording +indicator on unrecorded screens. + +Ubuntu distributes the original upstream tarball and a separate Debian packaging +archive. The original tarball alone does NOT include the distribution patches. +In particular, `debian/patches/0003-Fix-build-with-ffmpeg-5.0.patch` adds the +AVCodec const-correctness changes required by newer FFmpeg. The other patches +adjust the private GLInject library path and AppStream metadata. Ubuntu inherited +these patches from Debian; the build4 revision is a no-change rebuild. + +## Download, apply, and build (amd64) + +Install build dependencies, without needing deb-src repositories: + +```sh +sudo apt update +sudo apt install build-essential dpkg-dev wget cmake pkg-config \ + qtbase5-dev qttools5-dev qttools5-dev-tools libqt5x11extras5-dev \ + libavformat-dev libavcodec-dev libavutil-dev libswscale-dev \ + libasound2-dev libpulse-dev libjack-jackd2-dev libv4l-dev \ + libgl1-mesa-dev libglu1-mesa-dev libx11-dev libxext-dev \ + libxfixes-dev libxi-dev libxinerama-dev +``` + +In a new empty working directory: + +```sh +base=https://archive.ubuntu.com/ubuntu/pool/universe/s/simplescreenrecorder +wget "$base/simplescreenrecorder_0.4.4.orig.tar.gz" +wget "$base/simplescreenrecorder_0.4.4-3build4.debian.tar.xz" +wget "$base/simplescreenrecorder_0.4.4-3build4.dsc" +dpkg-source -x simplescreenrecorder_0.4.4-3build4.dsc ssr-ubuntu-24.04 +cd ssr-ubuntu-24.04 +patch --dry-run -p1 < /path/to/ssr-ubuntu-24.04-multitrack-and-recording-border.patch +patch -p1 < /path/to/ssr-ubuntu-24.04-multitrack-and-recording-border.patch +cmake -S . -B build -DWITH_QT5=TRUE -DENABLE_FFMPEG_VERSIONS=TRUE \ + -DCMAKE_INSTALL_LIBDIR=/usr/lib/x86_64-linux-gnu/simplescreenrecorder +cmake --build build -j"$(nproc)" +./build/src/simplescreenrecorder +``` + +The CMake options match `debian/rules` on amd64, including the private GLInject +library directory. Normal desktop recording works directly from the build tree. +To install the executable and GLInject library to the configured locations: + +```sh +sudo cmake --install build +``` + +This CMake install is not managed by apt. You can instead build Debian packages +using the packaging below. Neither installation step is needed to test desktop +recording from the build tree. + +For non-x86 architectures, follow `debian/rules`: disable `ENABLE_X86_ASM` and +`WITH_GLINJECT` and omit the amd64-specific install directory. + +Source archive SHA-256 checksums (as listed in the .dsc): + +- upstream: `69fccf8df6e7285d4df5033eed8748f4eae13b407488906dcf4810a277d557b8` +- packaging: `215bc70d3060144f8d8852f67238b39a3fb20e09d5589555621abed1d4291140` + +## Optional Debian package build + +The feature patch must be registered with quilt to build a modified source +package. Start again from a freshly extracted Ubuntu source directory, BEFORE +applying the feature patch directly: + +```sh +sudo apt install debhelper quilt +cp /path/to/ssr-ubuntu-24.04-multitrack-and-recording-border.patch \ + debian/patches/9000-multitrack-and-recording-border.patch +printf '%s\n' 9000-multitrack-and-recording-border.patch >> debian/patches/series +QUILT_PATCHES=debian/patches quilt push -a +dpkg-buildpackage -us -uc -b +``` + +The resulting .deb files are written in the parent directory. Keep both +simplescreenrecorder and simplescreenrecorder-lib together for an amd64 install. + +## Recording indicator + +In the initial input setup, select **Record the entire screen**, select the +projector monitor, and enable **Show a pulsing red border on screens that are not +being recorded** directly below it. This option defaults to off and is saved in +input profiles/settings. During active recording, other monitors receive an +opaque, click-through, 10-pixel red edge ring pulsing every 1.5 seconds. It works +on X11 without a compositor. Pausing, stopping, saving, or an error removes it; +preview alone does not activate it. Hiding SSR does not hide the indicator. + +Any monitor intersecting the actual capture rectangle is excluded, including +mirrored monitors. Recording all screens leaves no monitor eligible for a border. +Screen-layout changes update the indicators. This option applies to entire-screen +capture only; it does not change fixed-rectangle recording-area borders. + +## Record + +1. Enable **Record audio** and select **PulseAudio**. +2. Check your desktop/output monitor in **Sources (separate audio tracks)**. +3. Check your microphone in the same list. Select one to eight sources. + There is no separate source dropdown. Existing source selections are migrated. +4. Use a container that supports multiple audio streams, such as MKV or MP4. +5. Record normally. Each source gets its own stereo stream, independently + encoded at the selected audio bitrate. Source names become track titles. + The first checked source in list order is the default playback track. + +No microphone loopback or mixed PulseAudio device is created. Disable any +previous microphone loopback into desktop output to avoid recording the same +microphone again in the desktop track. Previously mixed sources cannot be unmixed. + +The preview meter shows the first checked source in list order. Selections are saved in +input profiles/settings. ALSA and JACK retain single-track capture. Tracks share +video timing and pause/resume boundaries, with independent resampling and drift +correction. Every selected source must supply audio; a stalled source can prevent +the shared timeline from advancing. + +Run on an X11 desktop session; this patch does not add Wayland screen capture. + +## Inspect or extract tracks + +```sh +ffprobe -v error -select_streams a \ + -show_entries stream=index,codec_name,channels:stream_tags=title \ + -of json recording.mkv +ffmpeg -i recording.mkv -map 0:a:0 -c:a pcm_s24le desktop.wav +ffmpeg -i recording.mkv -map 0:a:1 -c:a pcm_s24le microphone.wav +``` + +Some editors need the extraction step to import every audio stream. diff --git a/src/AV/Output/Muxer.h b/src/AV/Output/Muxer.h index b104bcb..7d0a546 100644 --- a/src/AV/Output/Muxer.h +++ b/src/AV/Output/Muxer.h @@ -22,7 +22,7 @@ along with SimpleScreenRecorder. If not, see . #include "MutexDataPair.h" -#define MUXER_MAX_STREAMS 2 +#define MUXER_MAX_STREAMS 9 // one video stream and up to eight audio streams class AVPacketWrapper; class BaseEncoder; diff --git a/src/AV/Output/OutputManager.cpp b/src/AV/Output/OutputManager.cpp index 283dcb9..ab63b3c 100644 --- a/src/AV/Output/OutputManager.cpp +++ b/src/AV/Output/OutputManager.cpp @@ -20,6 +20,7 @@ along with SimpleScreenRecorder. If not, see . #include "OutputManager.h" #include "Logger.h" +#include const size_t OutputManager::THROTTLE_THRESHOLD_FRAMES = 20; const size_t OutputManager::THROTTLE_THRESHOLD_PACKETS = 100; @@ -36,6 +37,8 @@ static QString GetNewFragmentFile(const QString& file, unsigned int fragment_num OutputManager::OutputManager(const OutputSettings& output_settings) { m_output_settings = output_settings; + m_audio_streams = output_settings.audio_codec_avname.isEmpty()? 0 : std::max(1, output_settings.audio_stream_names.size()); + if(m_audio_streams > SSR_MAX_AUDIO_STREAMS) throw std::runtime_error("Too many audio sources (maximum eight)"); m_fragmented = false; m_fragment_length = 5; @@ -45,7 +48,7 @@ OutputManager::OutputManager(const OutputSettings& output_settings) { SharedLock lock(&m_shared_data); lock->m_fragment_number = 0; lock->m_video_encoder = NULL; - lock->m_audio_encoder = NULL; + for(auto& encoder : lock->m_audio_encoder) encoder = NULL; } // initialize thread signals @@ -125,7 +128,8 @@ void OutputManager::AddVideoFrame(std::unique_ptr frame) { } } -void OutputManager::AddAudioFrame(std::unique_ptr frame) { +void OutputManager::AddAudioFrame(std::unique_ptr frame, unsigned int track) { + assert(track < m_audio_streams); assert(frame->GetFrame()->pts != (int64_t) AV_NOPTS_VALUE); SharedLock lock(&m_shared_data); if(m_fragmented) { @@ -133,14 +137,14 @@ void OutputManager::AddAudioFrame(std::unique_ptr frame) { int64_t fragment_end = m_fragment_length * m_output_format.m_audio_sample_rate * lock->m_fragment_number; if(frame->GetFrame()->pts < fragment_end) { frame->GetFrame()->pts -= fragment_begin; - assert(lock->m_audio_encoder != NULL); - lock->m_audio_encoder->AddFrame(std::move(frame)); + assert(lock->m_audio_encoder[track] != NULL); + lock->m_audio_encoder[track]->AddFrame(std::move(frame)); } else { - lock->m_audio_frame_queue.push_back(std::move(frame)); + lock->m_audio_frame_queue[track].push_back(std::move(frame)); } } else { - assert(lock->m_audio_encoder != NULL); - lock->m_audio_encoder->AddFrame(std::move(frame)); + assert(lock->m_audio_encoder[track] != NULL); + lock->m_audio_encoder[track]->AddFrame(std::move(frame)); } } @@ -215,13 +219,13 @@ void OutputManager::Init() { } else { m_output_format.m_video_enabled = false; } - if(lock->m_audio_encoder != NULL) { + if(lock->m_audio_encoder[0] != NULL) { m_output_format.m_audio_enabled = true; - m_output_format.m_audio_channels = lock->m_audio_encoder->GetChannels(); //TODO// never larger than AV_NUM_DATA_POINTERS + m_output_format.m_audio_channels = lock->m_audio_encoder[0]->GetChannels(); //TODO// never larger than AV_NUM_DATA_POINTERS assert(m_output_format.m_audio_channels <= AV_NUM_DATA_POINTERS); - m_output_format.m_audio_sample_rate = lock->m_audio_encoder->GetSampleRate(); - m_output_format.m_audio_frame_size = lock->m_audio_encoder->GetFrameSize(); - m_output_format.m_audio_sample_format = lock->m_audio_encoder->GetSampleFormat(); + m_output_format.m_audio_sample_rate = lock->m_audio_encoder[0]->GetSampleRate(); + m_output_format.m_audio_frame_size = lock->m_audio_encoder[0]->GetFrameSize(); + m_output_format.m_audio_sample_format = lock->m_audio_encoder[0]->GetSampleFormat(); } else { m_output_format.m_audio_enabled = false; } @@ -246,7 +250,7 @@ void OutputManager::Free() { { SharedLock lock(&m_shared_data); lock->m_video_encoder = NULL; // deleted by muxer - lock->m_audio_encoder = NULL; // deleted by muxer + for(auto& encoder : lock->m_audio_encoder) encoder = NULL; // deleted by muxer lock->m_muxer.reset(); } @@ -271,20 +275,32 @@ void OutputManager::StartFragment() { } std::unique_ptr muxer(new Muxer(m_output_settings.container_avname, filename)); VideoEncoder *video_encoder = NULL; - AudioEncoder *audio_encoder = NULL; + AudioEncoder *audio_encoder[SSR_MAX_AUDIO_STREAMS] = {}; if(!m_output_settings.video_codec_avname.isEmpty()) video_encoder = muxer->AddVideoEncoder(m_output_settings.video_codec_avname, m_output_settings.video_options, m_output_settings.video_kbit_rate * 1000, m_output_settings.video_width, m_output_settings.video_height, m_output_settings.video_frame_rate); - if(!m_output_settings.audio_codec_avname.isEmpty()) - audio_encoder = muxer->AddAudioEncoder(m_output_settings.audio_codec_avname, m_output_settings.audio_options, m_output_settings.audio_kbit_rate * 1000, - m_output_settings.audio_channels, m_output_settings.audio_sample_rate); + for(unsigned int track = 0; track < m_audio_streams; ++track) { + audio_encoder[track] = muxer->AddAudioEncoder(m_output_settings.audio_codec_avname, m_output_settings.audio_options, + m_output_settings.audio_kbit_rate * 1000, m_output_settings.audio_channels, m_output_settings.audio_sample_rate); + if(track > 0 && (audio_encoder[track]->GetFrameSize() != audio_encoder[0]->GetFrameSize() || + audio_encoder[track]->GetSampleFormat() != audio_encoder[0]->GetSampleFormat() || + audio_encoder[track]->GetSampleRate() != audio_encoder[0]->GetSampleRate() || + audio_encoder[track]->GetChannels() != audio_encoder[0]->GetChannels())) + throw std::runtime_error("Audio encoders returned different formats"); + if(track < (unsigned int) m_output_settings.audio_stream_names.size()) { + QByteArray title = m_output_settings.audio_stream_names[track].toUtf8(); + av_dict_set(&audio_encoder[track]->GetStream()->metadata, "title", title.constData(), 0); + } + audio_encoder[track]->GetStream()->disposition = (track == 0)? AV_DISPOSITION_DEFAULT : 0; + } muxer->Start(); // acquire lock and share the muxer and encoders SharedLock lock(&m_shared_data); lock->m_muxer = std::move(muxer); lock->m_video_encoder = video_encoder; - lock->m_audio_encoder = audio_encoder; + for(unsigned int track = 0; track < m_audio_streams; ++track) + lock->m_audio_encoder[track] = audio_encoder[track]; // increment fragment number // It's important that this is done here (i.e. after the encoders have been set up), because the fragment number @@ -303,15 +319,17 @@ void OutputManager::StartFragment() { frame->GetFrame()->pts -= fragment_begin; video_encoder->AddFrame(std::move(frame)); } - while(!lock->m_audio_frame_queue.empty()) { - int64_t fragment_begin = m_fragment_length * m_output_format.m_audio_sample_rate * (lock->m_fragment_number - 1); - int64_t fragment_end = m_fragment_length * m_output_format.m_audio_sample_rate * lock->m_fragment_number; - if(lock->m_audio_frame_queue.front()->GetFrame()->pts >= fragment_end) - break; - std::unique_ptr frame = std::move(lock->m_audio_frame_queue.front()); - lock->m_audio_frame_queue.pop_front(); - frame->GetFrame()->pts -= fragment_begin; - audio_encoder->AddFrame(std::move(frame)); + for(unsigned int track = 0; track < m_audio_streams; ++track) { + while(!lock->m_audio_frame_queue[track].empty()) { + int64_t fragment_begin = m_fragment_length * m_output_format.m_audio_sample_rate * (lock->m_fragment_number - 1); + int64_t fragment_end = m_fragment_length * m_output_format.m_audio_sample_rate * lock->m_fragment_number; + if(lock->m_audio_frame_queue[track].front()->GetFrame()->pts >= fragment_end) + break; + std::unique_ptr frame = std::move(lock->m_audio_frame_queue[track].front()); + lock->m_audio_frame_queue[track].pop_front(); + frame->GetFrame()->pts -= fragment_begin; + audio_encoder[track]->AddFrame(std::move(frame)); + } } } @@ -325,7 +343,7 @@ void OutputManager::StopFragment() { SharedLock lock(&m_shared_data); muxer = std::move(lock->m_muxer); lock->m_video_encoder = NULL; // deleted by muxer - lock->m_audio_encoder = NULL; // deleted by muxer + for(auto& encoder : lock->m_audio_encoder) encoder = NULL; // deleted by muxer } // wait until the muxer is finished @@ -354,9 +372,13 @@ void OutputManager::FragmentThread() { SharedLock lock(&m_shared_data); //TODO// this delays the creation of a new fragment by one frame, this could be improved if(finishing) { - next_fragment = (!lock->m_video_frame_queue.empty() || !lock->m_audio_frame_queue.empty()); + next_fragment = !lock->m_video_frame_queue.empty(); + for(unsigned int track = 0; track < m_audio_streams; ++track) + next_fragment |= !lock->m_audio_frame_queue[track].empty(); } else { - next_fragment = (!lock->m_video_frame_queue.empty() && !lock->m_audio_frame_queue.empty()); + next_fragment = !lock->m_video_frame_queue.empty(); + for(unsigned int track = 0; track < m_audio_streams; ++track) + next_fragment &= !lock->m_audio_frame_queue[track].empty(); } } diff --git a/src/AV/Output/OutputManager.h b/src/AV/Output/OutputManager.h index 6a48223..ccfcc29 100644 --- a/src/AV/Output/OutputManager.h +++ b/src/AV/Output/OutputManager.h @@ -33,13 +33,13 @@ private: // frame queue used while switching between fragments std::deque > m_video_frame_queue; - std::deque > m_audio_frame_queue; + std::deque > m_audio_frame_queue[SSR_MAX_AUDIO_STREAMS]; unsigned int m_fragment_number; // muxer and encoders std::unique_ptr m_muxer; VideoEncoder *m_video_encoder; - AudioEncoder *m_audio_encoder; + AudioEncoder *m_audio_encoder[SSR_MAX_AUDIO_STREAMS]; }; typedef MutexDataPair::Lock SharedLock; @@ -51,6 +51,7 @@ private: OutputSettings m_output_settings; OutputFormat m_output_format; + unsigned int m_audio_streams; bool m_fragmented; int64_t m_fragment_length; @@ -77,7 +78,7 @@ public: // Adds an audio frame to the frame queue. Called by the synchronizer. // This function is thread-safe. - void AddAudioFrame(std::unique_ptr frame); + void AddAudioFrame(std::unique_ptr frame, unsigned int track = 0); // Returns an additional delay (in us) between frames, based on the queue size, to avoid memory problems. // As long as the queues are relatively small, this function will just return 0. diff --git a/src/AV/Output/OutputSettings.h b/src/AV/Output/OutputSettings.h index 8343372..4064f97 100644 --- a/src/AV/Output/OutputSettings.h +++ b/src/AV/Output/OutputSettings.h @@ -20,6 +20,8 @@ along with SimpleScreenRecorder. If not, see . #pragma once #include "Global.h" +#define SSR_MAX_AUDIO_STREAMS 8 + struct OutputSettings { QString file; @@ -32,6 +34,8 @@ struct OutputSettings { unsigned int video_frame_rate; bool video_allow_frame_skipping; + // One encoder per source; empty preserves the original single-track behavior. + QStringList audio_stream_names; QString audio_codec_avname; unsigned int audio_kbit_rate; std::vector > audio_options; diff --git a/src/AV/Output/Synchronizer.cpp b/src/AV/Output/Synchronizer.cpp index b109dc6..2eb35fd 100644 --- a/src/AV/Output/Synchronizer.cpp +++ b/src/AV/Output/Synchronizer.cpp @@ -198,6 +198,8 @@ Synchronizer::Synchronizer(OutputManager *output_manager) { m_output_manager = output_manager; m_output_settings = m_output_manager->GetOutputSettings(); m_output_format = m_output_manager->GetOutputFormat(); + m_audio_streams = m_output_format->m_audio_enabled? std::max(1, m_output_settings->audio_stream_names.size()) : 0; + assert(m_audio_streams <= SSR_MAX_AUDIO_STREAMS); assert(m_output_format->m_video_enabled || m_output_format->m_audio_enabled); try { @@ -214,6 +216,7 @@ Synchronizer::~Synchronizer() { // disconnect ConnectVideoSource(NULL); ConnectAudioSource(NULL); + for(auto& sink : m_track_sinks) sink->ConnectAudioSource(NULL); // tell the thread to stop if(m_thread.joinable()) { @@ -244,11 +247,12 @@ void Synchronizer::Init() { } // initialize audio - if(m_output_format->m_audio_enabled) { - AudioLock audiolock(&m_audio_data); + for(unsigned int track = 0; track < m_audio_streams; ++track) { + AudioLock audiolock(&m_audio_data[track]); audiolock->m_fast_resampler.reset(new FastResampler(m_output_format->m_audio_channels, 0.9f)); InitAudioSegment(audiolock.get()); audiolock->m_warn_desync = true; + m_track_sinks.emplace_back(new TrackSink(this, track)); } // create sync diagram @@ -265,12 +269,13 @@ void Synchronizer::Init() { { SharedLock lock(&m_shared_data); - if(m_output_format->m_audio_enabled) { - lock->m_partial_audio_frame.Alloc(m_output_format->m_audio_frame_size * m_output_format->m_audio_channels); - lock->m_partial_audio_frame_samples = 0; + for(unsigned int track = 0; track < m_audio_streams; ++track) { + auto& audio = lock->m_audio[track]; + audio.m_partial_audio_frame.Alloc(m_output_format->m_audio_frame_size * m_output_format->m_audio_channels); + audio.m_partial_audio_frame_samples = 0; + audio.m_audio_samples = 0; } lock->m_video_pts = 0; - lock->m_audio_samples = 0; lock->m_time_offset = 0; InitSegment(lock.get()); @@ -292,8 +297,8 @@ void Synchronizer::Free() { void Synchronizer::NewSegment() { - if(m_output_format->m_audio_enabled) { - AudioLock audiolock(&m_audio_data); + for(unsigned int track = 0; track < m_audio_streams; ++track) { + AudioLock audiolock(&m_audio_data[track]); InitAudioSegment(audiolock.get()); } @@ -349,7 +354,7 @@ void Synchronizer::ReadVideoFrame(unsigned int width, unsigned int height, const // avoid memory problems by limiting the video buffer size if(lock->m_video_buffer.size() >= MAX_VIDEO_FRAMES_BUFFERED) { - if(lock->m_segment_audio_started) { + if(AudioStarted(lock.get())) { if(lock->m_warn_drop_video) { lock->m_warn_drop_video = false; Logger::LogWarning("[Synchronizer::ReadVideoFrame] " + Logger::tr("Warning: Video buffer overflow, some frames will be lost. The audio input seems to be too slow.")); @@ -393,7 +398,19 @@ void Synchronizer::ReadVideoPing(int64_t timestamp) { } -void Synchronizer::ReadAudioSamples(unsigned int channels, unsigned int sample_rate, AVSampleFormat format, unsigned int sample_count, const uint8_t* data, int64_t timestamp) { +void Synchronizer::ConnectAudioTrack(unsigned int track, AudioSource* source, int priority) { + assert(track < m_track_sinks.size()); + m_track_sinks[track]->ConnectAudioSource(source, priority); +} + +void Synchronizer::ReadAudioSamples(unsigned int channels, unsigned int rate, AVSampleFormat format, + unsigned int count, const uint8_t* data, int64_t timestamp) { + ReadAudioSamples(0, channels, rate, format, count, data, timestamp); +} + +void Synchronizer::ReadAudioHole() { ReadAudioHole(0); } + +void Synchronizer::ReadAudioSamples(unsigned int track, unsigned int channels, unsigned int sample_rate, AVSampleFormat format, unsigned int sample_count, const uint8_t* data, int64_t timestamp) { assert(m_output_format->m_audio_enabled); // sanity check @@ -404,7 +421,7 @@ void Synchronizer::ReadAudioSamples(unsigned int channels, unsigned int sample_r if(m_sync_diagram != NULL) m_sync_diagram->AddBlock(1, (double) timestamp * 1.0e-6, (double) timestamp * 1.0e-6 + (double) sample_count / (double) sample_rate, QColor(0, 255, 0)); - AudioLock audiolock(&m_audio_data); + AudioLock audiolock(&m_audio_data[track]); // check the timestamp if(timestamp < audiolock->m_last_timestamp) { @@ -547,9 +564,10 @@ void Synchronizer::ReadAudioSamples(unsigned int channels, unsigned int sample_r audiolock->m_samples_written += sample_count_out; SharedLock lock(&m_shared_data); + AudioSharedData* audio = &lock->m_audio[track]; // avoid memory problems by limiting the audio buffer size - if(lock->m_audio_buffer.GetSize() / m_output_format->m_audio_channels >= MAX_AUDIO_SAMPLES_BUFFERED) { + if(audio->m_audio_buffer.GetSize() / m_output_format->m_audio_channels >= MAX_AUDIO_SAMPLES_BUFFERED) { if(lock->m_segment_video_started) { Logger::LogWarning("[Synchronizer::ReadAudioSamples] " + Logger::tr("Warning: Audio buffer overflow, starting new segment to keep the audio in sync with the video " "(some video and/or audio may be lost). The video input seems to be too slow.")); @@ -558,32 +576,32 @@ void Synchronizer::ReadAudioSamples(unsigned int channels, unsigned int sample_r // If the video hasn't started yet, it makes more sense to drop the oldest samples. // Shifting the start time like this isn't completely accurate, but this shouldn't happen often anyway. // The number of samples dropped is calculated so that the buffer will be 90% full after this. - size_t n = lock->m_audio_buffer.GetSize() / m_output_format->m_audio_channels - MAX_AUDIO_SAMPLES_BUFFERED * 9 / 10; - lock->m_audio_buffer.Pop(n * m_output_format->m_audio_channels); - lock->m_segment_audio_start_time += (int64_t) round((double) n / (double) m_output_format->m_audio_sample_rate * 1.0e6); + size_t n = audio->m_audio_buffer.GetSize() / m_output_format->m_audio_channels - MAX_AUDIO_SAMPLES_BUFFERED * 9 / 10; + audio->m_audio_buffer.Pop(n * m_output_format->m_audio_channels); + audio->m_segment_audio_start_time += (int64_t) round((double) n / (double) m_output_format->m_audio_sample_rate * 1.0e6); } } // start audio - if(!lock->m_segment_audio_started) { - lock->m_segment_audio_started = true; - lock->m_segment_audio_start_time = timestamp; - lock->m_segment_audio_stop_time = timestamp; + if(!audio->m_segment_audio_started) { + audio->m_segment_audio_started = true; + audio->m_segment_audio_start_time = timestamp; + audio->m_segment_audio_stop_time = timestamp; } // store the samples - lock->m_audio_buffer.Push(audiolock->m_temp_output_buffer.GetData(), sample_count_out * m_output_format->m_audio_channels); + audio->m_audio_buffer.Push(audiolock->m_temp_output_buffer.GetData(), sample_count_out * m_output_format->m_audio_channels); // increase segment stop time - double new_sample_length = (double) (lock->m_segment_audio_samples_read + lock->m_audio_buffer.GetSize() / m_output_format->m_audio_channels) / (double) m_output_format->m_audio_sample_rate; - lock->m_segment_audio_stop_time = lock->m_segment_audio_start_time + (int64_t) round(new_sample_length * 1.0e6); + double new_sample_length = (double) (audio->m_segment_audio_samples_read + audio->m_audio_buffer.GetSize() / m_output_format->m_audio_channels) / (double) m_output_format->m_audio_sample_rate; + audio->m_segment_audio_stop_time = audio->m_segment_audio_start_time + (int64_t) round(new_sample_length * 1.0e6); } -void Synchronizer::ReadAudioHole() { +void Synchronizer::ReadAudioHole(unsigned int track) { assert(m_output_format->m_audio_enabled); - AudioLock audiolock(&m_audio_data); + AudioLock audiolock(&m_audio_data[track]); if(audiolock->m_first_timestamp != (int64_t) AV_NOPTS_VALUE) { audiolock->m_average_drift = 0.0; if(!audiolock->m_drop_samples || !audiolock->m_insert_samples) { @@ -610,68 +628,69 @@ double Synchronizer::GetAudioDrift(AudioData* audiolock, unsigned int extra_samp return sample_length - time_length; } +bool Synchronizer::AudioStarted(SharedData* lock) { + for(unsigned int track = 0; track < m_audio_streams; ++track) + if(!lock->m_audio[track].m_segment_audio_started) return false; + return true; +} + void Synchronizer::NewSegment(SharedData* lock) { FlushBuffers(lock); - if(lock->m_segment_video_started && lock->m_segment_audio_started) { - int64_t segment_start_time, segment_stop_time; - GetSegmentStartStop(lock, &segment_start_time, &segment_stop_time); - lock->m_time_offset += std::max((int64_t) 0, segment_stop_time - segment_start_time); + if(lock->m_segment_video_started && AudioStarted(lock)) { + int64_t start, stop; + GetSegmentStartStop(lock, &start, &stop); + lock->m_time_offset += std::max((int64_t) 0, stop - start); } lock->m_video_buffer.clear(); - lock->m_audio_buffer.Clear(); + for(unsigned int track = 0; track < m_audio_streams; ++track) + lock->m_audio[track].m_audio_buffer.Clear(); InitSegment(lock); } void Synchronizer::InitSegment(SharedData* lock) { lock->m_segment_video_started = !m_output_format->m_video_enabled; - lock->m_segment_audio_started = !m_output_format->m_audio_enabled; lock->m_segment_video_start_time = AV_NOPTS_VALUE; - lock->m_segment_audio_start_time = AV_NOPTS_VALUE; lock->m_segment_video_stop_time = AV_NOPTS_VALUE; - lock->m_segment_audio_stop_time = AV_NOPTS_VALUE; - lock->m_segment_audio_can_drop = true; - lock->m_segment_audio_samples_read = 0; lock->m_segment_video_accumulated_delay = 0; + for(unsigned int track = 0; track < m_audio_streams; ++track) { + auto& audio = lock->m_audio[track]; + audio.m_segment_audio_started = false; + audio.m_segment_audio_start_time = AV_NOPTS_VALUE; + audio.m_segment_audio_stop_time = AV_NOPTS_VALUE; + audio.m_segment_audio_can_drop = true; + audio.m_segment_audio_samples_read = 0; + } } -int64_t Synchronizer::GetTotalTime(Synchronizer::SharedData* lock) { - if(lock->m_segment_video_started && lock->m_segment_audio_started) { - int64_t segment_start_time, segment_stop_time; - GetSegmentStartStop(lock, &segment_start_time, &segment_stop_time); - return lock->m_time_offset + std::max((int64_t) 0, segment_stop_time - segment_start_time); - } else { - return lock->m_time_offset; - } +int64_t Synchronizer::GetTotalTime(SharedData* lock) { + if(!lock->m_segment_video_started || !AudioStarted(lock)) return lock->m_time_offset; + int64_t start, stop; + GetSegmentStartStop(lock, &start, &stop); + return lock->m_time_offset + std::max((int64_t) 0, stop - start); } -void Synchronizer::GetSegmentStartStop(SharedData* lock, int64_t* segment_start_time, int64_t* segment_stop_time) { - if(!m_output_format->m_audio_enabled) { - *segment_start_time = lock->m_segment_video_start_time; - *segment_stop_time = lock->m_segment_video_stop_time; - } else if(!m_output_format->m_video_enabled) { - *segment_start_time = lock->m_segment_audio_start_time; - *segment_stop_time = lock->m_segment_audio_stop_time; - } else { - *segment_start_time = std::max(lock->m_segment_video_start_time, lock->m_segment_audio_start_time); - *segment_stop_time = std::min(lock->m_segment_video_stop_time, lock->m_segment_audio_stop_time); +void Synchronizer::GetSegmentStartStop(SharedData* lock, int64_t* start, int64_t* stop) { + *start = std::numeric_limits::min(); + *stop = std::numeric_limits::max(); + if(m_output_format->m_video_enabled) { + *start = lock->m_segment_video_start_time; + *stop = lock->m_segment_video_stop_time; + } + // All tracks share the same segment origin and duration, including after a pause. + for(unsigned int track = 0; track < m_audio_streams; ++track) { + *start = std::max(*start, lock->m_audio[track].m_segment_audio_start_time); + *stop = std::min(*stop, lock->m_audio[track].m_segment_audio_stop_time); } } void Synchronizer::FlushBuffers(SharedData* lock) { - if(!lock->m_segment_video_started || !lock->m_segment_audio_started) - return; - - int64_t segment_start_time, segment_stop_time; - GetSegmentStartStop(lock, &segment_start_time, &segment_stop_time); - - // flush video - if(m_output_format->m_video_enabled) - FlushVideoBuffer(lock, segment_start_time, segment_stop_time); - - // flush audio - if(m_output_format->m_audio_enabled) - FlushAudioBuffer(lock, segment_start_time, segment_stop_time); - + if(!lock->m_segment_video_started || !AudioStarted(lock)) return; + int64_t start, stop; + GetSegmentStartStop(lock, &start, &stop); + if(stop <= start) return; + if(m_output_format->m_video_enabled) FlushVideoBuffer(lock, start, stop); + for(unsigned int track = 0; track < m_audio_streams; ++track) + FlushAudioBuffer(lock, track, start, stop); } void Synchronizer::FlushVideoBuffer(Synchronizer::SharedData* lock, int64_t segment_start_time, int64_t segment_stop_time) { @@ -769,61 +788,66 @@ void Synchronizer::FlushVideoBuffer(Synchronizer::SharedData* lock, int64_t segm } -void Synchronizer::FlushAudioBuffer(Synchronizer::SharedData* lock, int64_t segment_start_time, int64_t segment_stop_time) { +void Synchronizer::FlushAudioBuffer(Synchronizer::SharedData* lock, unsigned int track, int64_t segment_start_time, int64_t segment_stop_time) { + AudioSharedData* audio = &lock->m_audio[track]; - double sample_length = (double) (segment_stop_time - lock->m_segment_audio_start_time) * 1.0e-6; - int64_t samples_max = (int64_t) ceil(sample_length * (double) m_output_format->m_audio_sample_rate) - lock->m_segment_audio_samples_read; - if(lock->m_audio_buffer.GetSize() > 0) { + double sample_length = (double) (segment_stop_time - audio->m_segment_audio_start_time) * 1.0e-6; + int64_t samples_max = (int64_t) ceil(sample_length * (double) m_output_format->m_audio_sample_rate) - audio->m_segment_audio_samples_read; + if(audio->m_audio_buffer.GetSize() > 0) { + AudioSharedData* audio = &lock->m_audio[track]; // Normally, the correct way to calculate the position of the first sample would be: - // int64_t timestamp = lock->m_segment_audio_start_time + (int64_t) round((double) lock->m_segment_audio_samples_read / (double) m_audio_sample_rate * 1.0e6); + // int64_t timestamp = audio->m_segment_audio_start_time + (int64_t) round((double) audio->m_segment_audio_samples_read / (double) m_audio_sample_rate * 1.0e6); // int64_t pos = (int64_t) round((double) (lock->m_time_offset + (timestamp - segment_start_time)) * 1.0e-6 * (double) m_audio_sample_rate); // Simplified: - // int64_t pos = (int64_t) round((double) (lock->m_time_offset + (lock->m_segment_audio_start_time - segment_start_time)) * 1.0e-6 * (double) m_audio_sample_rate) - // + lock->m_segment_audio_samples_read; + // int64_t pos = (int64_t) round((double) (lock->m_time_offset + (audio->m_segment_audio_start_time - segment_start_time)) * 1.0e-6 * (double) m_audio_sample_rate) + // + audio->m_segment_audio_samples_read; // The first part of the expression is constant, so it only has to be calculated at the start of the segment. After that the increase in position is always // equal to the number of samples written. Samples are only dropped at the start of the segment, so actually - // the position doesn't have to be calculated anymore after that, since it is assumed to be equal to lock->m_audio_samples. + // the position doesn't have to be calculated anymore after that, since it is assumed to be equal to audio->m_audio_samples. - if(lock->m_segment_audio_can_drop) { + if(audio->m_segment_audio_can_drop) { + AudioSharedData* audio = &lock->m_audio[track]; // calculate the offset of the first sample - int64_t pos = (int64_t) round((double) (lock->m_time_offset + (lock->m_segment_audio_start_time - segment_start_time)) * 1.0e-6 * (double) m_output_format->m_audio_sample_rate) - + lock->m_segment_audio_samples_read; + int64_t pos = (int64_t) round((double) (lock->m_time_offset + (audio->m_segment_audio_start_time - segment_start_time)) * 1.0e-6 * (double) m_output_format->m_audio_sample_rate) + + audio->m_segment_audio_samples_read; // drop samples that are too early - if(pos < lock->m_audio_samples) { - int64_t n = std::min(lock->m_audio_samples - pos, (int64_t) lock->m_audio_buffer.GetSize() / m_output_format->m_audio_channels); - lock->m_audio_buffer.Pop(n * m_output_format->m_audio_channels); - lock->m_segment_audio_samples_read += n; + if(pos < audio->m_audio_samples) { + int64_t n = std::min(audio->m_audio_samples - pos, (int64_t) audio->m_audio_buffer.GetSize() / m_output_format->m_audio_channels); + audio->m_audio_buffer.Pop(n * m_output_format->m_audio_channels); + audio->m_segment_audio_samples_read += n; } } - int64_t samples_left = std::min(samples_max, (int64_t) lock->m_audio_buffer.GetSize() / m_output_format->m_audio_channels); + int64_t samples_left = std::min(samples_max, (int64_t) audio->m_audio_buffer.GetSize() / m_output_format->m_audio_channels); // add new block to sync diagram if(m_sync_diagram != NULL && samples_left > 0) { - double t = (double) lock->m_audio_samples / (double) m_output_format->m_audio_sample_rate; + double t = (double) audio->m_audio_samples / (double) m_output_format->m_audio_sample_rate; m_sync_diagram->AddBlock(3, t, t + (double) samples_left / (double) m_output_format->m_audio_sample_rate, QColor(0, 255, 0)); } // send the samples to the encoder while(samples_left > 0) { + AudioSharedData* audio = &lock->m_audio[track]; - lock->m_segment_audio_can_drop = false; + audio->m_segment_audio_can_drop = false; // copy samples until either the partial frame is full or there are no samples left //TODO// do direct copy/conversion to new audio frame? - int64_t n = std::min((int64_t) (m_output_format->m_audio_frame_size - lock->m_partial_audio_frame_samples), samples_left); - lock->m_audio_buffer.Pop(lock->m_partial_audio_frame.GetData() + lock->m_partial_audio_frame_samples * m_output_format->m_audio_channels, n * m_output_format->m_audio_channels); - lock->m_segment_audio_samples_read += n; - lock->m_partial_audio_frame_samples += n; - lock->m_audio_samples += n; + int64_t n = std::min((int64_t) (m_output_format->m_audio_frame_size - audio->m_partial_audio_frame_samples), samples_left); + audio->m_audio_buffer.Pop(audio->m_partial_audio_frame.GetData() + audio->m_partial_audio_frame_samples * m_output_format->m_audio_channels, n * m_output_format->m_audio_channels); + audio->m_segment_audio_samples_read += n; + audio->m_partial_audio_frame_samples += n; + audio->m_audio_samples += n; samples_left -= n; // is the partial frame full? - if(lock->m_partial_audio_frame_samples == m_output_format->m_audio_frame_size) { + if(audio->m_partial_audio_frame_samples == m_output_format->m_audio_frame_size) { + AudioSharedData* audio = &lock->m_audio[track]; // allocate a frame #if SSR_USE_AVUTIL_PLANAR_SAMPLE_FMT @@ -834,18 +858,18 @@ void Synchronizer::FlushAudioBuffer(Synchronizer::SharedData* lock, int64_t segm #endif std::unique_ptr audio_frame = CreateAudioFrame(m_output_format->m_audio_channels, m_output_format->m_audio_sample_rate, m_output_format->m_audio_frame_size, planes, m_output_format->m_audio_sample_format); - audio_frame->GetFrame()->pts = lock->m_audio_samples; + audio_frame->GetFrame()->pts = audio->m_audio_samples; // copy/convert the samples switch(m_output_format->m_audio_sample_format) { case AV_SAMPLE_FMT_S16: { - float *data_in = (float*) lock->m_partial_audio_frame.GetData(); + float *data_in = (float*) audio->m_partial_audio_frame.GetData(); int16_t *data_out = (int16_t*) audio_frame->GetFrame()->data[0]; SampleCopy(m_output_format->m_audio_frame_size * m_output_format->m_audio_channels, data_in, 1, data_out, 1); break; } case AV_SAMPLE_FMT_FLT: { - float *data_in = (float*) lock->m_partial_audio_frame.GetData(); + float *data_in = (float*) audio->m_partial_audio_frame.GetData(); float *data_out = (float*) audio_frame->GetFrame()->data[0]; memcpy(data_out, data_in, m_output_format->m_audio_frame_size * m_output_format->m_audio_channels * sizeof(float)); break; @@ -853,7 +877,7 @@ void Synchronizer::FlushAudioBuffer(Synchronizer::SharedData* lock, int64_t segm #if SSR_USE_AVUTIL_PLANAR_SAMPLE_FMT case AV_SAMPLE_FMT_S16P: { for(unsigned int p = 0; p < planes; ++p) { - float *data_in = (float*) lock->m_partial_audio_frame.GetData() + p; + float *data_in = (float*) audio->m_partial_audio_frame.GetData() + p; int16_t *data_out = (int16_t*) audio_frame->GetFrame()->data[p]; SampleCopy(m_output_format->m_audio_frame_size, data_in, planes, data_out, 1); } @@ -861,7 +885,7 @@ void Synchronizer::FlushAudioBuffer(Synchronizer::SharedData* lock, int64_t segm } case AV_SAMPLE_FMT_FLTP: { for(unsigned int p = 0; p < planes; ++p) { - float *data_in = (float*) lock->m_partial_audio_frame.GetData() + p; + float *data_in = (float*) audio->m_partial_audio_frame.GetData() + p; float *data_out = (float*) audio_frame->GetFrame()->data[p]; SampleCopy(m_output_format->m_audio_frame_size, data_in, planes, data_out, 1); } @@ -873,10 +897,10 @@ void Synchronizer::FlushAudioBuffer(Synchronizer::SharedData* lock, int64_t segm break; } } - lock->m_partial_audio_frame_samples = 0; + audio->m_partial_audio_frame_samples = 0; - //Logger::LogInfo("[Synchronizer::FlushAudioBuffer] Encoded audio frame [" + QString::number(lock->m_partial_audio_frame->pts) + "]."); - m_output_manager->AddAudioFrame(std::move(audio_frame)); + //Logger::LogInfo("[Synchronizer::FlushAudioBuffer] Encoded audio frame [" + QString::number(audio->m_partial_audio_frame->pts) + "]."); + m_output_manager->AddAudioFrame(std::move(audio_frame), track); } } diff --git a/src/AV/Output/Synchronizer.h b/src/AV/Output/Synchronizer.h index 6abab6f..3cc7079 100644 --- a/src/AV/Output/Synchronizer.h +++ b/src/AV/Output/Synchronizer.h @@ -21,6 +21,7 @@ along with SimpleScreenRecorder. If not, see . #include "Global.h" #include "SourceSink.h" +#include "OutputSettings.h" #include "MutexDataPair.h" #include "FastScaler.h" #include "FastResampler.h" @@ -60,28 +61,38 @@ private: bool m_warn_desync; }; - struct SharedData { - + struct AudioSharedData { TempBuffer m_partial_audio_frame; unsigned int m_partial_audio_frame_samples; - - std::deque > m_video_buffer; QueueBuffer m_audio_buffer; - int64_t m_video_pts, m_audio_samples; // video and audio position in the final stream (encoded frames and samples, including the partial audio frame) - int64_t m_time_offset; // the length of all previous segments combined (in microseconds) - - bool m_segment_video_started, m_segment_audio_started; // whether video and audio have started (always true if the corresponding stream is disabled) - int64_t m_segment_video_start_time, m_segment_audio_start_time; // the start time of video and audio (real-time, in microseconds) - int64_t m_segment_video_stop_time, m_segment_audio_stop_time; // the stop time of video and audio (real-time, in microseconds) - bool m_segment_audio_can_drop; // whether audio samples can still be dropped (i.e. no samples have been sent to the encoder yet) - int64_t m_segment_audio_samples_read; // the number of samples that have been read from the audio buffer (including dropped samples) - int64_t m_segment_video_accumulated_delay; // sum of all video frame delays that were applied so far - + int64_t m_audio_samples; + bool m_segment_audio_started; + int64_t m_segment_audio_start_time, m_segment_audio_stop_time; + bool m_segment_audio_can_drop; + int64_t m_segment_audio_samples_read; + }; + struct SharedData { + AudioSharedData m_audio[SSR_MAX_AUDIO_STREAMS]; + std::deque > m_video_buffer; + int64_t m_video_pts, m_time_offset; + bool m_segment_video_started; + int64_t m_segment_video_start_time, m_segment_video_stop_time; + int64_t m_segment_video_accumulated_delay; std::shared_ptr m_last_video_frame_data; - bool m_warn_drop_video; - }; + class TrackSink : public AudioSink { + Synchronizer* m_owner; + unsigned int m_track; + public: + TrackSink(Synchronizer* owner, unsigned int track) : m_owner(owner), m_track(track) {} + void ReadAudioSamples(unsigned int channels, unsigned int rate, AVSampleFormat format, + unsigned int count, const uint8_t* data, int64_t timestamp) override { + m_owner->ReadAudioSamples(m_track, channels, rate, format, count, data, timestamp); + } + void ReadAudioHole() override { m_owner->ReadAudioHole(m_track); } + }; + typedef MutexDataPair::Lock VideoLock; typedef MutexDataPair::Lock AudioLock; typedef MutexDataPair::Lock SharedLock; @@ -104,7 +115,9 @@ private: std::thread m_thread; MutexDataPair m_video_data; - MutexDataPair m_audio_data; + MutexDataPair m_audio_data[SSR_MAX_AUDIO_STREAMS]; + unsigned int m_audio_streams; + std::vector > m_track_sinks; MutexDataPair m_shared_data; std::atomic m_should_stop, m_error_occurred; @@ -125,6 +138,7 @@ public: // This function is thread-safe, but for best results you should still make sure that no input is running // while this function is called, because otherwise frames may end up in the wrong segment. void NewSegment(); + void ConnectAudioTrack(unsigned int track, AudioSource* source, int priority = 0); // Returns the total recording time (in microseconds). // This function is thread-safe. @@ -145,6 +159,9 @@ public: // internal virtual void ReadAudioHole() override; private: + void ReadAudioSamples(unsigned int track, unsigned int channels, unsigned int sample_rate, AVSampleFormat format, unsigned int sample_count, const uint8_t* data, int64_t timestamp); + void ReadAudioHole(unsigned int track); + bool AudioStarted(SharedData* lock); void InitAudioSegment(AudioData* audiolock); double GetAudioDrift(AudioData* audiolock, unsigned int extra_samples = 0); void NewSegment(SharedData* lock); @@ -153,7 +170,7 @@ private: void GetSegmentStartStop(SharedData* lock, int64_t* segment_start_time, int64_t* segment_stop_time); void FlushBuffers(SharedData* lock); void FlushVideoBuffer(SharedData* lock, int64_t segment_start_time, int64_t segment_stop_time); - void FlushAudioBuffer(SharedData* lock, int64_t segment_start_time, int64_t segment_stop_time); + void FlushAudioBuffer(SharedData* lock, unsigned int track, int64_t segment_start_time, int64_t segment_stop_time); private: void SynchronizerThread(); diff --git a/src/GUI/PageInput.cpp b/src/GUI/PageInput.cpp index 05434bd..1914b8a 100644 --- a/src/GUI/PageInput.cpp +++ b/src/GUI/PageInput.cpp @@ -50,7 +50,7 @@ ENUMSTRINGS(PageInput::enum_audio_backend) = { #endif }; -static std::vector GetScreenGeometries() { +std::vector GetScreenGeometries() { std::vector screen_geometries; #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) for(QScreen *screen : QApplication::screens()) { @@ -168,6 +168,52 @@ void QSpinBoxWithSignal::focusOutEvent(QFocusEvent* event) { #endif +std::vector GetUnrecordedScreenGeometries(const std::vector& screens, const QRect& capture) { + std::vector result; + if(capture.isEmpty()) return result; + for(const QRect& screen : screens) { + if(!screen.isEmpty() && !screen.intersects(capture) && + std::find(result.begin(), result.end(), screen) == result.end()) + result.push_back(screen); + } + return result; +} + +RecordingIndicatorWindow::RecordingIndicatorWindow(QWidget* parent, const QRect& physical_geometry) + : QWidget(parent, TRANSPARENT_WINDOW_FLAGS) { + setObjectName("unrecordedScreenIndicator"); + setAttribute(Qt::WA_ShowWithoutActivating); + setAttribute(Qt::WA_TransparentForMouseEvents); + TRANSPARENT_WINDOW_ATTRIBUTES(); + setGeometry(MapToLogicalCoordinates(physical_geometry)); + // A shaped, opaque ring works even when the X11 compositor is disabled. + setMask(QRegion(rect()).subtracted(QRegion(rect().adjusted(BORDER_WIDTH, BORDER_WIDTH, -BORDER_WIDTH, -BORDER_WIDTH)))); + m_pulse_clock.start(); + startTimer(40); + show(); + raise(); +} + +void RecordingIndicatorWindow::timerEvent(QTimerEvent* event) { + Q_UNUSED(event); + update(); + // Keep the indicator above fullscreen applications without taking focus. + raise(); +} + +void RecordingIndicatorWindow::paintEvent(QPaintEvent* event) { + Q_UNUSED(event); + const double phase = (m_pulse_clock.elapsed() % 1500) * (2.0 * M_PI / 1500.0); + const int red = 120 + (int) round(135.0 * (0.5 + 0.5 * cos(phase))); + QPainter painter(this); + painter.fillRect(rect(), QColor(red, 0, 0)); +} + +void RecordingIndicatorWindow::resizeEvent(QResizeEvent* event) { + Q_UNUSED(event); + setMask(QRegion(rect()).subtracted(QRegion(rect().adjusted(BORDER_WIDTH, BORDER_WIDTH, -BORDER_WIDTH, -BORDER_WIDTH)))); +} + ScreenLabelWindow::ScreenLabelWindow(QWidget* parent, const QString &text) : QWidget(parent, TRANSPARENT_WINDOW_FLAGS) { TRANSPARENT_WINDOW_ATTRIBUTES(); @@ -291,6 +337,9 @@ PageInput::PageInput(MainWindow* main_window) #if SSR_USE_V4L2 m_buttongroup_video_area->addButton(radio_area_v4l2, VIDEO_AREA_V4L2); #endif + m_checkbox_unrecorded_screen_indicator = new QCheckBox(tr("Show a pulsing red border on screens that are not being recorded"), groupbox_video); + m_checkbox_unrecorded_screen_indicator->setObjectName("unrecordedScreenIndicatorOption"); + m_checkbox_unrecorded_screen_indicator->setToolTip(tr("While recording a selected screen, show a red border on other screens.\nThe border disappears while paused or stopped and never appears on a screen intersecting the capture area.")); m_combobox_screens = new QComboBoxWithSignal(groupbox_video); m_combobox_screens->setToolTip(tr("Select what monitor should be recorded in a multi-monitor configuration.")); m_checkbox_follow_fullscreen = new QCheckBox(tr("Record entire screen with cursor"), groupbox_video); @@ -381,6 +430,12 @@ PageInput::PageInput(MainWindow* main_window) layout2->addWidget(radio_area_screen); layout2->addWidget(m_combobox_screens); } + { + QHBoxLayout *layout2 = new QHBoxLayout(); + layout2->addSpacing(20); + layout2->addWidget(m_checkbox_unrecorded_screen_indicator); + layout->addLayout(layout2); + } layout->addWidget(radio_area_fixed); { QHBoxLayout *layout2 = new QHBoxLayout(); @@ -468,11 +523,11 @@ PageInput::PageInput(MainWindow* main_window) m_pushbutton_alsa_refresh->setToolTip(tr("Refreshes the list of ALSA sources.")); #endif #if SSR_USE_PULSEAUDIO - m_label_pulseaudio_source = new QLabel(tr("Source:"), groupbox_audio); - m_combobox_pulseaudio_source = new QComboBox(groupbox_audio); - m_combobox_pulseaudio_source->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); - m_combobox_pulseaudio_source->setToolTip(tr("The PulseAudio source that will be used for recording.\n" - "A 'monitor' is a source that records the audio played by other applications.", "Don't translate 'monitor' unless PulseAudio does this as well")); + m_label_pulseaudio_sources = new QLabel(tr("Sources (separate audio tracks):"), groupbox_audio); + m_list_pulseaudio_sources = new QListWidget(groupbox_audio); + m_list_pulseaudio_sources->setObjectName("pulseAudioSources"); + m_list_pulseaudio_sources->setMaximumHeight(150); + m_list_pulseaudio_sources->setToolTip(tr("Check every source to record. Each checked source becomes a separate audio track.\nSelect one to eight sources. The first checked source supplies the preview meter and default playback track.")); m_pushbutton_pulseaudio_refresh = new QPushButton(tr("Refresh"), groupbox_audio); m_pushbutton_pulseaudio_refresh->setToolTip(tr("Refreshes the list of PulseAudio sources.")); #endif @@ -505,9 +560,9 @@ PageInput::PageInput(MainWindow* main_window) layout2->addWidget(m_pushbutton_alsa_refresh, 1, 2); #endif #if SSR_USE_PULSEAUDIO - layout2->addWidget(m_label_pulseaudio_source, 2, 0); - layout2->addWidget(m_combobox_pulseaudio_source, 2, 1); + layout2->addWidget(m_label_pulseaudio_sources, 2, 0, 1, 2); layout2->addWidget(m_pushbutton_pulseaudio_refresh, 2, 2); + layout2->addWidget(m_list_pulseaudio_sources, 3, 0, 1, 3); #endif } #if SSR_USE_JACK @@ -629,6 +684,7 @@ void PageInput::LoadProfileSettings(QSettings* settings) { // load settings SetVideoArea(StringToEnum(settings->value("input/video_area", QString()).toString(), VIDEO_AREA_SCREEN)); SetVideoAreaScreen(settings->value("input/video_area_screen", 0).toUInt()); + SetUnrecordedScreenIndicator(settings->value("input/unrecorded_screen_indicator", false).toBool()); SetVideoAreaFollowFullscreen(settings->value("input/video_area_follow_fullscreen", false).toBool()); #if SSR_USE_V4L2 SetVideoV4L2Device(settings->value("input/video_v4l2_device", "/dev/video0").toString()); @@ -648,7 +704,18 @@ void PageInput::LoadProfileSettings(QSettings* settings) { SetALSASource(FindALSASource(settings->value("input/audio_alsa_source", QString()).toString())); #endif #if SSR_USE_PULSEAUDIO - SetPulseAudioSource(FindPulseAudioSource(settings->value("input/audio_pulseaudio_source", QString()).toString())); + QStringList sources; + if(settings->contains("input/audio_pulseaudio_sources")) { + sources = settings->value("input/audio_pulseaudio_sources").toStringList(); + } else { + // Migrate both stock SSR's source and the previous patch's additional sources. + QString primary = settings->value("input/audio_pulseaudio_source").toString(); + if(primary.isEmpty() && m_pulseaudio_available) + primary = QString::fromStdString(m_pulseaudio_sources.front().m_name); + if(!primary.isEmpty()) sources.append(primary); + sources.append(settings->value("input/audio_pulseaudio_extra_sources").toStringList()); + } + SetPulseAudioSourceNames(sources); #endif #if SSR_USE_JACK SetJackConnectSystemCapture(settings->value("input/audio_jack_connect_system_capture", true).toBool()); @@ -674,6 +741,7 @@ void PageInput::LoadProfileSettings(QSettings* settings) { void PageInput::SaveProfileSettings(QSettings* settings) { settings->setValue("input/video_area", EnumToString(GetVideoArea())); settings->setValue("input/video_area_screen", GetVideoAreaScreen()); + settings->setValue("input/unrecorded_screen_indicator", GetUnrecordedScreenIndicator()); settings->setValue("input/video_area_follow_fullscreen", GetVideoAreaFollowFullscreen()); #if SSR_USE_V4L2 settings->setValue("input/video_v4l2_device", GetVideoV4L2Device()); @@ -693,7 +761,7 @@ void PageInput::SaveProfileSettings(QSettings* settings) { settings->setValue("input/audio_alsa_source", GetALSASourceName()); #endif #if SSR_USE_PULSEAUDIO - settings->setValue("input/audio_pulseaudio_source", GetPulseAudioSourceName()); + settings->setValue("input/audio_pulseaudio_sources", GetPulseAudioSourceNames()); #endif #if SSR_USE_JACK settings->setValue("input/audio_jack_connect_system_capture", GetJackConnectSystemCapture()); @@ -710,6 +778,16 @@ void PageInput::SaveProfileSettings(QSettings* settings) { } bool PageInput::Validate() { +#if SSR_USE_PULSEAUDIO + if(GetAudioEnabled() && GetAudioBackend() == AUDIO_BACKEND_PULSEAUDIO) { + int count = GetPulseAudioSourceNames().size(); + if(count == 0 || count > 8) { + QMessageBox::warning(this, tr("Select audio sources"), tr("Check between one and eight audio sources, or turn off Record audio.")); + return false; + } + } +#endif + if(m_grabbing) return false; return true; @@ -722,9 +800,23 @@ QString PageInput::GetALSASourceName() { #endif #if SSR_USE_PULSEAUDIO -QString PageInput::GetPulseAudioSourceName() { - return QString::fromStdString(m_pulseaudio_sources[GetPulseAudioSource()].m_name); +QStringList PageInput::GetPulseAudioSourceNames() { + QStringList names; + for(int i = 0; i < m_list_pulseaudio_sources->count(); ++i) { + auto item = m_list_pulseaudio_sources->item(i); + if(item->checkState() == Qt::Checked && (item->flags() & Qt::ItemIsEnabled)) + names.append(item->data(Qt::UserRole).toString()); + } + return names; +} + +void PageInput::SetPulseAudioSourceNames(const QStringList& names) { + for(int i = 0; i < m_list_pulseaudio_sources->count(); ++i) { + auto item = m_list_pulseaudio_sources->item(i); + item->setCheckState((item->flags() & Qt::ItemIsEnabled) && names.contains(item->data(Qt::UserRole).toString())? Qt::Checked : Qt::Unchecked); + } } + #endif #if SSR_USE_ALSA @@ -737,15 +829,6 @@ unsigned int PageInput::FindALSASource(const QString& name) { } #endif -#if SSR_USE_PULSEAUDIO -unsigned int PageInput::FindPulseAudioSource(const QString& name) { - for(unsigned int i = 0; i < m_pulseaudio_sources.size(); ++i) { - if(QString::fromStdString(m_pulseaudio_sources[i].m_name) == name) - return i; - } - return 0; -} -#endif // Tries to find the real window that corresponds to a top-level window (the actual window without window manager decorations). // Returns None if it can't find the window (probably because the window is not handled by the window manager). @@ -989,18 +1072,26 @@ void PageInput::LoadALSASources() { #if SSR_USE_PULSEAUDIO void PageInput::LoadPulseAudioSources() { + QStringList previous = GetPulseAudioSourceNames(); + bool first_load = (m_list_pulseaudio_sources->count() == 0); m_pulseaudio_sources = PulseAudioInput::GetSourceList(); - if(m_pulseaudio_sources.empty()) { - m_pulseaudio_available = false; - m_pulseaudio_sources.push_back(PulseAudioInput::Source("", "(no sources found)")); + m_pulseaudio_available = !m_pulseaudio_sources.empty(); + m_list_pulseaudio_sources->clear(); + if(!m_pulseaudio_available) { + auto item = new QListWidgetItem(tr("No PulseAudio sources found"), m_list_pulseaudio_sources); + item->setFlags(Qt::NoItemFlags); } else { - m_pulseaudio_available = true; - } - m_combobox_pulseaudio_source->clear(); - for(unsigned int i = 0; i < m_pulseaudio_sources.size(); ++i) { - QString elided = m_combobox_pulseaudio_source->fontMetrics().elidedText(QString::fromStdString(m_pulseaudio_sources[i].m_description), Qt::ElideMiddle, 400); - m_combobox_pulseaudio_source->addItem("\u200e" + elided + "\u200e"); + for(const auto& source : m_pulseaudio_sources) { + auto item = new QListWidgetItem(QString::fromStdString(source.m_description), m_list_pulseaudio_sources); + QString name = QString::fromStdString(source.m_name); + item->setData(Qt::UserRole, name); + item->setToolTip(name); + item->setFlags(item->flags() | Qt::ItemIsUserCheckable); + item->setCheckState(Qt::Unchecked); + } + if(first_load) previous.append(QString::fromStdString(m_pulseaudio_sources.front().m_name)); } + SetPulseAudioSourceNames(previous); } #endif @@ -1015,6 +1106,7 @@ void PageInput::OnUpdateRecordingFrame() { } void PageInput::OnUpdateVideoAreaFields() { + m_checkbox_unrecorded_screen_indicator->setEnabled(GetVideoArea() == VIDEO_AREA_SCREEN); switch(GetVideoArea()) { case VIDEO_AREA_SCREEN: { m_combobox_screens->setEnabled(true); @@ -1141,7 +1233,7 @@ void PageInput::OnUpdateAudioFields() { m_label_alsa_source, m_combobox_alsa_source, m_pushbutton_alsa_refresh, #endif #if SSR_USE_PULSEAUDIO - m_label_pulseaudio_source, m_combobox_pulseaudio_source, m_pushbutton_pulseaudio_refresh, + m_label_pulseaudio_sources, m_list_pulseaudio_sources, m_pushbutton_pulseaudio_refresh, #endif #if SSR_USE_JACK m_checkbox_jack_connect_system_capture, m_checkbox_jack_connect_system_playback, @@ -1152,7 +1244,7 @@ void PageInput::OnUpdateAudioFields() { {{m_label_alsa_source, m_combobox_alsa_source, m_pushbutton_alsa_refresh}, (backend == AUDIO_BACKEND_ALSA)}, #endif #if SSR_USE_PULSEAUDIO - {{m_label_pulseaudio_source, m_combobox_pulseaudio_source, m_pushbutton_pulseaudio_refresh}, (backend == AUDIO_BACKEND_PULSEAUDIO)}, + {{m_label_pulseaudio_sources, m_list_pulseaudio_sources, m_pushbutton_pulseaudio_refresh}, (backend == AUDIO_BACKEND_PULSEAUDIO)}, #endif #if SSR_USE_JACK {{m_checkbox_jack_connect_system_capture, m_checkbox_jack_connect_system_playback}, (backend == AUDIO_BACKEND_JACK)}, @@ -1192,9 +1284,7 @@ void PageInput::OnUpdateALSASources() { #if SSR_USE_PULSEAUDIO void PageInput::OnUpdatePulseAudioSources() { - QString selected_source = GetPulseAudioSourceName(); LoadPulseAudioSources(); - SetPulseAudioSource(FindPulseAudioSource(selected_source)); } #endif diff --git a/src/GUI/PageInput.h b/src/GUI/PageInput.h index 2b0a99f..2f52b2a 100644 --- a/src/GUI/PageInput.h +++ b/src/GUI/PageInput.h @@ -19,6 +19,7 @@ along with SimpleScreenRecorder. If not, see . #pragma once #include "Global.h" +#include #include "ProfileBox.h" @@ -98,6 +99,22 @@ protected: }; +// Physical screen rectangles are compared with the actual capture area. +// Mirrored or overlapping screens are omitted whenever they intersect the recording. +std::vector GetScreenGeometries(); +std::vector GetUnrecordedScreenGeometries(const std::vector& screens, const QRect& capture); + +class RecordingIndicatorWindow : public QWidget { + QElapsedTimer m_pulse_clock; +public: + static constexpr int BORDER_WIDTH = 10; + RecordingIndicatorWindow(QWidget* parent, const QRect& physical_geometry); +protected: + void timerEvent(QTimerEvent* event) override; + void paintEvent(QPaintEvent* event) override; + void resizeEvent(QResizeEvent* event) override; +}; + class MainWindow; class PageInput : public QWidget { @@ -159,6 +176,7 @@ private: QButtonGroup *m_buttongroup_video_area; QComboBoxWithSignal *m_combobox_screens; QCheckBox *m_checkbox_follow_fullscreen; + QCheckBox *m_checkbox_unrecorded_screen_indicator; QPushButton *m_pushbutton_video_select_rectangle, *m_pushbutton_video_select_window; #if SSR_USE_OPENGL_RECORDING QPushButton *m_pushbutton_video_opengl_settings; @@ -183,8 +201,8 @@ private: QPushButton *m_pushbutton_alsa_refresh; #endif #if SSR_USE_PULSEAUDIO - QLabel *m_label_pulseaudio_source; - QComboBox *m_combobox_pulseaudio_source; + QListWidget *m_list_pulseaudio_sources; + QLabel *m_label_pulseaudio_sources; QPushButton *m_pushbutton_pulseaudio_refresh; #endif #if SSR_USE_JACK @@ -210,7 +228,8 @@ public: QString GetALSASourceName(); #endif #if SSR_USE_PULSEAUDIO - QString GetPulseAudioSourceName(); + QStringList GetPulseAudioSourceNames(); + void SetPulseAudioSourceNames(const QStringList& names); #endif private: @@ -218,7 +237,6 @@ private: unsigned int FindALSASource(const QString& name); #endif #if SSR_USE_PULSEAUDIO - unsigned int FindPulseAudioSource(const QString& name); #endif protected: @@ -272,6 +290,7 @@ public: inline unsigned int GetProfile() { return m_profile_box->GetProfile(); } inline enum_video_area GetVideoArea() { return (enum_video_area) clamp(m_buttongroup_video_area->checkedId(), 0, VIDEO_AREA_COUNT - 1); } inline unsigned int GetVideoAreaScreen() { return m_combobox_screens->currentIndex(); } + inline bool GetUnrecordedScreenIndicator() { return m_checkbox_unrecorded_screen_indicator->isChecked(); } inline bool GetVideoAreaFollowFullscreen() { return m_checkbox_follow_fullscreen->isChecked(); } #if SSR_USE_V4L2 inline QString GetVideoV4L2Device() { return m_lineedit_v4l2_device->text(); } @@ -291,7 +310,6 @@ public: inline unsigned int GetALSASource() { return clamp(m_combobox_alsa_source->currentIndex(), 0, (int) m_alsa_sources.size() - 1); } #endif #if SSR_USE_PULSEAUDIO - inline unsigned int GetPulseAudioSource() { return clamp(m_combobox_pulseaudio_source->currentIndex(), 0, (int) m_pulseaudio_sources.size() - 1); } #endif #if SSR_USE_JACK inline bool GetJackConnectSystemCapture() { return m_checkbox_jack_connect_system_capture->isChecked(); } @@ -309,6 +327,7 @@ public: inline void SetProfile(unsigned int profile) { m_profile_box->SetProfile(profile); } inline void SetVideoArea(enum_video_area area) { QAbstractButton *b = m_buttongroup_video_area->button(area); if(b != NULL) b->setChecked(true); } inline void SetVideoAreaScreen(unsigned int screen) { m_combobox_screens->setCurrentIndex(clamp(screen, 0u, (unsigned int) m_combobox_screens->count() - 1)); } + inline void SetUnrecordedScreenIndicator(bool enabled) { m_checkbox_unrecorded_screen_indicator->setChecked(enabled); } inline void SetVideoAreaFollowFullscreen(bool follow_fulscreen) { m_checkbox_follow_fullscreen->setChecked(follow_fulscreen); } #if SSR_USE_V4L2 inline void SetVideoV4L2Device(const QString& device) { m_lineedit_v4l2_device->setText(device); } @@ -328,7 +347,6 @@ public: inline void SetALSASource(unsigned int source) { m_combobox_alsa_source->setCurrentIndex(clamp(source, 0u, (unsigned int) m_alsa_sources.size() - 1)); } #endif #if SSR_USE_PULSEAUDIO - inline void SetPulseAudioSource(unsigned int source) { m_combobox_pulseaudio_source->setCurrentIndex(clamp(source, 0u, (unsigned int) m_pulseaudio_sources.size() - 1)); } #endif #if SSR_USE_JACK inline void SetJackConnectSystemCapture(bool connect) { m_checkbox_jack_connect_system_capture->setChecked(connect); } diff --git a/src/GUI/PageRecord.cpp b/src/GUI/PageRecord.cpp index 2697121..6781970 100644 --- a/src/GUI/PageRecord.cpp +++ b/src/GUI/PageRecord.cpp @@ -162,6 +162,7 @@ PageRecord::PageRecord(MainWindow* main_window) m_page_started = false; m_input_started = false; m_output_started = false; + m_unrecorded_screen_indicator = false; m_previewing = false; m_schedule_active = false; @@ -418,6 +419,16 @@ PageRecord::PageRecord(MainWindow* main_window) UpdateRecordButton(); UpdateSchedule(); UpdatePreview(); +#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) + for(QScreen* screen : QApplication::screens()) + connect(screen, SIGNAL(geometryChanged(QRect)), this, SLOT(OnUpdateRecordingFrame())); + connect(qApp, &QGuiApplication::screenAdded, this, [this](QScreen* screen) { + connect(screen, SIGNAL(geometryChanged(QRect)), this, SLOT(OnUpdateRecordingFrame())); + OnUpdateRecordingFrame(); + }); + connect(qApp, &QGuiApplication::screenRemoved, this, [this](QScreen*) { OnUpdateRecordingFrame(); }); +#endif + OnUpdateRecordingFrame(); if(m_systray_icon != NULL) @@ -536,6 +547,7 @@ void PageRecord::StartPage() { // get the video input settings m_video_area = page_input->GetVideoArea(); + m_unrecorded_screen_indicator = page_input->GetUnrecordedScreenIndicator(); m_video_area_follow_fullscreen = page_input->GetVideoAreaFollowFullscreen(); #if SSR_USE_V4L2 m_v4l2_device = page_input->GetVideoV4L2Device(); @@ -570,7 +582,7 @@ void PageRecord::StartPage() { m_alsa_source = page_input->GetALSASourceName(); #endif #if SSR_USE_PULSEAUDIO - m_pulseaudio_source = page_input->GetPulseAudioSourceName(); + m_pulseaudio_sources = page_input->GetPulseAudioSourceNames(); #endif #if SSR_USE_JACK bool jack_connect_system_capture = page_input->GetJackConnectSystemCapture(); @@ -613,6 +625,11 @@ void PageRecord::StartPage() { m_output_settings.audio_codec_avname = (m_audio_enabled)? page_output->GetAudioCodecAVName() : QString(); m_output_settings.audio_kbit_rate = page_output->GetAudioKBitRate(); m_output_settings.audio_options.clear(); + m_output_settings.audio_stream_names.clear(); +#if SSR_USE_PULSEAUDIO + if(m_audio_enabled && m_audio_backend == PageInput::AUDIO_BACKEND_PULSEAUDIO) + m_output_settings.audio_stream_names = m_pulseaudio_sources; +#endif m_output_settings.audio_channels = m_audio_channels; m_output_settings.audio_sample_rate = m_audio_sample_rate; @@ -911,7 +928,7 @@ void PageRecord::StartInput() { assert(m_alsa_input == NULL); #endif #if SSR_USE_PULSEAUDIO - assert(m_pulseaudio_input == NULL); + assert(m_pulseaudio_inputs.empty()); #endif try { @@ -947,8 +964,10 @@ void PageRecord::StartInput() { m_alsa_input.reset(new ALSAInput(m_alsa_source, m_audio_sample_rate)); #endif #if SSR_USE_PULSEAUDIO - if(m_audio_backend == PageInput::AUDIO_BACKEND_PULSEAUDIO) - m_pulseaudio_input.reset(new PulseAudioInput(m_pulseaudio_source, m_audio_sample_rate)); + if(m_audio_backend == PageInput::AUDIO_BACKEND_PULSEAUDIO) { + for(const QString& source : m_pulseaudio_sources) + m_pulseaudio_inputs.emplace_back(new PulseAudioInput(source, m_audio_sample_rate)); + } #endif // JACK was started when the page was started } @@ -971,7 +990,7 @@ void PageRecord::StartInput() { m_alsa_input.reset(); #endif #if SSR_USE_PULSEAUDIO - m_pulseaudio_input.reset(); + m_pulseaudio_inputs.clear(); #endif // JACK shouldn't stop until the page stops return; @@ -999,7 +1018,7 @@ void PageRecord::StopInput() { m_alsa_input.reset(); #endif #if SSR_USE_PULSEAUDIO - m_pulseaudio_input.reset(); + m_pulseaudio_inputs.clear(); #endif // JACK shouldn't stop until the page stops @@ -1068,7 +1087,7 @@ void PageRecord::UpdateInput() { #endif #if SSR_USE_PULSEAUDIO if(m_audio_backend == PageInput::AUDIO_BACKEND_PULSEAUDIO) - audio_source = m_pulseaudio_input.get(); + audio_source = m_pulseaudio_inputs.empty()? NULL : m_pulseaudio_inputs[0].get(); #endif #if SSR_USE_JACK if(m_audio_backend == PageInput::AUDIO_BACKEND_JACK) @@ -1080,11 +1099,23 @@ void PageRecord::UpdateInput() { if(m_output_manager != NULL) { if(m_output_started) { m_output_manager->GetSynchronizer()->ConnectVideoSource(video_source, PRIORITY_RECORD); - m_output_manager->GetSynchronizer()->ConnectAudioSource(audio_source, PRIORITY_RECORD); + #if SSR_USE_PULSEAUDIO + if(m_audio_backend != PageInput::AUDIO_BACKEND_PULSEAUDIO) +#endif + m_output_manager->GetSynchronizer()->ConnectAudioSource(audio_source, PRIORITY_RECORD); } else { m_output_manager->GetSynchronizer()->ConnectVideoSource(NULL); m_output_manager->GetSynchronizer()->ConnectAudioSource(NULL); } + // Track sinks are disconnected even when input creation failed or recording is paused. +#if SSR_USE_PULSEAUDIO + if(m_audio_enabled && m_audio_backend == PageInput::AUDIO_BACKEND_PULSEAUDIO) { + m_output_manager->GetSynchronizer()->ConnectAudioSource(NULL); + for(int track = 0; track < m_pulseaudio_sources.size(); ++track) + m_output_manager->GetSynchronizer()->ConnectAudioTrack(track, + (m_output_started && track < (int) m_pulseaudio_inputs.size())? m_pulseaudio_inputs[track].get() : NULL, PRIORITY_RECORD); + } +#endif } if(m_previewing) { m_video_previewer->ConnectVideoSource(video_source, PRIORITY_PREVIEW); @@ -1206,7 +1237,24 @@ void PageRecord::OnUpdateSoundNotifications() { } #endif +void PageRecord::UpdateRecordingIndicators() { + std::vector geometries; + if(m_page_started && m_output_started && m_input_started && !m_wait_saving && !m_error_occurred && + m_unrecorded_screen_indicator && m_video_area == PageInput::VIDEO_AREA_SCREEN && m_x11_input != NULL) { + unsigned int x, y, width, height; + m_x11_input->GetCurrentRectangle(&x, &y, &width, &height); + geometries = GetUnrecordedScreenGeometries(GetScreenGeometries(), QRect(x, y, width, height)); + } + if(geometries == m_indicator_geometries) return; + // Destroy old borders before creating any new ones after a layout change. + m_recording_indicators.clear(); + m_indicator_geometries = geometries; + for(const QRect& geometry : geometries) + m_recording_indicators.emplace_back(new RecordingIndicatorWindow(this, geometry)); +} + void PageRecord::OnUpdateRecordingFrame() { + UpdateRecordingIndicators(); if(m_page_started && m_video_area == PageInput::VIDEO_AREA_FIXED && GetShowRecordingArea()) { if(m_recording_frame == NULL) m_recording_frame.reset(new RecordingFrameWindow(this, true)); @@ -1445,6 +1493,7 @@ void PageRecord::OnStdin() { } void PageRecord::OnUpdateInformation() { + UpdateRecordingIndicators(); if(m_page_started) { @@ -1558,6 +1607,7 @@ void PageRecord::OnNewLogLine(Logger::enum_type type, QString string) { // change system tray icon if an error has occurred if(m_page_started && type == Logger::TYPE_ERROR && !m_error_occurred) { m_error_occurred = true; + UpdateRecordingIndicators(); UpdateSysTray(); } diff --git a/src/GUI/PageRecord.h b/src/GUI/PageRecord.h index 9b53f05..79539f5 100644 --- a/src/GUI/PageRecord.h +++ b/src/GUI/PageRecord.h @@ -76,6 +76,7 @@ private: PageInput::enum_video_area m_video_area; bool m_video_area_follow_fullscreen; + bool m_unrecorded_screen_indicator; #if SSR_USE_V4L2 QString m_v4l2_device; #endif @@ -91,7 +92,7 @@ private: QString m_alsa_source; #endif #if SSR_USE_PULSEAUDIO - QString m_pulseaudio_source; + QStringList m_pulseaudio_sources; #endif OutputSettings m_output_settings; @@ -112,7 +113,7 @@ private: std::unique_ptr m_alsa_input; #endif #if SSR_USE_PULSEAUDIO - std::unique_ptr m_pulseaudio_input; + std::vector > m_pulseaudio_inputs; #endif #if SSR_USE_JACK std::unique_ptr m_jack_input; @@ -162,6 +163,9 @@ private: QTimer *m_timer_schedule, *m_timer_update_info; std::unique_ptr m_recording_frame; + std::vector m_indicator_geometries; + std::vector > m_recording_indicators; + void UpdateRecordingIndicators(); public: PageRecord(MainWindow* main_window);