Created
May 4, 2016 09:55
-
-
Save hightemp/e4fc1955c82698ff5d7031beea021a74 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Re: how to stream audio and video captured from mic and webcam in sync. | |
| // As mentioned by you, i am doing exactly like that . I have coded transmitter class. | |
| // But at the receiver side it is unable to play the stream. I am posting my code . | |
| // Can you please help with me with any mistake i m making in my code ?? | |
| // Or with way I should receive stream at receiver side. Plz see my code. I am streaming to ip address 172.31.80.67. | |
| // https://community.oracle.com/thread/1279991?start=0&tstart=0 | |
| package heyram; | |
| import java.util.logging.Level; | |
| import java.util.logging.Logger; | |
| import javax.media.control.*; | |
| import javax.media.protocol.*; | |
| import javax.media.format.*; | |
| import java.io.IOException; | |
| import java.util.Vector; | |
| import java.net.InetAddress; | |
| import java.awt.*; | |
| import java.io.*; | |
| import java.net.InetAddress; | |
| import javax.media.*; | |
| import javax.media.protocol.*; | |
| import javax.media.protocol.DataSource; | |
| import javax.media.format.*; | |
| import javax.media.control.TrackControl; | |
| import javax.media.control.QualityControl; | |
| import javax.media.rtp.*; | |
| import javax.media.rtp.rtcp.*; | |
| import com.sun.media.rtp.*; | |
| import java.lang.Thread.*; | |
| public class HEYRAM{ | |
| Format[] format=new Format[2]; | |
| Vector[] devices=new Vector[2]; | |
| CaptureDeviceInfo[] di=new CaptureDeviceInfo[2]; | |
| SessionManager rtpsm = new com.sun.media.rtp.RTPSessionMgr(); | |
| Processor p=null; | |
| Processor p1=null; | |
| boolean result; | |
| public HEYRAM(String address,int port,int ttl){ | |
| try{ | |
| InetAddress destaddr = InetAddress.getByName(address); | |
| SessionAddress sessaddr = new SessionAddress(destaddr, | |
| port, | |
| destaddr, | |
| port + 1); | |
| String cname = rtpsm.generateCNAME(); | |
| String username = null; | |
| try { | |
| username = System.getProperty("user.name"); | |
| } catch (SecurityException e){ | |
| username = "jmf-user"; | |
| } | |
| // create our local Session Address | |
| SessionAddress localaddr = new SessionAddress(); | |
| SourceDescription[] userdesclist= new SourceDescription[] | |
| { | |
| new SourceDescription(SourceDescription | |
| .SOURCE_DESC_EMAIL, | |
| "jmf-user@sun.com", | |
| 1, | |
| false), | |
| new SourceDescription(SourceDescription | |
| .SOURCE_DESC_CNAME, | |
| cname, | |
| 1, | |
| false), | |
| new SourceDescription(SourceDescription | |
| .SOURCE_DESC_TOOL, | |
| "JMF RTP Player v2.0", | |
| 1, | |
| false) | |
| }; | |
| rtpsm.initSession(localaddr, | |
| userdesclist, | |
| 0.05, | |
| 0.25); | |
| rtpsm.startSession(sessaddr,ttl,null); | |
| } catch (Exception e) { | |
| System.err.println(e.getMessage()); | |
| //return null; | |
| } | |
| // rtpsm.initSession(localAddress, defaultUserDesc, rtcp_bw_fraction, rtcp_sender_bw_fraction); | |
| // rtpsm.startSession(...); | |
| } | |
| public void createVideoSession() | |
| { | |
| format[1] = new VideoFormat(VideoFormat.YUV); | |
| devices[1]= CaptureDeviceManager.getDeviceList( format[1]); | |
| di[1]= null; | |
| if (devices[1].size() > 0) { | |
| di[1] = (CaptureDeviceInfo)devices[1].elementAt(0); | |
| System.out.println(di[1].toString()); | |
| } | |
| else { | |
| // exit if we could not find the relevant capture device. | |
| System.out.println("1234jjfjsajfjasf1"); | |
| System.exit(-1); | |
| } | |
| // Create a processor for this capture device & exit if we | |
| // cannot create it | |
| try { | |
| p1 = Manager.createProcessor(di[1].getLocator()); | |
| } catch (IOException e) { | |
| System.out.println("1234jjfjsajfjasf2"); | |
| System.exit(-1); | |
| } catch (NoProcessorException e) { | |
| System.out.println("1234jjfjsajfjasf3"); | |
| System.exit(-1); | |
| } | |
| // at this point, we have succesfully created the processor. | |
| // Realize it and block until it is configured. | |
| // p1.configure(); | |
| result = waitForState(p1, Processor.Configured); | |
| if (result == false) | |
| System.out.println("Couldn't realize processor"); | |
| p1.setContentDescriptor(new ContentDescriptor( ContentDescriptor.RAW_RTP)); | |
| // block until it has been configured | |
| TrackControl track[] = p1.getTrackControls(); | |
| boolean encodingOk = false; | |
| // Go through the tracks and try to program one of them to | |
| // output ULAW_RTP data. | |
| for (int i = 0; i < track.length; i++) { | |
| if (!encodingOk && track[i] instanceof FormatControl) { | |
| if (((FormatControl)track).setFormat( new VideoFormat(VideoFormat.YUV)) == null) { | |
| track[i].setEnabled(false); | |
| } | |
| else { | |
| encodingOk = true; | |
| } | |
| } | |
| else { | |
| // we could not set this track to gsm, so disable it | |
| track[i].setEnabled(false); | |
| } | |
| } | |
| // Realize it and block until it is realized. | |
| p1.realize(); | |
| result = waitForState(p1, Processor.Realized); | |
| if (result == false) | |
| System.out.println("Couldn't realize processor"); | |
| // block until realized. | |
| // get the output datasource of the processor and exit | |
| // if we fail | |
| DataSource ds = null; | |
| try { | |
| ds = p1.getDataOutput(); | |
| } catch (NotRealizedError e){ | |
| //System.exit(-1); | |
| System.out.println("1234jjfjsajfjasf4"); | |
| } | |
| // Create a SessionManager and hand over the | |
| // datasource for SendStream creation. | |
| // The session manager then needs to be initialized and started: | |
| // rtpsm.initSession(...); | |
| // rtpsm.startSession(...); | |
| try { | |
| (rtpsm.createSendStream(ds, 0)).start(); | |
| } catch (IOException e){ | |
| System.out.println("1234jjfjsajfjasf6"); | |
| e.printStackTrace(); | |
| } catch( UnsupportedFormatException e) { | |
| System.out.println("1234jjfjsajfjasf7"); | |
| e.printStackTrace(); | |
| } | |
| } | |
| public void createAudioSession(){ | |
| format[0] = new AudioFormat("linear",8000,8,1); | |
| devices[0]= CaptureDeviceManager.getDeviceList( format[0]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment