2011-01-29 17 views
9

के साथ स्ट्रीम नहीं करेगा मूलभूत विचार। एमपी 3 फ़ाइल तक पहुंचने और आरटीपी स्ट्रीम के माध्यम से अन्य क्लाइंट को भेजने के लिए है, जो उस गीत को खेलना चाहेंगे।एमपी 3 जेएमएफ

यहां RTPServer.java है, जिसे मैंने ऑनलाइन पाया और इसे मेरी पसंद में संशोधित किया।

package server; 

import java.net.InetAddress; 
import javax.media.rtp.*; 
import javax.media.rtp.rtcp.*; 
import javax.media.*; 
import javax.media.protocol.*; 
import javax.media.control.*; 

public class RTPServer implements ControllerListener, Runnable { 
    private boolean realized = false; 
    private boolean configured = false; 
    private String ipAddress; 
    Processor p; 
    MediaLocator src; 

    public static void main (String[] args) { 
     RTPServer rtp = new RTPServer("192.168.1.101", "04 - Blue.mp3"); 
     Thread t = new Thread(rtp); 
     t.start(); 
    } 

    public RTPServer(String ip, String song) { 
     ipAddress = ip; 
String srcFile = "Muzika\\" + song; 
src = new MediaLocator("file:" + srcFile); 

    } 

    private void setTrackFormat(Processor p) { 
// Get the tracks from the processor 
TrackControl [] tracks = p.getTrackControls(); 
// Do we have atleast one track? 
if (tracks == null || tracks.length < 1) { 
    System.out.println("Couldn't find tracks in processor"); 
    System.exit(1); 
} 

// Set the output content descriptor to RAW_RTP 
// This will limit the supported formats reported from 
// Track.getSupportedFormats to only valid RTP formats. 
ContentDescriptor cd = new ContentDescriptor(ContentDescriptor.RAW_RTP); 
p.setContentDescriptor(cd); 

Format supported[]; 
Format chosen; 
boolean atLeastOneTrack = false; 

// Program the tracks. 
for (int i = 0; i < tracks.length; i++) { 
    Format format = tracks[i].getFormat(); 
      System.out.println("Trenutni format je " +format.getEncoding()); 
    if (tracks[i].isEnabled()) { 
    supported = tracks[i].getSupportedFormats(); 
    for (int n = 0; n < supported.length; n++) 
     System.out.println("Supported format: " + supported[n]); 

    // We've set the output content to the RAW_RTP. 
    // So all the supported formats should work with RTP. 
    // We'll just pick the first one. 

    if (supported.length > 0) { 
     chosen = supported[0]; // this is where I tried changing formats 
     tracks[i].setFormat(chosen); 
     System.err.println("Track " + i + " is set to transmit as: " +chosen); 
     atLeastOneTrack = true; 
    } else 
     tracks[i].setEnabled(false); 
    } else 
    tracks[i].setEnabled(false); 
} 
    } 

    private void transmit(Processor p) { 
try { 
    DataSource output = p.getDataOutput(); 
    PushBufferDataSource pbds = (PushBufferDataSource) output; 
    RTPManager rtpMgr = RTPManager.newInstance(); 
    SessionAddress localAddr, destAddr; 
    SendStream sendStream; 
    int port = 42050; 
    SourceDescription srcDesList[]; 
    localAddr = new SessionAddress(InetAddress.getLocalHost(), port); 
    InetAddress ipAddr = InetAddress.getByName(ipAddress); 
    destAddr = new SessionAddress(ipAddr, port); 
    rtpMgr.initialize(localAddr); 
    rtpMgr.addTarget(destAddr); 
    sendStream = rtpMgr.createSendStream(output, 0); 
    sendStream.start(); 
    System.err.println("Created RTP session: " + ipAddress + " " + port); 
    p.start(); 
} catch(Exception e) { 
    e.printStackTrace(); 
} 
    } 

    public synchronized void controllerUpdate(ControllerEvent evt) { 
if (evt instanceof RealizeCompleteEvent) { 
    realized = true; 
} else if (evt instanceof ConfigureCompleteEvent) { 
    configured = true; 
} else if (evt instanceof EndOfMediaEvent) { 
    System.exit(0); 
} else { 
    // System.out.println(evt.toString()); 
} 
    } 

    public void run() { 

try { 
    p = Manager.createProcessor(src); 
    p.addControllerListener(this); 
    p.configure(); 
    while (! configured) { 
    try { 
     Thread.currentThread().sleep(100L);; 
    } catch (InterruptedException e) { 
     // ignore 
    } 
    } 

    setTrackFormat(p); 
    p.setContentDescriptor(new ContentDescriptor(ContentDescriptor.RAW_RTP)); 

    p.realize(); 
    while (! realized) { 
    try { 
     Thread.currentThread().sleep(100L);; 
    } catch (InterruptedException e) { 
     // ignore 
    } 
    } 
    transmit(p); 

} catch(Exception e) { 
    e.printStackTrace(); 
    System.exit(1); 
} 
    } 
} 

और यहाँ, अंत प्राप्त कर रहा है RTPClient:

package client; 


import javax.media.*; 

public class RTPClient implements ControllerListener, Runnable { 

    Player p; 
    MediaLocator src; 

    public static void main(String[] args) { 
     RTPClient rtp = new RTPClient("192.168.1.100"); 
     Thread t = new Thread(rtp); 
     t.start(); 

    } 

    public RTPClient(String ip) { 
String srcUrl = "rtp://" + ip + ":42050/audio/1"; 
DataSink sink; 
src = new MediaLocator(srcUrl); 
    } 
    public void run() { 
     try { 
    p = Manager.createPlayer(src); 
    p.addControllerListener(this); 
    p.start(); 
} catch(Exception e) { 
    e.printStackTrace(); 
    System.exit(1); 
} 
    } 

    public synchronized void controllerUpdate(ControllerEvent evt) { 
if (evt instanceof EndOfMediaEvent) { 
    System.exit(0); 
} else { 
    System.out.println(evt.toString()); 
} 
    } 
} 

मैं लगा, इसे सफलतापूर्वक जो कुछ फ़ाइल मैं चुन भेजता है, लेकिन जब मैं .mp3 भेजने के लिए, ग्राहक यह नहीं खेलेंगे। मुझे मिलता है:

RTP Handler internal error: 
javax.media.ControllerErrorEvent[[email protected],message=Internal 
module [email protected]: failed to handle a data 
format change!] 

दिलचस्प बात यह है कि .wav पूरी तरह से भेजा गया है। तो मेरा अनुमान भेजने से पहले प्रारूप सेट था। और मैंने कुछ अन्य समर्थित प्रारूप में प्रारूप बदलने की कोशिश की, लेकिन फिर मुझे अन्य त्रुटियों का गुच्छा मिल गया। (बिल्ट-इन भेजने/JMF में मीडिया धाराओं प्राप्त करने के लिए एप्लिकेशन), और जब मैं .mp3 स्ट्रीम करने के लिए प्रयास करते हैं, मैं जब मेरे एप्लिकेशन चला रहे के रूप में ठीक उसी त्रुटि मिलती है

 Failed to build a graph for the given custom options. 
Failed to realize: [email protected] 
    Cannot build a flow graph with the customized options: 
    Unable to transcode format: mpegaudio, 48000.0 Hz, 16-bit, Stereo, LittleEndian, Signed, 20000.0 frame rate, FrameSize=11264 bits 
     to: ULAW/rtp, 8000.0 Hz, 8-bit, Stereo 
     outputting to: RAW/RTP 
Error: Unable to realize [email protected] 

अंत में, मैं JMStudio खोला। जेएमएफ ठीक है, मैंने पैथ और क्लास्स्पैट की जांच की है, मैंने एमपी 3 प्लगइन भी स्थापित किया है जो भी ठीक है। सबकुछ ठीक लगता है, लेकिन यह काम नहीं करता है! कम से कम। एमपी 3 नहीं है। तो, मैं। एमपी 3 कैसे कर सकता हूं "दूसरे छोर पर जाएं"?

उत्तर

8

हल हो गया।

मुझे बस इतना करना था कि प्रेषक/रिसीवर के लिए इन लाइनों को कन्स्ट्रक्टर में जोड़ें।

Format input1 = new AudioFormat(AudioFormat.MPEGLAYER3); 
Format input2 = new AudioFormat(AudioFormat.MPEG); 
Format output = new AudioFormat(AudioFormat.LINEAR); 
PlugInManager.addPlugIn(
     "com.sun.media.codec.audio.mp3.JavaDecoder", 
     new Format[]{input1, input2}, 
     new Format[]{output}, 
     PlugInManager.CODEC); 

इस समस्या के साथ किसी और को मदद कर सकता है :) फिर भी क्यों JMStudio काम नहीं कर रहा नहीं पता ... ऐसा नहीं है कि मैं अब और नहीं परवाह है।

+1

क्लाइंट/सर्वर करता है differnt ips पर होना है या वे एक स्थानीय होस्ट की यानी bothwork/पर दोनों काम करते हैं – LmC

संबंधित मुद्दे