WebRTC(TODO)

1 简介

只是简单学学。。。

简单看了下,貌似就是SIP的下一代。因为我对SIP很熟,所以就比对着来写。

传统的SIP

WebRTC

网络端

终端

2 树莓派的实现

代码

bash 复制代码
webrtc-pi/
├── server.py
├── static/
│   └── client.html

server.py

python 复制代码
import cv2
import asyncio
from aiortc import MediaStreamTrack, RTCPeerConnection, RTCSessionDescription
from aiortc.contrib.media import MediaRelay
from flask import Flask, render_template, request, jsonify, send_from_directory
import json

app = Flask(__name__, static_url_path='')

pcs = set()
relay = MediaRelay()

# 视频采集轨道(OpenCV)
class VideoTrack(MediaStreamTrack):
    kind = "video"

    def __init__(self):
        super().__init__()
        self.cap = cv2.VideoCapture(0)

    async def recv(self):
        pts, time_base = await self.next_timestamp()
        ret, frame = self.cap.read()
        if not ret:
            return None
        # 关键:OpenCV 转 RGB
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        from av import VideoFrame
        video_frame = VideoFrame.from_ndarray(frame, format="rgb24")
        video_frame.pts = pts
        video_frame.time_base = time_base
        return video_frame

@app.route('/')
def index():
    return send_from_directory('static', 'client.html')

@app.route('/offer', methods=['POST'])
def offer():
    offer_sdp = request.get_json()
    pc = RTCPeerConnection()
    pcs.add(pc)

    video = VideoTrack()
    pc.addTrack(video)

    async def create_answer():
        await pc.setRemoteDescription(RTCSessionDescription(**offer_sdp))
        answer = await pc.createAnswer()
        await pc.setLocalDescription(answer)
        return pc.localDescription

    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    desc = loop.run_until_complete(create_answer())
    return jsonify({'sdp': desc.sdp, 'type': desc.type})

@app.route('/answer', methods=['POST'])
def answer():
    return "OK"

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

client.html

html 复制代码
<!DOCTYPE html>
<html>
<head>
  <title>Raspberry Pi WebRTC Camera</title>
</head>
<body>
  <h2>WebRTC Camera Stream</h2>
  <video id="video" autoplay playsinline controls></video>
  <script>
    const pc = new RTCPeerConnection();
    const video = document.getElementById("video");

    pc.ontrack = function (event) {
      video.srcObject = event.streams[0];
    };

    fetch("/offer", { method: "POST" })
      .then(res => res.json())
      .then(async ({ sdp, type }) => {
        await pc.setRemoteDescription({ sdp, type });
        const answer = await pc.createAnswer();
        await pc.setLocalDescription(answer);

        fetch("/answer", {
          method: "POST",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify(pc.localDescription),
        });
      });
  </script>
</body>
</html>

TODO

相关推荐
某林2129 小时前
ROS2 + WebRTC + MQTT 异构系统架构
架构·系统架构·机器人·硬件架构·webrtc·ros2
EasyGBS9 天前
RTSP、RTMP、HLS、FLV、WebRTC……国标GB28181视频平台EasyGBS五大流媒体协议,你的场景该用哪个?
音视频·webrtc
REDcker10 天前
libdatachannel 快速入门
c++·webrtc·datachannel
冯程13 天前
WebRTC 之 AllocationSequence 详解
webrtc·音视频开发
阿拉斯攀登15 天前
售货柜实战:IPC 拉流 → 抽帧 → YOLO 识别完整流水线
yolo·ffmpeg·音视频·webrtc·视频编解码
阿拉斯攀登15 天前
RTSP 拉流与录制:IPC 摄像头本地录像完整方案
ffmpeg·音视频·webrtc·实时音视频·视频编解码
阿拉斯攀登16 天前
数据通道:RTCDataChannel 可靠与不可靠传输
音视频·webrtc·实时音视频·媒体·视频编解码
REDcker16 天前
腾讯TRRO SDK深度剖析
音视频·webrtc·实时音视频·直播·rtc·腾讯
阿拉斯攀登16 天前
FFmpeg 转码详解:封装格式、编码器、码率控制
ffmpeg·音视频·webrtc·实时音视频·视频编解码
阿拉斯攀登16 天前
FFmpeg 入门:命令行的 20 个常用场景
ffmpeg·音视频·webrtc·视频编解码