随笔 - 1936  文章 - 0  评论 - 8  阅读 - 44万

TSINGSEE青犀视频流媒体播放器EasyPlayer-RTMP定制窗体开发过程分享

因为2020年年底的时候各大浏览器厂商逐渐开始摒弃FLASH,导致基于WEB的RTMP协议流播放被大家诟病,这时候客户端又逐渐被大家捡起来使用。这两天就有一个用户需要定制一个RTMP低延迟的播放器,需求如下:

1、界面简洁,支持窗体大小控制;
2、功能按钮通过右键给出菜单;
3、播放流地址、缓存设置、OSD叠加功能等放到配置文件中;
4、最主要的是低延迟播放;

根据需求内容,我们打算用EasyPlayer-RTMP进行改造,因为EasyPlayer-RTMP底层是基于EasyRTMPClient做的低延迟播放器,EasyRTMPClient可以提供稳定的拉流,回调数据清晰,兼容H264和H265。

我们先看下改造后的界面如下图:

 

对比下改造前的页面:

 

 

 接下来介绍改造过程:
1、将界面的配置项全部修改到配置文件中去,增加一个XML读写的类XMLOperate(尾部附加),如下,可以读写配置文件:

 

2、将播放、截图、录像、OSD显示等功能做到右键菜单中,增加contextMenuStrip控件:

 

 播放功能实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
private void 播放ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            switch (XMLOperate.RENDER_FORMAT)
            {
                case "GDI":
                    RENDER_FORMAT = PlayerSdk.RENDER_FORMAT.DISPLAY_FORMAT_RGB24_GDI; break;
                case "RGB565":
                    RENDER_FORMAT = PlayerSdk.RENDER_FORMAT.DISPLAY_FORMAT_RGB565; break;
                case "YV12":
                    RENDER_FORMAT = PlayerSdk.RENDER_FORMAT.DISPLAY_FORMAT_YV12; break;
                case "YUY2":
                    RENDER_FORMAT = PlayerSdk.RENDER_FORMAT.DISPLAY_FORMAT_YUY2; break;
                default:
                    break;
            }
            var isPlay = false;
            if (this.播放ToolStripMenuItem.Text == "播放")
            { isPlay = true; }
            else
            { isPlay = false; }
            
            if (isPlay)
            {
                string RTSPStreamURI = XMLOperate.PlayerURL;// "rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov";
                channelID = PlayerSdk.EasyPlayer_OpenStream(RTSPStreamURI, this.panel1.Handle, RENDER_FORMAT, isTCP ? 1 : 0, "", "", callBack, IntPtr.Zero, isHardEncode);
                if (channelID > 0)
                {
                    PlayerSdk.EasyPlayer_SetFrameCache(channelID, 3);
                    this.播放ToolStripMenuItem.Text = "停止";
                    this.DecodeType.Enabled = false;
                }
            }
            else
            {
                int ret = PlayerSdk.EasyPlayer_CloseStream(channelID);
                if (ret == 0)
                {
                    this.播放ToolStripMenuItem.Text = "播放";
                    this.DecodeType.Enabled = true;
                    channelID = -1;
                    this.panel1.Refresh();
                }
            }
        }

截图功能实现:

1
2
3
4
5
6
7
8
9
10
11
12
/// <summary>
        /// 截图.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void Snop_MenuItem_Click(object sender, EventArgs e)
        {
            if (channelID <= 0)
                return;
            int ret = PlayerSdk.EasyPlayer_PicShot(channelID);
        }

其它功能类似实现。最后我们来看下效果:

 

 

配置参数如下:

1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="utf-8"?>
<启动配置参数>
  <URL>rtmp://183.224.164.130:10085/hls/hhsx2</URL>
  <渲染模式>GDI</渲染模式>
  <硬解>false</硬解>
  <缓存值>3</缓存值>
  <校验KEY值>59615A67426F69576B5A7541306C74676E3651744A663478567778576F502B6C2F32566863336B3D</校验KEY值>
  <备用参数A>RTMPMediaPlayer</备用参数A>
  <备用参数B>这是EasyPlayer-RTMP-Win播放器的字幕叠加接口的效果!</备用参数B>
</启动配置参数>

XMLOperate类实现如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
class XMLOperate
    {
       /// <summary>
       /// 播放的URL
       /// </summary>
        public static string PlayerURL = "";
        /// <summary>
        /// 渲染模式
        /// </summary>
        public static string RENDER_FORMAT = "";
        /// <summary>
        /// 是否硬解
        /// </summary>
        public static string isHardEncode = "";
        /// <summary>
        /// 缓存帧数
        /// </summary>
        public static string CacheFream = "";
        /// <summary>
        /// 授权KEY
        /// </summary>
        public static string KEY = "";
        /// <summary>
        /// 窗体名称
        /// </summary>
        public static string BYCSA = "";
        /// <summary>
        /// OSD叠加内容
        /// </summary>
        public static string BYCSB = "";
         
        public static void InitAppsettings()
        {
            CreateAndInitParamFileAppSettings();
            ReadAppSettingParamsFromConfigFile();
        }
  
        private static void CreateAndInitParamFileAppSettings()
        {
            try
            {
                //创建缺省文件
                XmlDocument XmlDoc = new XmlDocument();
                string FileContent = "<?xml version='1.0' encoding='utf-8' ?>";
                FileContent += "<启动配置参数>";
                FileContent += @"<URL>rtmp://183.224.164.130:10085/hls/df8</URL>";
                FileContent += "<渲染模式>1</渲染模式>";
                FileContent += "<硬解>false</硬解>";
                FileContent += "<缓存值>3</缓存值>";
                FileContent += "<校验KEY值>59615A67426F69576B5A7541306C74676E3651744A663478567778576F502B6C2F32566863336B3D</校验KEY值>";
                 
                FileContent += "<备用参数A>500</备用参数A>";
                FileContent += "<备用参数B>500</备用参数B>";
                FileContent += "</启动配置参数>";
                XmlDoc.LoadXml(FileContent);
                //判断文件是否存在,如果不存在则创建
                if (!System.IO.File.Exists("./EasyPlayerConfig.xml"))
                {
                    //判断路径是否存在,如果不存在则创建
                    if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName("./EasyPlayerConfig.xml")))
                        System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName("./EasyPlayerConfig.xml"));
                    //创建文件
                    System.IO.File.Create("./EasyPlayerConfig.xml").Dispose();
                    XmlDoc.Save("./EasyPlayerConfig.xml");
                }
  
            }
            catch (Exception e)
            {
                //写异常日志
                throw new Exception("创建配置文件时发生异常!\n" + e.Message);
            }
        }
  
        private static bool ReadAppSettingParamsFromConfigFile()
        {
            string fileName = "./EasyPlayerConfig.xml";
            if (fileName == "")
                return false;
            try
            {
                string xPath = @"//启动配置参数";
                XmlDocument XmlDoc = new XmlDocument();
                XmlDoc.Load(fileName);
                XmlDocumentFragment DocFrag = XmlDoc.CreateDocumentFragment();
                XmlNode RootNode = XmlDoc.DocumentElement;
                XmlNode ReadingNode = RootNode.SelectSingleNode(xPath);
                if (Object.Equals(ReadingNode, null))
                {
                }
                else
                {
                    xPath = @"//启动配置参数//URL";
                    ReadingNode = RootNode.SelectSingleNode(xPath);
                    string path = ReadingNode != null ? ReadingNode.InnerText : @"";
                    try
                    {
                        PlayerURL = path;
                    }
                    catch
                    {
                        PlayerURL = "";
                    }
  
                    xPath = @"//启动配置参数//渲染模式";
                    ReadingNode = RootNode.SelectSingleNode(xPath);
                    path = ReadingNode != null ? ReadingNode.InnerText : @"1";
                    try
                    {
                        RENDER_FORMAT = path;
                    }
                    catch
                    {
                        RENDER_FORMAT = @"1";
                    }
  
                    xPath = @"//启动配置参数//硬解";
                    ReadingNode = RootNode.SelectSingleNode(xPath);
                    string nRet = ReadingNode != null ? ReadingNode.InnerText : "false";
                    try
                    {
  
                        isHardEncode = nRet;
  
                    }
                    catch
                    {
                        isHardEncode = "";
                    }
  
                    xPath = @"//启动配置参数//缓存值";
                    ReadingNode = RootNode.SelectSingleNode(xPath);
                    nRet = ReadingNode != null ? ReadingNode.InnerText : "2";
                    try
                    {
  
                        CacheFream = nRet;
  
                    }
                    catch
                    {
                        CacheFream = "2";
                    }
  
                    xPath = @"//启动配置参数//校验KEY值";
                    ReadingNode = RootNode.SelectSingleNode(xPath);
                    path = ReadingNode != null ? ReadingNode.InnerText : "false";
                    try
                    {
                        KEY = path;
                    }
                    catch
                    {
                        KEY = "";
                    }
  
                    
                    xPath = @"//启动配置参数//备用参数A";
                    ReadingNode = RootNode.SelectSingleNode(xPath);
                    path = ReadingNode != null ? ReadingNode.InnerText : "500";
                    try
                    {
                        BYCSA = path;
                    }
                    catch
                    {
                        BYCSA = "500";
                    }
  
                    xPath = @"//启动配置参数//备用参数B";
                    ReadingNode = RootNode.SelectSingleNode(xPath);
                    path = ReadingNode != null ? ReadingNode.InnerText : "500";
                    try
                    {
                        BYCSB = path;
                    }
                    catch
                    {
                        BYCSB = "500";
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }

  

  

 

 
posted on   TSINGSEE  阅读(300)  评论(0编辑  收藏  举报
编辑推荐:
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
阅读排行:
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 《HelloGitHub》第 106 期
· 数据库服务器 SQL Server 版本升级公告
· 深入理解Mybatis分库分表执行原理
· 使用 Dify + LLM 构建精确任务处理应用
历史上的今天:
2020-04-15 现阶段的语音视频通话SDK需要解决哪些问题?
2020-04-15 流媒体RTMP推流组件如何默认开启外接摄像头采集视频?
2020-04-15 流媒体服务器推流可以实现采集一路视频同时推多路视频流吗?
2020-04-15 rtmp协议视频流媒体播放器安卓版本在电话通话后视频无法打开的问题解决
< 2025年1月 >
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1
2 3 4 5 6 7 8

点击右上角即可分享
微信分享提示