实时音视频之toy_rtc(6)——网络模拟器

本文介绍了toy_rtc项目中的网络模拟器实现,用于在测试实时音视频时模拟弱网环境,包括丢包、延迟、抖动和带宽限制等功能。通过随机数产生器和数据包定义,该模拟器能够创建各种网络状况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

介绍

 为了模拟弱网环境,在测试的时候需要使用弱网工具制造各种各样的弱网场景。常见的弱网工具有TC等。toy_rtc里面简单的实现了一个弱网模拟器。


包含的功能

  • 模拟丢包
  • 模拟延迟
  • 模拟抖动
  • 模拟带宽限制

实现

随机数产生器
#ifndef RANDOMGENERATOR_H
#define RANDOMGENERATOR_H
/*
 * 随机数产生器
 */
#include <random>
#include <iostream>
#include <iomanip>
#include <string>
#include <map>
#include <random>
#include <cmath>
#include <time.h>

class RandomGenerator{
   
public:
    enum{
   
        kNormalDistribution,
        kRandomDistribution ,
    };

    RandomGenerator(){
   
        this->mode = kNormalDistribution;
        SetNormalDistributionParam(700,300);
        SetRandomDistributionParam(400,1000);
    }

    void SetMode(int mode){
   
        this->mode = mode;
    }

    void SetNormalDistributionParam(int avg,int mse){
   
		engine tmp_gen(GetSeed());
        normal_gen = tmp_gen;

		std::normal_distribution<double> tmp_normal(avg,mse);
        normal_dist = tmp_normal;
    }

    void SetRandomDistributionParam(int low,int up){
   
		engine tmp_gen(GetSeed());
        random_gen = tmp_gen;

		std::uniform_int_distribution<int> tmp_random(low,up);
        random_dist = tmp_random;
    }

    int RandomNum(){
   
        if(mode == kNormalDistribution){
   
            return normal_dist(normal_gen);
        }
        else{
   
            return random_dist(random_gen);
        }
    }
    static void Test(int mode,int first,int second){
   
        RandomGenerator g;

        g.SetMode(mode);
        g.SetNormalDistributionParam(first,second);
        g.SetRandomDistributionParam(first,second);

        if(mode == RandomGenerator::kNormalDistribution){
   
            std::map<int, int> hist;
            for(int n=0; n<10000; ++n) {
   
                ++hist[g.RandomNum()];
            }
            for(auto p : hist) {
   
                std::cout << std::fixed << std::setprecision(1) << std::setw(2)
                          << p.first << ' ' << std::string(p.second/10, '*') << '\n';
            }
        }
        else{
   
            for(int n=0; n<10; ++n)
                std::cout << g.RandomNum() << ' ';
            std::cout << '\n';
        }
    }

private:
    int64_t GetSeed(){
   
        return rd();
        //return time(NULL);
    }

  //  typedef std::default_random_engine engine ;
    typedef std::mt19937 engine ;

private:

	std::random_device rd; // 种子产生器
	engine normal_gen; // 随机数引擎,用于产生随机数
	engine random_gen;
	std::normal_distribution<double> normal_dist; // 分布模型,让引擎产生的随机数符合某一个分布
	std::uniform_int_distribution
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值