n4rzul
n4rzul

Reputation: 4078

Stream audio using Java Media Framework on http protocol

I am trying to stream audio over http that can be opened by VLC winamp or WMP. I currently have code that does this over the rtp protocol but not http. Just changing the url in the code below to http does not work. How do I modifify the code below to stream via http?

public class MediaTransmitter {

        private MediaLocator mediaLocator = null;
        private DataSink dataSink = null;
        private Processor mediaProcessor = null;
        private static final Format[] FORMATS = new Format[]{new AudioFormat(AudioFormat.MPEG_RTP)};
        private static final ContentDescriptor CONTENT_DESCRIPTOR = new ContentDescriptor(ContentDescriptor.RAW_RTP);

        public MediaTransmitter(MediaLocator locator) {
            mediaLocator = locator;
        }

        public void startTransmitting() throws IOException {
            mediaProcessor.start();
            dataSink.open();
            dataSink.start();
        }

        public void stopTransmitting() throws IOException {
            dataSink.stop();
            dataSink.close();
            mediaProcessor.stop();
            mediaProcessor.close();
        }

        public void setDataSource(DataSource ds) throws IOException, NoProcessorException, CannotRealizeException, NoDataSinkException {
            mediaProcessor = Manager.createRealizedProcessor(new ProcessorModel(ds, FORMATS, CONTENT_DESCRIPTOR));
            dataSink = Manager.createDataSink(mediaProcessor.getDataOutput(), mediaLocator);
        }



        public static void main(String[] args) {

            try {
                MediaLocator locator = new MediaLocator("rtp://MY_PUBLIC_IP_HERE:10000/audio");
                MediaTransmitter transmitter = new MediaTransmitter(locator);
                System.out.println("-> Created media locator: '" + locator + "'");

                File mediaFile = new File("D:\\data\\audio\\posters.mp3");
                DataSource source = Manager.createDataSource(new MediaLocator(mediaFile.toURL()));
                System.out.println("-> Created data source: '" + mediaFile.getAbsolutePath() + "'");

                // set the data source.
                transmitter.setDataSource(source);
                System.out.println("-> Set the data source on the transmitter");

                // start transmitting the file over the network.
                transmitter.startTransmitting();
                System.out.println("-> Transmitting...");
                System.out.println("   Press the Enter key to exit");

                // wait for the user to press Enter to proceed and exit.
                System.in.read();
                System.out.println("-> Exiting");
                transmitter.stopTransmitting();
            } catch (Throwable t) {
                t.printStackTrace();
            }

            System.exit(0);
        }
    }

Upvotes: 0

Views: 1005

Answers (1)

n4rzul
n4rzul

Reputation: 4078

As it turns out RTP is a push protocol so using the same url to open the stream failed in VLC. However if I change rtp to http in VLC and specified the url up until the port (minus the rest of the url) it works.

Upvotes: 1

Related Questions