From 55fb142c2f34ba9108f0a968fa17ed11407d8abc 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 | 29 ++++++++++ .../tiny/plugins/recordrtc/db/upgrade.php | 53 +++++++++++++++++++ .../recordrtc/lang/en/tiny_recordrtc.php | 1 + .../tiny/plugins/recordrtc/settings.php | 14 ++++- lib/editor/tiny/plugins/recordrtc/version.php | 2 +- 5 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 lib/editor/tiny/plugins/recordrtc/classes/constants.php create mode 100644 lib/editor/tiny/plugins/recordrtc/db/upgrade.php diff --git a/lib/editor/tiny/plugins/recordrtc/classes/constants.php b/lib/editor/tiny/plugins/recordrtc/classes/constants.php new file mode 100644 index 00000000000..861b323ed93 --- /dev/null +++ b/lib/editor/tiny/plugins/recordrtc/classes/constants.php @@ -0,0 +1,29 @@ +. + +namespace tiny_recordrtc; + +/** + * Constants for Tiny RecordRTC plugin. + * + * @package tiny_recordrtc + * @copyright Meirza + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class constants { + /** @var array TINYRECORDRTC_AUDIO_BITRATE 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 new file mode 100644 index 00000000000..0974a8a6554 --- /dev/null +++ b/lib/editor/tiny/plugins/recordrtc/db/upgrade.php @@ -0,0 +1,53 @@ +. + +/** + * Tiny text editor recordrtc plugin upgrade script. + * + * @package tiny_recordrtc + * @copyright Meirza + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Run all Tiny recordrtc upgrade steps between the current DB version and the current version on disk. + * @param int $oldversion The old version of atto equation in the DB. + * @return bool + */ +function xmldb_tiny_recordrtc_upgrade($oldversion) { + if ($oldversion < 2024042201) { + // 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, 2024042201); + } + + // Automatically generated Moodle v4.4.0 release upgrade line. + // Put any upgrade step following this. + + return true; +} 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 8f15afbf349..c3e0de13262 100644 --- a/lib/editor/tiny/plugins/recordrtc/lang/en/tiny_recordrtc.php +++ b/lib/editor/tiny/plugins/recordrtc/lang/en/tiny_recordrtc.php @@ -53,6 +53,7 @@ $string['gumtype'] = 'Tried to get stream from the webcam/microphone, but no con $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 405722809d1..4179ef1e077 100644 --- a/lib/editor/tiny/plugins/recordrtc/settings.php +++ b/lib/editor/tiny/plugins/recordrtc/settings.php @@ -58,8 +58,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 b6442a3a3de..5396d43702e 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 = 2024042200; +$plugin->version = 2024042201; $plugin->requires = 2024041600; $plugin->component = 'tiny_recordrtc';