Home | History | Annotate | Download | only in source
      1 /*
      2  *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
      3  *
      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.
      9  */
     10 
     11 #include "webrtc/modules/rtp_rtcp/source/ssrc_database.h"
     12 
     13 #include <assert.h>
     14 #include <stdlib.h>
     15 
     16 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
     17 
     18 #ifdef _WIN32
     19     #include <windows.h>
     20     #include <MMSystem.h> //timeGetTime
     21 
     22 // TODO(hellner): investigate if it is necessary to disable these warnings.
     23     #pragma warning(disable:4311)
     24     #pragma warning(disable:4312)
     25 #else
     26     #include <stdio.h>
     27     #include <string.h>
     28     #include <time.h>
     29     #include <sys/time.h>
     30 #endif
     31 
     32 namespace webrtc {
     33 SSRCDatabase*
     34 SSRCDatabase::StaticInstance(CountOperation count_operation)
     35 {
     36   SSRCDatabase* impl =
     37       GetStaticInstance<SSRCDatabase>(count_operation);
     38   return impl;
     39 }
     40 
     41 SSRCDatabase*
     42 SSRCDatabase::GetSSRCDatabase()
     43 {
     44     return StaticInstance(kAddRef);
     45 }
     46 
     47 void
     48 SSRCDatabase::ReturnSSRCDatabase()
     49 {
     50     StaticInstance(kRelease);
     51 }
     52 
     53 uint32_t
     54 SSRCDatabase::CreateSSRC()
     55 {
     56     CriticalSectionScoped lock(_critSect);
     57 
     58     uint32_t ssrc = GenerateRandom();
     59 
     60     while(_ssrcMap.find(ssrc) != _ssrcMap.end())
     61     {
     62         ssrc = GenerateRandom();
     63     }
     64     _ssrcMap[ssrc] = 0;
     65 
     66     return ssrc;
     67 }
     68 
     69 int32_t
     70 SSRCDatabase::RegisterSSRC(const uint32_t ssrc)
     71 {
     72     CriticalSectionScoped lock(_critSect);
     73     _ssrcMap[ssrc] = 0;
     74     return 0;
     75 }
     76 
     77 int32_t
     78 SSRCDatabase::ReturnSSRC(const uint32_t ssrc)
     79 {
     80     CriticalSectionScoped lock(_critSect);
     81     _ssrcMap.erase(ssrc);
     82     return 0;
     83 }
     84 
     85 SSRCDatabase::SSRCDatabase()
     86 {
     87     // we need to seed the random generator, otherwise we get 26500 each time, hardly a random value :)
     88 #ifdef _WIN32
     89     srand(timeGetTime());
     90 #else
     91     struct timeval tv;
     92     struct timezone tz;
     93     gettimeofday(&tv, &tz);
     94     srand(tv.tv_usec);
     95 #endif
     96 
     97     _critSect = CriticalSectionWrapper::CreateCriticalSection();
     98 }
     99 
    100 SSRCDatabase::~SSRCDatabase()
    101 {
    102     _ssrcMap.clear();
    103     delete _critSect;
    104 }
    105 
    106 uint32_t SSRCDatabase::GenerateRandom()
    107 {
    108     uint32_t ssrc = 0;
    109     do
    110     {
    111         ssrc = rand();
    112         ssrc = ssrc <<16;
    113         ssrc += rand();
    114 
    115     } while (ssrc == 0 || ssrc == 0xffffffff);
    116 
    117     return ssrc;
    118 }
    119 }  // namespace webrtc
    120