public void WriteSynchroChunk(MoLiveChunk Chunk) { if (Chunk == null) { MoLive.WriteVariableByte(mDestinationStream, 0); MoLive.WriteVariableByte(mDestinationStream, 0); return; } MoLive.WriteVariableByte(mDestinationStream, Chunk.Id); MoLive.WriteVariableByte(mDestinationStream, Chunk.Size); Chunk.Write(mDestinationStream); }
public Endpoint(MoLiveChunk Chunk) { this.Chunk = Chunk; }
void d_OnCompleteFrameReceived(MoLiveChunk Chunk, byte[] Data) { if ((Chunk is MoLiveStreamVideo || Chunk is MoLiveStreamVideoWithLayout) && ((PlayingVideoStream == -1) || ((MoLiveStream)Chunk).StreamIndex == PlayingVideoStream)) { if (!sizeset) Invoke((Action)delegate { ClientSize = new Size((int)((MoLiveStreamVideo)Chunk).Width, (int)((MoLiveStreamVideo)Chunk).Height); }); sizeset = true; left = !left; if (ddd == null) { ddd = new LibMobiclip.Codec.Mobiclip.MobiclipDecoder(((MoLiveStreamVideo)Chunk).Width, ((MoLiveStreamVideo)Chunk).Height, LibMobiclip.Codec.Mobiclip.MobiclipDecoder.MobiclipVersion.Moflex3DS); PlayingVideoStream = ((MoLiveStream)Chunk).StreamIndex; if (!(Chunk is MoLiveStreamVideoWithLayout)) Is3D = false; else if (((MoLiveStreamVideoWithLayout)Chunk).ImageLayout == MoLiveStreamVideoWithLayout.VideoLayout.Simple2D) Is3D = false; else Is3D = true; } ddd.Data = Data; ddd.Offset = 0; Bitmap b = ddd.DecodeFrame(); if (!Is3D || left) { TimeSpan ts = TimeSpan.FromMilliseconds((!Is3D ? 1000d : 2000d) / ((double)((MoLiveStreamVideo)Chunk).FpsRate / (double)((MoLiveStreamVideo)Chunk).FpsScale)); if (lastval != 0) { while ((s.Value - lastval) < (long)(ts.TotalSeconds * s.Frequency)) ;//milliseconds) ; } lastval = s.Value; try { pictureBox1.BeginInvoke((Action)delegate { pictureBox1.Image = b; pictureBox1.Invalidate(); }); } catch { } } } else if (Chunk is MoLiveStreamAudio) { if (AudioBuffer == null) { AudioBuffer = new BufferedWaveProvider(new WaveFormat((int)((MoLiveStreamAudio)Chunk).Frequency, 16, (int)((MoLiveStreamAudio)Chunk).Channel)); AudioBuffer.DiscardOnBufferOverflow = true; AudioBuffer.BufferLength = 1024 * 512; Player = new WaveOut(); Player.DesiredLatency = 150; Player.Init(AudioBuffer); Player.Play(); } switch ((int)((MoLiveStreamAudio)Chunk).CodecId) { case 0://fastaudio { if (mFastAudioDecoders == null) { mFastAudioDecoders = new FastAudioDecoder[(int)((MoLiveStreamAudio)Chunk).Channel]; for (int i = 0; i < (int)((MoLiveStreamAudio)Chunk).Channel; i++) { mFastAudioDecoders[i] = new FastAudioDecoder(); } } List<short>[] channels = new List<short>[(int)((MoLiveStreamAudio)Chunk).Channel]; for (int i = 0; i < (int)((MoLiveStreamAudio)Chunk).Channel; i++) { channels[i] = new List<short>(); } int offset = 0; int size = 40; while (offset + size < Data.Length) { for (int i = 0; i < (int)((MoLiveStreamAudio)Chunk).Channel; i++) { mFastAudioDecoders[i].Data = Data; mFastAudioDecoders[i].Offset = offset; channels[i].AddRange(mFastAudioDecoders[i].Decode()); offset = mFastAudioDecoders[i].Offset; } } short[][] channelsresult = new short[(int)((MoLiveStreamAudio)Chunk).Channel][]; for (int i = 0; i < (int)((MoLiveStreamAudio)Chunk).Channel; i++) { channelsresult[i] = channels[i].ToArray(); } byte[] result = InterleaveChannels(channelsresult); AudioBuffer.AddSamples(result, 0, result.Length); } break; case 1://IMA-ADPCM { IMAADPCMDecoder[] decoders = new IMAADPCMDecoder[(int)((MoLiveStreamAudio)Chunk).Channel]; List<short>[] channels = new List<short>[(int)((MoLiveStreamAudio)Chunk).Channel]; for (int i = 0; i < (int)((MoLiveStreamAudio)Chunk).Channel; i++) { decoders[i] = new IMAADPCMDecoder(); decoders[i].GetWaveData(Data, 4 * i, 4); channels[i] = new List<short>(); } int offset = 4 * (int)((MoLiveStreamAudio)Chunk).Channel; int size = 128 * (int)((MoLiveStreamAudio)Chunk).Channel; while (offset + size < Data.Length) { for (int i = 0; i < (int)((MoLiveStreamAudio)Chunk).Channel; i++) { channels[i].AddRange(decoders[i].GetWaveData(Data, offset, 128)); offset += 128; } } short[][] channelsresult = new short[(int)((MoLiveStreamAudio)Chunk).Channel][]; for (int i = 0; i < (int)((MoLiveStreamAudio)Chunk).Channel; i++) { channelsresult[i] = channels[i].ToArray(); } byte[] result = InterleaveChannels(channelsresult); AudioBuffer.AddSamples(result, 0, result.Length); } break; case 2://PCM16 { AudioBuffer.AddSamples(Data, 0, Data.Length - (Data.Length % ((int)((MoLiveStreamAudio)Chunk).Channel * 2))); } break; } } }
//int videostreamid = -1; public uint ReadSynchroChunk(byte[] packet, ref uint pos, uint psize) { //long curpos = packet.Position; //packet.Position += pos; uint type; uint size; if (!MoLive.ReadVariableByte(packet, out type, ref pos, psize) || !MoLive.ReadVariableByte(packet, out size, ref pos, psize)) { goto LABEL_119; } MoLiveChunk chunk = null; switch (type) { case 0: pos += size; //packet.Position = curpos; return(0x100); case 1: //MoLiveStreamVideo chunk = new MoLiveStreamVideo() { Id = 1, Size = 12, StreamIndex = -1 }; break; case 2: //MoLiveStreamAudio chunk = new MoLiveStreamAudio() { Id = 2, Size = 6, StreamIndex = -1 }; break; case 3: //MoLiveStreamVideoWithLayout chunk = new MoLiveStreamVideoWithLayout() { Id = 3, Size = 13, StreamIndex = -1, ImageRotation = 0 }; break; case 4: //MoLiveStreamTimeline chunk = new MoLiveStreamTimeline() { Id = 4, Size = 2, StreamIndex = -1 }; break; case 0x100000: //MoLiveChunkFoo chunk = new MoLiveChunkFoo() { Id = 0x100000, Size = 20 }; break; } if (chunk == null) { return(0x44); } if (chunk.Size != size || chunk.Read(packet, (int)pos) == 0) { //error return(0x45); } //if (chunk is MoLiveStreamVideo || chunk is MoLiveStreamVideoWithLayout) // videostreamid = ((MoLiveStream)chunk).StreamIndex; Streams.Add(((MoLiveStream)chunk).StreamIndex, new Endpoint(chunk)); pos += size; if (pos <= psize) { return(0); } LABEL_119: Desynchronize(); //packet.Position = curpos; return(0x43u); }