Home | History | Annotate | Download | only in media
      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 
     36 #include "talk/base/basictypes.h"
     37 #include "talk/base/sigslot.h"
     38 
     39 namespace cricket {
     40 
     41 class BaseSession;
     42 class Call;
     43 class Session;
     44 struct AudioInfo;
     45 struct MediaStreams;
     46 
     47 // Note that the call's audio monitor must be started before this is started.
     48 // It's recommended that the audio monitor be started with a 100 ms period.
     49 class CurrentSpeakerMonitor : public sigslot::has_slots<> {
     50  public:
     51   CurrentSpeakerMonitor(Call* call, BaseSession* session);
     52   ~CurrentSpeakerMonitor();
     53 
     54   BaseSession* session() const { return session_; }
     55 
     56   void Start();
     57   void Stop();
     58 
     59   // Used by tests.  Note that the actual minimum time between switches
     60   // enforced by the monitor will be the given value plus or minus the
     61   // resolution of the system clock.
     62   void set_min_time_between_switches(uint32 min_time_between_switches);
     63 
     64   // This is fired when the current speaker changes, and provides his audio
     65   // SSRC.  This only fires after the audio monitor on the underlying Call has
     66   // been started.
     67   sigslot::signal2<CurrentSpeakerMonitor*, uint32> SignalUpdate;
     68 
     69  private:
     70   void OnAudioMonitor(Call* call, const AudioInfo& info);
     71   void OnMediaStreamsUpdate(Call* call,
     72                             Session* session,
     73                             const MediaStreams& added,
     74                             const MediaStreams& removed);
     75 
     76   // These are states that a participant will pass through so that we gradually
     77   // recognize that they have started and stopped speaking.  This avoids
     78   // "twitchiness".
     79   enum SpeakingState {
     80     SS_NOT_SPEAKING,
     81     SS_MIGHT_BE_SPEAKING,
     82     SS_SPEAKING,
     83     SS_WAS_SPEAKING_RECENTLY1,
     84     SS_WAS_SPEAKING_RECENTLY2
     85   };
     86 
     87   bool started_;
     88   Call* call_;
     89   BaseSession* session_;
     90   std::map<uint32, SpeakingState> ssrc_to_speaking_state_map_;
     91   uint32 current_speaker_ssrc_;
     92   // To prevent overswitching, switching is disabled for some time after a
     93   // switch is made.  This gives us the earliest time a switch is permitted.
     94   uint32 earliest_permitted_switch_time_;
     95   uint32 min_time_between_switches_;
     96 };
     97 
     98 }
     99 
    100 #endif  // TALK_SESSION_MEDIA_CURRENTSPEAKERMONITOR_H_
    101