{"id":477,"date":"2026-04-08T17:17:32","date_gmt":"2026-04-08T09:17:32","guid":{"rendered":"https:\/\/ziyuan.work\/?p=477"},"modified":"2026-04-08T17:17:32","modified_gmt":"2026-04-08T09:17:32","slug":"webrtc%ef%bc%88golang%ef%bc%89","status":"publish","type":"post","link":"https:\/\/ziyuan.work\/?p=477","title":{"rendered":"webRTC\uff08Golang\uff09"},"content":{"rendered":"\n<p>\u5728\u89c6\u9891\u76d1\u63a7\u3001\u5de5\u4e1a\u5de1\u68c0\u4ee5\u53ca\u8fb9\u7f18\u8ba1\u7b97\u7b49\u573a\u666f\u4e2d\uff0c\u5e38\u89c1\u7684\u4e00\u4e2a\u73b0\u5b9e\u95ee\u9898\u662f\u534f\u8bae\u5272\u88c2\uff1a\u524d\u7aef\u8bbe\u5907\u901a\u5e38\u901a\u8fc7RTSP\u63a8\u6d41\uff0c\u800c\u6d4f\u89c8\u5668\u539f\u751f\u5e76\u4e0d\u652f\u6301\u76f4\u63a5\u64ad\u653eRTSP\u3002\u8fd9\u5c31\u5e26\u6765\u4e86\u4e00\u4e2a\u5de5\u7a0b\u4e0a\u7684\u5173\u952e\u6311\u6218\u2014\u2014\u5982\u4f55\u5728\u4e0d\u5f15\u5165\u9ad8\u5ef6\u8fdf\u548c\u590d\u6742\u8f6c\u7801\u7684\u524d\u63d0\u4e0b\uff0c\u5c06\u8bbe\u5907\u4fa7\u7684\u89c6\u9891\u6d41\u9ad8\u6548\u5730\u5206\u53d1\u5230\u6d4f\u89c8\u5668\u7aef\u3002<\/p>\n\n\n\n<p>\u4e00\u79cd\u66f4\u76f4\u63a5\u4e14\u9ad8\u6548\u7684\u601d\u8def\uff0c\u662f\u5229\u7528WebRTC\u4f5c\u4e3a\u6d4f\u89c8\u5668\u4fa7\u7684\u5b9e\u65f6\u4f20\u8f93\u534f\u8bae\uff0c\u540c\u65f6\u5728\u670d\u52a1\u7aef\u5b8c\u6210\u534f\u8bae\u5c42\u7684\u6865\u63a5\uff0c\u5c06RTSP\u6d41\u8f6c\u6362\u4e3aWebRTC\u53ef\u6d88\u8d39\u7684RTP\u6570\u636e\u6d41\u3002\u8fd9\u79cd\u65b9\u5f0f\u907f\u514d\u4e86\u4f20\u7edf\u8f6c\u7801\u94fe\u8def\uff08\u4f8b\u5982<strong>FFMPEG+\u7eaf\u63a5\u53e3\u8f6c\u53d1<\/strong>\uff09\u5e26\u6765\u7684\u6027\u80fd\u635f\u8017\uff0c\u540c\u65f6\u4fdd\u7559\u4e86\u5b9e\u65f6\u6027\u7684\u4f18\u52bf\u3002<\/p>\n\n\n\n<p class=\"is-style-default\">\u901a\u8fc7golang\u7a0b\u5e8f\u5b9e\u73b0\u4e00\u4e2a\u5178\u578b\u7684\u89c6\u9891\u6865\u63a5\u67b6\u6784\uff1a\u4e0a\u6e38\u901a\u8fc7RTSP\u62c9\u6d41\uff08\u901a\u5e38\u6765\u81ea\u6444\u50cf\u5934\u6216\u63a8\u6d41\u5de5\u5177\uff09\uff0c\u670d\u52a1\u7aef\u5c06RTP\u5305\u8f6c\u53d1\u5230WebRTC PeerConnection\uff0c\u4e0b\u6e38\u6d4f\u89c8\u5668\u901a\u8fc7 WebRTC \u5b9e\u65f6\u64ad\u653e\u89c6\u9891\u3002\u6574\u4f53\u94fe\u8def\u4e3a\uff1aRTSP \u2192 RTP \u2192 Go\u670d\u52a1 \u2192 WebRTC \u2192 \u6d4f\u89c8\u5668\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large has-custom-border is-style-default\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"683\" src=\"https:\/\/ziyuan.work\/wp-content\/uploads\/2026\/04\/RTSP\u8f6cWebRTC\u89c6\u9891\u6865\u63a5\u67b6\u6784-1024x683.png\" alt=\"\" class=\"wp-image-498\" style=\"border-width:1px;border-top-left-radius:20px;border-top-right-radius:20px;border-bottom-left-radius:20px;border-bottom-right-radius:20px;box-shadow:var(--wp--preset--shadow--natural)\" srcset=\"https:\/\/ziyuan.work\/wp-content\/uploads\/2026\/04\/RTSP\u8f6cWebRTC\u89c6\u9891\u6865\u63a5\u67b6\u6784-1024x683.png 1024w, https:\/\/ziyuan.work\/wp-content\/uploads\/2026\/04\/RTSP\u8f6cWebRTC\u89c6\u9891\u6865\u63a5\u67b6\u6784-300x200.png 300w, https:\/\/ziyuan.work\/wp-content\/uploads\/2026\/04\/RTSP\u8f6cWebRTC\u89c6\u9891\u6865\u63a5\u67b6\u6784-768x512.png 768w, https:\/\/ziyuan.work\/wp-content\/uploads\/2026\/04\/RTSP\u8f6cWebRTC\u89c6\u9891\u6865\u63a5\u67b6\u6784.png 1536w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u96f6\u3001\u5728\u672c\u673a\u542f\u4e00\u4e2amediamtx\u4f5c\u4e3aRTSP\u670d\u52a1\u7aef\uff0c\u518d\u901a\u8fc7ffmpeg\u628a\u672c\u673a\u6444\u50cf\u5934\u63a8\u6d41\u5230\u670d\u52a1\u5668\u6765\u6a21\u62df\u6444\u50cf\u5934\u7684RTSP\u6d41<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/mediamtx &amp;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/ffmpeg -f v4l2 -i \/dev\/video0 \\\n-vcodec libx264 -preset veryfast -tune zerolatency \\\n-f rtsp -rtsp_transport tcp \\\nrtsp:\/\/test:123456@127.0.0.1:8554\/test<\/code><\/pre>\n\n\n\n<p>\u4e00\u3001golang\u7a0b\u5e8f\u5165\u53e3main\u4e2d\u9996\u5148\u786e\u5b9aRTSP\u5730\u5740\uff0c\u5e76\u5f00\u4e00\u4e2aGin HTTP\u670d\u52a1\uff0c\u540c\u65f6\u7ef4\u62a4\u4e00\u4e2aclients\u6620\u5c04\uff0c\u7528\u4e8e\u4fdd\u5b58\u6bcf\u4e2aWebRTC\u5ba2\u6237\u7aef\u5bf9\u5e94\u7684Track\uff1a<\/p>\n\n\n\n<p class=\"is-style-text-annotation is-style-text-annotation--1\">\u6bcf\u4e2a\u5ba2\u6237\u7aef\u5e76\u4e0d\u662f\u5355\u72ec\u62c9\u6d41\uff0c\u800c\u662f\u5171\u4eab\u540c\u4e00 RTSP\u8f93\u5165\u6d41\uff0c\u670d\u52a1\u7aef\u901a\u8fc7fan-out\uff08\u6247\u51fa\uff09\u673a\u5236\u5c06RTP\u5305\u5199\u5165\u591a\u4e2aWebRTC Track\uff0c\u4ece\u800c\u5b9e\u73b0\u201c\u4e00\u8def\u8f93\u5165\uff0c\u591a\u8def\u8f93\u51fa\u201d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import (\n\t\"github.com\/bluenviron\/gortsplib\/v5\"\n\t\"github.com\/bluenviron\/gortsplib\/v5\/pkg\/base\"\n\t\"github.com\/bluenviron\/gortsplib\/v5\/pkg\/description\"\n\t\"github.com\/bluenviron\/gortsplib\/v5\/pkg\/format\"\n\t\"github.com\/gin-gonic\/gin\"\n\t\"github.com\/pion\/rtp\"\n\t\"github.com\/pion\/webrtc\/v3\"\n)\n\nrtspURL := os.Getenv(\"RTSP_URL\")\nif rtspURL == \"\" {\n\trtspURL = \"rtsp:\/\/test:123456@127.0.0.1:8554\/test\"\n}\n\nvar clientsMu sync.Mutex\nclients := map&#91;string]*webrtc.TrackLocalStaticRTP{}<\/code><\/pre>\n\n\n\n<p>\u4e8c\u3001HTTP\u90e8\u5206\u5206\u4e3a\u4e09\u4e2a\u8def\u7531\uff1a<\/p>\n\n\n\n<p>1\uff09\u201c\/\u201d\u8def\u7531\u8fd4\u56de\u64ad\u653e\u5668\u9875\u9762<br>2\uff09\u201c\/offer\u201d\u8def\u7531\u5904\u7406WebRTC SDP\u4fe1\u4ee4\u4ea4\u6362<br>3\uff09\u201c\/stats\u201d\u8def\u7531\u8fd4\u56de\u5b9e\u65f6\u7801\u7387\u4e0e\u5305\u901f\u7387<\/p>\n\n\n\n<p>\u524d\u7aef\u9875\u9762\u6838\u5fc3\u903b\u8f91\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p class=\"is-style-text-annotation is-style-text-annotation--2\">\u6d4f\u89c8\u5668\u521b\u5efaRTCPeerConnection \u2192 \u751f\u6210Offer\uff08SDP\uff09\u2192 \u53d1\u9001\u7ed9\u670d\u52a1\u7aef \u2192 \u670d\u52a1\u7aef\u751f\u6210Answer \u2192 \u6d4f\u89c8\u5668\u8bbe\u7f6e\u8fdc\u7aef\u63cf\u8ff0\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u91cc\u6ca1\u6709\u4f7f\u7528STUN\/TURN\uff08iceServers \u4e3a\u7a7a\uff09\uff0c\u610f\u5473\u7740\u8be5\u65b9\u6848\u9ed8\u8ba4\u8fd0\u884c\u5728\u5185\u7f51\u6216\u53ef\u76f4\u8fde\u73af\u5883\uff0c\u5426\u5219\u65e0\u6cd5\u7a7f\u900fNAT\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pc=new RTCPeerConnection({iceServers:&#91;]});\npc.addTransceiver('video',{direction:'recvonly'});\n\nconst offer=await pc.createOffer();\nawait pc.setLocalDescription(offer);\n\nconst resp=await fetch('\/offer',{\n  method:'POST',\n  headers:{'Content-Type':'application\/json'},\n  body:JSON.stringify({sdp:offer.sdp,type:offer.type})\n});\n\nconst answer=await resp.json();\nawait pc.setRemoteDescription({type:answer.type,sdp:answer.sdp});<\/code><\/pre>\n\n\n\n<p>\u670d\u52a1\u7aef\u201c\/offer\u201d\u8def\u7531\u5904\u7406\u903b\u8f91\u662fWebRTC\u7684\u6838\u5fc3\uff1a<\/p>\n\n\n\n<p class=\"is-style-text-annotation is-style-text-annotation--3\">\u8fd9\u91cc\u505a\u4e86\u4e24\u4ef6\u5173\u952e\u4e8b\u60c5\uff1a<br>1\uff09\u6ce8\u518c\u7f16\u89e3\u7801\u5668\uff08H264\/H265\uff09<br>2\uff09\u521b\u5efa PeerConnection<br><br>WebRTC\u672c\u8d28\u4e0a\u662fRTP\u7684\u589e\u5f3a\u7248\uff0c\u4f46\u6d4f\u89c8\u5668\u5bf9\u7f16\u7801\u683c\u5f0f\u8981\u6c42\u4e25\u683c\uff0c\u56e0\u6b64\u5fc5\u987b\u663e\u5f0f\u6ce8\u518ccodec\uff0c\u5426\u5219SDP\u534f\u5546\u65e0\u6cd5\u5339\u914d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>m := webrtc.MediaEngine{}\n_ = m.RegisterCodec(webrtc.RTPCodecParameters{\n\tRTPCodecCapability: webrtc.RTPCodecCapability{\n\t\tMimeType: webrtc.MimeTypeH264,\n\t\tClockRate: 90000,\n\t\tSDPFmtpLine: \"packetization-mode=1;profile-level-id=42e01f\",\n\t},\n\tPayloadType: 96,\n}, webrtc.RTPCodecTypeVideo)\n\napi := webrtc.NewAPI(webrtc.WithMediaEngine(&amp;m))\npc, _ := api.NewPeerConnection(webrtc.Configuration{})<\/code><\/pre>\n\n\n\n<p>\u521b\u5efaTrack\uff0c\u5e76\u7ed1\u5b9a\u5230PeerConnection\uff1a<\/p>\n\n\n\n<p class=\"is-style-text-annotation is-style-text-annotation--4\">Track\u662fWebRTC\u4e2d\u5a92\u4f53\u53d1\u9001\u7684\u62bd\u8c61\uff0c\u672c\u8d28\u4e0a\u662fRTP\u6d41\u7684\u51fa\u53e3\u3002\u8fd9\u91cc\u4f7f\u7528TrackLocalStaticRTP\uff0c\u610f\u5473\u7740\u53ef\u4ee5\u624b\u52a8\u5199\u5165RTP\u5305<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>track, _ := webrtc.NewTrackLocalStaticRTP(\n\twebrtc.RTPCodecCapability{\n\t\tMimeType: webrtc.MimeTypeH264,\n\t\tClockRate: 90000,\n\t},\n\t\"video\", \"pion\",\n)\npc.AddTrack(track)<\/code><\/pre>\n\n\n\n<p>\u4fe1\u4ee4\u4ea4\u6362\u90e8\u5206\uff1a<\/p>\n\n\n\n<p class=\"is-style-text-annotation is-style-text-annotation--5\">\u63a5\u6536\u6d4f\u89c8\u5668Offer \u2192 \u8bbe\u7f6e\u8fdc\u7aef SDP \u2192 \u751f\u6210 Answer \u2192 \u8fd4\u56de\u7ed9\u6d4f\u89c8\u5668\u3002GatheringCompletePromise\u7528\u4e8e\u7b49\u5f85ICE candidate\u6536\u96c6\u5b8c\u6210\uff0c\u5426\u5219SDP\u4e0d\u5b8c\u6574\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>offer := webrtc.SessionDescription{Type: webrtc.SDPTypeOffer, SDP: req.SDP}\npc.SetRemoteDescription(offer)\n\nans, _ := pc.CreateAnswer(nil)\npc.SetLocalDescription(ans)\n\n&lt;-webrtc.GatheringCompletePromise(pc)<\/code><\/pre>\n\n\n\n<p>\u5c06Track\u5b58\u5165clients\uff0c\u7528\u4e8e\u540e\u7eedRTP\u5206\u53d1\uff1a<\/p>\n\n\n\n<p class=\"is-style-text-annotation is-style-text-annotation--6\">\u8fd9\u91cc\u901a\u8fc7DESCRIBE + SETUP\u5efa\u7acbRTSP\u4f1a\u8bdd\uff0c\u5e76\u89e3\u6790\u5a92\u4f53\u683c\u5f0f\uff08H264\/H265\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>clientsMu.Lock()\nclients&#91;c.ClientIP()+\"_\"+time.Now().Format(\"150405\")] = track\nclientsMu.Unlock()<\/code><\/pre>\n\n\n\n<p>\u6838\u5fc3\u8f6c\u53d1\u903b\u8f91\u5728OnPacketRTP\uff1a<\/p>\n\n\n\n<p class=\"is-style-text-annotation is-style-text-annotation--7\">RTSP \u2192 \u89e3\u590d\u7528 \u2192 \u5f97\u5230RTP\u5305 \u2192 \u5e7f\u64ad\u5199\u5165\u6240\u6709WebRTC Track<br>\u670d\u52a1\u7aef\u672c\u8d28\u4e0a\u53ea\u662f\u4e00\u4e2aRTP\u8f6c\u53d1\u5668\uff0c\u5e76\u4e0d\u8fdb\u884c\u8f6c\u7801\uff0c\u8fd9\u5e26\u6765\u4e24\u4e2a\u91cd\u8981\u7279\u6027\uff1a<br>\u4f18\u70b9\uff1a\u5ef6\u8fdf\u6781\u4f4e\uff08\u57fa\u672c\u65e0\u7f16\u7801\u5ef6\u8fdf\uff09<br>CPU\u5360\u7528\u6781\u5c0f<br>\u67b6\u6784\u7b80\u5355\u7a33\u5b9a<br>\u9650\u5236\uff1a\u6d4f\u89c8\u5668\u5fc5\u987b\u652f\u6301\u8be5\u7f16\u7801\uff08\u901a\u5e38\u662fH264\uff09<br>RTSP\u8f93\u5165\u5fc5\u987b\u4e0eWebRTC codec\u517c\u5bb9<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cli.OnPacketRTP(media, formaH264, func(pkt *rtp.Packet) {\n\tclientsMu.Lock()\n\ttotalBytes += uint64(len(pkt.Payload))\n\ttotalPackets++\n\n\tactiveClients := make(&#91;]*webrtc.TrackLocalStaticRTP, 0, len(clients))\n\tfor _, t := range clients {\n\t\tactiveClients = append(activeClients, t)\n\t}\n\tclientsMu.Unlock()\n\n\tfor _, t := range activeClients {\n\t\tt.WriteRTP(pkt)\n\t}\n})<\/code><\/pre>\n\n\n\n<p>\u7edf\u8ba1\u63a5\u53e3\u201c\/stats\u201d\u8def\u7531\u901a\u8fc7\u7b80\u5355\u8ba1\u6570\u5b9e\u73b0\u7801\u7387\u8ba1\u7b97\uff1a<\/p>\n\n\n\n<p class=\"is-style-text-annotation is-style-text-annotation--8\">\u524d\u7aef\u6bcf\u79d2\u62c9\u53d6\u4e00\u6b21\uff0c\u5b9e\u73b0\u5b9e\u65f6\u76d1\u63a7\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kbps := uint64(float64(totalBytes*8) \/ 1024 \/ duration)\npktps := uint64(float64(totalPackets) \/ duration)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u7b2c\u4e00\uff0cWebRTC\u5e76\u4e0d\u8d1f\u8d23\u201c\u83b7\u53d6\u89c6\u9891\u201d\uff0c\u5b83\u53ea\u8d1f\u8d23\u201c\u4f20\u8f93\u5a92\u4f53\u6d41\u201d\u3002\u89c6\u9891\u6e90\u53ef\u4ee5\u6765\u81ea RTSP\u3001\u6587\u4ef6\u3001\u6444\u50cf\u5934\u7b49\u3002<\/p>\n\n\n\n<p>\u7b2c\u4e8c\uff0cWebRTC\u7684\u5173\u952e\u4e0d\u662fAPI\uff0c\u800c\u662f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>SDP\u534f\u5546<\/li>\n\n\n\n<li>ICE\u5efa\u8fde<\/li>\n\n\n\n<li>RTP\u6536\u53d1<\/li>\n<\/ul>\n\n\n\n<p>\u7b2c\u4e09\uff0c\u8fd9\u79cd\u67b6\u6784\u5c5e\u4e8e\u5178\u578b\u7684\u201c\u8fb9\u7f18\u7f51\u5173\u6a21\u5f0f\u201d\uff1a<\/p>\n\n\n\n<p>RTSP\uff08\u8bbe\u5907\u4fa7\u534f\u8bae\uff09 \u2192 WebRTC\uff08\u6d4f\u89c8\u5668\u534f\u8bae\uff09<\/p>\n\n\n\n<p>\u5728\u5de5\u4e1a\u76d1\u63a7\u3001\u89c6\u9891\u5de1\u68c0\u3001\u8fb9\u7f18\u8ba1\u7b97\u4e2d\u975e\u5e38\u5e38\u89c1<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u89c6\u9891\u76d1\u63a7\u3001\u5de5\u4e1a\u5de1\u68c0\u4ee5\u53ca\u8fb9\u7f18\u8ba1\u7b97\u7b49\u573a\u666f\u4e2d\uff0c\u5e38\u89c1\u7684\u4e00\u4e2a\u73b0\u5b9e\u95ee\u9898\u662f\u534f\u8bae\u5272\u88c2\uff1a\u524d\u7aef\u8bbe\u5907\u901a\u5e38\u901a\u8fc7RTSP\u63a8\u6d41\uff0c\u800c\u6d4f\u89c8\u5668 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-477","post","type-post","status-publish","format-standard","hentry","category-solution"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ziyuan.work\/index.php?rest_route=\/wp\/v2\/posts\/477","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ziyuan.work\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ziyuan.work\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ziyuan.work\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ziyuan.work\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=477"}],"version-history":[{"count":27,"href":"https:\/\/ziyuan.work\/index.php?rest_route=\/wp\/v2\/posts\/477\/revisions"}],"predecessor-version":[{"id":507,"href":"https:\/\/ziyuan.work\/index.php?rest_route=\/wp\/v2\/posts\/477\/revisions\/507"}],"wp:attachment":[{"href":"https:\/\/ziyuan.work\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ziyuan.work\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ziyuan.work\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}