blob: 730ded075b3bfd1e11b41c18c81f52ca3bf84ac1 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander65c7f672016-02-12 00:05:01 -08002 * Copyright 2011 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander65c7f672016-02-12 00:05:01 -08004 * 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.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
11// CurrentSpeakerMonitor monitors the audio levels for a session and determines
12// which participant is currently speaking.
13
terelius8c011e52016-04-26 05:28:11 -070014#ifndef WEBRTC_PC_CURRENTSPEAKERMONITOR_H_
15#define WEBRTC_PC_CURRENTSPEAKERMONITOR_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000016
17#include <map>
18
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000019#include "webrtc/base/basictypes.h"
20#include "webrtc/base/sigslot.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021
22namespace cricket {
23
henrike@webrtc.org28e20752013-07-10 00:45:36 +000024struct AudioInfo;
25struct MediaStreams;
26
buildbot@webrtc.orgca272362014-05-08 23:10:23 +000027class AudioSourceContext {
28 public:
29 sigslot::signal2<AudioSourceContext*, const cricket::AudioInfo&>
30 SignalAudioMonitor;
deadbeefd59daf82015-10-14 15:02:44 -070031 sigslot::signal1<AudioSourceContext*> SignalMediaStreamsReset;
32 sigslot::signal3<AudioSourceContext*,
33 const cricket::MediaStreams&,
34 const cricket::MediaStreams&> SignalMediaStreamsUpdate;
buildbot@webrtc.orgca272362014-05-08 23:10:23 +000035};
36
37// CurrentSpeakerMonitor can be used to monitor the audio-levels from
38// many audio-sources and report on changes in the loudest audio-source.
39// Its a generic type and relies on an AudioSourceContext which is aware of
40// the audio-sources. AudioSourceContext needs to provide two signals namely
41// SignalAudioInfoMonitor - provides audio info of the all current speakers.
42// SignalMediaSourcesUpdated - provides updates when a speaker leaves or joins.
43// Note that the AudioSourceContext's audio monitor must be started
44// before this is started.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045// It's recommended that the audio monitor be started with a 100 ms period.
46class CurrentSpeakerMonitor : public sigslot::has_slots<> {
47 public:
terelius8c011e52016-04-26 05:28:11 -070048 explicit CurrentSpeakerMonitor(AudioSourceContext* audio_source_context);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049 ~CurrentSpeakerMonitor();
50
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051 void Start();
52 void Stop();
53
54 // Used by tests. Note that the actual minimum time between switches
55 // enforced by the monitor will be the given value plus or minus the
56 // resolution of the system clock.
Honghai Zhang82d78622016-05-06 11:29:15 -070057 void set_min_time_between_switches(int min_time_between_switches);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058
59 // This is fired when the current speaker changes, and provides his audio
buildbot@webrtc.org117afee2014-06-16 07:11:01 +000060 // SSRC. This only fires after the audio monitor on the underlying
61 // AudioSourceContext has been started.
Peter Boström0c4e06b2015-10-07 12:23:21 +020062 sigslot::signal2<CurrentSpeakerMonitor*, uint32_t> SignalUpdate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063
64 private:
buildbot@webrtc.org117afee2014-06-16 07:11:01 +000065 void OnAudioMonitor(AudioSourceContext* audio_source_context,
66 const AudioInfo& info);
67 void OnMediaStreamsUpdate(AudioSourceContext* audio_source_context,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068 const MediaStreams& added,
69 const MediaStreams& removed);
deadbeefd59daf82015-10-14 15:02:44 -070070 void OnMediaStreamsReset(AudioSourceContext* audio_source_context);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071
72 // These are states that a participant will pass through so that we gradually
73 // recognize that they have started and stopped speaking. This avoids
74 // "twitchiness".
75 enum SpeakingState {
76 SS_NOT_SPEAKING,
77 SS_MIGHT_BE_SPEAKING,
78 SS_SPEAKING,
79 SS_WAS_SPEAKING_RECENTLY1,
80 SS_WAS_SPEAKING_RECENTLY2
81 };
82
83 bool started_;
buildbot@webrtc.orgca272362014-05-08 23:10:23 +000084 AudioSourceContext* audio_source_context_;
Peter Boström0c4e06b2015-10-07 12:23:21 +020085 std::map<uint32_t, SpeakingState> ssrc_to_speaking_state_map_;
86 uint32_t current_speaker_ssrc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087 // To prevent overswitching, switching is disabled for some time after a
88 // switch is made. This gives us the earliest time a switch is permitted.
Honghai Zhang82d78622016-05-06 11:29:15 -070089 int64_t earliest_permitted_switch_time_;
90 int min_time_between_switches_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091};
92
terelius8c011e52016-04-26 05:28:11 -070093} // namespace cricket
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094
terelius8c011e52016-04-26 05:28:11 -070095#endif // WEBRTC_PC_CURRENTSPEAKERMONITOR_H_