blob: 589fb1b1d55c495a9641efa80bb2220f6aac127c [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#ifndef PC_CURRENTSPEAKERMONITOR_H_
15#define PC_CURRENTSPEAKERMONITOR_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000016
pbosc7c26a02017-01-02 08:42:32 -080017#include <stdint.h>
18
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019#include <map>
20
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/sigslot.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000022
23namespace cricket {
24
henrike@webrtc.org28e20752013-07-10 00:45:36 +000025struct AudioInfo;
26struct MediaStreams;
27
buildbot@webrtc.orgca272362014-05-08 23:10:23 +000028class AudioSourceContext {
29 public:
30 sigslot::signal2<AudioSourceContext*, const cricket::AudioInfo&>
31 SignalAudioMonitor;
deadbeefd59daf82015-10-14 15:02:44 -070032 sigslot::signal1<AudioSourceContext*> SignalMediaStreamsReset;
33 sigslot::signal3<AudioSourceContext*,
34 const cricket::MediaStreams&,
35 const cricket::MediaStreams&> SignalMediaStreamsUpdate;
buildbot@webrtc.orgca272362014-05-08 23:10:23 +000036};
37
38// CurrentSpeakerMonitor can be used to monitor the audio-levels from
39// many audio-sources and report on changes in the loudest audio-source.
40// Its a generic type and relies on an AudioSourceContext which is aware of
41// the audio-sources. AudioSourceContext needs to provide two signals namely
42// SignalAudioInfoMonitor - provides audio info of the all current speakers.
43// SignalMediaSourcesUpdated - provides updates when a speaker leaves or joins.
44// Note that the AudioSourceContext's audio monitor must be started
45// before this is started.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046// It's recommended that the audio monitor be started with a 100 ms period.
47class CurrentSpeakerMonitor : public sigslot::has_slots<> {
48 public:
terelius8c011e52016-04-26 05:28:11 -070049 explicit CurrentSpeakerMonitor(AudioSourceContext* audio_source_context);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000050 ~CurrentSpeakerMonitor();
51
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052 void Start();
53 void Stop();
54
55 // Used by tests. Note that the actual minimum time between switches
56 // enforced by the monitor will be the given value plus or minus the
57 // resolution of the system clock.
Honghai Zhang82d78622016-05-06 11:29:15 -070058 void set_min_time_between_switches(int min_time_between_switches);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059
60 // This is fired when the current speaker changes, and provides his audio
buildbot@webrtc.org117afee2014-06-16 07:11:01 +000061 // SSRC. This only fires after the audio monitor on the underlying
62 // AudioSourceContext has been started.
Peter Boström0c4e06b2015-10-07 12:23:21 +020063 sigslot::signal2<CurrentSpeakerMonitor*, uint32_t> SignalUpdate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064
65 private:
buildbot@webrtc.org117afee2014-06-16 07:11:01 +000066 void OnAudioMonitor(AudioSourceContext* audio_source_context,
67 const AudioInfo& info);
68 void OnMediaStreamsUpdate(AudioSourceContext* audio_source_context,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069 const MediaStreams& added,
70 const MediaStreams& removed);
deadbeefd59daf82015-10-14 15:02:44 -070071 void OnMediaStreamsReset(AudioSourceContext* audio_source_context);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072
73 // These are states that a participant will pass through so that we gradually
74 // recognize that they have started and stopped speaking. This avoids
75 // "twitchiness".
76 enum SpeakingState {
77 SS_NOT_SPEAKING,
78 SS_MIGHT_BE_SPEAKING,
79 SS_SPEAKING,
80 SS_WAS_SPEAKING_RECENTLY1,
81 SS_WAS_SPEAKING_RECENTLY2
82 };
83
84 bool started_;
buildbot@webrtc.orgca272362014-05-08 23:10:23 +000085 AudioSourceContext* audio_source_context_;
Peter Boström0c4e06b2015-10-07 12:23:21 +020086 std::map<uint32_t, SpeakingState> ssrc_to_speaking_state_map_;
87 uint32_t current_speaker_ssrc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088 // To prevent overswitching, switching is disabled for some time after a
89 // switch is made. This gives us the earliest time a switch is permitted.
Honghai Zhang82d78622016-05-06 11:29:15 -070090 int64_t earliest_permitted_switch_time_;
91 int min_time_between_switches_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092};
93
terelius8c011e52016-04-26 05:28:11 -070094} // namespace cricket
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020096#endif // PC_CURRENTSPEAKERMONITOR_H_