package com.lingniu.ingest.protocol.jt1078.media;

import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.ChannelHandlerContext;

public final class Jt1078ReceivedMediaStreamHandler extends SimpleChannelInboundHandler<Object> {

    private final Jt1078MediaArchiveService archiveService;
    private final Jt1078MalformedRtpEventPublisher malformedPublisher;

    public Jt1078ReceivedMediaStreamHandler(Jt1078MediaArchiveService archiveService,
                                           Jt1078MalformedRtpEventPublisher malformedPublisher) {
        this.archiveService = archiveService;
        this.malformedPublisher = malformedPublisher;
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Object msg) {
        if (msg instanceof Jt1078ReceivedRtpPacket received) {
            archiveService.archive(received.packet(), received.peer());
        } else if (msg instanceof Jt1078MalformedRtpPacket malformed) {
            malformedPublisher.publish(malformed);
        } else {
            ctx.fireChannelRead(msg);
        }
    }
}
