internal void ProcessPacketQueueChanges(PacketQueueOp operation, MediaPacket packet, MediaType mediaType) { if (OnPacketQueueChanged == null) { return; } var state = default(PacketBufferState); state.HasEnoughPackets = true; state.Duration = TimeSpan.MaxValue; foreach (var c in All) { state.Length += c.BufferLength; state.Count += c.BufferCount; state.CountThreshold += c.BufferCountThreshold; if (c.HasEnoughPackets == false) { state.HasEnoughPackets = false; } if ((c.MediaType == MediaType.Audio || c.MediaType == MediaType.Video) && c.BufferDuration != TimeSpan.MinValue && c.BufferDuration.Ticks < state.Duration.Ticks) { state.Duration = c.BufferDuration; } } if (state.Duration == TimeSpan.MaxValue) { state.Duration = TimeSpan.MinValue; } // Update the buffer state lock (BufferSyncLock) BufferState = state; // Send the callback OnPacketQueueChanged?.Invoke(operation, packet, mediaType, state); }
/// <summary> /// Sends the specified packet to the correct component by reading the stream index /// of the packet that is being sent. No packet is sent if the provided packet is set to null. /// Returns the media type of the component that accepted the packet. /// </summary> /// <param name="packet">The packet.</param> /// <returns>The media type.</returns> public MediaType SendPacket(MediaPacket packet) { if (packet == null) { return(MediaType.None); } foreach (var component in All) { if (component.StreamIndex != packet.StreamIndex) { continue; } component.SendPacket(packet); return(component.MediaType); } return(MediaType.None); }
/// <summary> /// Tries to handle processing of a data packet. If the packet is in fact a data packet, it is /// automatically disposed after executing the appropriate callbacks and returns true. /// If the packet is not a data packet, this method returns false and does not dispose of the /// packet so that the media component set tries to handle it. /// </summary> /// <param name="container">The container.</param> /// <param name="packet">The packet.</param> /// <returns>Returns false if the packet is not a data packet.</returns> public bool TryHandleDataPacket(MediaContainer container, MediaPacket packet) { lock (SyncLock) { // ensure packet and container are not null if (packet == null || container == null) { return(false); } // Get the associated stream var stream = container.MediaInfo.Streams.ContainsKey(packet.StreamIndex) ? container.MediaInfo.Streams[packet.StreamIndex] : null; // Ensure the stream is in fact a data stream if (stream == null || !stream.IsNonMedia) { return(false); } try { // Execute the packet handling callback OnDataPacketReceived?.Invoke(packet, stream); } catch { // Ignore } finally { // always dispose of the packet packet.Dispose(); } // Signal that the packet has been handled as a data packet return(true); } }