protected override bool DecodeDataCore(int target_size, out byte[] out_data, out int out_size) { out_size = 0; out_data = new byte[target_size + 65535]; int total_decode = 0; int remain = target_size; while (true) { int done = 0; byte[] tmpbuf = new byte[remain]; IntPtr tmpptr = Marshal.UnsafeAddrOfPinnedArrayElement(tmpbuf, 0); int ret = mpg123.mpg123_read(this.mpg123Handle, tmpptr, tmpbuf.Length, out done); if (done == 0 && ret == mpg123.MPG123_ERR) { logger.ErrorFormat("mpg123_read失败, {0}", ret); return(false); } Buffer.BlockCopy(tmpbuf, 0, out_data, total_decode, done); total_decode += done; remain -= done; /* 解码的数据不够,继续解析音频帧然后解码 */ if (total_decode < target_size) { AudioDemuxPacket packet = null; if (!this.demuxer.DemuxNextPacket <AudioDemuxPacket>(out packet)) { return(false); } IntPtr frameptr = Marshal.UnsafeAddrOfPinnedArrayElement(packet.Data, 0); if ((ret = mpg123.mpg123_feed(this.mpg123Handle, frameptr, packet.Data.Length)) != mpg123.MPG123_OK) { return(false); } } else { return(true); } } }
public override bool Open(AbstractAVDemuxer demuxer) { #region 初始化mpg123句柄 int error; if ((this.mpg123Handle = mpg123.mpg123_new(IntPtr.Zero, out error)) == IntPtr.Zero) { logger.ErrorFormat("mpg123_new失败, {0}", error); return(false); } if ((error = mpg123.mpg123_open_feed(this.mpg123Handle)) != mpg123.MPG123_OK) { logger.ErrorFormat("mpg123_open_feed失败, {0}", error); return(false); } #endregion #region 探测流格式 IntPtr container_hdr_ptr = Marshal.UnsafeAddrOfPinnedArrayElement(demuxer.ContainerHeader, 0); if ((error = mpg123.mpg123_feed(this.mpg123Handle, container_hdr_ptr, demuxer.ContainerHeader.Length)) != mpg123.MPG123_OK) { logger.ErrorFormat("mpg123_feed ContainerHeader失败, {0}", error); return(false); } /* 如果当前没有解析数据帧,则读取一帧数据 */ AudioDemuxPacket packet = null; if (demuxer.CurrentPacket == null) { if (!demuxer.DemuxNextPacket <AudioDemuxPacket>(out packet)) { logger.ErrorFormat("NextPacket失败"); return(false); } } int total_probe_times = 0; int rate, channel, encoding; do { IntPtr frameptr = Marshal.UnsafeAddrOfPinnedArrayElement(packet.Data, 0); if ((error = mpg123.mpg123_feed(this.mpg123Handle, frameptr, packet.Data.Length)) != mpg123.MPG123_OK) { logger.ErrorFormat("feed数据失败, {0}", error); return(false); } if (total_probe_times++ > MAX_PROBE_TIMES) { logger.ErrorFormat("Open超时"); return(false); } }while ((error = mpg123.mpg123_getformat(this.mpg123Handle, out rate, out channel, out encoding)) != mpg123.MPG123_OK); logger.InfoFormat("OpenDemuxer成功, rate={0}, channel={1}, encoding={2}", rate, channel, encoding); #endregion this.demuxer = demuxer; return(true); }