com.wowza.wms.stream.publish
Class Publisher

Object
  extended by com.wowza.wms.stream.publish.Publisher

public class Publisher
extends Object

Publisher: clientless stream publisher. This class can be used to publish raw video, audio and metadata packets to the Wowza Pro server. Here is a quick snippet of code that illustrates how to use it.

This code below will publish data the stream named "myStream". It will be streamed to the default virtual host and avaible at the rtmp address rtmp://[server-ip-address]/streamtest.

IVHost vhost = VHostSingleton.getInstance(VHost.VHOST_DEFAULT);
Publisher publisher = Publisher.createInstance(vhost, "streamtest");

publisher.setFileExtension("flv");
publisher.setStreamType("live");

publisher.publish("myStream", "live");

// sit in a loop adding data
boolean done = false;
while(true)
{
        AMFPacket amfPacket;

        // read packet from audio, video, data source
        // amfPacket = readPacketFromSomewhere();

        switch (amfPacket.getType())
        {
        case IVHost.CONTENTTYPE_AUDIO:
                publisher.addAudioData(amfPacket.getData(), amfPacket.getSize(), amfPacket.getTimecode());
                break;
        case IVHost.CONTENTTYPE_VIDEO:
                publisher.addVideoData(amfPacket.getData(), amfPacket.getSize(), amfPacket.getTimecode());
                break;
        case IVHost.CONTENTTYPE_DATA:
                publisher.addDataData(amfPacket.getData(), amfPacket.getSize(), amfPacket.getTimecode());
                break;
        }
        if (done)
                break;
}

publisher.unpublish();
publisher.close();
Basic packet format:

Audio:

AAC
[1-byte header]
[1-byte codec config indicator (0 - audio data, 1 - codec config packet)]
[n-bytes audio content or codec config data]

All others
[1-byte header]
[n-bytes audio content]

Below is the bit 
layout of the header byte of data (table goes from least significant bit to most significant bit):

1 bit Number of channels:   
  0    mono 
  1    stereo 

1 bit Sample size:   
  0    8 bits per sample 
  1    16 bits per sample 

2 bits Sample rate:   
  0    special or 8KHz 
  1    11KHz 
  2    22KHz 
  3    44KHz 

4 bits Audio type:   
  0    PCM (big endian) 
  1    PCM (swf - ADPCM) 
  2    MP3 
  3    PCM (little endian) 
  4    Nelly Moser ASAO 16KHz Mono 
  5    Nelly Moser ASAO 8KHz Mono 
  6    Nelly Moser ASAO 
  7    G.711 ALaw 
  8    G.711 MULaw 
  9    Reserved 
  a    AAC 
  b    Speex 
  f    MP3 8Khz 

Note: For AAC the codec config data is generally a two byte packet that describes the stream. It must
be published first. Here is the basic code to fill in the codec config data.

AACFrame frame = new AACFrame();
int sampleRate = 22100;
int channels = 2;
frame.setSampleRate(sampleRate);
frame.setRateIndex(AACUtils.sampleRateToIndex(sampleRate));
frame.setChannels(channels);
frame.setChannelIndex(AACUtils.channelCountToIndex(sampleRate));
byte[] codecConfig = new byte[2];
AACUtils.encodeAACCodecConfig(frame, codecConfig, 0);

Note: For AAC the header byte is always 0xaf

Note: For Speex the audio data must be encoded as 16000Hz wide band

Video:

H.264
[1-byte header]
[1-byte codec config indicator (1 - video data, 0 - codec config packet)]
[3-byte time difference between dts and pts in milliseconds]
[n-bytes video content or codec config data]

All others
[1-byte header]
[n-bytes audio content]

Below is the bit layout of the header byte of data (table goes from least significant bit to most significant bit):

4 bits Video type:   
  2    Sorenson Spark (H.263) 
  3    Screen 
  4    On2 VP6 
  5    On2 VP6A 
  6    Screen2 
  7    H.264 

2 bit Frame type:   
  1    K frame (key frame) 
  2    P frame 
  3    B frame 

Note: H.264 codec config data is the same as the AVCc packet in a QuickTime container.

Note: All timecode data is in milliseconds


Method Summary
 void addAudioData(byte[] data, int offset, int len, long timecode)
          Add audio data
 void addAudioData(byte[] data, int len, long timecode)
          Add audio data
 void addAudioData(byte[] data, long timecode)
          Add audio data
 void addDataData(byte[] data, int offset, int len, long timecode)
          Add metadata
 void addDataData(byte[] data, int len, long timecode)
          Add metadata
 void addDataData(byte[] data, long timecode)
          Add metadata
 void addVideoData(byte[] data, int offset, int len, long timecode)
          Add video data
 void addVideoData(byte[] data, int len, long timecode)
          Add video data
 void addVideoData(byte[] data, long timecode)
          Add video data
 void close()
          Close the publisher
static Publisher createInstance(IApplicationInstance appInstance)
           
static Publisher createInstance(IVHost vhost, String applicationName)
           
static Publisher createInstance(IVHost vhost, String applicationName, String appInstanceName)
           
 void flush()
          Flush the packets from the input buffer to the output buffer
 IApplicationInstance getAppInstance()
           
 String getFileExtension()
          Get the file extension (default flv)
 long getMaxTimecode()
          Highest timecode written through this publisher (millseconds).
 IMediaStream getStream()
          Get the media stream object
 String getStreamType()
           
 void publish(String streamName)
          Publish a stream (null to stop publishing)
 void publish(String streamName, String howToPublish)
          Start publishing a stream (streamName = null to stop).
 void setFileExtension(String fileExtension)
          Set the file extension
 void setStream(IMediaStream stream)
          Set the media stream object
 void setStreamType(String streamType)
          Set the stream type (default live)
 void unpublish()
           
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

addAudioData

public void addAudioData(byte[] data,
                         int offset,
                         int len,
                         long timecode)
Add audio data

Parameters:
data - data
len - data length
offset - offset
timecode - absolute timecode (milliseconds)

addAudioData

public void addAudioData(byte[] data,
                         int len,
                         long timecode)
Add audio data

Parameters:
data - data
len - data length
timecode - absolute timecode (milliseconds)

addAudioData

public void addAudioData(byte[] data,
                         long timecode)
Add audio data

Parameters:
data - data
timecode - absolute timecode (milliseconds)

addDataData

public void addDataData(byte[] data,
                        int offset,
                        int len,
                        long timecode)
Add metadata

Parameters:
data - data
offset - offset
len - data length
timecode - absolute timecode (milliseconds)

addDataData

public void addDataData(byte[] data,
                        int len,
                        long timecode)
Add metadata

Parameters:
data - data
len - data length
timecode - absolute timecode (milliseconds)

addDataData

public void addDataData(byte[] data,
                        long timecode)
Add metadata

Parameters:
data - data
timecode - absolute timecode (milliseconds)

addVideoData

public void addVideoData(byte[] data,
                         int offset,
                         int len,
                         long timecode)
Add video data

Parameters:
data - data
offset - offset
len - data length
timecode - absolute timecode (milliseconds)

addVideoData

public void addVideoData(byte[] data,
                         int len,
                         long timecode)
Add video data

Parameters:
data - data
len - data length
timecode - absolute timecode (milliseconds)

addVideoData

public void addVideoData(byte[] data,
                         long timecode)
Add video data

Parameters:
data - data
timecode - absolute timecode (milliseconds)

close

public void close()
Close the publisher


createInstance

public static Publisher createInstance(IApplicationInstance appInstance)

createInstance

public static Publisher createInstance(IVHost vhost,
                                       String applicationName)

createInstance

public static Publisher createInstance(IVHost vhost,
                                       String applicationName,
                                       String appInstanceName)

flush

public void flush()
Flush the packets from the input buffer to the output buffer


getAppInstance

public IApplicationInstance getAppInstance()

getFileExtension

public String getFileExtension()
Get the file extension (default flv)

Returns:
file extension

getMaxTimecode

public long getMaxTimecode()
Highest timecode written through this publisher (millseconds).

Returns:
highest timecode written through this publisher (millseconds)

getStream

public IMediaStream getStream()
Get the media stream object

Returns:
media stream object

getStreamType

public String getStreamType()

publish

public void publish(String streamName)
Publish a stream (null to stop publishing)

Parameters:
streamName - stream name

publish

public void publish(String streamName,
                    String howToPublish)
Start publishing a stream (streamName = null to stop). Valid howToPublish values are (live, record, append)

Parameters:
streamName - stream name
howToPublish - publish method (live, record, append)

setFileExtension

public void setFileExtension(String fileExtension)
Set the file extension

Parameters:
fileExtension - file extension

setStream

public void setStream(IMediaStream stream)
Set the media stream object

Parameters:
stream - media stream object

setStreamType

public void setStreamType(String streamType)
Set the stream type (default live)

Parameters:
streamType - stream type

unpublish

public void unpublish()