11#include <stdlib.h>
2+ #include <string.h>
23#include <assert.h>
34
45#include <opus_multistream.h>
@@ -18,7 +19,38 @@ static int frame_size = 0, unit_size = 0;
1819
1920AUDIO_INFO audio_stream_info ;
2021
21- static size_t opus_head_serialize (const OPUS_MULTISTREAM_CONFIGURATION * config , unsigned char * data );
22+ static size_t opus_head_serialize (const POPUS_MULTISTREAM_CONFIGURATION * config , unsigned char * data );
23+
24+ #if TARGET_WEBOS
25+ /* NDL/webOS Opus 5.1 layout (FL FR RL RR C LFE). Same as surround_params "642014523". */
26+ static const unsigned char WEBOS_OPUS_51_MAPPING [6 ] = {0 , 1 , 4 , 5 , 2 , 3 };
27+
28+ static void maybe_remap_webos_51 (OPUS_MULTISTREAM_CONFIGURATION * config ) {
29+ if (config -> channelCount != 6 ) {
30+ return ;
31+ }
32+ /* Hosts that honor surround_params already send webOS order — remapping again
33+ * swaps channels (regression for Apollo on some sets after #50). Only remap when
34+ * the stream is still in Moonlight/SDL order (FL FR C LFE RL RR). */
35+ if (memcmp (config -> mapping , WEBOS_OPUS_51_MAPPING , sizeof (WEBOS_OPUS_51_MAPPING )) == 0 ) {
36+ commons_log_info ("Session" , "5.1 Opus mapping already webOS order; skip remap" );
37+ return ;
38+ }
39+
40+ unsigned char tmp ;
41+ /* Center (2) <-> Surround Left (4); LFE (3) <-> Surround Right (5) */
42+ tmp = config -> mapping [2 ];
43+ config -> mapping [2 ] = config -> mapping [4 ];
44+ config -> mapping [4 ] = tmp ;
45+ tmp = config -> mapping [3 ];
46+ config -> mapping [3 ] = config -> mapping [5 ];
47+ config -> mapping [5 ] = tmp ;
48+ commons_log_info ("Session" ,
49+ "5.1 Opus mapping remapped to webOS order [%u,%u,%u,%u,%u,%u]" ,
50+ config -> mapping [0 ], config -> mapping [1 ], config -> mapping [2 ],
51+ config -> mapping [3 ], config -> mapping [4 ], config -> mapping [5 ]);
52+ }
53+ #endif
2254
2355static int aud_init (int audioConfiguration , const POPUS_MULTISTREAM_CONFIGURATION opusConfig , void * context ,
2456 int arFlags ) {
@@ -32,20 +64,9 @@ static int aud_init(int audioConfiguration, const POPUS_MULTISTREAM_CONFIGURATIO
3264
3365 OPUS_MULTISTREAM_CONFIGURATION config_copy = * opusConfig ;
3466
35- // Swap channels for 5.1 surround sound because the Moonlight library normalizes to
36- // FL, FR, Center, LFE, RL, RR but WebOS hardware expects FL, FR, RL, RR, Center, LFE.
37- if (config_copy .channelCount == 6 ) {
38- unsigned char tmp ;
39- // Swap Front Center (2) <-> Rear/Surround Left (4)
40- tmp = config_copy .mapping [2 ];
41- config_copy .mapping [2 ] = config_copy .mapping [4 ];
42- config_copy .mapping [4 ] = tmp ;
43-
44- // Swap Subwoofer/LFE (3) <-> Rear/Surround Right (5)
45- tmp = config_copy .mapping [3 ];
46- config_copy .mapping [3 ] = config_copy .mapping [5 ];
47- config_copy .mapping [5 ] = tmp ;
48- }
67+ #if TARGET_WEBOS
68+ maybe_remap_webos_51 (& config_copy );
69+ #endif
4970
5071 SS4S_AudioInfo info = {
5172 .numOfChannels = config_copy .channelCount ,
0 commit comments