diff --git a/src/Rtsp/Rtsp.cpp b/src/Rtsp/Rtsp.cpp index b5b50360..579a881e 100644 --- a/src/Rtsp/Rtsp.cpp +++ b/src/Rtsp/Rtsp.cpp @@ -8,12 +8,13 @@ * may be found in the AUTHORS file in the root of the source tree. */ +#include +#include +#include #include "Rtsp.h" #include "Common/Parser.h" #include "Common/config.h" #include "Network/Socket.h" -#include -#include using namespace std; using namespace toolkit; @@ -392,9 +393,13 @@ public: private: void setRange(uint16_t start_pos, uint16_t end_pos) { + std::mt19937 rng(std::random_device {}()); lock_guard lck(_pool_mtx); + auto it = _port_pair_pool.begin(); while (start_pos < end_pos) { - _port_pair_pool.emplace_back(start_pos++); + // 随机端口排序,防止重启后导致分配的端口重复 + _port_pair_pool.insert(it, start_pos++); + it = _port_pair_pool.begin() + (rng() % (1 + _port_pair_pool.size())); } }