xref: /aosp_15_r20/external/webrtc/api/audio/audio_mixer.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef API_AUDIO_AUDIO_MIXER_H_
12 #define API_AUDIO_AUDIO_MIXER_H_
13 
14 #include <memory>
15 
16 #include "api/audio/audio_frame.h"
17 #include "rtc_base/ref_count.h"
18 
19 namespace webrtc {
20 
21 // WORK IN PROGRESS
22 // This class is under development and is not yet intended for for use outside
23 // of WebRtc/Libjingle.
24 class AudioMixer : public rtc::RefCountInterface {
25  public:
26   // A callback class that all mixer participants must inherit from/implement.
27   class Source {
28    public:
29     enum class AudioFrameInfo {
30       kNormal,  // The samples in audio_frame are valid and should be used.
31       kMuted,   // The samples in audio_frame should not be used, but
32                 // should be implicitly interpreted as zero. Other
33                 // fields in audio_frame may be read and should
34                 // contain meaningful values.
35       kError,   // The audio_frame will not be used.
36     };
37 
38     // Overwrites `audio_frame`. The data_ field is overwritten with
39     // 10 ms of new audio (either 1 or 2 interleaved channels) at
40     // `sample_rate_hz`. All fields in `audio_frame` must be updated.
41     virtual AudioFrameInfo GetAudioFrameWithInfo(int sample_rate_hz,
42                                                  AudioFrame* audio_frame) = 0;
43 
44     // A way for a mixer implementation to distinguish participants.
45     virtual int Ssrc() const = 0;
46 
47     // A way for this source to say that GetAudioFrameWithInfo called
48     // with this sample rate or higher will not cause quality loss.
49     virtual int PreferredSampleRate() const = 0;
50 
~Source()51     virtual ~Source() {}
52   };
53 
54   // Returns true if adding was successful. A source is never added
55   // twice. Addition and removal can happen on different threads.
56   virtual bool AddSource(Source* audio_source) = 0;
57 
58   // Removal is never attempted if a source has not been successfully
59   // added to the mixer.
60   virtual void RemoveSource(Source* audio_source) = 0;
61 
62   // Performs mixing by asking registered audio sources for audio. The
63   // mixed result is placed in the provided AudioFrame. This method
64   // will only be called from a single thread. The channels argument
65   // specifies the number of channels of the mix result. The mixer
66   // should mix at a rate that doesn't cause quality loss of the
67   // sources' audio. The mixing rate is one of the rates listed in
68   // AudioProcessing::NativeRate. All fields in
69   // `audio_frame_for_mixing` must be updated.
70   virtual void Mix(size_t number_of_channels,
71                    AudioFrame* audio_frame_for_mixing) = 0;
72 
73  protected:
74   // Since the mixer is reference counted, the destructor may be
75   // called from any thread.
~AudioMixer()76   ~AudioMixer() override {}
77 };
78 }  // namespace webrtc
79 
80 #endif  // API_AUDIO_AUDIO_MIXER_H_
81