henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 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.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 36 | #include "webrtc/base/basictypes.h" |
| 37 | #include "webrtc/base/sigslot.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 38 | |
| 39 | namespace cricket { |
| 40 | |
| 41 | class BaseSession; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 42 | struct AudioInfo; |
| 43 | struct MediaStreams; |
| 44 | |
buildbot@webrtc.org | ca27236 | 2014-05-08 23:10:23 +0000 | [diff] [blame] | 45 | class AudioSourceContext { |
| 46 | public: |
| 47 | sigslot::signal2<AudioSourceContext*, const cricket::AudioInfo&> |
| 48 | SignalAudioMonitor; |
buildbot@webrtc.org | 49a6a27 | 2014-05-21 00:24:54 +0000 | [diff] [blame] | 49 | sigslot::signal2<AudioSourceContext*, cricket::BaseSession*> |
| 50 | SignalMediaStreamsReset; |
| 51 | sigslot::signal4<AudioSourceContext*, cricket::BaseSession*, |
buildbot@webrtc.org | ca27236 | 2014-05-08 23:10:23 +0000 | [diff] [blame] | 52 | const cricket::MediaStreams&, const cricket::MediaStreams&> |
| 53 | SignalMediaStreamsUpdate; |
| 54 | }; |
| 55 | |
| 56 | // CurrentSpeakerMonitor can be used to monitor the audio-levels from |
| 57 | // many audio-sources and report on changes in the loudest audio-source. |
| 58 | // Its a generic type and relies on an AudioSourceContext which is aware of |
| 59 | // the audio-sources. AudioSourceContext needs to provide two signals namely |
| 60 | // SignalAudioInfoMonitor - provides audio info of the all current speakers. |
| 61 | // SignalMediaSourcesUpdated - provides updates when a speaker leaves or joins. |
| 62 | // Note that the AudioSourceContext's audio monitor must be started |
| 63 | // before this is started. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 64 | // It's recommended that the audio monitor be started with a 100 ms period. |
| 65 | class CurrentSpeakerMonitor : public sigslot::has_slots<> { |
| 66 | public: |
buildbot@webrtc.org | 117afee | 2014-06-16 07:11:01 +0000 | [diff] [blame] | 67 | CurrentSpeakerMonitor(AudioSourceContext* audio_source_context, |
| 68 | BaseSession* session); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 69 | ~CurrentSpeakerMonitor(); |
| 70 | |
| 71 | BaseSession* session() const { return session_; } |
| 72 | |
| 73 | void Start(); |
| 74 | void Stop(); |
| 75 | |
| 76 | // Used by tests. Note that the actual minimum time between switches |
| 77 | // enforced by the monitor will be the given value plus or minus the |
| 78 | // resolution of the system clock. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 79 | void set_min_time_between_switches(uint32_t min_time_between_switches); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 80 | |
| 81 | // 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] | 82 | // SSRC. This only fires after the audio monitor on the underlying |
| 83 | // AudioSourceContext has been started. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 84 | sigslot::signal2<CurrentSpeakerMonitor*, uint32_t> SignalUpdate; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 85 | |
| 86 | private: |
buildbot@webrtc.org | 117afee | 2014-06-16 07:11:01 +0000 | [diff] [blame] | 87 | void OnAudioMonitor(AudioSourceContext* audio_source_context, |
| 88 | const AudioInfo& info); |
| 89 | void OnMediaStreamsUpdate(AudioSourceContext* audio_source_context, |
buildbot@webrtc.org | 49a6a27 | 2014-05-21 00:24:54 +0000 | [diff] [blame] | 90 | BaseSession* session, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 91 | const MediaStreams& added, |
| 92 | const MediaStreams& removed); |
buildbot@webrtc.org | 49a6a27 | 2014-05-21 00:24:54 +0000 | [diff] [blame] | 93 | void OnMediaStreamsReset(AudioSourceContext* audio_source_context, |
| 94 | BaseSession* session); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 95 | |
| 96 | // These are states that a participant will pass through so that we gradually |
| 97 | // recognize that they have started and stopped speaking. This avoids |
| 98 | // "twitchiness". |
| 99 | enum SpeakingState { |
| 100 | SS_NOT_SPEAKING, |
| 101 | SS_MIGHT_BE_SPEAKING, |
| 102 | SS_SPEAKING, |
| 103 | SS_WAS_SPEAKING_RECENTLY1, |
| 104 | SS_WAS_SPEAKING_RECENTLY2 |
| 105 | }; |
| 106 | |
| 107 | bool started_; |
buildbot@webrtc.org | ca27236 | 2014-05-08 23:10:23 +0000 | [diff] [blame] | 108 | AudioSourceContext* audio_source_context_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 109 | BaseSession* session_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 110 | std::map<uint32_t, SpeakingState> ssrc_to_speaking_state_map_; |
| 111 | uint32_t current_speaker_ssrc_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 112 | // To prevent overswitching, switching is disabled for some time after a |
| 113 | // switch is made. This gives us the earliest time a switch is permitted. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 114 | uint32_t earliest_permitted_switch_time_; |
| 115 | uint32_t min_time_between_switches_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | } |
| 119 | |
| 120 | #endif // TALK_SESSION_MEDIA_CURRENTSPEAKERMONITOR_H_ |