public static void del_on_mk_media_changed(int regist, ref IntPtr sender) { try { if (regist == 1)//注册 { string schema = Marshal.PtrToStringAnsi(MediaServer.mk_media_source_get_schema(ref sender)); if (schema == "rtsp")//hls比其他两个的注册回调晚3秒,如果调用停止接口过快,会造成hls注册回调触发的时候重新将停止接口删除的stream插入到redis中,此处增加rtsp判断来解决此问题 { string vhost = Marshal.PtrToStringAnsi(MediaServer.mk_media_source_get_vhost(ref sender)); string app = Marshal.PtrToStringAnsi(MediaServer.mk_media_source_get_app(ref sender)); string stream = Marshal.PtrToStringAnsi(MediaServer.mk_media_source_get_stream(ref sender)); int readCount = MediaServer.mk_media_source_get_total_reader_count(ref sender); IntPtr trackinfos = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TrackInfos)) * 2); //IntPtr trackinfos = IntPtr.Zero; int trackCount = MediaServer.mk_media_source_get_tracks(ref sender, trackinfos); TrackInfos[] infos = new TrackInfos[trackCount]; for (int i = 0; i < trackCount; i++) { infos[i] = (TrackInfos)(Marshal.PtrToStructure(trackinfos + i * Marshal.SizeOf(typeof(TrackInfos)), typeof(TrackInfos))); } string redisValue = mediaServerConfig.Ipaddress + "_" + mediaServerConfig.mk_http_port + "_" + app + "_" + stream; Task task = Task.Factory.StartNew ( new Action(() => { if (app != "rtp") { stream = app + "_" + stream; } //else { if (!RedisHelper.GetDatabase().HashExists("live", stream)) { RedisHelper.GetDatabase().HashSet("live", new StackExchange.Redis.HashEntry[] { new StackExchange.Redis.HashEntry(stream, redisValue) }); } } } ) ); //Action action = new Action(() => //{ // if (app != "rtp") // { // stream = app + "_" + stream; // } // //else // { // if (!RedisHelper.GetDatabase().HashExists("live", stream)) // { // RedisHelper.GetDatabase().HashSet("live", new StackExchange.Redis.HashEntry[] { new StackExchange.Redis.HashEntry(stream, redisValue) }); // } // } //} // ); //action.BeginInvoke(null, null); //Task.Run(() => media_source_for_each()); } } else//反注册 { } } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); } }