henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | 65c7f67 | 2016-02-12 00:05:01 -0800 | [diff] [blame] | 2 | * Copyright 2011 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | 65c7f67 | 2016-02-12 00:05:01 -0800 | [diff] [blame] | 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. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | // CurrentSpeakerMonitor monitors the audio levels for a session and determines |
| 12 | // which participant is currently speaking. |
| 13 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #ifndef PC_CURRENTSPEAKERMONITOR_H_ |
| 15 | #define PC_CURRENTSPEAKERMONITOR_H_ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 16 | |
pbos | c7c26a0 | 2017-01-02 08:42:32 -0800 | [diff] [blame] | 17 | #include <stdint.h> |
| 18 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 19 | #include <map> |
| 20 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "rtc_base/sigslot.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 22 | |
| 23 | namespace cricket { |
| 24 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 25 | struct AudioInfo; |
| 26 | struct MediaStreams; |
| 27 | |
buildbot@webrtc.org | ca27236 | 2014-05-08 23:10:23 +0000 | [diff] [blame] | 28 | class AudioSourceContext { |
| 29 | public: |
| 30 | sigslot::signal2<AudioSourceContext*, const cricket::AudioInfo&> |
| 31 | SignalAudioMonitor; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 32 | sigslot::signal1<AudioSourceContext*> SignalMediaStreamsReset; |
| 33 | sigslot::signal3<AudioSourceContext*, |
| 34 | const cricket::MediaStreams&, |
| 35 | const cricket::MediaStreams&> SignalMediaStreamsUpdate; |
buildbot@webrtc.org | ca27236 | 2014-05-08 23:10:23 +0000 | [diff] [blame] | 36 | }; |
| 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 46 | // It's recommended that the audio monitor be started with a 100 ms period. |
| 47 | class CurrentSpeakerMonitor : public sigslot::has_slots<> { |
| 48 | public: |
terelius | 8c011e5 | 2016-04-26 05:28:11 -0700 | [diff] [blame] | 49 | explicit CurrentSpeakerMonitor(AudioSourceContext* audio_source_context); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 50 | ~CurrentSpeakerMonitor(); |
| 51 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 52 | 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 Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 58 | void set_min_time_between_switches(int min_time_between_switches); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 59 | |
| 60 | // This is fired when the current speaker changes, and provides his audio |
buildbot@webrtc.org | 117afee | 2014-06-16 07:11:01 +0000 | [diff] [blame] | 61 | // SSRC. This only fires after the audio monitor on the underlying |
| 62 | // AudioSourceContext has been started. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 63 | sigslot::signal2<CurrentSpeakerMonitor*, uint32_t> SignalUpdate; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 64 | |
| 65 | private: |
buildbot@webrtc.org | 117afee | 2014-06-16 07:11:01 +0000 | [diff] [blame] | 66 | void OnAudioMonitor(AudioSourceContext* audio_source_context, |
| 67 | const AudioInfo& info); |
| 68 | void OnMediaStreamsUpdate(AudioSourceContext* audio_source_context, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 69 | const MediaStreams& added, |
| 70 | const MediaStreams& removed); |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 71 | void OnMediaStreamsReset(AudioSourceContext* audio_source_context); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 72 | |
| 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.org | ca27236 | 2014-05-08 23:10:23 +0000 | [diff] [blame] | 85 | AudioSourceContext* audio_source_context_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 86 | std::map<uint32_t, SpeakingState> ssrc_to_speaking_state_map_; |
| 87 | uint32_t current_speaker_ssrc_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 88 | // 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 Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 90 | int64_t earliest_permitted_switch_time_; |
| 91 | int min_time_between_switches_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
terelius | 8c011e5 | 2016-04-26 05:28:11 -0700 | [diff] [blame] | 94 | } // namespace cricket |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 95 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 96 | #endif // PC_CURRENTSPEAKERMONITOR_H_ |