blob: 960adbbd430b8cb24403a5736aa9f0d039a43a6d [file] [log] [blame]
aleloi77ad3942016-07-04 06:33:02 -07001/*
aleloi5d167d62016-08-24 02:20:54 -07002 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
aleloi77ad3942016-07-04 06:33:02 -07003 *
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
aleloi201dfe92016-10-20 05:06:39 -070011#ifndef WEBRTC_API_AUDIO_AUDIO_MIXER_H_
12#define WEBRTC_API_AUDIO_AUDIO_MIXER_H_
aleloi77ad3942016-07-04 06:33:02 -070013
kwiberg5a25d952016-08-17 07:31:12 -070014#include <memory>
15
aleloi116ec6d2016-10-12 06:07:07 -070016#include "webrtc/base/refcount.h"
aleloi5d167d62016-08-24 02:20:54 -070017#include "webrtc/modules/include/module_common_types.h"
aleloi77ad3942016-07-04 06:33:02 -070018
19namespace webrtc {
20
aleloi201dfe92016-10-20 05:06:39 -070021// WORK IN PROGRESS
22// This class is under development and is not yet intended for for use outside
23// of WebRtc/Libjingle.
aleloi116ec6d2016-10-12 06:07:07 -070024class AudioMixer : public rtc::RefCountInterface {
aleloi77ad3942016-07-04 06:33:02 -070025 public:
aleloie8914152016-10-11 06:18:31 -070026 // 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.
aleloi201dfe92016-10-20 05:06:39 -070031 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.
aleloie8914152016-10-11 06:18:31 -070036 };
37
38 struct AudioFrameWithInfo {
39 AudioFrame* audio_frame;
40 AudioFrameInfo audio_frame_info;
41 };
42
43 // The implementation of GetAudioFrameWithInfo should update
44 // audio_frame with new audio every time it's called. Implementing
45 // classes are allowed to return the same AudioFrame pointer on
46 // different calls. The pointer must stay valid until the next
47 // mixing call or until this audio source is disconnected from the
48 // mixer. The mixer may modify the contents of the passed
49 // AudioFrame pointer at any time until the next call to
50 // GetAudioFrameWithInfo, or until the source is removed from the
51 // mixer.
aleloie97974d2016-10-12 03:06:09 -070052 virtual AudioFrameWithInfo GetAudioFrameWithInfo(int sample_rate_hz) = 0;
aleloie8914152016-10-11 06:18:31 -070053
aleloi201dfe92016-10-20 05:06:39 -070054 // A way for a mixer implementation to distinguish participants.
55 virtual int Ssrc() = 0;
aleloi116ec6d2016-10-12 06:07:07 -070056
aleloie8914152016-10-11 06:18:31 -070057 protected:
58 virtual ~Source() {}
59 };
60
aleloi116ec6d2016-10-12 06:07:07 -070061 // Since the mixer is reference counted, the destructor may be
62 // called from any thread.
63 ~AudioMixer() override {}
aleloi77ad3942016-07-04 06:33:02 -070064
aleloi116ec6d2016-10-12 06:07:07 -070065 // Returns true if adding/removing was successful. A source is never
66 // added twice and removal is never attempted if a source has not
67 // been successfully added to the mixer. Addition and removal can
68 // happen on different threads.
69 virtual bool AddSource(Source* audio_source) = 0;
70 virtual bool RemoveSource(Source* audio_source) = 0;
aleloi77ad3942016-07-04 06:33:02 -070071
aleloi5d167d62016-08-24 02:20:54 -070072 // Performs mixing by asking registered audio sources for audio. The
aleloie97974d2016-10-12 03:06:09 -070073 // mixed result is placed in the provided AudioFrame. Will only be
aleloi5d167d62016-08-24 02:20:54 -070074 // called from a single thread. The rate and channels arguments
75 // specify the rate and number of channels of the mix result.
aleloie97974d2016-10-12 03:06:09 -070076 virtual void Mix(int sample_rate_hz,
aleloi5d167d62016-08-24 02:20:54 -070077 size_t number_of_channels,
78 AudioFrame* audio_frame_for_mixing) = 0;
aleloi77ad3942016-07-04 06:33:02 -070079};
aleloi77ad3942016-07-04 06:33:02 -070080} // namespace webrtc
81
aleloi201dfe92016-10-20 05:06:39 -070082#endif // WEBRTC_API_AUDIO_AUDIO_MIXER_H_