立创庐山派使用的是K230芯片,按照教程刷了canmv固件,下载canmv ide,使用嘉楠社区的rtsp和wlan例程,修改成连接wifi以及RTSP推流例程
# Description: This example demonstrates how to stream video and audio to the network using the RTSP server.
#
# Note: You will need an SD card to run this example.
#
# You can run the rtsp server to stream video and audio to the networkimport networkfrom media.vencoder import *
from media.sensor import *
from media.media import *
import time, os
import _thread
import multimedia as mm
from time import *class RtspServer:def __init__(self,session_name="test",port=8554,video_type = mm.multi_media_type.media_h264,enable_audio=False):self.session_name = session_name # session nameself.video_type = video_type # 视频类型264/265self.enable_audio = enable_audio # 是否启用音频self.port = port #rtsp 端口号self.rtspserver = mm.rtsp_server() # 实例化rtsp serverself.venc_chn = VENC_CHN_ID_0 #venc通道self.start_stream = False #是否启动推流线程self.runthread_over = False #推流线程是否结束def start(self):# 初始化推流self._init_stream()self.rtspserver.rtspserver_init(self.port)# 创建sessionself.rtspserver.rtspserver_createsession(self.session_name,self.video_type,self.enable_audio)# 启动rtsp serverself.rtspserver.rtspserver_start()self._start_stream()# 启动推流线程self.start_stream = True_thread.start_new_thread(self._do_rtsp_stream,())def stop(self):if (self.start_stream == False):return# 等待推流线程退出self.start_stream = Falsewhile not self.runthread_over:sleep(0.1)self.runthread_over = False# 停止推流self._stop_stream()self.rtspserver.rtspserver_stop()#self.rtspserver.rtspserver_destroysession(self.session_name)self.rtspserver.rtspserver_deinit()def get_rtsp_url(self):return self.rtspserver.rtspserver_getrtspurl(self.session_name)def _init_stream(self):width = 1280height = 720width = ALIGN_UP(width, 16)# 初始化sensorself.sensor = Sensor()self.sensor.reset()self.sensor.set_framesize(width = width, height = height, alignment=12)self.sensor.set_pixformat(Sensor.YUV420SP)# 实例化video encoderself.encoder = Encoder()self.encoder.SetOutBufs(self.venc_chn, 8, width, height)# 绑定camera和vencself.link = MediaManager.link(self.sensor.bind_info()['src'], (VIDEO_ENCODE_MOD_ID, VENC_DEV_ID, self.venc_chn))# init media managerMediaManager.init()# 创建编码器chnAttr = ChnAttrStr(self.encoder.PAYLOAD_TYPE_H264, self.encoder.H264_PROFILE_MAIN, width, height)self.encoder.Create(self.venc_chn, chnAttr)def _start_stream(self):# 开始编码self.encoder.Start(self.venc_chn)# 启动cameraself.sensor.run()def _stop_stream(self):# 停止cameraself.sensor.stop()# 接绑定camera和vencdel self.link# 停止编码self.encoder.Stop(self.venc_chn)self.encoder.Destroy(self.venc_chn)# 清理bufferMediaManager.deinit()def _do_rtsp_stream(self):try:streamData = StreamData()while self.start_stream:os.exitpoint()# 获取一帧码流self.encoder.GetStream(self.venc_chn, streamData)# 推流for pack_idx in range(0, streamData.pack_cnt):stream_data = bytes(uctypes.bytearray_at(streamData.data[pack_idx], streamData.data_size[pack_idx]))self.rtspserver.rtspserver_sendvideodata(self.session_name,stream_data, streamData.data_size[pack_idx],1000)#print("stream size: ", streamData.data_size[pack_idx], "stream type: ", streamData.stream_type[pack_idx])# 释放一帧码流self.encoder.ReleaseStream(self.venc_chn, streamData)except BaseException as e:print(f"Exception {e}")finally:self.runthread_over = True# 停止rtsp serverself.stop()self.runthread_over = Trueif __name__ == "__main__":os.exitpoint(os.EXITPOINT_ENABLE)sta=network.WLAN(0)while(sta.active() == False):sta.active(1)print("wifi active......")while(sta.status() == False):#sta 连接 apprint(sta.connect("ssid名称","wifi密码"))print("wifi connected......")sleep(5)print(sta.ifconfig())# 创建rtsp server对象rtspserver = RtspServer()# 启动rtsp serverrtspserver.start()# 打印rtsp urlprint("rtsp server start:",rtspserver.get_rtsp_url())# 推流60s#sleep(60)while(1):sleep(60)# 停止rtsp serverrtspserver.stop()print("done")
在canmv ide里面保存为main.py自运行文件,重启后使用vlc拉流成功