CSharpRTMP.Core.Streaming.VideoAvc.Init C# (CSharp) Method

Init() public method

public Init ( byte pSPS, byte pPPS ) : bool
pSPS byte
pPPS byte
return bool
        public bool Init(byte[] pSPS, byte[] pPPS)
        {
            Clear();
            _SPSInfo = Variant.Get();
            _PPSInfo = Variant.Get();
            if (pSPS.Length == 0 || pSPS.Length > 65535 || pPPS.Length == 0 || pPPS.Length > 65535)
            {
                Logger.FATAL("Invalid SPS/PPS lengths");
                return false;
            }
            SPS = pSPS;
            PPS = pPPS;
            Rate = 90000;
            var ms = new MemoryStream(SpsLength-1);
            for (ushort i = 1; i < pSPS.Length; i++)
            {
                if (i + 2 < pSPS.Length - 1 && SPS[i] == 0 && SPS[i + 1] == 0 && SPS[i + 2] == 3)
                {
                    ms.WriteByte(0);
                    ms.WriteByte(0);
                    i += 2;
                }
                else
                {
                    ms.WriteByte(SPS[i]);
                }
            }
            ms.Position = 0;
            using (var spsReader = new BitReader(ms))
            {
                if (!ReadSPS(spsReader, _SPSInfo))
                {
                    Logger.WARN("Unable to parse SPS");
                }
                else
                {
                    _SPSInfo.Compact();
                    Width = ((uint)_SPSInfo["pic_width_in_mbs_minus1"] + 1) * 16;
                    Height = ((uint)_SPSInfo["pic_height_in_map_units_minus1"] + 1) * 16;
                    //		FINEST("_width: %u (%u); _height: %u (%u)",
                    //				_width, (uint32_t) _SPSInfo["pic_width_in_mbs_minus1"],
                    //				_height, (uint32_t) _SPSInfo["pic_height_in_map_units_minus1"]);
                }
            }
            ms = new MemoryStream(PpsLength-1);
            for (ushort i = 1; i < pPPS.Length; i++)
            {
                if (i + 2 < pPPS.Length - 1 && PPS[i] == 0 && PPS[i + 1] == 0 && PPS[i + 2] == 3)
                {
                    ms.WriteByte(0);
                    ms.WriteByte(0);
                    i += 2;
                }
                else
                {
                    ms.WriteByte(PPS[i]);
                }
            }
            ms.Position = 0;
            using (var ppsReader = new BitReader(ms))
            {
                if (!ReadPPS(ppsReader, _PPSInfo))
                {
                    Logger.WARN("Unable to read PPS info");
                }
            }
            return true;
        }

Usage Example

        public static bool Deserialize(Stream src, out VideoAvc dest)
        {
            dest = new VideoAvc();
            if (src.GetAvaliableByteCounts() < 2)
            {
                Logger.FATAL("Not enough data");
                return(false);
            }
            var reader = new N2HBinaryReader(src);

            var _spsLength = reader.ReadUInt16();

            if (src.GetAvaliableByteCounts() < _spsLength + 2 + 8)
            {
                Logger.FATAL("Not enough data");
                return(false);
            }
            var psps       = reader.ReadBytes(_spsLength);
            var _ppsLength = reader.ReadUInt16();

            if (src.GetAvaliableByteCounts() < _ppsLength + 2 + 8)
            {
                Logger.FATAL("Not enough data");
                return(false);
            }
            var ppps = reader.ReadBytes(_ppsLength);

            dest.Init(psps, ppps);
            dest._widthOverride  = reader.ReadUInt32();
            dest._heightOverride = reader.ReadUInt32();

            return(true);
        }
All Usage Examples Of CSharpRTMP.Core.Streaming.VideoAvc::Init