The transactionID is for createStream and RPC-call in RTMP which is infrequent in most cases, thus we just use a map protected with a mutex to allocate the id. To lower the possibility of ABA issues, we increase transaction id on each allocation. To jump over crowded area fastly, we double the increment of `_trans_id_allocator' on each try.
| 1015 | // transaction id on each allocation. To jump over crowded area fastly, we |
| 1016 | // double the increment of `_trans_id_allocator' on each try. |
| 1017 | bool RtmpContext::AddTransaction(uint32_t* out_transaction_id, |
| 1018 | RtmpTransactionHandler* handler) { |
| 1019 | uint32_t transaction_id = 0; |
| 1020 | uint32_t step = 1; |
| 1021 | BAIDU_SCOPED_LOCK(_trans_mutex); |
| 1022 | for (int i = 0; i < 10; ++i) { |
| 1023 | transaction_id = _trans_id_allocator; |
| 1024 | _trans_id_allocator += step; |
| 1025 | if (transaction_id < 2) { // 0 and 1 are reserved by rtmp spec. |
| 1026 | continue; |
| 1027 | } |
| 1028 | step *= 2; // 1,2,4,8,16,32,64,128,256,512,1024 |
| 1029 | if (_trans_map.seek(transaction_id) == NULL) { |
| 1030 | _trans_map[transaction_id] = handler; |
| 1031 | *out_transaction_id = transaction_id; |
| 1032 | return true; |
| 1033 | } |
| 1034 | } |
| 1035 | return false; |
| 1036 | } |
| 1037 | |
| 1038 | RtmpTransactionHandler* |
| 1039 | RtmpContext::RemoveTransaction(uint32_t transaction_id) { |