output_device_mixer_manager_unittest.cc 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485
  1. // Copyright (c) 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "services/audio/output_device_mixer_manager.h"
  5. #include "base/test/bind.h"
  6. #include "base/test/gmock_callback_support.h"
  7. #include "base/test/task_environment.h"
  8. #include "media/audio/audio_device_description.h"
  9. #include "media/audio/audio_io.h"
  10. #include "media/audio/mock_audio_manager.h"
  11. #include "media/audio/test_audio_thread.h"
  12. #include "media/base/audio_parameters.h"
  13. #include "services/audio/output_device_mixer.h"
  14. #include "services/audio/reference_output.h"
  15. #include "testing/gmock/include/gmock/gmock.h"
  16. #include "testing/gtest/include/gtest/gtest.h"
  17. #include "third_party/abseil-cpp/absl/types/optional.h"
  18. using ::testing::_;
  19. using ::testing::ByMove;
  20. using ::testing::Eq;
  21. using ::testing::InSequence;
  22. using ::testing::NiceMock;
  23. using ::testing::Ref;
  24. using ::testing::Return;
  25. using ::testing::ReturnRef;
  26. using ::testing::StrictMock;
  27. using media::AudioOutputStream;
  28. using media::AudioParameters;
  29. using base::test::RunOnceClosure;
  30. namespace audio {
  31. namespace {
  32. // The "default" and "communications" strings represent reserved device IDs.
  33. // They are used in different situations, but the OutputDeviceMixerManager
  34. // should treat all reserved device IDs the same way.
  35. enum class ReservedIdTestType {
  36. kDefault,
  37. kCommunications,
  38. };
  39. // Matches non-null device change callbacks.
  40. MATCHER(ValidDeviceChangeCallback, "") {
  41. return !arg.is_null();
  42. }
  43. // Matches an expected media::AudioParameters.
  44. MATCHER_P(ExactParams, expected, "") {
  45. return expected.Equals(arg);
  46. }
  47. // Matches media::AudioParameters that are equal in all aspects,
  48. // except for sample_per_buffer()
  49. MATCHER_P(CompatibleParams, expected, "") {
  50. return expected.format() == arg.format() &&
  51. expected.channel_layout() == arg.channel_layout() &&
  52. expected.channels() == arg.channels() &&
  53. expected.effects() == arg.effects() &&
  54. expected.mic_positions() == arg.mic_positions() &&
  55. expected.latency_tag() == arg.latency_tag();
  56. }
  57. const std::string kFakeDeviceId = "0x1234";
  58. const std::string kOtherFakeDeviceId = "0x9876";
  59. const std::string kFakeCommunicationsId = "0xabcd";
  60. const std::string kEmptyDeviceId = std::string();
  61. const std::string kNormalizedDefaultDeviceId = kEmptyDeviceId;
  62. const auto* kReservedDefaultId =
  63. media::AudioDeviceDescription::kDefaultDeviceId;
  64. const auto* kReservedCommsId =
  65. media::AudioDeviceDescription::kCommunicationsDeviceId;
  66. class MockAudioOutputStream : public AudioOutputStream {
  67. public:
  68. MockAudioOutputStream() = default;
  69. ~MockAudioOutputStream() override = default;
  70. MOCK_METHOD1(Start, void(AudioOutputStream::AudioSourceCallback*));
  71. MOCK_METHOD0(Stop, void());
  72. MOCK_METHOD0(Open, bool());
  73. MOCK_METHOD1(SetVolume, void(double volume));
  74. MOCK_METHOD1(GetVolume, void(double* volume));
  75. MOCK_METHOD0(Close, void());
  76. MOCK_METHOD0(Flush, void());
  77. };
  78. class LocalMockAudioManager : public media::MockAudioManager {
  79. public:
  80. LocalMockAudioManager()
  81. : media::MockAudioManager(
  82. std::make_unique<media::TestAudioThread>(false)) {}
  83. ~LocalMockAudioManager() override = default;
  84. MOCK_METHOD(std::string, GetDefaultOutputDeviceID, (), (override));
  85. MOCK_METHOD(std::string, GetCommunicationsOutputDeviceID, (), (override));
  86. MOCK_METHOD(AudioParameters,
  87. GetOutputStreamParameters,
  88. (const std::string&),
  89. (override));
  90. MOCK_METHOD(AudioParameters,
  91. GetDefaultOutputStreamParameters,
  92. (),
  93. (override));
  94. MOCK_METHOD(AudioOutputStream*,
  95. MakeAudioOutputStreamProxy,
  96. (const media::AudioParameters&, const std::string&),
  97. (override));
  98. };
  99. class MockListener : public audio::ReferenceOutput::Listener {
  100. public:
  101. MockListener() = default;
  102. ~MockListener() override = default;
  103. MOCK_METHOD(void,
  104. OnPlayoutData,
  105. (const media::AudioBus&, int, base::TimeDelta),
  106. (override));
  107. };
  108. class MockOutputDeviceMixer : public audio::OutputDeviceMixer {
  109. public:
  110. explicit MockOutputDeviceMixer(const std::string& device_id)
  111. : OutputDeviceMixer(device_id) {}
  112. ~MockOutputDeviceMixer() override = default;
  113. MOCK_METHOD(media::AudioOutputStream*,
  114. MakeMixableStream,
  115. (const AudioParameters&, base::OnceClosure),
  116. (override));
  117. MOCK_METHOD(void, ProcessDeviceChange, (), (override));
  118. MOCK_METHOD(void, StartListening, (Listener*), (override));
  119. MOCK_METHOD(void, StopListening, (Listener*), (override));
  120. };
  121. } // namespace
  122. class OutputDeviceMixerManagerTest
  123. : public ::testing::TestWithParam<ReservedIdTestType> {
  124. public:
  125. OutputDeviceMixerManagerTest()
  126. : current_default_physical_device_id_(kFakeDeviceId),
  127. current_communications_physical_device_id_(kFakeCommunicationsId),
  128. default_params_(AudioParameters::Format::AUDIO_PCM_LOW_LATENCY,
  129. media::ChannelLayout::CHANNEL_LAYOUT_STEREO,
  130. /*sample_rate=*/8000,
  131. /*frames_per_buffer=*/800),
  132. output_mixer_manager_(
  133. &audio_manager_,
  134. base::BindRepeating(
  135. &OutputDeviceMixerManagerTest::CreateOutputDeviceMixerCalled,
  136. base::Unretained(this))) {
  137. EXPECT_CALL(audio_manager_, GetOutputStreamParameters(_))
  138. .WillRepeatedly(Return(default_params_));
  139. EXPECT_CALL(audio_manager_, GetDefaultOutputStreamParameters())
  140. .WillRepeatedly(Return(default_params_));
  141. EXPECT_CALL(audio_manager_, GetDefaultOutputDeviceID()).WillRepeatedly([&] {
  142. return audio_manager_supports_default_physical_id_
  143. ? current_default_physical_device_id_
  144. : kEmptyDeviceId;
  145. });
  146. EXPECT_CALL(audio_manager_, GetCommunicationsOutputDeviceID())
  147. .WillRepeatedly([&] {
  148. return audio_manager_supports_communications_physical_id_
  149. ? current_communications_physical_device_id_
  150. : kEmptyDeviceId;
  151. });
  152. // Force |output_mixer_manager_| to pick up the latest default device ID
  153. // from AudioManager::GetDefaultOutputDeviceID().
  154. output_mixer_manager_.OnDeviceChange();
  155. }
  156. ~OutputDeviceMixerManagerTest() override { audio_manager_.Shutdown(); }
  157. MOCK_METHOD(std::unique_ptr<OutputDeviceMixer>,
  158. CreateOutputDeviceMixerCalled,
  159. (const std::string&,
  160. const media::AudioParameters&,
  161. OutputDeviceMixer::CreateStreamCallback,
  162. scoped_refptr<base::SingleThreadTaskRunner>));
  163. protected:
  164. std::string current_default_physical_device() {
  165. return current_default_physical_device_id_;
  166. }
  167. std::string current_communications_physical_device() {
  168. return current_communications_physical_device_id_;
  169. }
  170. void SetAudioManagerDefaultIdSupport(bool support) {
  171. bool needs_device_change =
  172. audio_manager_supports_default_physical_id_ != support;
  173. audio_manager_supports_default_physical_id_ = support;
  174. // Force |output_mixer_manager_| to pick up the latest default device ID.
  175. if (needs_device_change)
  176. output_mixer_manager_.OnDeviceChange();
  177. }
  178. void SetAudioManagerCommunicationsIdSupport(bool support) {
  179. bool needs_device_change =
  180. audio_manager_supports_communications_physical_id_ != support;
  181. audio_manager_supports_communications_physical_id_ = support;
  182. // Force |output_mixer_manager_| to pick up the latest default device ID.
  183. if (needs_device_change)
  184. output_mixer_manager_.OnDeviceChange();
  185. }
  186. MockOutputDeviceMixer* SetUpMockMixerCreation(
  187. std::string device_id = kNormalizedDefaultDeviceId) {
  188. auto mock_output_mixer =
  189. std::make_unique<NiceMock<MockOutputDeviceMixer>>(device_id);
  190. MockOutputDeviceMixer* mixer = mock_output_mixer.get();
  191. EXPECT_CALL(*this, CreateOutputDeviceMixerCalled(
  192. device_id, CompatibleParams(default_params_), _, _))
  193. .WillOnce(Return(ByMove(std::move(mock_output_mixer))));
  194. return mixer;
  195. }
  196. // Sets up a mock OutputDeviceMixer for creation, which will only return
  197. // nullptr when creating streams.
  198. MockOutputDeviceMixer* SetUpMockMixer_NoStreams(
  199. std::string device_id = kNormalizedDefaultDeviceId) {
  200. MockOutputDeviceMixer* output_mixer = SetUpMockMixerCreation(device_id);
  201. EXPECT_CALL(*output_mixer, MakeMixableStream(_, _))
  202. .WillRepeatedly(Return(nullptr));
  203. return output_mixer;
  204. }
  205. std::unique_ptr<NiceMock<MockListener>> GetListener_MixerExpectsStartStop(
  206. MockOutputDeviceMixer* mixer) {
  207. return GetListenerWithStartStopExpectations(mixer, 1, 1);
  208. }
  209. std::unique_ptr<NiceMock<MockListener>> GetListener_MixerExpectsStart(
  210. MockOutputDeviceMixer* mixer) {
  211. return GetListenerWithStartStopExpectations(mixer, 1, 0);
  212. }
  213. std::unique_ptr<NiceMock<MockListener>> GetListener_MixerExpectsNoCalls(
  214. MockOutputDeviceMixer* mixer) {
  215. return GetListenerWithStartStopExpectations(mixer, 0, 0);
  216. }
  217. void ForceOutputMixerCreation(const std::string& device_id) {
  218. output_mixer_manager_.MakeOutputStream(device_id, default_params_,
  219. GetNoopDeviceChangeCallback());
  220. }
  221. void SimulateDeviceChange() {
  222. SimulateDeviceChange(absl::nullopt, absl::nullopt);
  223. }
  224. void SimulateDeviceChange(
  225. absl::optional<std::string> new_default_physical_device,
  226. absl::optional<std::string> new_communications_physical_device) {
  227. if (new_default_physical_device)
  228. current_default_physical_device_id_ = *new_default_physical_device;
  229. if (new_communications_physical_device) {
  230. current_communications_physical_device_id_ =
  231. *new_communications_physical_device;
  232. }
  233. output_mixer_manager_.OnDeviceChange();
  234. }
  235. void ExpectNoMixerCreated() {
  236. EXPECT_CALL(*this, CreateOutputDeviceMixerCalled(_, _, _, _)).Times(0);
  237. }
  238. base::OnceClosure GetOnDeviceChangeCallback() {
  239. return output_mixer_manager_.GetOnDeviceChangeCallback();
  240. }
  241. // Syntactic sugar, to differentiate from base::OnceClosure in tests.
  242. base::OnceClosure GetNoopDeviceChangeCallback() { return base::DoNothing(); }
  243. // ----------------------------------------------------------
  244. // The following methods are use to parameterize tests that are identical for
  245. // the "communications" and "default" reserved IDs.
  246. //
  247. // Whether we are testing the "default" or "communications" reserved ID.
  248. ReservedIdTestType reserved_id_test_type() { return GetParam(); }
  249. void SetAudioManagerReservedIdSupport(bool support) {
  250. switch (reserved_id_test_type()) {
  251. case ReservedIdTestType::kDefault:
  252. return SetAudioManagerDefaultIdSupport(support);
  253. case ReservedIdTestType::kCommunications:
  254. return SetAudioManagerCommunicationsIdSupport(support);
  255. }
  256. }
  257. MockOutputDeviceMixer* SetUpReservedMixerCreation() {
  258. switch (reserved_id_test_type()) {
  259. case ReservedIdTestType::kDefault:
  260. return SetUpMockMixerCreation(kNormalizedDefaultDeviceId);
  261. case ReservedIdTestType::kCommunications:
  262. return SetUpMockMixerCreation(kReservedCommsId);
  263. }
  264. }
  265. MockOutputDeviceMixer* SetUpReservedMixer_NoStreams() {
  266. switch (reserved_id_test_type()) {
  267. case ReservedIdTestType::kDefault:
  268. return SetUpMockMixer_NoStreams(kNormalizedDefaultDeviceId);
  269. case ReservedIdTestType::kCommunications:
  270. return SetUpMockMixer_NoStreams(kReservedCommsId);
  271. }
  272. }
  273. std::string reserved_device_id() {
  274. switch (reserved_id_test_type()) {
  275. case ReservedIdTestType::kDefault:
  276. return kReservedDefaultId;
  277. case ReservedIdTestType::kCommunications:
  278. return kReservedCommsId;
  279. }
  280. }
  281. std::string current_reserved_physical_device() {
  282. switch (reserved_id_test_type()) {
  283. case ReservedIdTestType::kDefault:
  284. return current_default_physical_device();
  285. case ReservedIdTestType::kCommunications:
  286. return current_communications_physical_device();
  287. }
  288. }
  289. void SimulateReservedDeviceChange(std::string new_reserved_physical_id) {
  290. switch (reserved_id_test_type()) {
  291. case ReservedIdTestType::kDefault:
  292. SimulateDeviceChange(new_reserved_physical_id, absl::nullopt);
  293. return;
  294. case ReservedIdTestType::kCommunications:
  295. SimulateDeviceChange(absl::nullopt, new_reserved_physical_id);
  296. return;
  297. }
  298. }
  299. bool audio_manager_supports_default_physical_id_ = true;
  300. bool audio_manager_supports_communications_physical_id_ = true;
  301. // Simulate the value that would be returned by
  302. // AudioManager::GetDefaultOutputDeviceId() if it is supported.
  303. std::string current_default_physical_device_id_;
  304. // Simulate the value that would be returned by
  305. // AudioManager::GetCommunicationsOutputDeviceId() if it is supported.
  306. std::string current_communications_physical_device_id_;
  307. base::test::SingleThreadTaskEnvironment task_environment_;
  308. AudioParameters default_params_;
  309. NiceMock<LocalMockAudioManager> audio_manager_;
  310. OutputDeviceMixerManager output_mixer_manager_;
  311. private:
  312. std::unique_ptr<NiceMock<MockListener>> GetListenerWithStartStopExpectations(
  313. MockOutputDeviceMixer* mixer,
  314. int starts,
  315. int stops) {
  316. auto listener = std::make_unique<NiceMock<MockListener>>();
  317. auto* listener_ptr = listener.get();
  318. EXPECT_CALL(*mixer, StartListening(listener_ptr)).Times(starts);
  319. EXPECT_CALL(*mixer, StopListening(listener_ptr)).Times(stops);
  320. return listener;
  321. }
  322. };
  323. // Makes sure we can create an output stream for the reserved output devices.
  324. TEST_P(OutputDeviceMixerManagerTest, MakeOutputStream_ForReservedDevice) {
  325. MockOutputDeviceMixer* reserved_mixer = SetUpReservedMixerCreation();
  326. MockAudioOutputStream mock_stream;
  327. EXPECT_CALL(*reserved_mixer, MakeMixableStream(ExactParams(default_params_),
  328. ValidDeviceChangeCallback()))
  329. .WillOnce(Return(&mock_stream));
  330. AudioOutputStream* out_stream = output_mixer_manager_.MakeOutputStream(
  331. reserved_device_id(), default_params_, GetNoopDeviceChangeCallback());
  332. EXPECT_EQ(&mock_stream, out_stream);
  333. }
  334. // Makes sure we can create a default output stream when AudioManager doesn't
  335. // support getting the current default ID.
  336. TEST_P(OutputDeviceMixerManagerTest,
  337. MakeOutputStream_ForReservedDevice_NoGetReservedOuputDeviceIdSupport) {
  338. SetAudioManagerReservedIdSupport(false);
  339. // Note: kReservedCommsId maps to kNormalizedDefaultDeviceId when
  340. // |!audio_manager_supports_communications_physical_id_|.
  341. MockOutputDeviceMixer* reserved_mixer =
  342. SetUpMockMixerCreation(kNormalizedDefaultDeviceId);
  343. MockAudioOutputStream mock_stream;
  344. EXPECT_CALL(*reserved_mixer, MakeMixableStream(ExactParams(default_params_),
  345. ValidDeviceChangeCallback()))
  346. .WillOnce(Return(&mock_stream));
  347. AudioOutputStream* out_stream = output_mixer_manager_.MakeOutputStream(
  348. reserved_device_id(), default_params_, GetNoopDeviceChangeCallback());
  349. EXPECT_EQ(&mock_stream, out_stream);
  350. }
  351. // Makes sure the empty string resolves to the "default" device.
  352. TEST_F(OutputDeviceMixerManagerTest,
  353. MakeOutputStream_ForDefaultDevice_EmptyDeviceId) {
  354. MockOutputDeviceMixer* default_mixer = SetUpMockMixerCreation();
  355. MockAudioOutputStream mock_stream;
  356. EXPECT_CALL(*default_mixer, MakeMixableStream(ExactParams(default_params_),
  357. ValidDeviceChangeCallback()))
  358. .WillOnce(Return(&mock_stream));
  359. // kEmptyDeviceId should be treated the same as kReservedDefaultId.
  360. AudioOutputStream* out_stream = output_mixer_manager_.MakeOutputStream(
  361. kEmptyDeviceId, default_params_, GetNoopDeviceChangeCallback());
  362. EXPECT_EQ(&mock_stream, out_stream);
  363. }
  364. // Makes sure we can create an output stream for physical IDs that match a
  365. // reserved ID's.
  366. TEST_P(OutputDeviceMixerManagerTest,
  367. MakeOutputStream_ForSpecificDeviceId_MatchesCurrentReservedId) {
  368. SetAudioManagerReservedIdSupport(true);
  369. MockOutputDeviceMixer* reserved_mixer = SetUpReservedMixerCreation();
  370. MockAudioOutputStream mock_stream;
  371. EXPECT_CALL(*reserved_mixer, MakeMixableStream(ExactParams(default_params_),
  372. ValidDeviceChangeCallback()))
  373. .WillOnce(Return(&mock_stream));
  374. // Getting a stream for current_reserved_physical_device() should create
  375. // the |reserved_mixer| instead of a mixer for that physical ID.
  376. AudioOutputStream* out_stream = output_mixer_manager_.MakeOutputStream(
  377. current_reserved_physical_device(), default_params_,
  378. GetNoopDeviceChangeCallback());
  379. EXPECT_EQ(&mock_stream, out_stream);
  380. }
  381. // Makes sure we can create an output stream for a device ID when
  382. // AudioManager::GetDefaultOutputDeviceId() is unsupported.
  383. TEST_P(OutputDeviceMixerManagerTest,
  384. MakeOutputStream_ForSpecificDeviceId_NoGetDefaultOuputDeviceIdSupport) {
  385. SetAudioManagerDefaultIdSupport(false);
  386. // A mixer for the physical device ID should be created, instead of the
  387. // default mixer.
  388. MockOutputDeviceMixer* physical_device_mixer =
  389. SetUpMockMixerCreation(current_default_physical_device());
  390. MockAudioOutputStream mock_stream;
  391. EXPECT_CALL(*physical_device_mixer,
  392. MakeMixableStream(ExactParams(default_params_),
  393. ValidDeviceChangeCallback()))
  394. .WillOnce(Return(&mock_stream));
  395. AudioOutputStream* out_stream = output_mixer_manager_.MakeOutputStream(
  396. current_default_physical_device(), default_params_,
  397. GetNoopDeviceChangeCallback());
  398. EXPECT_EQ(&mock_stream, out_stream);
  399. }
  400. // Makes sure we can create an output stream for a device ID when
  401. // AudioManager doesn't support getting the current reserved ID.
  402. TEST_P(OutputDeviceMixerManagerTest,
  403. MakeOutputStream_ForSpecificDeviceId_NoGetGetReservedIdSupport) {
  404. SetAudioManagerReservedIdSupport(false);
  405. // A mixer for the physical device ID should be created, instead of the
  406. // reserved mixer.
  407. MockOutputDeviceMixer* physical_device_mixer =
  408. SetUpMockMixerCreation(current_reserved_physical_device());
  409. MockAudioOutputStream mock_stream;
  410. EXPECT_CALL(*physical_device_mixer,
  411. MakeMixableStream(ExactParams(default_params_),
  412. ValidDeviceChangeCallback()))
  413. .WillOnce(Return(&mock_stream));
  414. AudioOutputStream* out_stream = output_mixer_manager_.MakeOutputStream(
  415. current_reserved_physical_device(), default_params_,
  416. GetNoopDeviceChangeCallback());
  417. EXPECT_EQ(&mock_stream, out_stream);
  418. }
  419. // Makes sure we can create an output stream a device ID for a device that is
  420. // not any device.
  421. TEST_F(OutputDeviceMixerManagerTest,
  422. MakeOutputStream_ForSpecificDeviceId_IdDoesntMatchReservedIds) {
  423. ASSERT_NE(kOtherFakeDeviceId, current_default_physical_device());
  424. ASSERT_NE(kOtherFakeDeviceId, current_communications_physical_device());
  425. MockOutputDeviceMixer* mock_mixer =
  426. SetUpMockMixerCreation(kOtherFakeDeviceId);
  427. MockAudioOutputStream mock_stream;
  428. EXPECT_CALL(*mock_mixer, MakeMixableStream(ExactParams(default_params_),
  429. ValidDeviceChangeCallback()))
  430. .WillOnce(Return(&mock_stream));
  431. AudioOutputStream* out_stream = output_mixer_manager_.MakeOutputStream(
  432. kOtherFakeDeviceId, default_params_, GetNoopDeviceChangeCallback());
  433. EXPECT_EQ(&mock_stream, out_stream);
  434. }
  435. // Makes sure we get the correct output parameters from the AudioManager when
  436. // creating streams.
  437. TEST_F(OutputDeviceMixerManagerTest,
  438. MakeOutputStream_GetsDeviceOrDefaultParams) {
  439. // Reset default test setup expectations.
  440. testing::Mock::VerifyAndClearExpectations(&audio_manager_);
  441. SetUpMockMixerCreation();
  442. EXPECT_CALL(audio_manager_, GetOutputStreamParameters(_)).Times(0);
  443. EXPECT_CALL(audio_manager_, GetDefaultOutputStreamParameters())
  444. .WillOnce(Return(default_params_));
  445. output_mixer_manager_.MakeOutputStream(kReservedDefaultId, default_params_,
  446. GetNoopDeviceChangeCallback());
  447. testing::Mock::VerifyAndClearExpectations(this);
  448. testing::Mock::VerifyAndClearExpectations(&audio_manager_);
  449. SetUpMockMixerCreation(kOtherFakeDeviceId);
  450. EXPECT_CALL(audio_manager_, GetDefaultOutputStreamParameters()).Times(0);
  451. EXPECT_CALL(audio_manager_, GetOutputStreamParameters(kOtherFakeDeviceId))
  452. .WillOnce(Return(default_params_));
  453. output_mixer_manager_.MakeOutputStream(kOtherFakeDeviceId, default_params_,
  454. GetNoopDeviceChangeCallback());
  455. }
  456. // Makes sure we still get an unmixable stream when requesting bitstream
  457. // formats.
  458. TEST_F(OutputDeviceMixerManagerTest, MakeOutputStream_WithBitstreamFormat) {
  459. ExpectNoMixerCreated();
  460. MockAudioOutputStream mock_stream;
  461. EXPECT_CALL(audio_manager_, MakeAudioOutputStreamProxy(_, _))
  462. .WillOnce(Return(&mock_stream));
  463. AudioParameters bitstream_params{AudioParameters::Format::AUDIO_BITSTREAM_AC3,
  464. media::ChannelLayout::CHANNEL_LAYOUT_STEREO,
  465. /*sample_rate=*/8000,
  466. /*frames_per_buffer=*/800};
  467. AudioOutputStream* out_stream = output_mixer_manager_.MakeOutputStream(
  468. kOtherFakeDeviceId, bitstream_params, GetNoopDeviceChangeCallback());
  469. EXPECT_TRUE(out_stream);
  470. // Test cleanup.
  471. out_stream->Close();
  472. }
  473. // Makes sure we still get an unmixable stream if device info is stale and
  474. // AudioManager::GetOutputStreamParameters() returns invalid parameters.
  475. TEST_F(OutputDeviceMixerManagerTest, MakeOutputStream_WithStaleDeviceInfo) {
  476. EXPECT_CALL(audio_manager_, GetDefaultOutputStreamParameters()).Times(0);
  477. // Return invalid parameters, which should fail mixer creation.
  478. EXPECT_CALL(audio_manager_, GetOutputStreamParameters(kOtherFakeDeviceId))
  479. .WillOnce(Return(media::AudioParameters()));
  480. ExpectNoMixerCreated();
  481. MockAudioOutputStream mock_stream;
  482. EXPECT_CALL(audio_manager_, MakeAudioOutputStreamProxy(_, _))
  483. .WillOnce(Return(&mock_stream));
  484. AudioOutputStream* out_stream = output_mixer_manager_.MakeOutputStream(
  485. kOtherFakeDeviceId, default_params_, GetNoopDeviceChangeCallback());
  486. EXPECT_TRUE(out_stream);
  487. // Test cleanup.
  488. out_stream->Close();
  489. }
  490. // Makes sure we handle running out of stream proxies.
  491. TEST_F(OutputDeviceMixerManagerTest, MakeOutputStream_MaxProxies) {
  492. ExpectNoMixerCreated();
  493. EXPECT_CALL(audio_manager_, MakeAudioOutputStreamProxy(_, _))
  494. .WillOnce(Return(nullptr));
  495. // We use bitstream parameters to simplify hitting a portion of the code that
  496. // creates an AudioOutputStream directly.
  497. AudioParameters bitstream_params{AudioParameters::Format::AUDIO_BITSTREAM_AC3,
  498. media::ChannelLayout::CHANNEL_LAYOUT_STEREO,
  499. /*sample_rate=*/8000,
  500. /*frames_per_buffer=*/800};
  501. AudioOutputStream* out_stream = output_mixer_manager_.MakeOutputStream(
  502. kOtherFakeDeviceId, bitstream_params, GetNoopDeviceChangeCallback());
  503. EXPECT_FALSE(out_stream);
  504. }
  505. // Makes sure we handle failing to create a mixer.
  506. TEST_F(OutputDeviceMixerManagerTest, MakeOutputStream_MixerCreationFails) {
  507. EXPECT_CALL(*this, CreateOutputDeviceMixerCalled(
  508. kNormalizedDefaultDeviceId,
  509. CompatibleParams(default_params_), _, _))
  510. .WillOnce(Return(ByMove(nullptr)));
  511. AudioOutputStream* out_stream = output_mixer_manager_.MakeOutputStream(
  512. kReservedDefaultId, default_params_, GetNoopDeviceChangeCallback());
  513. EXPECT_FALSE(out_stream);
  514. }
  515. // Makes sure we handle the case when the output mixer returns a nullptr when
  516. // creating a stream.
  517. TEST_F(OutputDeviceMixerManagerTest, MakeOutputStream_MixerReturnsNull) {
  518. MockOutputDeviceMixer* default_mixer = SetUpMockMixerCreation();
  519. EXPECT_CALL(*default_mixer, MakeMixableStream(ExactParams(default_params_),
  520. ValidDeviceChangeCallback()))
  521. .WillOnce(Return(nullptr));
  522. AudioOutputStream* out_stream = output_mixer_manager_.MakeOutputStream(
  523. kReservedDefaultId, default_params_, GetNoopDeviceChangeCallback());
  524. EXPECT_FALSE(out_stream);
  525. }
  526. // Makes sure creating multiple output streams for the same device ID re-uses
  527. // the same OutputDeviceMixer.
  528. TEST_F(OutputDeviceMixerManagerTest, MakeOutputStream_OneMixerPerId) {
  529. MockOutputDeviceMixer* physical_id_mixer =
  530. SetUpMockMixerCreation(kOtherFakeDeviceId);
  531. MockAudioOutputStream mock_stream_a;
  532. MockAudioOutputStream mock_stream_b;
  533. EXPECT_CALL(*physical_id_mixer,
  534. MakeMixableStream(ExactParams(default_params_),
  535. ValidDeviceChangeCallback()))
  536. .WillOnce(Return(&mock_stream_b))
  537. .WillOnce(Return(&mock_stream_a));
  538. // This call should create an OutputDeviceMixer.
  539. AudioOutputStream* out_stream_a = output_mixer_manager_.MakeOutputStream(
  540. kOtherFakeDeviceId, default_params_, GetNoopDeviceChangeCallback());
  541. // This call should re-use the OutputDeviceMixer.
  542. AudioOutputStream* out_stream_b = output_mixer_manager_.MakeOutputStream(
  543. kOtherFakeDeviceId, default_params_, GetNoopDeviceChangeCallback());
  544. EXPECT_NE(out_stream_a, out_stream_b);
  545. }
  546. // Makes sure creating an output stream for a "reserved ID" or the
  547. // "current reserved device ID" is equivalent, and the mixer is shared.
  548. TEST_P(OutputDeviceMixerManagerTest,
  549. MakeOutputStream_ReservedIdAndCurrentReservedDeviceIdShareOneMixer) {
  550. MockOutputDeviceMixer* special_mixer = SetUpReservedMixerCreation();
  551. MockAudioOutputStream mock_stream_a;
  552. MockAudioOutputStream mock_stream_b;
  553. EXPECT_CALL(*special_mixer, MakeMixableStream(ExactParams(default_params_),
  554. ValidDeviceChangeCallback()))
  555. .WillOnce(Return(&mock_stream_b))
  556. .WillOnce(Return(&mock_stream_a));
  557. // This call should create an OutputDeviceMixer.
  558. AudioOutputStream* out_stream_a = output_mixer_manager_.MakeOutputStream(
  559. current_reserved_physical_device(), default_params_,
  560. GetNoopDeviceChangeCallback());
  561. // This call should re-use the same OutputDeviceMixer.
  562. AudioOutputStream* out_stream_b = output_mixer_manager_.MakeOutputStream(
  563. reserved_device_id(), default_params_, GetNoopDeviceChangeCallback());
  564. EXPECT_NE(out_stream_a, out_stream_b);
  565. }
  566. // Makes sure we create one output mixer per device ID.
  567. TEST_F(OutputDeviceMixerManagerTest, MakeOutputStream_TwoDevicesTwoMixers) {
  568. SetAudioManagerDefaultIdSupport(false);
  569. SetAudioManagerCommunicationsIdSupport(false);
  570. InSequence s;
  571. MockOutputDeviceMixer* mock_mixer_a = SetUpMockMixerCreation(kFakeDeviceId);
  572. MockAudioOutputStream mock_stream_a;
  573. EXPECT_CALL(*mock_mixer_a, MakeMixableStream(ExactParams(default_params_),
  574. ValidDeviceChangeCallback()))
  575. .WillOnce(Return(&mock_stream_a));
  576. MockOutputDeviceMixer* mock_mixer_b =
  577. SetUpMockMixerCreation(kOtherFakeDeviceId);
  578. MockAudioOutputStream mock_stream_b;
  579. EXPECT_CALL(*mock_mixer_b, MakeMixableStream(ExactParams(default_params_),
  580. ValidDeviceChangeCallback()))
  581. .WillOnce(Return(&mock_stream_b));
  582. // Create the first OutputDeviceMixer.
  583. AudioOutputStream* out_stream_a = output_mixer_manager_.MakeOutputStream(
  584. kFakeDeviceId, default_params_, GetNoopDeviceChangeCallback());
  585. // Create a second OutputDeviceMixer.
  586. AudioOutputStream* out_stream_b = output_mixer_manager_.MakeOutputStream(
  587. kOtherFakeDeviceId, default_params_, GetNoopDeviceChangeCallback());
  588. EXPECT_NE(out_stream_a, out_stream_b);
  589. }
  590. // Makes sure the default mixer is separate from other mixers.
  591. TEST_F(OutputDeviceMixerManagerTest,
  592. MakeOutputStream_DefaultMixerDistinctFromOtherMixers) {
  593. SetAudioManagerDefaultIdSupport(false);
  594. InSequence s;
  595. MockOutputDeviceMixer* fake_device_mixer =
  596. SetUpMockMixerCreation(kFakeDeviceId);
  597. MockAudioOutputStream fake_stream;
  598. EXPECT_CALL(*fake_device_mixer,
  599. MakeMixableStream(ExactParams(default_params_),
  600. ValidDeviceChangeCallback()))
  601. .WillOnce(Return(&fake_stream));
  602. MockOutputDeviceMixer* default_mixer =
  603. SetUpMockMixerCreation(kNormalizedDefaultDeviceId);
  604. MockAudioOutputStream default_stream;
  605. EXPECT_CALL(*default_mixer, MakeMixableStream(ExactParams(default_params_),
  606. ValidDeviceChangeCallback()))
  607. .WillOnce(Return(&default_stream));
  608. // Create the first OutputDeviceMixer.
  609. AudioOutputStream* out_stream_a = output_mixer_manager_.MakeOutputStream(
  610. kFakeDeviceId, default_params_, GetNoopDeviceChangeCallback());
  611. // Create a second OutputDeviceMixer.
  612. AudioOutputStream* out_stream_b = output_mixer_manager_.MakeOutputStream(
  613. kReservedDefaultId, default_params_, GetNoopDeviceChangeCallback());
  614. EXPECT_NE(out_stream_a, out_stream_b);
  615. }
  616. // Makes sure the communications mixer is separate from other mixers.
  617. TEST_F(OutputDeviceMixerManagerTest,
  618. MakeOutputStream_CommunicationsMixerDistinctFromOtherMixers) {
  619. SetAudioManagerCommunicationsIdSupport(false);
  620. InSequence s;
  621. MockOutputDeviceMixer* fake_device_mixer =
  622. SetUpMockMixerCreation(kFakeCommunicationsId);
  623. MockAudioOutputStream fake_stream;
  624. EXPECT_CALL(*fake_device_mixer,
  625. MakeMixableStream(ExactParams(default_params_),
  626. ValidDeviceChangeCallback()))
  627. .WillOnce(Return(&fake_stream));
  628. // Note: kReservedCommsId maps to kNormalizedDefaultDeviceId when
  629. // |!audio_manager_supports_communications_physical_id_|.
  630. MockOutputDeviceMixer* default_mixer =
  631. SetUpMockMixerCreation(kNormalizedDefaultDeviceId);
  632. MockAudioOutputStream default_stream;
  633. EXPECT_CALL(*default_mixer, MakeMixableStream(ExactParams(default_params_),
  634. ValidDeviceChangeCallback()))
  635. .WillOnce(Return(&default_stream));
  636. // Create the first OutputDeviceMixer.
  637. AudioOutputStream* out_stream_a = output_mixer_manager_.MakeOutputStream(
  638. kFakeCommunicationsId, default_params_, GetNoopDeviceChangeCallback());
  639. // Create a second OutputDeviceMixer.
  640. AudioOutputStream* out_stream_b = output_mixer_manager_.MakeOutputStream(
  641. kReservedCommsId, default_params_, GetNoopDeviceChangeCallback());
  642. EXPECT_NE(out_stream_a, out_stream_b);
  643. }
  644. // Makes sure we get the latest reserved device ID each time we create a stream
  645. // for a reserved ID.
  646. TEST_P(OutputDeviceMixerManagerTest,
  647. MakeOutputStream_CurrentReseredIdIsUpdatedAfterDeviceChange) {
  648. SetAudioManagerReservedIdSupport(true);
  649. MockOutputDeviceMixer* reserved_mixer_a = SetUpReservedMixerCreation();
  650. MockAudioOutputStream reserved_stream_a;
  651. EXPECT_CALL(*reserved_mixer_a, MakeMixableStream(ExactParams(default_params_),
  652. ValidDeviceChangeCallback()))
  653. .WillOnce(Return(&reserved_stream_a));
  654. // Force the creation of |reserved_mixer_a|.
  655. AudioOutputStream* out_stream_a = output_mixer_manager_.MakeOutputStream(
  656. current_reserved_physical_device(), default_params_,
  657. GetNoopDeviceChangeCallback());
  658. // Update the current default physical device.
  659. ASSERT_NE(current_reserved_physical_device(), kOtherFakeDeviceId);
  660. SimulateReservedDeviceChange(kOtherFakeDeviceId);
  661. ASSERT_EQ(current_reserved_physical_device(), kOtherFakeDeviceId);
  662. testing::Mock::VerifyAndClearExpectations(this);
  663. MockOutputDeviceMixer* reserved_mixer_b = SetUpReservedMixerCreation();
  664. MockAudioOutputStream reseved_stream_b;
  665. EXPECT_CALL(*reserved_mixer_b, MakeMixableStream(ExactParams(default_params_),
  666. ValidDeviceChangeCallback()))
  667. .WillOnce(Return(&reseved_stream_b));
  668. // Force the creation of |reserved_mixer_b|, with a new
  669. // current_reserved_physical_device().
  670. AudioOutputStream* out_stream_b = output_mixer_manager_.MakeOutputStream(
  671. current_reserved_physical_device(), default_params_,
  672. GetNoopDeviceChangeCallback());
  673. EXPECT_NE(out_stream_a, out_stream_b);
  674. }
  675. // Makes sure OutputDeviceMixers are notified of device changes.
  676. TEST_P(OutputDeviceMixerManagerTest,
  677. OnDeviceChange_MixersReceiveDeviceChanges) {
  678. SetAudioManagerReservedIdSupport(false);
  679. // We don't care about the streams these devices will create.
  680. InSequence s;
  681. MockOutputDeviceMixer* pre_mock_mixer_a =
  682. SetUpMockMixer_NoStreams(current_reserved_physical_device());
  683. MockOutputDeviceMixer* pre_mock_mixer_b =
  684. SetUpMockMixer_NoStreams(kOtherFakeDeviceId);
  685. // Note: kReservedCommsId maps to kNormalizedDefaultDeviceId when
  686. // |!audio_manager_supports_communications_physical_id_|.
  687. MockOutputDeviceMixer* pre_mock_mixer_c =
  688. SetUpMockMixer_NoStreams(kNormalizedDefaultDeviceId);
  689. EXPECT_CALL(*pre_mock_mixer_a, ProcessDeviceChange()).Times(1);
  690. EXPECT_CALL(*pre_mock_mixer_b, ProcessDeviceChange()).Times(1);
  691. EXPECT_CALL(*pre_mock_mixer_c, ProcessDeviceChange()).Times(1);
  692. // Create the OutputDeviceMixers.
  693. output_mixer_manager_.MakeOutputStream(current_reserved_physical_device(),
  694. default_params_,
  695. GetNoopDeviceChangeCallback());
  696. output_mixer_manager_.MakeOutputStream(kOtherFakeDeviceId, default_params_,
  697. GetNoopDeviceChangeCallback());
  698. output_mixer_manager_.MakeOutputStream(reserved_device_id(), default_params_,
  699. GetNoopDeviceChangeCallback());
  700. // Trigger the calls to ProcessDeviceChange()
  701. SimulateDeviceChange();
  702. }
  703. // Makes sure OnDeviceChange() is only called once per device change.
  704. TEST_F(OutputDeviceMixerManagerTest, OnDeviceChange_OncePerDeviceChange) {
  705. // Setup a mixer that expects exactly 1 device change.
  706. MockOutputDeviceMixer* default_mixer = SetUpMockMixer_NoStreams();
  707. EXPECT_CALL(*default_mixer, ProcessDeviceChange()).Times(1);
  708. // Create the mixer.
  709. ForceOutputMixerCreation(kReservedDefaultId);
  710. auto first_device_change_callback = GetOnDeviceChangeCallback();
  711. auto second_device_change_callback = GetOnDeviceChangeCallback();
  712. // |default_mixer| be notified of the device change.
  713. std::move(first_device_change_callback).Run();
  714. testing::Mock::VerifyAndClearExpectations(default_mixer);
  715. // Setup a new mixer.
  716. testing::Mock::VerifyAndClearExpectations(this);
  717. MockOutputDeviceMixer* new_default_mixer = SetUpMockMixer_NoStreams();
  718. // Make sure old callbacks don't trigger new device change events.
  719. EXPECT_CALL(*new_default_mixer, ProcessDeviceChange()).Times(0);
  720. ForceOutputMixerCreation(kReservedDefaultId);
  721. std::move(second_device_change_callback).Run();
  722. testing::Mock::VerifyAndClearExpectations(new_default_mixer);
  723. // Make sure the new mixer gets notified of changes through this new
  724. // callback.
  725. EXPECT_CALL(*new_default_mixer, ProcessDeviceChange()).Times(1);
  726. GetOnDeviceChangeCallback().Run();
  727. }
  728. // Attach/detach listeners with no mixer.
  729. TEST_F(OutputDeviceMixerManagerTest, DeviceOutputListener_StartStop) {
  730. ExpectNoMixerCreated();
  731. StrictMock<MockListener> listener;
  732. // Attach/detach multiple listeners to/from multiple devices.
  733. output_mixer_manager_.StartListening(&listener, kFakeDeviceId);
  734. output_mixer_manager_.StopListening(&listener);
  735. }
  736. // Attach/detach listeners to multiple devices with no mixers.
  737. TEST_F(OutputDeviceMixerManagerTest,
  738. DeviceOutputListener_StartStop_MultipleDevice) {
  739. ExpectNoMixerCreated();
  740. StrictMock<MockListener> listener_a;
  741. StrictMock<MockListener> listener_b;
  742. output_mixer_manager_.StartListening(&listener_a, kFakeDeviceId);
  743. output_mixer_manager_.StartListening(&listener_b, kOtherFakeDeviceId);
  744. output_mixer_manager_.StopListening(&listener_a);
  745. output_mixer_manager_.StopListening(&listener_b);
  746. }
  747. // Attach/detach multiple listeners to a single device with no mixer.
  748. TEST_F(OutputDeviceMixerManagerTest,
  749. DeviceOutputListener_StartStop_MultipleListener) {
  750. ExpectNoMixerCreated();
  751. StrictMock<MockListener> listener_a;
  752. StrictMock<MockListener> listener_b;
  753. output_mixer_manager_.StartListening(&listener_a, kFakeDeviceId);
  754. output_mixer_manager_.StartListening(&listener_b, kFakeDeviceId);
  755. output_mixer_manager_.StopListening(&listener_a);
  756. output_mixer_manager_.StopListening(&listener_b);
  757. }
  758. // Attach/detach to the reserved device.
  759. TEST_P(OutputDeviceMixerManagerTest,
  760. DeviceOutputListener_StartStop_ReservedId) {
  761. ExpectNoMixerCreated();
  762. StrictMock<MockListener> listener;
  763. output_mixer_manager_.StartListening(&listener, reserved_device_id());
  764. output_mixer_manager_.StopListening(&listener);
  765. }
  766. // Listeners are attached as they are added.
  767. TEST_F(OutputDeviceMixerManagerTest, DeviceOutputListener_CreateStartStop) {
  768. MockOutputDeviceMixer* mixer = SetUpMockMixer_NoStreams(kOtherFakeDeviceId);
  769. auto listener = GetListener_MixerExpectsStartStop(mixer);
  770. ForceOutputMixerCreation(kOtherFakeDeviceId);
  771. output_mixer_manager_.StartListening(listener.get(), kOtherFakeDeviceId);
  772. output_mixer_manager_.StopListening(listener.get());
  773. }
  774. // Listeners are attached on mixer creation.
  775. TEST_F(OutputDeviceMixerManagerTest, DeviceOutputListener_StartCreateStop) {
  776. MockOutputDeviceMixer* mixer = SetUpMockMixer_NoStreams(kOtherFakeDeviceId);
  777. auto listener = GetListener_MixerExpectsStartStop(mixer);
  778. output_mixer_manager_.StartListening(listener.get(), kOtherFakeDeviceId);
  779. ForceOutputMixerCreation(kOtherFakeDeviceId);
  780. output_mixer_manager_.StopListening(listener.get());
  781. }
  782. // Removed listeners are not attached.
  783. TEST_F(OutputDeviceMixerManagerTest, DeviceOutputListener_StartStopCreate) {
  784. MockOutputDeviceMixer* mixer = SetUpMockMixer_NoStreams(kOtherFakeDeviceId);
  785. auto listener = GetListener_MixerExpectsNoCalls(mixer);
  786. output_mixer_manager_.StartListening(listener.get(), kOtherFakeDeviceId);
  787. output_mixer_manager_.StopListening(listener.get());
  788. ForceOutputMixerCreation(kOtherFakeDeviceId);
  789. }
  790. // Listeners are attached as they are added.
  791. TEST_P(OutputDeviceMixerManagerTest,
  792. DeviceOutputListener_CreateStartStop_NoGetReservedIdSupport) {
  793. SetAudioManagerReservedIdSupport(false);
  794. MockOutputDeviceMixer* mixer =
  795. SetUpMockMixer_NoStreams(current_reserved_physical_device());
  796. auto listener = GetListener_MixerExpectsStartStop(mixer);
  797. ForceOutputMixerCreation(current_reserved_physical_device());
  798. output_mixer_manager_.StartListening(listener.get(),
  799. current_reserved_physical_device());
  800. output_mixer_manager_.StopListening(listener.get());
  801. }
  802. // Listeners are attached on mixer creation.
  803. TEST_P(OutputDeviceMixerManagerTest,
  804. DeviceOutputListener_StartCreateStop_NoGetReservedIdSupport) {
  805. SetAudioManagerReservedIdSupport(false);
  806. MockOutputDeviceMixer* mixer =
  807. SetUpMockMixer_NoStreams(current_reserved_physical_device());
  808. auto listener = GetListener_MixerExpectsStartStop(mixer);
  809. output_mixer_manager_.StartListening(listener.get(),
  810. current_reserved_physical_device());
  811. ForceOutputMixerCreation(current_reserved_physical_device());
  812. output_mixer_manager_.StopListening(listener.get());
  813. }
  814. // Removed listeners are not attached.
  815. TEST_P(OutputDeviceMixerManagerTest,
  816. DeviceOutputListener_StartStopCreate_NoGetReservedIdSupport) {
  817. SetAudioManagerReservedIdSupport(false);
  818. MockOutputDeviceMixer* mixer =
  819. SetUpMockMixer_NoStreams(current_reserved_physical_device());
  820. auto listener = GetListener_MixerExpectsNoCalls(mixer);
  821. output_mixer_manager_.StartListening(listener.get(),
  822. current_reserved_physical_device());
  823. output_mixer_manager_.StopListening(listener.get());
  824. ForceOutputMixerCreation(current_reserved_physical_device());
  825. }
  826. // Removed listeners are not attached, and remaining listeners are.
  827. TEST_F(OutputDeviceMixerManagerTest,
  828. DeviceOutputListener_StartStopCreate_TwoListeners) {
  829. MockOutputDeviceMixer* default_mixer = SetUpMockMixer_NoStreams();
  830. auto listener = GetListener_MixerExpectsStart(default_mixer);
  831. auto removed_listener = GetListener_MixerExpectsNoCalls(default_mixer);
  832. output_mixer_manager_.StartListening(listener.get(),
  833. current_default_physical_device());
  834. output_mixer_manager_.StartListening(removed_listener.get(),
  835. current_default_physical_device());
  836. output_mixer_manager_.StopListening(removed_listener.get());
  837. ForceOutputMixerCreation(current_default_physical_device());
  838. }
  839. TEST_P(OutputDeviceMixerManagerTest,
  840. DeviceOutputListener_CreateStartStop_ReservedId) {
  841. MockOutputDeviceMixer* reserved_mixer = SetUpReservedMixer_NoStreams();
  842. auto listener = GetListener_MixerExpectsStartStop(reserved_mixer);
  843. ForceOutputMixerCreation(reserved_device_id());
  844. output_mixer_manager_.StartListening(listener.get(), reserved_device_id());
  845. output_mixer_manager_.StopListening(listener.get());
  846. }
  847. TEST_P(OutputDeviceMixerManagerTest,
  848. DeviceOutputListener_StartCreateStop_ReservedId) {
  849. MockOutputDeviceMixer* reserved_mixer = SetUpReservedMixer_NoStreams();
  850. auto listener = GetListener_MixerExpectsStartStop(reserved_mixer);
  851. output_mixer_manager_.StartListening(listener.get(), reserved_device_id());
  852. ForceOutputMixerCreation(reserved_device_id());
  853. output_mixer_manager_.StopListening(listener.get());
  854. }
  855. TEST_P(OutputDeviceMixerManagerTest,
  856. DeviceOutputListener_StartStopCreate_ReservedId) {
  857. MockOutputDeviceMixer* reserved_mixer = SetUpReservedMixer_NoStreams();
  858. auto listener = GetListener_MixerExpectsNoCalls(reserved_mixer);
  859. output_mixer_manager_.StartListening(listener.get(), reserved_device_id());
  860. output_mixer_manager_.StopListening(listener.get());
  861. ForceOutputMixerCreation(reserved_device_id());
  862. }
  863. TEST_F(OutputDeviceMixerManagerTest,
  864. DeviceOutputListener_StartCreateStop_DefaultId_EmptyDeviceId) {
  865. MockOutputDeviceMixer* default_mixer = SetUpMockMixer_NoStreams();
  866. auto listener = GetListener_MixerExpectsStartStop(default_mixer);
  867. // kEmptyDeviceId should be treated the same as kReservedDefaultId.
  868. output_mixer_manager_.StartListening(listener.get(), kEmptyDeviceId);
  869. ForceOutputMixerCreation(kEmptyDeviceId);
  870. output_mixer_manager_.StopListening(listener.get());
  871. }
  872. // Makes sure reserved-listeners are attached to the reserved-mixer when it is
  873. // created via current_reserved_physical_device().
  874. TEST_P(OutputDeviceMixerManagerTest,
  875. DeviceOutputListener_ReservedListenersAttachToCurrentReservedIdMixer) {
  876. MockOutputDeviceMixer* reserved_mixer = SetUpReservedMixer_NoStreams();
  877. auto listener = GetListener_MixerExpectsStartStop(reserved_mixer);
  878. output_mixer_manager_.StartListening(listener.get(), reserved_device_id());
  879. ForceOutputMixerCreation(current_reserved_physical_device());
  880. output_mixer_manager_.StopListening(listener.get());
  881. }
  882. // Makes sure current_reserved_physical_device() listeners are attached when the
  883. // reserved-mixer is created.
  884. TEST_P(OutputDeviceMixerManagerTest,
  885. DeviceOutputListener_CurrentReservedIdListenersAttachToReservedMixer) {
  886. MockOutputDeviceMixer* reserved_mixer = SetUpReservedMixer_NoStreams();
  887. auto listener = GetListener_MixerExpectsStartStop(reserved_mixer);
  888. output_mixer_manager_.StartListening(listener.get(),
  889. current_reserved_physical_device());
  890. ForceOutputMixerCreation(reserved_device_id());
  891. output_mixer_manager_.StopListening(listener.get());
  892. }
  893. // Makes sure the presence of listeners does not force device recreation
  894. // on device change.
  895. TEST_F(OutputDeviceMixerManagerTest,
  896. DeviceOutputListener_NoCreateAfterDeviceChange_WithListeners) {
  897. MockOutputDeviceMixer* mixer = SetUpMockMixer_NoStreams(kOtherFakeDeviceId);
  898. // |mixer| should never get a call to StopListening(|listener|).
  899. auto listener = GetListener_MixerExpectsStart(mixer);
  900. ForceOutputMixerCreation(kOtherFakeDeviceId);
  901. output_mixer_manager_.StartListening(listener.get(), kOtherFakeDeviceId);
  902. SimulateDeviceChange();
  903. output_mixer_manager_.StopListening(listener.get());
  904. }
  905. // Makes sure listeners are re-attached when mixers are recreated.
  906. TEST_F(OutputDeviceMixerManagerTest,
  907. DeviceOutputListener_ListenersReattachedAfterDeviceChange) {
  908. MockOutputDeviceMixer* mixer = SetUpMockMixer_NoStreams(kOtherFakeDeviceId);
  909. // |mixer| should never get a call to StopListening(|listener|).
  910. auto listener = GetListener_MixerExpectsStart(mixer);
  911. ForceOutputMixerCreation(kOtherFakeDeviceId);
  912. output_mixer_manager_.StartListening(listener.get(), kOtherFakeDeviceId);
  913. SimulateDeviceChange();
  914. // Clear expectations so we can set up new ones.
  915. testing::Mock::VerifyAndClearExpectations(this);
  916. testing::Mock::VerifyAndClearExpectations(listener.get());
  917. // The same |listener| should be started when |new_mixer| is created.
  918. MockOutputDeviceMixer* new_mixer =
  919. SetUpMockMixer_NoStreams(kOtherFakeDeviceId);
  920. EXPECT_CALL(*new_mixer, StartListening(listener.get())).Times(1);
  921. ForceOutputMixerCreation(kOtherFakeDeviceId);
  922. }
  923. // Makes sure the reserved listeners are re-attached when mixers are
  924. // re-created.
  925. TEST_P(OutputDeviceMixerManagerTest,
  926. DeviceOutputListener_ReservedIdListenersReattachedAfterDeviceChange) {
  927. SetAudioManagerReservedIdSupport(true);
  928. MockOutputDeviceMixer* reserved_mixer = SetUpReservedMixer_NoStreams();
  929. auto listener = GetListener_MixerExpectsStart(reserved_mixer);
  930. output_mixer_manager_.StartListening(listener.get(), reserved_device_id());
  931. // |listener| will be started when |reserved_mixer| is created.
  932. ForceOutputMixerCreation(current_reserved_physical_device());
  933. // Make sure the AudioManager::GetDefaultOutputDeviceId() returns a new value.
  934. ASSERT_NE(current_reserved_physical_device(), kOtherFakeDeviceId);
  935. SimulateReservedDeviceChange(kOtherFakeDeviceId);
  936. testing::Mock::VerifyAndClearExpectations(this);
  937. testing::Mock::VerifyAndClearExpectations(listener.get());
  938. // |listener| should be attached to |new_default_mixer| when it is created.
  939. MockOutputDeviceMixer* new_reserved_mixer = SetUpReservedMixer_NoStreams();
  940. EXPECT_CALL(*new_reserved_mixer, StartListening(listener.get())).Times(1);
  941. ASSERT_EQ(kOtherFakeDeviceId, current_reserved_physical_device());
  942. ForceOutputMixerCreation(kOtherFakeDeviceId);
  943. }
  944. // Makes sure the reserved listeners are not attached to non-reserved listeners,
  945. // if support for AudioManager's GetDefaultOutputDeviceID() or
  946. // GetCommunicationsOutputDeviceID() changes.
  947. TEST_P(OutputDeviceMixerManagerTest,
  948. DeviceOutputListener_CurrentDefaultListenersNotReattached) {
  949. SetAudioManagerReservedIdSupport(true);
  950. MockOutputDeviceMixer* reserved_mixer = SetUpReservedMixer_NoStreams();
  951. // |reserved_mixer| should never get a call to StopListening(|listener|).
  952. auto listener = GetListener_MixerExpectsStart(reserved_mixer);
  953. output_mixer_manager_.StartListening(listener.get(),
  954. current_reserved_physical_device());
  955. // |listener| should be attached to |mixer|.
  956. ForceOutputMixerCreation(reserved_device_id());
  957. SetAudioManagerReservedIdSupport(false);
  958. SimulateDeviceChange();
  959. testing::Mock::VerifyAndClearExpectations(this);
  960. testing::Mock::VerifyAndClearExpectations(listener.get());
  961. // Now that AudioManager::GetDefaultOutputDeviceID() or
  962. // AudioManager::GetCommunicationsOutputDeviceID() only returns
  963. // kEmptyDeviceId, |listener| should not be attached to |new_reserved_mixer|.
  964. // Note: kReservedCommsId maps to kNormalizedDefaultDeviceId when
  965. // |!audio_manager_supports_communications_physical_id_|.
  966. MockOutputDeviceMixer* new_reserved_mixer =
  967. SetUpMockMixer_NoStreams(kNormalizedDefaultDeviceId);
  968. EXPECT_CALL(*new_reserved_mixer, StartListening(listener.get())).Times(0);
  969. ForceOutputMixerCreation(reserved_device_id());
  970. testing::Mock::VerifyAndClearExpectations(this);
  971. testing::Mock::VerifyAndClearExpectations(listener.get());
  972. // |listener| should still be attached to |new_physical_mixer| when it's
  973. // created after a device change.
  974. MockOutputDeviceMixer* new_physical_mixer =
  975. SetUpMockMixer_NoStreams(current_reserved_physical_device());
  976. EXPECT_CALL(*new_physical_mixer, StartListening(listener.get())).Times(1);
  977. // |listener| should be attached to |new_physical_mixer|.
  978. ForceOutputMixerCreation(current_reserved_physical_device());
  979. }
  980. // Makes sure both "default listeners" and "current_reserved_physical_device()
  981. // listeners" get attached to the same current_reserved_physical_device() mixer.
  982. TEST_P(OutputDeviceMixerManagerTest,
  983. DeviceOutputListener_CurrentDefaultMixerCreation_ListenersAttached) {
  984. MockOutputDeviceMixer* reserved_mixer = SetUpReservedMixer_NoStreams();
  985. // Create listeners for reserved_device_id() and
  986. // current_reserved_physical_device(), BOTH listening to |reserved_mixer|.
  987. auto reserved_listener = GetListener_MixerExpectsStart(reserved_mixer);
  988. auto current_reserved_physical_listener =
  989. GetListener_MixerExpectsStart(reserved_mixer);
  990. // Create another listener, NOT listening to |reserved_mixer|.
  991. ASSERT_NE(kOtherFakeDeviceId, current_reserved_physical_device());
  992. auto other_listener = GetListener_MixerExpectsNoCalls(reserved_mixer);
  993. // Start all listeners.
  994. output_mixer_manager_.StartListening(reserved_listener.get(),
  995. reserved_device_id());
  996. output_mixer_manager_.StartListening(current_reserved_physical_listener.get(),
  997. current_reserved_physical_device());
  998. output_mixer_manager_.StartListening(other_listener.get(),
  999. kOtherFakeDeviceId);
  1000. // |default_listener| and |current_default_physical_listener| should be
  1001. // attached to |default_mixer|.
  1002. ForceOutputMixerCreation(current_reserved_physical_device());
  1003. }
  1004. // Makes sure both "reserved listeners" and "current_reserve_physical_device()
  1005. // listeners" get attached to the same reserved mixer.
  1006. TEST_P(OutputDeviceMixerManagerTest,
  1007. DeviceOutputListener_ReservedIdMixerCreation_ListenersAttached) {
  1008. MockOutputDeviceMixer* reserved_mixer = SetUpReservedMixer_NoStreams();
  1009. // Create listeners for reserved_device_id() and
  1010. // current_reserved_physical_device(), BOTH listening to |reserved_mixer|.
  1011. auto reserved_listener = GetListener_MixerExpectsStart(reserved_mixer);
  1012. auto current_reserved_physical_listener =
  1013. GetListener_MixerExpectsStart(reserved_mixer);
  1014. // Create another listener, NOT listening to |reserved_mixer|.
  1015. ASSERT_NE(kOtherFakeDeviceId, current_reserved_physical_device());
  1016. auto other_listener = GetListener_MixerExpectsNoCalls(reserved_mixer);
  1017. // Start all listeners.
  1018. output_mixer_manager_.StartListening(reserved_listener.get(),
  1019. reserved_device_id());
  1020. output_mixer_manager_.StartListening(current_reserved_physical_listener.get(),
  1021. current_reserved_physical_device());
  1022. output_mixer_manager_.StartListening(other_listener.get(),
  1023. kOtherFakeDeviceId);
  1024. // |reserved_listener| and |current_reserved_physical_listener| should be
  1025. // attached to |reserved_mixer|.
  1026. ForceOutputMixerCreation(reserved_device_id());
  1027. }
  1028. // Makes sure both "reserved listeners" and "current_reserved_physical_device()
  1029. // listeners" don't get attached to non-reserved mixers.
  1030. TEST_P(OutputDeviceMixerManagerTest,
  1031. DeviceOutputListener_OtherDeviceMixerCreation_ListenersNotAttached) {
  1032. MockOutputDeviceMixer* other_mixer =
  1033. SetUpMockMixer_NoStreams(kOtherFakeDeviceId);
  1034. // Create listeners for reserved_device_id() and
  1035. // current_reserved_physical_device(), BOTH NOT listening to |other_mixer|.
  1036. auto reserved_listener = GetListener_MixerExpectsNoCalls(other_mixer);
  1037. auto current_reserved_physical_listener =
  1038. GetListener_MixerExpectsNoCalls(other_mixer);
  1039. // Create another listener, listening to |other_mixer|.
  1040. ASSERT_NE(kOtherFakeDeviceId, current_reserved_physical_device());
  1041. auto other_listener = GetListener_MixerExpectsStart(other_mixer);
  1042. // Start all listeners.
  1043. output_mixer_manager_.StartListening(reserved_listener.get(),
  1044. reserved_device_id());
  1045. output_mixer_manager_.StartListening(current_reserved_physical_listener.get(),
  1046. current_reserved_physical_device());
  1047. output_mixer_manager_.StartListening(other_listener.get(),
  1048. kOtherFakeDeviceId);
  1049. // Only |other_listener| should be attached to |other_mixer|.
  1050. ForceOutputMixerCreation(kOtherFakeDeviceId);
  1051. }
  1052. // Makes sure we can call StartListening multiple times with the same listener,
  1053. // when the different device IDs map to the same mixer.
  1054. TEST_P(OutputDeviceMixerManagerTest,
  1055. DeviceOutputListener_MultipleStarts_EquivalentIds) {
  1056. MockOutputDeviceMixer* default_mixer = SetUpReservedMixer_NoStreams();
  1057. ForceOutputMixerCreation(reserved_device_id());
  1058. auto listener = GetListener_MixerExpectsStartStop(default_mixer);
  1059. // Start listener.
  1060. output_mixer_manager_.StartListening(listener.get(), reserved_device_id());
  1061. // Verify starting with the same ID.
  1062. output_mixer_manager_.StartListening(listener.get(), reserved_device_id());
  1063. // Verify starting with equivalent IDs.
  1064. if (reserved_id_test_type() == ReservedIdTestType::kDefault) {
  1065. // The kEmptyDeviceId also maps to kReservedDefaultId.
  1066. output_mixer_manager_.StartListening(listener.get(), kEmptyDeviceId);
  1067. }
  1068. output_mixer_manager_.StartListening(listener.get(),
  1069. current_reserved_physical_device());
  1070. // Return to the original ID.
  1071. output_mixer_manager_.StartListening(listener.get(), reserved_device_id());
  1072. output_mixer_manager_.StopListening(listener.get());
  1073. }
  1074. // Makes sure we can call StartListening multiple times with the same listener,
  1075. // with different device IDs.
  1076. TEST_F(OutputDeviceMixerManagerTest,
  1077. DeviceOutputListener_MultipleStarts_DifferentIds) {
  1078. MockOutputDeviceMixer* default_mixer;
  1079. MockOutputDeviceMixer* other_mixer;
  1080. {
  1081. InSequence s;
  1082. default_mixer = SetUpMockMixer_NoStreams(kNormalizedDefaultDeviceId);
  1083. other_mixer = SetUpMockMixer_NoStreams(kOtherFakeDeviceId);
  1084. ForceOutputMixerCreation(kReservedDefaultId);
  1085. ForceOutputMixerCreation(kOtherFakeDeviceId);
  1086. }
  1087. auto listener = GetListener_MixerExpectsStartStop(default_mixer);
  1088. EXPECT_CALL(*other_mixer, StartListening(listener.get())).Times(1);
  1089. EXPECT_CALL(*other_mixer, StopListening(listener.get())).Times(0);
  1090. output_mixer_manager_.StartListening(listener.get(), kReservedDefaultId);
  1091. // This call should stop |default_mixer|.
  1092. output_mixer_manager_.StartListening(listener.get(), kOtherFakeDeviceId);
  1093. }
  1094. // Makes sure listeners are properly updated internally when going from a
  1095. // reserved to a specific device.
  1096. TEST_P(OutputDeviceMixerManagerTest,
  1097. DeviceOutputListener_MultipleStarts_ReservedToSpecific) {
  1098. MockOutputDeviceMixer* reserved_mixer = SetUpReservedMixer_NoStreams();
  1099. ForceOutputMixerCreation(reserved_device_id());
  1100. testing::Mock::VerifyAndClearExpectations(this);
  1101. std::string original_reserved_id = current_reserved_physical_device();
  1102. auto listener = GetListener_MixerExpectsStart(reserved_mixer);
  1103. output_mixer_manager_.StartListening(listener.get(), reserved_device_id());
  1104. // Switch |listener| to listen to the current reserved device ID.
  1105. output_mixer_manager_.StartListening(listener.get(), original_reserved_id);
  1106. // Change the reserved device ID.
  1107. ASSERT_NE(current_reserved_physical_device(), kOtherFakeDeviceId);
  1108. SimulateReservedDeviceChange(kOtherFakeDeviceId);
  1109. ASSERT_EQ(current_reserved_physical_device(), kOtherFakeDeviceId);
  1110. // The reserved mixer should not receive any start/stop calls with listener.
  1111. MockOutputDeviceMixer* new_reserved_mixer = SetUpReservedMixer_NoStreams();
  1112. EXPECT_CALL(*new_reserved_mixer, StartListening(listener.get())).Times(0);
  1113. EXPECT_CALL(*new_reserved_mixer, StopListening(listener.get())).Times(0);
  1114. ForceOutputMixerCreation(reserved_device_id());
  1115. testing::Mock::VerifyAndClearExpectations(this);
  1116. // The |original_reserved_id| mixer should be started with listener.
  1117. MockOutputDeviceMixer* physical_mixer =
  1118. SetUpMockMixer_NoStreams(original_reserved_id);
  1119. EXPECT_CALL(*physical_mixer, StartListening(listener.get())).Times(1);
  1120. EXPECT_CALL(*physical_mixer, StopListening(listener.get())).Times(0);
  1121. ForceOutputMixerCreation(original_reserved_id);
  1122. }
  1123. // Makes sure that there one shared default and communications mixer, if the
  1124. // current default and communication physical IDs are identical.
  1125. TEST_F(OutputDeviceMixerManagerTest,
  1126. ReservedIds_DefaultAndCommunicationsPhysicalIdsShared) {
  1127. SetAudioManagerDefaultIdSupport(true);
  1128. SetAudioManagerCommunicationsIdSupport(true);
  1129. // Set both the "default" and "communications" physical ID to be the same.
  1130. SimulateDeviceChange(kFakeDeviceId, kFakeDeviceId);
  1131. SetUpMockMixer_NoStreams(kNormalizedDefaultDeviceId);
  1132. // All three IDs should resolve to a single kNormalizedDefaultDeviceId device.
  1133. ForceOutputMixerCreation(kReservedDefaultId);
  1134. ForceOutputMixerCreation(kReservedCommsId);
  1135. ForceOutputMixerCreation(kFakeDeviceId);
  1136. }
  1137. // Makes sure that we map "communications" to kNormalizedDefaultDeviceId if the
  1138. // current physical communications device ID is empty.
  1139. TEST_F(OutputDeviceMixerManagerTest,
  1140. ReservedIds_EmptyCommunicationsPhysicalId) {
  1141. SetAudioManagerDefaultIdSupport(true);
  1142. SetAudioManagerCommunicationsIdSupport(true);
  1143. // Set the "communications" physical ID be empty.
  1144. SimulateDeviceChange(kFakeDeviceId, kEmptyDeviceId);
  1145. SetUpMockMixer_NoStreams(kNormalizedDefaultDeviceId);
  1146. // The "communications" ID should resolve to kNormalizedDefaultDeviceId.
  1147. ForceOutputMixerCreation(kReservedCommsId);
  1148. }
  1149. INSTANTIATE_TEST_SUITE_P(OutputDeviceMixerManagerTest,
  1150. OutputDeviceMixerManagerTest,
  1151. testing::Values(ReservedIdTestType::kDefault,
  1152. ReservedIdTestType::kCommunications),
  1153. [](const ::testing::TestParamInfo<
  1154. OutputDeviceMixerManagerTest::ParamType>& info) {
  1155. switch (info.param) {
  1156. case ReservedIdTestType::kDefault:
  1157. return "Default";
  1158. case ReservedIdTestType::kCommunications:
  1159. return "Comms";
  1160. }
  1161. });
  1162. } // namespace audio