用mqtt制作音视频会议系统

以下是我通过搭建emqx实现将mqtt搭建为一个视频会议的实现方式

1.创建emqx 使用docker部署mqtt服务器

docker run -d --name emqx -p 1883:1883 -p 8083:8083 -p 8084:8084 -p 8883:8883 -p 18083:18083 emqx/emqx:5.3.0

2.推送本地画面(桌面画面)

def capture_and_publish_image(client):
    # 对当前屏幕进行截图并压缩为JPEG格式,压缩质量为30%,然后转换为base64编码发送到MQTT broker
    screenshot = ImageGrab.grab()
    screenshot = screenshot.convert("RGB")
    # screenshot = screenshot.resize((screenshot.width, screenshot.height), Image.ANTIALIAS)
    screenshot = screenshot.save("screenshot.jpg", format="JPEG", quality=30)
    with open("screenshot.jpg", "rb") as image_file:
        encoded_image = base64.b64encode(image_file.read())

    # 取出encoded_image 长度-2的数据 这里去掉了== ,不然无法收到mqtt发送的消息
    encoded_image = encoded_image[:-2]
    # 将数据推送到/video 中
    client.publish("/video", encoded_image)
    
# Connect to MQTT broker
client = mqtt.Client()
client.on_connect = on_connect
client.on_publish = on_publish
#client.username_pw_set("账号", "密码")
client.connect("127.0.0.1")

# Capture and publish image continuously
while True:
    capture_and_publish_image(client)

3.客户端渲染画面

# -*- coding: utf-8 -*-
import cv2
import base64
import numpy as np
import threading
import time
import paho.mqtt.client as mqtt
def play_video_stream():
    cap = cv2.VideoCapture(0)
    while True:
        ret, frame = cap.read()
        if not ret:
            break
        
        cv2.imshow("Video Stream", frame)
        if cv2.waitKey(1) == ord('q'):
            break
    
    cap.release()
    cv2.destroyAllWindows()

def on_message(client, userdata, msg):
    if msg.topic == "/video":

        # msg.payload 是bytes类型,因为发送时候去掉了==,所以需要补上
        data  =  msg.payload + b'=='
        encoded_image = data
        image_data = base64.b64decode(encoded_image)
        nparr = np.frombuffer(image_data, np.uint8)
        frame = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
        cv2.imshow("Video Stream", frame)
        cv2.waitKey(1)

def on_connect(client, userdata, flags, rc):
    print("Connected with result code " + str(rc))

def connect_and_subscribe():
    client = mqtt.Client()
    client.on_connect = on_connect
    client.on_message = on_message
    #client.username_pw_set("mqtt账号", "mqtt密码")
    client.connect("127.0.0.1")
    client.subscribe("/video")
    client.loop_forever()

if __name__ == "__main__":
    threading.Thread(target=play_video_stream).start()
    threading.Thread(target=connect_and_subscribe).start()

呈现效果如下(图片为显示的推送图片,很流畅)
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值