diff --git a/frameworks/native/audiodefinitions/include/audio_pipe_info.h b/frameworks/native/audiodefinitions/include/audio_pipe_info.h index 3472c14d75b308819ed8859c15f69b9198995724..3f72ff5889cd7d967d2f0fce76b2c4a0e9867c62 100644 --- a/frameworks/native/audiodefinitions/include/audio_pipe_info.h +++ b/frameworks/native/audiodefinitions/include/audio_pipe_info.h @@ -105,6 +105,14 @@ public: return adapterName_ == targetAdapterName; } + bool IsSameRole(const std::shared_ptr stream) const + { + if (stream == nullptr) { + return false; + } + return (stream->IsPlayback() == IsOutput()); + } + void SetAction(AudioPipeAction action) { pipeAction_ = action; diff --git a/frameworks/native/audiodefinitions/test/unittest/audio_pipe_info_unit_test.cpp b/frameworks/native/audiodefinitions/test/unittest/audio_pipe_info_unit_test.cpp index 0ac64b3aeb5ad3956f4ed463d469d7691081c0ae..ff3fbedd00ec61432f252e1370085206f79dd5b4 100644 --- a/frameworks/native/audiodefinitions/test/unittest/audio_pipe_info_unit_test.cpp +++ b/frameworks/native/audiodefinitions/test/unittest/audio_pipe_info_unit_test.cpp @@ -219,5 +219,23 @@ HWTEST_F(AudioPipeInfoUnitTest, StreamOperation_002, TestSize.Level4) EXPECT_EQ(false, testOutputPipe_->ContainStream(stream->GetSessionId())); } +/** + * @tc.name : AudioPipeInfo_IsSameRole_001 + * @tc.number : IsSameRole_001 + * @tc.desc : Test role check func by default output pipe in different cases + */ +HWTEST_F(AudioPipeInfoUnitTest, IsSameRole_001, TestSize.Level4) +{ + // Test nullptr case + EXPECT_EQ(false, testOutputPipe_->IsSameRole(nullptr)); + + // Test same role case + auto playbackStream = AudioDefinitionsUnitTestUtil::GenerateCommonStream(AUDIO_MODE_PLAYBACK); + EXPECT_EQ(true, testOutputPipe_->IsSameRole(playbackStream)); + + // Test different role case + auto recordStream = AudioDefinitionsUnitTestUtil::GenerateCommonStream(AUDIO_MODE_RECORD); + EXPECT_EQ(false, testOutputPipe_->IsSameRole(recordStream)); +} } // namespace AudioStandard } // namespace OHOS \ No newline at end of file diff --git a/services/audio_policy/server/domain/pipe/src/audio_pipe_selector.cpp b/services/audio_policy/server/domain/pipe/src/audio_pipe_selector.cpp index e5a8262ae80ac98a36eeab39010062a5956b6536..5e44823fa9dc31a860d364899604ec17c2f036c1 100644 --- a/services/audio_policy/server/domain/pipe/src/audio_pipe_selector.cpp +++ b/services/audio_policy/server/domain/pipe/src/audio_pipe_selector.cpp @@ -558,7 +558,7 @@ void AudioPipeSelector::MoveStreamsToNormalPipes( // Put each stream to its according normal pipe for (auto &stream : streamsToMove) { for (auto &pipe : pipeInfoList) { - if (pipe->IsRouteNormal() && pipe->IsSameAdapter(streamToAdapter[stream])) { + if (pipe->IsSameRole(stream) && pipe->IsRouteNormal() && pipe->IsSameAdapter(streamToAdapter[stream])) { AddStreamToPipeAndUpdateAction(stream, pipe); break; } diff --git a/services/audio_policy/test/unittest/audio_pipe_selector_unit_test/src/audio_pipe_selector_unit_test.cpp b/services/audio_policy/test/unittest/audio_pipe_selector_unit_test/src/audio_pipe_selector_unit_test.cpp index da9845de50d286827c2d47a3f77c6711d7223d25..4dab190fb724443689c041727612594197c9cf6a 100644 --- a/services/audio_policy/test/unittest/audio_pipe_selector_unit_test/src/audio_pipe_selector_unit_test.cpp +++ b/services/audio_policy/test/unittest/audio_pipe_selector_unit_test/src/audio_pipe_selector_unit_test.cpp @@ -31,6 +31,15 @@ void AudioPipeSelectorUnitTest::TearDownTestCase(void) {} void AudioPipeSelectorUnitTest::SetUp(void) {} void AudioPipeSelectorUnitTest::TearDown(void) {} +static std::shared_ptr MakeTestPipe(AudioPipeRole role, std::string adapterName, uint32_t route) +{ + auto newPipe = std::make_shared(); + newPipe->pipeRole_ = role; + newPipe->adapterName_ = adapterName; + newPipe->routeFlag_ = route; + return newPipe; +} + /** * @tc.name: GetPipeType_001 * @tc.desc: Test GetPipeType when audioMode is AUDIO_MODE_PLAYBACK and flag contains @@ -588,62 +597,51 @@ HWTEST_F(AudioPipeSelectorUnitTest, ProcessConcurrency_001, TestSize.Level4) /** * @tc.name: AudioPipeSelectorUnitTest_MoveStreamsToNormalPipes_001 * @tc.number: MoveStreamsToNormalPipes_001 - * @tc.desc: Test MoveStreamsToNormalPipes cases + * @tc.desc: Test MoveStreamsToNormalPipes different cases */ HWTEST_F(AudioPipeSelectorUnitTest, MoveStreamsToNormalPipes_001, TestSize.Level4) { auto testSelector = AudioPipeSelector::GetPipeSelector(); std::vector> testStreamsToMove; std::vector> testPipeInfoList; - // Make a normal pipe - auto normalPipe = std::make_shared(); - normalPipe->routeFlag_ = AUDIO_OUTPUT_FLAG_NORMAL; - testPipeInfoList.push_back(normalPipe); - - // Make a offload pipe and add one remove stream - auto offloadPipe = std::make_shared(); - offloadPipe->routeFlag_ = AUDIO_OUTPUT_FLAG_LOWPOWER; - auto stream = std::make_shared(); - stream->sessionId_ = TEST_STREAM_1_SESSION_ID; - offloadPipe->AddStream(stream); - testStreamsToMove.push_back(stream); - testPipeInfoList.push_back(offloadPipe); - - testSelector->MoveStreamsToNormalPipes(testStreamsToMove, testPipeInfoList); - EXPECT_EQ(true, normalPipe->ContainStream(TEST_STREAM_1_SESSION_ID)); - EXPECT_EQ(PIPE_ACTION_UPDATE, normalPipe->GetAction()); - EXPECT_EQ(false, offloadPipe->ContainStream(TEST_STREAM_1_SESSION_ID)); -} - -/** - * @tc.name: AudioPipeSelectorUnitTest_MoveStreamsToNormalPipes_002 - * @tc.number: MoveStreamsToNormalPipes_002 - * @tc.desc: Test MoveStreamsToNormalPipes cases - */ -HWTEST_F(AudioPipeSelectorUnitTest, MoveStreamsToNormalPipes_002, TestSize.Level4) -{ - auto testSelector = AudioPipeSelector::GetPipeSelector(); - std::vector> testStreamsToMove; - std::vector> testPipeInfoList; - // Make a normal pipe - auto normalPipe = std::make_shared(); - normalPipe->routeFlag_ = AUDIO_OUTPUT_FLAG_NORMAL; - normalPipe->SetAction(PIPE_ACTION_NEW); - testPipeInfoList.push_back(normalPipe); - - // Make a offload pipe and add one remove stream - auto offloadPipe = std::make_shared(); - offloadPipe->routeFlag_ = AUDIO_OUTPUT_FLAG_LOWPOWER; + + // Test pipe IsSameRole() false, pipe IsRouteNormal() false, pipe->IsSameAdapter() false + auto usbFastInputPipe = MakeTestPipe(PIPE_ROLE_INPUT, "usb", AUDIO_INPUT_FLAG_FAST); + testPipeInfoList.push_back(usbFastInputPipe); + + // Test pipe IsSameRole() false, pipe IsRouteNormal() true, pipe->IsSameAdapter() false + auto usbNormalInputPipe = MakeTestPipe(PIPE_ROLE_INPUT, "usb", AUDIO_INPUT_FLAG_NORMAL); + testPipeInfoList.push_back(usbNormalInputPipe); + + // Test pipe IsSameRole() false, pipe IsRouteNormal() true, pipe->IsSameAdapter() true + auto primaryNormalInputPipe = MakeTestPipe(PIPE_ROLE_INPUT, "primary", AUDIO_INPUT_FLAG_NORMAL); + testPipeInfoList.push_back(primaryNormalInputPipe); + + // Test pipe IsSameRole() true, pipe IsRouteNormal() false, pipe->IsSameAdapter() false + auto usbFastOutputPipe = MakeTestPipe(PIPE_ROLE_OUTPUT, "usb", AUDIO_OUTPUT_FLAG_FAST); + testPipeInfoList.push_back(usbFastOutputPipe); + + // Test pipe IsSameRole() true, pipe IsRouteNormal() true && pipe->IsSameAdapter() false + auto usbNormalOutputPipe = MakeTestPipe(PIPE_ROLE_OUTPUT, "usb", AUDIO_OUTPUT_FLAG_NORMAL); + testPipeInfoList.push_back(usbNormalOutputPipe); + + // Test pipe IsSameRole() true, pipe IsRouteNormal() true && pipe->IsSameAdapter() true + auto primaryNormalOutputPipe = MakeTestPipe(PIPE_ROLE_OUTPUT, "primary", AUDIO_OUTPUT_FLAG_NORMAL); + testPipeInfoList.push_back(primaryNormalOutputPipe); + + // Test stream will be moved from primary adapter offload output pipe + auto primaryOffloadOutputPipe = MakeTestPipe(PIPE_ROLE_OUTPUT, "primary", AUDIO_OUTPUT_FLAG_LOWPOWER); + testPipeInfoList.push_back(primaryNormalOutputPipe); + auto stream = std::make_shared(); stream->sessionId_ = TEST_STREAM_1_SESSION_ID; - offloadPipe->AddStream(stream); + primaryOffloadOutputPipe->AddStream(stream); testStreamsToMove.push_back(stream); - testPipeInfoList.push_back(offloadPipe); + testPipeInfoList.push_back(primaryOffloadOutputPipe); testSelector->MoveStreamsToNormalPipes(testStreamsToMove, testPipeInfoList); - EXPECT_EQ(true, normalPipe->ContainStream(TEST_STREAM_1_SESSION_ID)); - EXPECT_EQ(PIPE_ACTION_NEW, normalPipe->GetAction()); - EXPECT_EQ(false, offloadPipe->ContainStream(TEST_STREAM_1_SESSION_ID)); + EXPECT_EQ(false, primaryOffloadOutputPipe->ContainStream(TEST_STREAM_1_SESSION_ID)); + EXPECT_EQ(PIPE_ACTION_UPDATE, primaryNormalOutputPipe->GetAction()); } /**