1 // Copyright (c) 2013 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.
5 // AudioMirroringManager is a singleton object that maintains a set of active
6 // audio mirroring destinations and auto-connects/disconnects audio streams
7 // to/from those destinations. It is meant to be used exclusively on the IO
12 // 1. AudioRendererHost gets a CreateStream message from the render process
13 // and, among other things, creates an AudioOutputController to control the
14 // audio data flow between the render and browser processes.
15 // 2. At some point, AudioRendererHost receives an "associate with render
16 // view" message. Among other actions, it registers the
17 // AudioOutputController with AudioMirroringManager (as a Diverter).
18 // 3. A user request to mirror all the audio for a single RenderView is made.
19 // A MirroringDestination is created, and StartMirroring() is called to
20 // begin the mirroring session. This causes AudioMirroringManager to
21 // instruct any matching Diverters to divert their audio data to the
22 // MirroringDestination.
24 // #2 and #3 above may occur in any order, as it is the job of
25 // AudioMirroringManager to realize when the players can be "matched up."
27 #ifndef CONTENT_BROWSER_MEDIA_CAPTURE_AUDIO_MIRRORING_MANAGER_H_
28 #define CONTENT_BROWSER_MEDIA_CAPTURE_AUDIO_MIRRORING_MANAGER_H_
33 #include "base/basictypes.h"
34 #include "base/threading/thread_checker.h"
35 #include "content/common/content_export.h"
36 #include "media/audio/audio_source_diverter.h"
39 class AudioOutputStream
;
44 class CONTENT_EXPORT AudioMirroringManager
{
46 // Interface for diverting audio data to an alternative AudioOutputStream.
47 typedef media::AudioSourceDiverter Diverter
;
49 // Interface to be implemented by audio mirroring destinations. See comments
50 // for StartMirroring() and StopMirroring() below.
51 class MirroringDestination
{
53 // Create a consumer of audio data in the format specified by |params|, and
54 // connect it as an input to mirroring. When Close() is called on the
55 // returned AudioOutputStream, the input is disconnected and the object
57 virtual media::AudioOutputStream
* AddInput(
58 const media::AudioParameters
& params
) = 0;
61 virtual ~MirroringDestination() {}
64 // Note: Use GetInstance() for non-test code.
65 AudioMirroringManager();
66 virtual ~AudioMirroringManager();
68 // Returns the global instance.
69 static AudioMirroringManager
* GetInstance();
71 // Add/Remove a diverter for an audio stream with a known RenderView target
72 // (represented by |render_process_id| + |render_view_id|). Multiple
73 // diverters may be added for the same target. |diverter| must live until
74 // after RemoveDiverter() is called.
76 // Re-entrancy warning: These methods should not be called by a Diverter
77 // during a Start/StopDiverting() invocation.
78 virtual void AddDiverter(int render_process_id
, int render_view_id
,
80 virtual void RemoveDiverter(int render_process_id
, int render_view_id
,
83 // Start/stop mirroring all audio output streams associated with a RenderView
84 // target (represented by |render_process_id| + |render_view_id|) to
85 // |destination|. |destination| must live until after StopMirroring() is
87 virtual void StartMirroring(int render_process_id
, int render_view_id
,
88 MirroringDestination
* destination
);
89 virtual void StopMirroring(int render_process_id
, int render_view_id
,
90 MirroringDestination
* destination
);
93 // A mirroring target is a RenderView identified by a
94 // <render_process_id, render_view_id> pair.
95 typedef std::pair
<int, int> Target
;
97 // Note: Objects in these maps are not owned.
98 typedef std::multimap
<Target
, Diverter
*> DiverterMap
;
99 typedef std::map
<Target
, MirroringDestination
*> SessionMap
;
101 // Currently-active divertable audio streams.
102 DiverterMap diverters_
;
104 // Currently-active mirroring sessions.
105 SessionMap sessions_
;
107 // Used to check that all AudioMirroringManager code runs on the same thread.
108 base::ThreadChecker thread_checker_
;
110 DISALLOW_COPY_AND_ASSIGN(AudioMirroringManager
);
113 } // namespace content
115 #endif // CONTENT_BROWSER_MEDIA_CAPTURE_AUDIO_MIRRORING_MANAGER_H_