Fix Chef unable to set audio-output through RPC (#36295)

* Fix Chef unable to set audio-output through RPC

* Fix restyle issue
diff --git a/examples/chef/common/clusters/audio-output/AudioOutputManager.cpp b/examples/chef/common/clusters/audio-output/AudioOutputManager.cpp
index 65df8db..776baf5 100644
--- a/examples/chef/common/clusters/audio-output/AudioOutputManager.cpp
+++ b/examples/chef/common/clusters/audio-output/AudioOutputManager.cpp
@@ -16,15 +16,21 @@
  *    limitations under the License.
  */
 
+#include <app-common/zap-generated/attributes/Accessors.h>
+#include <app/AttributeValueEncoder.h>
 #include <app/util/config.h>
+#include <map>
+
 #ifdef MATTER_DM_PLUGIN_AUDIO_OUTPUT_SERVER
 #include "AudioOutputManager.h"
 
+using namespace chip;
 using namespace chip::app;
 using namespace chip::app::Clusters::AudioOutput;
 using chip::app::AttributeValueEncoder;
+using chip::Protocols::InteractionModel::Status;
 
-AudioOutputManager::AudioOutputManager()
+AudioOutputManager::AudioOutputManager(chip::EndpointId endpoint) : mEndpoint(endpoint)
 {
     struct OutputData outputData1(1, chip::app::Clusters::AudioOutput::OutputTypeEnum::kHdmi, "HDMI 1");
     mOutputs.push_back(outputData1);
@@ -32,13 +38,17 @@
     mOutputs.push_back(outputData2);
     struct OutputData outputData3(3, chip::app::Clusters::AudioOutput::OutputTypeEnum::kHdmi, "HDMI 3");
     mOutputs.push_back(outputData3);
-
-    mCurrentOutput = 1;
 }
 
 uint8_t AudioOutputManager::HandleGetCurrentOutput()
 {
-    return mCurrentOutput;
+    uint8_t currentOutput = 1;
+    Status status         = Attributes::CurrentOutput::Get(mEndpoint, &currentOutput);
+    if (Status::Success != status)
+    {
+        ChipLogError(Zcl, "Unable to get CurrentOutput attribute, err:0x%x", to_underlying(status));
+    }
+    return currentOutput;
 }
 
 CHIP_ERROR AudioOutputManager::HandleGetOutputList(AttributeValueEncoder & aEncoder)
@@ -72,11 +82,27 @@
     {
         if (outputData.index == index)
         {
-            mCurrentOutput = index;
+            // Sync the CurrentOutput to attribute storage while reporting changes
+            Status status = Attributes::CurrentOutput::Set(mEndpoint, index);
+            if (Status::Success != status)
+            {
+                ChipLogError(Zcl, "CurrentOutput is not stored successfully, err:0x%x", to_underlying(status));
+            }
             return true;
         }
     }
 
     return false;
 }
+
+static std::map<chip::EndpointId, std::unique_ptr<AudioOutputManager>> gAudioOutputManagerInstance{};
+
+void emberAfAudioOutputClusterInitCallback(EndpointId endpoint)
+{
+    ChipLogProgress(Zcl, "TV Linux App: AudioOutput::SetDefaultDelegate, endpoint=%x", endpoint);
+
+    gAudioOutputManagerInstance[endpoint] = std::make_unique<AudioOutputManager>(endpoint);
+
+    SetDefaultDelegate(endpoint, gAudioOutputManagerInstance[endpoint].get());
+}
 #endif // MATTER_DM_PLUGIN_AUDIO_OUTPUT_SERVER
diff --git a/examples/chef/common/clusters/audio-output/AudioOutputManager.h b/examples/chef/common/clusters/audio-output/AudioOutputManager.h
index ad64b7c..93c94d6 100644
--- a/examples/chef/common/clusters/audio-output/AudioOutputManager.h
+++ b/examples/chef/common/clusters/audio-output/AudioOutputManager.h
@@ -27,7 +27,7 @@
     using OutputInfoType = chip::app::Clusters::AudioOutput::Structs::OutputInfoStruct::Type;
 
 public:
-    AudioOutputManager();
+    AudioOutputManager(chip::EndpointId endpoint);
 
     uint8_t HandleGetCurrentOutput() override;
     CHIP_ERROR HandleGetOutputList(chip::app::AttributeValueEncoder & aEncoder) override;
@@ -54,6 +54,6 @@
     };
 
 protected:
-    uint8_t mCurrentOutput = 1;
+    chip::EndpointId mEndpoint;
     std::vector<struct OutputData> mOutputs;
 };
diff --git a/examples/chef/common/stubs.cpp b/examples/chef/common/stubs.cpp
index 513bb41..5cbe80a 100644
--- a/examples/chef/common/stubs.cpp
+++ b/examples/chef/common/stubs.cpp
@@ -270,17 +270,6 @@
  */
 void emberAfOnOffClusterInitCallback(EndpointId endpoint) {}
 
-#ifdef MATTER_DM_PLUGIN_AUDIO_OUTPUT_SERVER
-#include "audio-output/AudioOutputManager.h"
-static AudioOutputManager audioOutputManager;
-
-void emberAfAudioOutputClusterInitCallback(EndpointId endpoint)
-{
-    ChipLogProgress(Zcl, "TV Linux App: AudioOutput::SetDefaultDelegate");
-    AudioOutput::SetDefaultDelegate(endpoint, &audioOutputManager);
-}
-#endif
-
 #ifdef MATTER_DM_PLUGIN_CHANNEL_SERVER
 #include "channel/ChannelManager.h"
 static ChannelManager channelManager;