前言
视频播放在上位机开发中经常会遇到,基本上是两种常见的解决方案

1.采用厂家提供的sdk和前端控件进行展示,常见的海康/大华都提供了相关sdk及文档

2.开启相机onvif协议,捅过rtsp视频流进行播放,前端可以采用web方式,或者wpf中的视频控件进行展示。

项目需求,决定了最终采用开启相机onvif供能,wpf中播放的方式。

网络调研一阵子之后,基本都是推荐Vlc.DotNet或者libvlcsharp.wpf进行前端展示。

参考了很多代码,无论是官方文档,还是不同博客里的代码,很难做到用mvvm的方式对于逻辑解耦。

而且Vlc.DotNet已经不再更新了。

Libvlcasharp.wpf的设计有些反人类,可以参考这篇文章WPF中使用LibVLCSharp.WPF 播放rtsp - Naylor - 博客园 (cnblogs.com)

所以这部分逻辑写的很难受,需要寻找其他方案。

最近有空了,调研了几个其他开源项目,大家的思路都比较一致,相机打开onvif协议推送rtsp视频流,本地通过ffmpeg进行视频转流,然后推送到wpf前端控件上。

unosquare/ffmediaelement: FFME: The Advanced WPF MediaElement (based on FFmpeg) (github.com)

SuRGeoNix/Flyleaf: Media Player .NET Library for WinUI 3/ WPF/WinForms (based on FFmpeg/DirectX) (github.com)

网上有FFME的样例代码,我在本地搭建没有成功,应该是我的ffmpeg编译版本问题,可以参考这个项目。

DG-Wangtao/FFMEVideoPlayer: 使用FFmepg封装的WPF MideaElement,可以播放rtsp视频流。感谢 https://github.com/unosquare/ffmediaelement

最终选择了Flyleaf的方案,简单搭建了demo给大家参考。

Flyleaf官方项目地址SuRGeoNix/Flyleaf: Media Player .NET Library for WinUI 3/ WPF/WinForms (based on FFmpeg/DirectX) (github.com)

MVVM框架使用的是CommunityToolKit.MVVM

正文

Flyleaf的使用整体分成四步走,

1.App.xaml及App.xaml.cs中配置ffmpeg的dll文件地址;

1.1ffmpeg的dll文件,我才用的是Flyleaf官方sample中的文件,版本不是最新的。

1.2文件统一放在项目中的FFmpeg文件夹中

1.3生成操作(Build Action)配置为 无(None)

1.4复制到输出目录(Copy to Output Directory)配置为 如果较新则复制(Copy if newer)

1.5App.xaml中添加startup事件

    <Application x:Class="FlyleafDemo.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:FlyleafDemo"
StartupUri="MainWindow.xaml"
Startup="Application_Startup">
<Application.Resources> </Application.Resources>
</Application>

1.6App.xaml.cs中配置ffmpeg的dll路径,项目编译后会复制ffmpeg文件夹及dll。

    using FlyleafLib;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows; namespace FlyleafDemo
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
Engine.Start(new EngineConfig()
{
FFmpegPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "FFmpeg"),
FFmpegDevices = false, // Prevents loading avdevice/avfilter dll files. Enable it only if you plan to use dshow/gdigrab etc. #if RELEASE
FFmpegLogLevel = FFmpegLogLevel.Quiet,
LogLevel = LogLevel.Quiet, #else
FFmpegLogLevel = FFmpegLogLevel.Warning,
LogLevel = LogLevel.Debug,
LogOutput = ":debug",
//LogOutput = ":console",
//LogOutput = @"C:\Flyleaf\Logs\flyleaf.log",
#endif //PluginsPath = @"C:\Flyleaf\Plugins", UIRefresh = false, // Required for Activity, BufferedDuration, Stats in combination with Config.Player.Stats = true
UIRefreshInterval = 250, // How often (in ms) to notify the UI
UICurTimePerSecond = true, // Whether to notify UI for CurTime only when it's second changed or by UIRefreshInterval
});
}
}
}

2.ViewModel中配置参数等信息;

    using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using FlyleafLib.MediaPlayer;
using FlyleafLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media; namespace FlyleafDemo
{
public class MainViewModel:ObservableObject
{
private Player player; public Player Player
{
get => player;
set => SetProperty(ref player, value);
} private Config config; public Config Config
{
get => config;
set => SetProperty(ref config, value);
} private string uriString; public string UriString
{
get => uriString;
set => SetProperty(ref uriString, value);
} public IRelayCommand<string> PlayCommand { get; set; }
public MainViewModel()
{
Config = new Config();
Config.Video.BackgroundColor = Colors.Transparent;
// 设置播放延迟为100ms,可能我理解有误,具体可以在项目issues里查看
// Config.Player.MaxLatency = 100 * 10000; Player = new Player(Config);
PlayCommand = new RelayCommand<string>(PlayAction);
UriString = uri1;
} private string currentUri = string.Empty;
private string uri1 = "rtsp://192.168.20.2:554/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif";
private string uri2 = "rtsp://192.168.20.3:554/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif";
private void PlayAction(string uri)
{
if (!string.IsNullOrEmpty(uri))
{
if (currentUri == uri1)
{
//Player.Commands.Stop.Execute(null);
currentUri = uri2;
Player.Commands.Open.Execute(uri2);
}
else
{
//Player.Commands.Stop.Execute(null);
currentUri = uri1;
Player.Commands.Open.Execute(uri1);
}
}
}
}
}

3.View中配置布局等信息;

    <Window
x:Class="FlyleafDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:fl="clr-namespace:FlyleafLib.Controls.WPF;assembly=FlyleafLib"
xmlns:local="clr-namespace:FlyleafDemo"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="800"
Height="450"
mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="5*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<fl:FlyleafHost
AttachedDragMove="Both"
KeyBindings="Both"
Player="{Binding Player, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
<Viewbox>
<TextBlock Foreground="DarkOrange" Text="Hello Flyleaf Overlay!" />
</Viewbox>
</fl:FlyleafHost>
<Button
Grid.Row="1"
Command="{Binding PlayCommand}"
CommandParameter="{Binding UriString, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
</Grid>
</Window>

4.在xaml.cs中确定View和ViewModel的绑定关系

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace FlyleafDemo
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainViewModel();
}
}
}

总结

前端控件绑定比较方便,减少了在xaml.cs中的耦合逻辑
我尝试过三路视频同时播放,效果不错,系统资源消耗也不高
很多参数都可以在Config中配置,一些交互逻辑可以在Player中执行,比较清晰
但是,单视频控件切换视频流的时候,会有一定时间延迟,我尝试过使用
Player.Commands.Stop.Execute(null);
但效果不大。
感兴趣的可以深挖源码,我这里只是抛砖引玉。
Demo效果图如下

————————————————
版权声明:本文为CSDN博主「maoleigepu」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/maoleigepu/article/details/133268837

WPF中以MVVM方式,实现RTSP视频播放的更多相关文章

  1. WPF中使用MVVM模式进行简单的数据绑定

    计划慢慢整理自己在WPF学习和工作应用中的一些心得和想法,先从一个简单的用法说起 在WPF中,XAML标记语言中绑定数据,而数据源就是指定为ViewModel类,而非界面本身的逻辑代码类 这样一定程度 ...

  2. 简单的介绍下WPF中的MVVM框架

    最近在研究学习Swift,苹果希望它迅速取代复杂的Objective-C开发,引发了一大堆热潮去学它,放眼望去各个培训机构都已打着Swift开发0基础快速上手的招牌了.不过我觉得,等同于无C++基础上 ...

  3. WPF中资源引用方式汇总

    在WPF应用程序开发中,总是难以记住各种访问资源的方法,遂逐一记下. 先从资源是否编译到程序集分类 一.程序集资源 资源在编译的时候嵌入到程序集中.WPF中的XAML会被编译为BAML,图片等其他资源 ...

  4. WPF中在MVVM模式下,后台绑定ListCollectionView事件触发问题

    问题:WPF中MVVM模式下 ListView绑定ListCollectionView时,CurrentChanged无法触发 解决方案: 初期方案:利用ListView的SelectionChang ...

  5. 浅谈WPF中的MVVM框架--MVVMFoundation

    先科普一下:什么是WPF,请看下图 微软对于WPF技术的构想是很宏大的,可惜普及率不高,不过如果你要做Windows客户端开发的话WPF技术还是值得一学的. 什么是MVVM模式 简单来说它是一种高级的 ...

  6. WPF 中使用MVVM模式后,找回ListBox中的ListBoxItem元素

    ListBoxItem lstitem = this.list.ItemContainerGenerator.ContainerFromItem(m) as ListBoxItem; 其中this.l ...

  7. WPF 中的绑定方式

    1.元素间的绑定 xaml方式 <Slider Name="slider1" Value="20"/>        <TextBlock T ...

  8. WPF中使用MVVM进行multibinding

    背景描述:在Number1和Number2文本框中输入数字后,在Answer文本框中会按照下图所示显示. xaml代码: <Window.Resources> <local:MyVa ...

  9. MVVM模式解析和在WPF中的实现(六) 用依赖注入的方式配置ViewModel并注册消息

    MVVM模式解析和在WPF中的实现(六) 用依赖注入的方式配置ViewModel并注册消息 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二 ...

  10. WPF中使用LibVLCSharp.WPF 播放rtsp

    目录 LibVLCSharp.WPF简介 vlc:VideoView基本使用 安装LibVLC 播放rtsp 引入命名空间 xaml 代码 cs代码 截图 概述 代码示例 vlc:VideoView进 ...

随机推荐

  1. Centos7安装配置Hive

    Centos7安装配置 一 . 安装 安装就不多做详述,选择好自己的镜像设置好路径即可 二 .配置 2.1 网络配置 桌面右键进入 cmd 命令编辑窗口,在 Linux 中设置网络的相关配置都需要管理 ...

  2. SpringBoot定义优雅全局统一Restful API 响应框架六

    闲话不多说,继续优化 全局统一Restful API 响应框架 做到项目通用 接口可扩展. 如果没有看前面几篇文章请先看前面几篇 SpringBoot定义优雅全局统一Restful API 响应框架 ...

  3. CKS 考试题整理 (18)-TLS 安全配置

    Task 通过 TLS 加强 kube-apiserver 安全配置,要求 kube-apiserver 除了 VersionTLS13 及以上的版本可以使用,其他版本都不允许使用. 密码套件(Cip ...

  4. chatgpt入口,免费在线chatgpt--与人工智能聊天?尝试chatgpt入口,免费在线chatgpt吧!

    介绍一款人工智能聊天机器人--chatgpt入口 chatgpt是一款智能聊天机器人,它能够与人类进行自然语言对话,可以回答问题.提供建议,还可以玩游戏和聊天互动,是当前最受欢迎的人工智能聊天工具之一 ...

  5. GPT3的技术突破:实现更准确、更真实的语言生成

    目录 1. 引言 2. 技术原理及概念 3. 实现步骤与流程 4. 应用示例与代码实现讲解 5. 优化与改进 6. 结论与展望 7. 附录:常见问题与解答 GPT-3 技术突破:实现更准确.更真实的语 ...

  6. git reset --hard 撤回后commit的代码消失了的解决办法

    楼主在今天的工作中使用了这个命令 git reset --hard 撤回后commit的代码消失了,因为有commit,所以暂时得到了拯救,太不容易了,差点以为自己写的代码没了. 网上到处找帖子,看看 ...

  7. Mybatis-plus自定义Sql注入器

    最近在学习mybatis-plus,知道了在mp中通过AbstractSqlInjector将BaseMapper中的方法注入到了Mybatis容器,这样这些方法才可以正常执行. 下面是一个关系图 那 ...

  8. 前端Vue自定义简单通用省市区选择器picker地区选择器picker 收获地址界面模版

    前端Vue自定义简单通用省市区选择器picker地区选择器picker 收获地址界面模版,下载完整代码请访问uni-app插件市场地址:https://ext.dcloud.net.cn/plugin ...

  9. Stable Diffusion生成图片的参数查看与抹除方法

    前几天分享了几张Stable Diffusion生成的艺术二维码,有同学反映不知道怎么查看图片的参数信息,还有的同学问怎么保护自己的图片生成参数不会泄露,这篇文章就来专门分享如何查看和抹除图片的参数. ...

  10. RabbitMQ 中 exchange、route、queue 的关系

    从 AMQP 协议可以看出,MessageQueue.Exchange 和 Binding 构成了 AMQP 协议的核心,下面我们就围绕这三个主要组件 从应用使用的角度全面的介绍如何利用 Rabbit ...