技术背景
我们在做Windows平台RTSP|RTMP播放器的时候,特别是多路播放场景下,开发者希望可以给每一路RTSP或RTMP流添加个额外的OSD台标,以区分不同的设备信息(比如添加摄像头所在位置),本文主要探讨,如何动态添加OSD台标。
技术实现
以大牛直播SDK的C#的RTSP|RTMP播放demo为例,分享下设计实现流程。选中“设置台标”复选框,在player窗口左上角显示“叠加字符展示”,具体内容、坐标可自定义,如果需要关闭台标,可以随时关闭:
实现代码如下:
/*
* SmartPlayerForm.cs.cs
* Created by daniusdk.com on 2017/04/19.
* WeChat: xinsheng120
*/
private void DrawOSD(string draw_text)
{
// gdi 绘制的话,文本请自己绘制
if (is_gdi_render_)
return;
if (player_handle_ == IntPtr.Zero)
return;
if (draw_text == null || draw_text.Length < 1)
{
NTSmartPlayerSDK.NT_SP_SetRenderARGBLogo(player_handle_, IntPtr.Zero, 0, 0, 0, 0, 0, 0, 0);
return;
}
Graphics graphics = this.CreateGraphics();
SolidBrush solid_brush = new SolidBrush(Color.FromArgb(255, 255, 255));
graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
SizeF text_size = new SizeF();
text_size = graphics.MeasureString(draw_text, this.Font);
int image_w = (int)text_size.Width + 4;
int image_h = (int)text_size.Height + 4;
image_w = (int)ByteAlign((UInt32)image_w, 4);
image_h = (int)ByteAlign((UInt32)image_h, 4);
Bitmap bmp = new Bitmap(image_w, image_h, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bmp);
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
float left = image_w / 2 - text_size.Width / 2;
float top = image_h / 2 - text_size.Height / 2;
g.DrawString(draw_text, this.Font, solid_brush, left, top);
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmp_data = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat);
IntPtr ptr = bmp_data.Scan0;
int strdie = Math.Abs(bmp_data.Stride);
NTSmartPlayerSDK.NT_SP_SetRenderARGBLogo(player_handle_, ptr, strdie, bmp_data.Width,
bmp_data.Height, 6, 6, bmp_data.Width, bmp_data.Height);
// Unlock the bits.
bmp.UnlockBits(bmp_data);
}
}
总结
Windows平台RTSP|RTMP播放器动态添加OSD文字台标,在实时监控场景下,非常实用,特别是多路场景,以上是大概的实现思路,可能还有开发者说,如果是添加的OSD文字台标,想录制下来,怎么办?实际上,我们也有分享过,可以把播放端解码后的YUV数据回调上来,投递到推送模块,然后,OSD文字水印,以图层的形式添加进去,录像即可,感兴趣的开发者,可以单独跟我沟通探讨。