1 // Copyright 2020 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 
5 #ifndef CHROMECAST_MEDIA_AUDIO_CAST_AUDIO_MANAGER_HELPER_H_
6 #define CHROMECAST_MEDIA_AUDIO_CAST_AUDIO_MANAGER_HELPER_H_
7 
8 #include <string>
9 
10 #include "base/callback.h"
11 #include "base/memory/scoped_refptr.h"
12 #include "chromecast/common/mojom/service_connector.mojom.h"
13 #include "mojo/public/cpp/bindings/pending_receiver.h"
14 #include "mojo/public/cpp/bindings/pending_remote.h"
15 #include "mojo/public/cpp/bindings/remote.h"
16 
17 namespace base {
18 class SingleThreadTaskRunner;
19 }  // namespace base
20 
21 namespace media {
22 class AudioManagerBase;
23 }  // namespace media
24 
25 namespace chromecast {
26 namespace media {
27 
28 class CmaBackendFactory;
29 
30 // Helper class to share common logic between different AudioManager.
31 class CastAudioManagerHelper {
32  public:
33   // Callback to get session ID from group ID.
34   using GetSessionIdCallback =
35       base::RepeatingCallback<std::string(const std::string&)>;
36 
37   CastAudioManagerHelper(
38       ::media::AudioManagerBase* audio_manager,
39       base::RepeatingCallback<CmaBackendFactory*()> backend_factory_getter,
40       GetSessionIdCallback get_session_id_callback,
41       scoped_refptr<base::SingleThreadTaskRunner> media_task_runner,
42       mojo::PendingRemote<chromecast::mojom::ServiceConnector> connector);
43   ~CastAudioManagerHelper();
44 
audio_manager()45   ::media::AudioManagerBase* audio_manager() { return audio_manager_; }
46 
media_task_runner()47   base::SingleThreadTaskRunner* media_task_runner() {
48     return media_task_runner_.get();
49   }
50 
51   CmaBackendFactory* GetCmaBackendFactory();
52   std::string GetSessionId(const std::string& audio_group_id);
53   chromecast::mojom::ServiceConnector* GetConnector();
54 
55  private:
56   ::media::AudioManagerBase* const audio_manager_;
57   base::RepeatingCallback<CmaBackendFactory*()> backend_factory_getter_;
58   GetSessionIdCallback get_session_id_callback_;
59   CmaBackendFactory* cma_backend_factory_ = nullptr;
60   scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_;
61 
62   // |connector_| is bound to |pending_connector_| lazily on first use, as it is
63   // created on the main thread but used on the Audio thread, which may differ.
64   mojo::PendingRemote<chromecast::mojom::ServiceConnector> pending_connector_;
65   mojo::Remote<chromecast::mojom::ServiceConnector> connector_;
66 
67   CastAudioManagerHelper(const CastAudioManagerHelper&) = delete;
68   CastAudioManagerHelper& operator=(const CastAudioManagerHelper&) = delete;
69 };
70 
71 }  // namespace media
72 }  // namespace chromecast
73 
74 #endif  // CHROMECAST_MEDIA_AUDIO_CAST_AUDIO_MANAGER_HELPER_H_
75