'Computer/프로그래밍'에 해당되는 글 44건

  1. 2007.04.23 [JMF] Customized DataSource(PushBuferDataSource)을 만들어 보자
반응형

프로그래머가 원하는 DataSource를 만들기 위해서는 PullDataSource/PullBufferDataSource 혹은 PushDataSource/PushBufferDataSource를 구현해야 할 때도 있다.

PushBuferDataSource를 구현하려면 우선 PushBufferStream을 구현해야 한다.

Interface PushBufferStream은 Interface SourceStream을 Extend 하고 있고 SourceStream은 DataSource를 상속 받고 있다.

관계는 다음과 같다.

사용자 삽입 이미지


java.sun.com에서 LiveStream 예제에서 보여주는 DataSource는 다음과 같다. 아래 코드에서 빨간색으로 보이는 부분이 PushBufferStream이 구현된 클래스이다. 설명은 다음 Post에서..
--------------------------------------------------------------------------
public class DataSource extends PushBufferDataSource {
    protected Object [] controls = new Object[0];
    protected boolean started = false;
    protected String contentType = "raw";
    protected boolean connected = false;
    protected Time duration = DURATION_UNKNOWN;
    protected LiveStream [] streams = null;
    protected LiveStream stream = null;
   
    public DataSource() {
    }
    public String getContentType() {
        if (!connected){
            System.err.println("Error: DataSource not connected");
            return null;
        }
        return contentType;
    }
    public void connect() throws IOException {
        if (connected) return;
        connected = true;
    }
    public void disconnect() {
        try {
            if (started)
                stop();
        } catch (IOException e) {}
           connected = false;
    }

    public void start() throws IOException {
        // connect가 이미 호출되지 않았으면 Error를 Throw한다.
        if (!connected)
            throw new java.lang.Error("DataSource must be connected before it can be started");
        if (started) return;
        started = true;
        stream.start(true);
    }

    public void stop() throws IOException {
        if ((!connected) || (!started)) return;
        started = false;
        stream.start(false);
    }
    public Object [] getControls() {
        return controls;
    }
    public Object getControl(String controlType) {
       try {
          Class  cls = Class.forName(controlType);
          Object cs[] = getControls();
          for (int i = 0; i < cs.length; i++) {
             if (cls.isInstance(cs[i]))
                return cs[i];
          }
          return null;
       } catch (Exception e) {   // no such controlType or such control
         return null;
       }
    }
    public Time getDuration() {
        return duration;
    }
    public PushBufferStream [] getStreams() {
        if (streams == null) {
            streams = new LiveStream[1];
            stream = streams[0] = new LiveStream();

        }
        return streams;
    }
}

반응형
Posted by alias
,