From 392ed9c62712d980ae34de9949144d3280b18381 Mon Sep 17 00:00:00 2001 From: meirzamoodle Date: Tue, 29 Oct 2024 07:00:24 +0700 Subject: [PATCH] MDL-83570 tiny_recordrtc: Turn the audio bitrate input to Select field The current code may have a bug. If users set the audio bit rate to a non-supported value, such as 1000, Firefox will display an error in the console, while Chrome will create the recorded audio file successfully, but it will have no sound. I tested this on Ubuntu 24.04. The minimum bit rate threshold varies depending on the audio format. During my tests with Firefox version 131.0.3, which uses the audio/Ogg format, I found that the minimum supported value is 24000. In Chrome 129.0.6668.70, which uses the audio/MP4 format, the minimum supported value is 2400. Due to these differences, I decided to change the input from a text field to a select field, offering options that support both Ogg and MP4 audio formats. The database conversion for the old value has been provided in the lib/db/upgrade.php. The script will find the closest match to the current data and update it accordingly. --- .../plugins/recordrtc/classes/constants.php | 3 +++ .../tiny/plugins/recordrtc/db/upgrade.php | 18 ++++++++++++++++++ .../recordrtc/lang/en/tiny_recordrtc.php | 1 + lib/editor/tiny/plugins/recordrtc/settings.php | 14 ++++++++++++-- lib/editor/tiny/plugins/recordrtc/version.php | 2 +- 5 files changed, 35 insertions(+), 3 deletions(-) diff --git a/lib/editor/tiny/plugins/recordrtc/classes/constants.php b/lib/editor/tiny/plugins/recordrtc/classes/constants.php index 93f863488d5..14459c51de9 100644 --- a/lib/editor/tiny/plugins/recordrtc/classes/constants.php +++ b/lib/editor/tiny/plugins/recordrtc/classes/constants.php @@ -39,4 +39,7 @@ class constants { /** @var string TINYRECORDRTC_SCREEN_FHD The Full-HD screen-sharing resolution. */ public const TINYRECORDRTC_SCREEN_FHD = '1920,1080'; + + /** @var array TINYRECORDRTC_AUDIO_BITRATES The audio bitrate options. */ + public const TINYRECORDRTC_AUDIO_BITRATES = [24000, 32000, 48000, 64000, 96000, 128000, 160000, 192000, 256000, 320000]; } diff --git a/lib/editor/tiny/plugins/recordrtc/db/upgrade.php b/lib/editor/tiny/plugins/recordrtc/db/upgrade.php index d5a3076a463..5138aa1f99d 100644 --- a/lib/editor/tiny/plugins/recordrtc/db/upgrade.php +++ b/lib/editor/tiny/plugins/recordrtc/db/upgrade.php @@ -45,6 +45,24 @@ function xmldb_tiny_recordrtc_upgrade($oldversion) { upgrade_plugin_savepoint(true, 2024042400, 'tiny', 'recordrtc'); } + if ($oldversion < 2024112000) { + // The input bitrate to be converted. + $currentbitrate = get_config('tiny_recordrtc', 'audiobitrate'); + + // Supported bitrates. + $supportedbitrates = \tiny_recordrtc\constants::TINYRECORDRTC_AUDIO_BITRATES; + + // Find the nearest value. + usort($supportedbitrates, fn($a, $b) => abs($currentbitrate - $a) <=> abs($currentbitrate - $b)); + $nearestbitrate = $supportedbitrates[0]; + + // Update the bitrate setting with the nearest supported bitrate. + set_config('audiobitrate', $nearestbitrate, 'tiny_recordrtc'); + + // Main savepoint reached. + upgrade_main_savepoint(true, 2024112000); + } + // Automatically generated Moodle v4.5.0 release upgrade line. // Put any upgrade step following this. diff --git a/lib/editor/tiny/plugins/recordrtc/lang/en/tiny_recordrtc.php b/lib/editor/tiny/plugins/recordrtc/lang/en/tiny_recordrtc.php index fc82a7b502d..a07e4cc1363 100644 --- a/lib/editor/tiny/plugins/recordrtc/lang/en/tiny_recordrtc.php +++ b/lib/editor/tiny/plugins/recordrtc/lang/en/tiny_recordrtc.php @@ -54,6 +54,7 @@ $string['gumtype'] = 'Tried to get stream from the webcam/microphone/screen, but $string['gumtype_title'] = 'No constraints specified'; $string['insecurealert'] = 'Your browser might not allow this plugin to work unless it is used either over HTTPS or from localhost.'; $string['insecurealert_title'] = 'Insecure connection!'; +$string['kbrate'] = '{$a} kb/s'; $string['maxfilesizehit'] = 'You have reached the maximum size limit for file uploads.'; $string['maxfilesizehit_title'] = 'Recording stopped'; $string['norecordingfound'] = 'Something has gone wrong. Nothing has been recorded.'; diff --git a/lib/editor/tiny/plugins/recordrtc/settings.php b/lib/editor/tiny/plugins/recordrtc/settings.php index b96e7b1166d..4dda79110b6 100644 --- a/lib/editor/tiny/plugins/recordrtc/settings.php +++ b/lib/editor/tiny/plugins/recordrtc/settings.php @@ -61,8 +61,18 @@ if ($ADMIN->fulltree) { // Audio bitrate. $name = get_string('audiobitrate', 'tiny_recordrtc'); $desc = get_string('audiobitrate_desc', 'tiny_recordrtc'); - $default = '128000'; - $setting = new admin_setting_configtext('tiny_recordrtc/audiobitrate', $name, $desc, $default, PARAM_INT, 8); + $options = []; + foreach (\tiny_recordrtc\constants::TINYRECORDRTC_AUDIO_BITRATES as $rate) { + $kbrate = $rate / 1000; + $options[$rate] = get_string('kbrate', 'tiny_recordrtc', $kbrate); + } + $setting = new admin_setting_configselect( + name: 'tiny_recordrtc/audiobitrate', + visiblename: $name, + description: $desc, + defaultsetting: \tiny_recordrtc\constants::TINYRECORDRTC_AUDIO_BITRATES[5], + choices: $options, + ); $settings->add($setting); // Video bitrate. diff --git a/lib/editor/tiny/plugins/recordrtc/version.php b/lib/editor/tiny/plugins/recordrtc/version.php index f852957a41c..76ce75876f2 100644 --- a/lib/editor/tiny/plugins/recordrtc/version.php +++ b/lib/editor/tiny/plugins/recordrtc/version.php @@ -24,6 +24,6 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2024100700; +$plugin->version = 2024112000; $plugin->requires = 2024100100; $plugin->component = 'tiny_recordrtc';