基於AES加密的RTSP/RTMP多路轉發設計方案
- 2019 年 10 月 6 日
- 筆記
很多開發者跟我們探討,除了Windows推送端採集編碼的音影片數據可以加密外,其他RTSP/RTMP流如果想更安全的轉推到RTMP伺服器或相應CDN改怎麼辦?
實際上,RTMP整體加密方案的時候已經考慮到這種情況,SmartStreamRelayDemo在拉取RTSP或RTMP流,轉推RTMP的時候,可以選擇加密影片,加密音頻或音影片都加密,廢話不多說,參看程式碼:
bool nt_stream_relay_wrapper::StartPush(const std::string& url) { if ( is_pushing_ ) return false; if ( url.empty() ) return false; if ( !OpenPushHandle() ) return false; auto push_handle = GetPushHandle(); ASSERT(push_handle != nullptr); ASSERT(push_api_ != NULL); if ( NT_ERC_OK != push_api_->SetURL(push_handle, url.c_str(), NULL) ) { if ( !is_started_rtsp_stream_ ) { push_api_->Close(push_handle); SetPushHandle(nullptr); } return false; } // 加密測試 +++ push_api_->SetRtmpEncryptionOption(push_handle, url.c_str(), 1, 1); NT_BYTE test_key[16] = {'1', '2', '3'}; push_api_->SetRtmpEncryptionKey(push_handle, url.c_str(), test_key, 16); // 加密測試 -- if ( NT_ERC_OK != push_api_->StartPublisher(push_handle, NULL) ) { if ( !is_started_rtsp_stream_ ) { push_api_->Close(push_handle); SetPushHandle(nullptr); } return false; } // // test push rtsp ++ // push_api_->SetPushRtspTransportProtocol(push_handle, 1); // // push_api_->SetPushRtspTransportProtocol(push_handle, 2); // push_api_->SetPushRtspURL(push_handle, "rtsp://player.daniulive.com:554/liverelay111.sdp"); // push_api_->StartPushRtsp(push_handle, 0); // // test push rtsp-- is_pushing_ = true; return true; }
上圖:

這個時候,輸入轉發設置的Key(支援aes 128, aes 192, aes 256加密,即將發布SM4加密),方可正常播放音影片數據。
大牛直播SDK(Github)這種方案的優勢在於基於AES音影片逐幀數據加密音影片數據,第三方即便是破解了URL,也沒法播放,通過抓包工具抓取到數據,也沒法正常顯示。