public static void del_on_mk_media_source_find_cb(IntPtr userdata, ref IntPtr mk_media_source) { try { //IntPtr ptr = mk_media_source; string schema = Marshal.PtrToStringAnsi(MediaServer.mk_media_source_get_schema(ref mk_media_source)); if (schema != "rtsp") { return; } string vhost = Marshal.PtrToStringAnsi(MediaServer.mk_media_source_get_vhost(ref mk_media_source)); string app = Marshal.PtrToStringAnsi(MediaServer.mk_media_source_get_app(ref mk_media_source)); string stream = Marshal.PtrToStringAnsi(MediaServer.mk_media_source_get_stream(ref mk_media_source)); int readCount = MediaServer.mk_media_source_get_total_reader_count(ref mk_media_source); string key = GetKey(app, stream); if (!dic_readerCount.ContainsKey(key)) { dic_readerCount.Add(key, readCount); } else { if (dic_readerCount[key] == 0 && readCount == 0) { string ret = DoMedia.Stop(app, stream); } else { dic_readerCount[key] = readCount; } } //Thread thread = new Thread(new ParameterizedThreadStart(GetTotalReaderCount)); //thread.Start(mk_media_source); //Task.Run(() => GetTotalReaderCount(ref ptr)); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); } }
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); } }