• 大小: 8KB
    文件类型: .zip
    金币: 2
    下载: 2 次
    发布日期: 2021-06-13
  • 语言: C#
  • 标签: 海康  海康ipc  提取h264  

资源简介

c# VS2017代码 demo 模拟ehome从海康ipc获取的流中提取h264流。设备型号DS-2CD2T10D-I3

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.xml; 

namespace MyEhome.Test
{
     
    class Program
    {
        static int port = 7660;
        static IPEndPoint ipep = new IPEndPoint(IPAddress.Any port);
        static Socket _newsock;
        static int tcpPort = 16781;
        static void Main(string[] args)
        {
            ///开启socket 等待流连接
            Task.Run(() => NatlinkServer());

            _newsock = new Socket(AddressFamily.InterNetwork SocketType.Dgram ProtocolType.Udp);
            //绑定网络地址 
            _newsock.Bind(ipep);
            Console.WriteLine(“This is a Server host name is {0}“ Dns.GetHostName());
            //等待客户机连接 
            Console.WriteLine(“Waiting for a client“); 
            Task.Run(() => receiveUdpMsg(_newsock));
            ///处理流
            Task.Run(() => OptStram());


            Console.ReadKey();
        }
        static Dictionary DevKeys = new Dictionary();
        static object lockDev = new object();

        static List PesStream = new List();
        static object lockPes = new object();
        static AutoResetEvent autoReset = new AutoResetEvent(false);

        private static void NatlinkServer()
        {
            Socket _serverSock = new Socket(SocketType.Stream ProtocolType.Tcp);
            _serverSock.Bind(new IPEndPoint(IPAddress.Any tcpPort));
            _serverSock.Listen(250); 
            while(true)
            {
                Socket client1 = _serverSock.Accept();
                Task.Run(() => RecvSocket(client1));
            }
           
        }
        /// 
        /// 
        /// 

        private static void OptStram()
        {
            Console.WriteLine(“开始提取 h264 流“);
            FileStream fs = new FileStream(“d:\\1.mpg“ FileMode.Create); 
            while (true)
            {
                if(PesStream.Count<1000)
                {
                    autoReset.WaitOne();
                    continue;
                }
                ////////////////////////////////////
                lock (lockPes)
                {
                    byte[] pesHeader = PesStream.Take(14).ToArray();
                    ///e0
                    if (pesHeader[0] == 0 && pesHeader[1] == 0 && pesHeader[2] == 1 && pesHeader[3] == 224)
                    {
                        int pesHeaderBufLength = pesHeader[4] * 256 + pesHeader[5];
                        var h264 = PesStream.Skip(6).Take(pesHeaderBufLength).Skip(3 + pesHeader[8]).ToArray();
                        if (h264.Length != (pesHeaderBufLength - 3 - pesHeader[8]))
                        {
                            continue;
            

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件         187  2018-08-21 09:49  MyEhome.Test\App.config
     文件        2331  2018-08-28 10:22  MyEhome.Test\MyEhome.Test.csproj
     文件        1122  2018-08-28 10:19  MyEhome.Test\MyEhome.Test.sln
     文件       25655  2018-08-28 10:22  MyEhome.Test\Program.cs
     目录           0  2018-08-28 10:22  MyEhome.Test\Properties\
     文件        1149  2018-08-28 10:22  MyEhome.Test\Properties\AssemblyInfo.cs
     目录           0  2018-08-28 10:23  MyEhome.Test\

评论

共有 条评论