PublishManager.java 4.15 KB
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.genersoft.iot.vmp.jt1078.publisher;

import com.genersoft.iot.vmp.jt1078.entity.Media;
import com.genersoft.iot.vmp.jt1078.entity.Media.Type;
import com.genersoft.iot.vmp.jt1078.server.Jtt1078Handler;
import com.genersoft.iot.vmp.jt1078.subscriber.Subscriber;
import io.netty.channel.ChannelHandlerContext;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class PublishManager {
    static Logger logger = LoggerFactory.getLogger(PublishManager.class);
    ConcurrentHashMap<String, Channel> channels = new ConcurrentHashMap();
    static final PublishManager instance = new PublishManager();
    private Integer httpPort;

    private PublishManager() {
    }

    public Subscriber subscribe(String tag, Media.Type type, ChannelHandlerContext ctx, Integer httpPort) {
        Channel chl = (Channel)this.channels.get(tag);
        if (chl == null) {
            chl = new Channel(tag, httpPort);
            this.channels.put(tag, chl);
        }
        this.httpPort = httpPort;

        Subscriber subscriber = null;
        if (type.equals(Type.Video)) {
            subscriber = chl.subscribe(ctx);
            subscriber.setName("subscriber-" + tag + "-" + subscriber.getId());
            subscriber.start();
            return subscriber;
        } else {
            throw new RuntimeException("unknown media type: " + type);
        }
    }

    public void publishAudio(String tag, int sequence, long timestamp, int payloadType, byte[] data) {
        Channel chl = (Channel)this.channels.get(tag);
        if (chl != null) {
            chl.writeAudio(timestamp, payloadType, data);
        }

    }

    public void publishVideo(String tag, int sequence, long timestamp, int payloadType, byte[] data) {
        int length = data.length;
        StringBuilder builder = new StringBuilder();

        for(int i = 0; i < length; ++i) {
            builder.append(this.valu(data, i));
        }

        Channel chl = (Channel)this.channels.get(tag);
        if (chl != null) {
            chl.writeVideo((long)sequence, timestamp, payloadType, data);
        }

    }

    public String valu(byte[] data, int index) {
        byte val = data[index++];
        int ch1 = val >> 4 & 15;
        int ch2 = val & 15;
        return ch1 + "" + ch2;
    }

    public Channel open(String tag, Integer httpPort) {
        Channel chl = (Channel)this.channels.get(tag);
        if (chl == null) {
            chl = new Channel(tag, httpPort);
            this.channels.put(tag, chl);
        }

        logger.info("Thread id:[{}]", Thread.currentThread().getId());
        if (chl.isPublishing()) {
            throw new RuntimeException("channel already publishing");
        } else {
            return chl;
        }
    }

    public void close(String tag) {
        Channel chl = (Channel)this.channels.remove(tag);
        if (chl != null) {
            chl.close();
        }

    }

    public void unsubscribe(String tag, long watcherId) {
        Channel chl = (Channel)this.channels.get(tag);
        if (chl != null) {
            chl.unsubscribe(watcherId);
        }


        logger.info("unsubscribe: {} - {}", tag, watcherId);
    }

    public void unsubscribeAndClose(String tag) {
        try {
            Channel chl = (Channel)this.channels.get(tag);
            if (chl != null) {
                long watcherId = chl.getWatcherId(tag);
                this.unsubscribe(tag, watcherId);
            }
        } catch (Exception var6) {
            logger.error("unsubscribeAndClose unsubscribe error;[{}]", tag);
        }

        try {
            this.close(tag);
        } catch (Exception var5) {
            logger.error("unsubscribeAndClose close error;[{}]", tag);
        }

    }

    public static void init() {
    }

    public static PublishManager getInstance() {
        return instance;
    }

    private void createChannel(String tag, String tagMapping) {
        Channel chl = (Channel)this.channels.get(tag);
    }
}