Skip to content

Commit 6279836

Browse files
committed
feat(codecs): set modern default codec set for fresh installs
Fresh installs boot from seed PBXVersion=0.0.2 and run the whole upgrade chain, whose legacy scripts (Ver20202754/Ver202202103/Ver202302161) reshape m_Codecs with obsolete lists: they leave only h264/alaw/ulaw/opus/ ilbc enabled plus JPEG/H.261 garbage and drop g722/g729/h265/vp8/vp9. Editing the seed DB is therefore futile. Add a single source of truth (CodecSync::DEFAULT_CODEC_SET / applyDefaultCodecSet) and a late upgrade release (Ver2026271) that runs last and re-applies the canonical default set, gated to installs that went through the destructive legacy chain (previous < 2020.2.754). Upgrades from newer versions keep the administrator's codec choices. Enabled by default: opus, g722, alaw, ulaw, g729 (audio) and h265, h264, vp9, vp8 (video); gsm stays enabled last (system sounds). ilbc/g726/ g726aal2/speex/g719 ship available-but-off. CodecSync then refines the set against the running Asterisk build. RestoreDefaultSettings reuses applyDefaultCodecSet (removes the duplicated resetCodecsToDefaults and its dead Codecs import). DEFAULT_AUDIO/VIDEO_ PRIORITIES reordered to match. Tested on boffart: live pjsip endpoint negotiates opus,g722,alaw,ulaw,g729,gsm,h265,h264,vp9,vp8; version gate verified in both directions.
1 parent def3daf commit 6279836

4 files changed

Lines changed: 225 additions & 120 deletions

File tree

src/Core/Asterisk/CLAUDE.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,9 @@ Generates dialplan for CallerID/DID extraction from SIP headers. Sources:
105105
## CodecSync
106106

107107
Syncs the codec DB with Asterisk's available codecs. Default audio priorities:
108-
alaw(1), ulaw(2), opus(3), g722(4), g729(5). Video: h264(1), h263(2), h263p(3),
109-
vp8(4), vp9(5).
108+
opus(1), g722(2), alaw(3), ulaw(4), g729(5). Video: h265(1), h264(2), vp9(3),
109+
vp8(4). New codecs are added enabled; IGNORED_CODECS/unsupported are deleted;
110+
existing enable/disable flags are preserved; gsm is force-enabled.
110111

111112
## AsteriskManager — AMI client
112113

src/Core/Asterisk/Configs/Generators/CodecSync.php

Lines changed: 111 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,33 +37,36 @@ class CodecSync
3737
{
3838
/**
3939
* Default codec priorities for audio codecs.
40-
* Used when adding new codecs to database.
40+
* Used only by addCodecToDatabase() when syncCodecsWithAsterisk() discovers a
41+
* codec that is not yet in the database. The authoritative fresh-install layout
42+
* (including enabled/disabled flags and gsm at the bottom) lives in
43+
* DEFAULT_CODEC_SET; the gsm priority intentionally differs between the two.
4144
*/
4245
private const array DEFAULT_AUDIO_PRIORITIES = [
43-
'alaw' => 1,
44-
'ulaw' => 2,
45-
'opus' => 3,
46-
'g722' => 4,
46+
'opus' => 1,
47+
'g722' => 2,
48+
'alaw' => 3,
49+
'ulaw' => 4,
4750
'g729' => 5,
4851
'ilbc' => 6,
4952
'g726' => 7,
5053
'g726aal2' => 8,
5154
'gsm' => 9,
52-
'adpcm' => 10,
53-
'lpc10' => 11,
54-
'speex' => 12,
55-
'speex16' => 13,
56-
'speex32' => 14,
57-
'slin' => 15,
58-
'slin12' => 16,
59-
'slin16' => 17,
60-
'slin24' => 18,
61-
'slin32' => 19,
62-
'slin44' => 20,
63-
'slin48' => 21,
64-
'slin96' => 22,
65-
'slin192' => 23,
66-
'g719' => 24,
55+
'speex' => 10,
56+
'g719' => 11,
57+
'adpcm' => 12,
58+
'lpc10' => 13,
59+
'speex16' => 14,
60+
'speex32' => 15,
61+
'slin' => 16,
62+
'slin12' => 17,
63+
'slin16' => 18,
64+
'slin24' => 19,
65+
'slin32' => 20,
66+
'slin44' => 21,
67+
'slin48' => 22,
68+
'slin96' => 23,
69+
'slin192' => 24,
6770
'silk' => 25,
6871
'silk8' => 26,
6972
'silk12' => 27,
@@ -80,16 +83,46 @@ class CodecSync
8083
* Used when adding new codecs to database.
8184
*/
8285
private const array DEFAULT_VIDEO_PRIORITIES = [
83-
'h264' => 1,
84-
'h263' => 2,
85-
'h263p' => 3,
86+
'h265' => 1,
87+
'h264' => 2,
88+
'vp9' => 3,
8689
'vp8' => 4,
87-
'vp9' => 5,
88-
'h261' => 6,
89-
'h265' => 7,
90+
'h263' => 5,
91+
'h263p' => 6,
92+
'h261' => 7,
9093
'mpeg4' => 8,
9194
];
9295

96+
/**
97+
* Canonical default codec set for a fresh installation.
98+
*
99+
* Format: name => [type, priority, disabled ('0' = enabled, '1' = disabled)].
100+
* Descriptions are taken from CODEC_DESCRIPTIONS.
101+
*
102+
* Enabled by default: opus, g722, alaw, ulaw, g729 (audio) and
103+
* h265, h264, vp9, vp8 (video). gsm stays enabled (system sound files use
104+
* GSM format) and is placed last. ilbc, g726, g726aal2, speex and g719 are
105+
* shipped available-but-off. Codecs the running build does not provide are
106+
* removed later by syncCodecsWithAsterisk().
107+
*/
108+
private const array DEFAULT_CODEC_SET = [
109+
'opus' => ['audio', 1, '0'],
110+
'g722' => ['audio', 2, '0'],
111+
'alaw' => ['audio', 3, '0'],
112+
'ulaw' => ['audio', 4, '0'],
113+
'g729' => ['audio', 5, '0'],
114+
'ilbc' => ['audio', 6, '1'],
115+
'g726' => ['audio', 7, '1'],
116+
'g726aal2' => ['audio', 8, '1'],
117+
'speex' => ['audio', 9, '1'],
118+
'g719' => ['audio', 10, '1'],
119+
'gsm' => ['audio', 11, '0'],
120+
'h265' => ['video', 1, '0'],
121+
'h264' => ['video', 2, '0'],
122+
'vp9' => ['video', 3, '0'],
123+
'vp8' => ['video', 4, '0'],
124+
];
125+
93126
/**
94127
* Codecs to ignore during synchronization.
95128
* These codecs are rarely used in real-world scenarios and clutter the interface.
@@ -410,6 +443,58 @@ public static function getEnabledSupportedCodecs(): array
410443
return $result;
411444
}
412445

446+
/**
447+
* Apply the canonical default codec set (DEFAULT_CODEC_SET) to the database.
448+
*
449+
* Removes all existing codec rows and re-creates the default set, so the
450+
* result is deterministic regardless of prior state. This is required on a
451+
* fresh install because the legacy upgrade release scripts (e.g.
452+
* UpdateConfigsUpToVer20202754) run first from the 0.0.2 seed baseline and
453+
* reshape m_Codecs with their own obsolete lists.
454+
*
455+
* Callers: the fresh-install upgrade step and "restore default settings".
456+
* A subsequent syncCodecsWithAsterisk() run refines the list against the
457+
* capabilities of the actual Asterisk build.
458+
*
459+
* @return void
460+
*/
461+
public static function applyDefaultCodecSet(): void
462+
{
463+
// Remove all existing codecs to guarantee a clean, deterministic set.
464+
foreach (Codecs::find() as $codec) {
465+
if (!$codec->delete()) {
466+
SystemMessages::sysLogMsg(
467+
__CLASS__,
468+
'Failed to delete codec ' . $codec->name . ': ' . implode(', ', $codec->getMessages()),
469+
LOG_WARNING
470+
);
471+
}
472+
}
473+
474+
foreach (self::DEFAULT_CODEC_SET as $name => [$type, $priority, $disabled]) {
475+
$codec = new Codecs();
476+
$codec->name = $name;
477+
$codec->type = $type;
478+
$codec->priority = (string)$priority;
479+
$codec->disabled = $disabled;
480+
$codec->description = self::CODEC_DESCRIPTIONS[$name] ?? ucfirst($name);
481+
482+
if (!$codec->save()) {
483+
SystemMessages::sysLogMsg(
484+
__CLASS__,
485+
'Failed to create codec ' . $name . ': ' . implode(', ', $codec->getMessages()),
486+
LOG_WARNING
487+
);
488+
}
489+
}
490+
491+
SystemMessages::sysLogMsg(
492+
__CLASS__,
493+
'Applied default codec set (' . count(self::DEFAULT_CODEC_SET) . ' codecs).',
494+
LOG_INFO
495+
);
496+
}
497+
413498
/**
414499
* Temporarily load all codec modules for transcoding validation.
415500
*
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
/*
4+
* MikoPBX - free phone system for small business
5+
* Copyright © 2017-2026 Alexey Portnov and Nikolay Beketov
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License along with this program.
18+
* If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
namespace MikoPBX\Core\System\Upgrade\Releases;
22+
23+
use MikoPBX\Common\Models\PbxSettings;
24+
use MikoPBX\Core\Asterisk\Configs\Generators\CodecSync;
25+
use MikoPBX\Core\System\Upgrade\UpgradeSystemConfigInterface;
26+
use Phalcon\Di\Injectable;
27+
28+
/**
29+
* Class UpdateConfigsUpToVer2026271
30+
*
31+
* Normalises the codec table to the modern default set on fresh installs.
32+
*
33+
* On a fresh install the seed database ships PBXVersion=0.0.2, so the whole
34+
* upgrade chain runs from scratch — including the legacy codec scripts
35+
* (UpdateConfigsUpToVer20202754::deleteOrphanCodecs()/disableCodecs(),
36+
* UpdateConfigsUpToVer202202103, UpdateConfigsUpToVer202302161). Those impose
37+
* an obsolete codec list (they delete g729/g722/h265/vp8/vp9 and leave only
38+
* h264/alaw/ulaw/opus/ilbc enabled, plus JPEG/H.261 garbage). This release runs
39+
* last (highest version) and re-applies the canonical default set so a fresh
40+
* install ends up with opus, g722, alaw, ulaw, g729 (audio) and h265, h264,
41+
* vp9, vp8 (video) enabled. CodecSync::syncCodecsWithAsterisk() then refines it
42+
* against the running Asterisk build.
43+
*
44+
* The reset is gated to installs that actually went through the destructive
45+
* legacy chain (previous version < 2020.2.754, which includes fresh 0.0.2).
46+
* Upgrades from newer versions keep the administrator's own codec choices.
47+
*
48+
* @package MikoPBX\Core\System\Upgrade\Releases
49+
*/
50+
class UpdateConfigsUpToVer2026271 extends Injectable implements UpgradeSystemConfigInterface
51+
{
52+
public const string PBX_VERSION = '2026.2.71';
53+
54+
/**
55+
* Last version whose upgrade scripts destructively rewrote the codec table.
56+
*/
57+
private const string LEGACY_CODEC_CHAIN_VERSION = '2020.2.754';
58+
59+
/**
60+
* Main update method
61+
*
62+
* @return void
63+
*/
64+
public function processUpdate(): void
65+
{
66+
// During the upgrade run PBX_VERSION still holds the pre-upgrade value;
67+
// it is bumped to the current firmware only after the whole chain finishes.
68+
$previousVersion = (string)str_ireplace(
69+
'-dev',
70+
'',
71+
PbxSettings::getValueByKey(PbxSettings::PBX_VERSION)
72+
);
73+
74+
// Only reset codecs for installs that ran the destructive legacy chain
75+
// (fresh install starts at 0.0.2). Preserve codec choices on newer upgrades.
76+
if (version_compare($previousVersion, self::LEGACY_CODEC_CHAIN_VERSION, '<')) {
77+
CodecSync::applyDefaultCodecSet();
78+
echo "Applied default codec set after legacy upgrade chain\n";
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)