blob: 14ed8762a8b3092fe7b8f0d5771c038aead49930 [file] [log] [blame]
kjellander@webrtc.org4056bb62016-02-12 05:47:591/*
kjellander96ce6822016-02-12 08:05:012 * Copyright 2011 The WebRTC project authors. All Rights Reserved.
kjellander@webrtc.org4056bb62016-02-12 05:47:593 *
kjellander96ce6822016-02-12 08:05:014 * 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.
kjellander@webrtc.org4056bb62016-02-12 05:47:599 */
10
11// CurrentSpeakerMonitor monitors the audio levels for a session and determines
12// which participant is currently speaking.
13
tereliusbfc1d552016-04-26 12:28:1114#ifndef WEBRTC_PC_CURRENTSPEAKERMONITOR_H_
15#define WEBRTC_PC_CURRENTSPEAKERMONITOR_H_
kjellander@webrtc.org4056bb62016-02-12 05:47:5916
pbos67d12ed2017-01-02 16:42:3217#include <stdint.h>
18
kjellander@webrtc.org4056bb62016-02-12 05:47:5919#include <map>
20
kjellander@webrtc.org4056bb62016-02-12 05:47:5921#include "webrtc/base/sigslot.h"
22
23namespace cricket {
24
25struct AudioInfo;
26struct MediaStreams;
27
28class AudioSourceContext {
29 public:
30 sigslot::signal2<AudioSourceContext*, const cricket::AudioInfo&>
31 SignalAudioMonitor;
32 sigslot::signal1<AudioSourceContext*> SignalMediaStreamsReset;
33 sigslot::signal3<AudioSourceContext*,
34 const cricket::MediaStreams&,
35 const cricket::MediaStreams&> SignalMediaStreamsUpdate;
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.
46// It's recommended that the audio monitor be started with a 100 ms period.
47class CurrentSpeakerMonitor : public sigslot::has_slots<> {
48 public:
tereliusbfc1d552016-04-26 12:28:1149 explicit CurrentSpeakerMonitor(AudioSourceContext* audio_source_context);
kjellander@webrtc.org4056bb62016-02-12 05:47:5950 ~CurrentSpeakerMonitor();
51
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 Zhang3f20ddb2016-05-06 18:29:1558 void set_min_time_between_switches(int min_time_between_switches);
kjellander@webrtc.org4056bb62016-02-12 05:47:5959
60 // This is fired when the current speaker changes, and provides his audio
61 // SSRC. This only fires after the audio monitor on the underlying
62 // AudioSourceContext has been started.
63 sigslot::signal2<CurrentSpeakerMonitor*, uint32_t> SignalUpdate;
64
65 private:
66 void OnAudioMonitor(AudioSourceContext* audio_source_context,
67 const AudioInfo& info);
68 void OnMediaStreamsUpdate(AudioSourceContext* audio_source_context,
69 const MediaStreams& added,
70 const MediaStreams& removed);
71 void OnMediaStreamsReset(AudioSourceContext* audio_source_context);
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_;
85 AudioSourceContext* audio_source_context_;
86 std::map<uint32_t, SpeakingState> ssrc_to_speaking_state_map_;
87 uint32_t current_speaker_ssrc_;
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 Zhang3f20ddb2016-05-06 18:29:1590 int64_t earliest_permitted_switch_time_;
91 int min_time_between_switches_;
kjellander@webrtc.org4056bb62016-02-12 05:47:5992};
93
tereliusbfc1d552016-04-26 12:28:1194} // namespace cricket
kjellander@webrtc.org4056bb62016-02-12 05:47:5995
tereliusbfc1d552016-04-26 12:28:1196#endif // WEBRTC_PC_CURRENTSPEAKERMONITOR_H_