blob: cef8b733dcd0464fb58b32d6fb6c7dd5c4be5932 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2011 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28// CurrentSpeakerMonitor monitors the audio levels for a session and determines
29// which participant is currently speaking.
30
31#ifndef TALK_SESSION_MEDIA_CURRENTSPEAKERMONITOR_H_
32#define TALK_SESSION_MEDIA_CURRENTSPEAKERMONITOR_H_
33
34#include <map>
35
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000036#include "webrtc/base/basictypes.h"
37#include "webrtc/base/sigslot.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000038
39namespace cricket {
40
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041struct AudioInfo;
42struct MediaStreams;
43
buildbot@webrtc.orgca272362014-05-08 23:10:23 +000044class AudioSourceContext {
45 public:
46 sigslot::signal2<AudioSourceContext*, const cricket::AudioInfo&>
47 SignalAudioMonitor;
deadbeefd59daf82015-10-14 15:02:44 -070048 sigslot::signal1<AudioSourceContext*> SignalMediaStreamsReset;
49 sigslot::signal3<AudioSourceContext*,
50 const cricket::MediaStreams&,
51 const cricket::MediaStreams&> SignalMediaStreamsUpdate;
buildbot@webrtc.orgca272362014-05-08 23:10:23 +000052};
53
54// CurrentSpeakerMonitor can be used to monitor the audio-levels from
55// many audio-sources and report on changes in the loudest audio-source.
56// Its a generic type and relies on an AudioSourceContext which is aware of
57// the audio-sources. AudioSourceContext needs to provide two signals namely
58// SignalAudioInfoMonitor - provides audio info of the all current speakers.
59// SignalMediaSourcesUpdated - provides updates when a speaker leaves or joins.
60// Note that the AudioSourceContext's audio monitor must be started
61// before this is started.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062// It's recommended that the audio monitor be started with a 100 ms period.
63class CurrentSpeakerMonitor : public sigslot::has_slots<> {
64 public:
deadbeefd59daf82015-10-14 15:02:44 -070065 CurrentSpeakerMonitor(AudioSourceContext* audio_source_context);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066 ~CurrentSpeakerMonitor();
67
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068 void Start();
69 void Stop();
70
71 // Used by tests. Note that the actual minimum time between switches
72 // enforced by the monitor will be the given value plus or minus the
73 // resolution of the system clock.
Peter Boström0c4e06b2015-10-07 12:23:21 +020074 void set_min_time_between_switches(uint32_t min_time_between_switches);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075
76 // This is fired when the current speaker changes, and provides his audio
buildbot@webrtc.org117afee2014-06-16 07:11:01 +000077 // SSRC. This only fires after the audio monitor on the underlying
78 // AudioSourceContext has been started.
Peter Boström0c4e06b2015-10-07 12:23:21 +020079 sigslot::signal2<CurrentSpeakerMonitor*, uint32_t> SignalUpdate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080
81 private:
buildbot@webrtc.org117afee2014-06-16 07:11:01 +000082 void OnAudioMonitor(AudioSourceContext* audio_source_context,
83 const AudioInfo& info);
84 void OnMediaStreamsUpdate(AudioSourceContext* audio_source_context,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085 const MediaStreams& added,
86 const MediaStreams& removed);
deadbeefd59daf82015-10-14 15:02:44 -070087 void OnMediaStreamsReset(AudioSourceContext* audio_source_context);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088
89 // These are states that a participant will pass through so that we gradually
90 // recognize that they have started and stopped speaking. This avoids
91 // "twitchiness".
92 enum SpeakingState {
93 SS_NOT_SPEAKING,
94 SS_MIGHT_BE_SPEAKING,
95 SS_SPEAKING,
96 SS_WAS_SPEAKING_RECENTLY1,
97 SS_WAS_SPEAKING_RECENTLY2
98 };
99
100 bool started_;
buildbot@webrtc.orgca272362014-05-08 23:10:23 +0000101 AudioSourceContext* audio_source_context_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200102 std::map<uint32_t, SpeakingState> ssrc_to_speaking_state_map_;
103 uint32_t current_speaker_ssrc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104 // To prevent overswitching, switching is disabled for some time after a
105 // switch is made. This gives us the earliest time a switch is permitted.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200106 uint32_t earliest_permitted_switch_time_;
107 uint32_t min_time_between_switches_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000108};
109
110}
111
112#endif // TALK_SESSION_MEDIA_CURRENTSPEAKERMONITOR_H_