Advertisement
kuhajeyan

red5

Jun 2nd, 2016
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.56 KB | None | 0 0
  1. public class Red5ClientTest {
  2.  
  3.     private static Timer timer;
  4.  
  5.     private static RTMPClient client;
  6.  
  7.     private static String sourceStreamName;
  8.  
  9.     private static int videoTs;
  10.  
  11.     private static int audioTs;
  12.  
  13.     private static FLVWriter writer;
  14.  
  15.     private static int bytesRead =0;
  16.  
  17.     public static void main(String[] args) throws IOException {
  18.         String sourceHost = "localhost";
  19.         int sourcePort = 1935;
  20.         String sourceApp = "oflaDemo";
  21.         sourceStreamName = "myStream";
  22.         timer = new Timer();        
  23.  
  24.         client = new RTMPClient();
  25.  
  26.         String path = "c:\\temp\\out.flv";
  27.         File file = new File(path);
  28.         if (!file.exists()) {
  29.             file.createNewFile();
  30.         }
  31.         writer = new FLVWriter(file,true);
  32.  
  33.         client.setStreamEventDispatcher(new StreamEventDispatcher());
  34.         client.setStreamEventHandler(new INetStreamEventHandler() {
  35.             public void onStreamEvent(Notify notify) {
  36.                 System.out.printf("onStreamEvent: %s\n", notify);
  37.                 ObjectMap<?, ?> map = (ObjectMap<?, ?>) notify.getCall().getArguments()[0];
  38.                 String code = (String) map.get("code");
  39.                 System.out.printf("<:%s\n", code);
  40.                 if (StatusCodes.NS_PLAY_STREAMNOTFOUND.equals(code)) {
  41.                     System.out.println("Requested stream was not found");
  42.                     client.disconnect();
  43.  
  44.                 }
  45.                 else if (StatusCodes.NS_PLAY_UNPUBLISHNOTIFY.equals(code)
  46.                         || StatusCodes.NS_PLAY_COMPLETE.equals(code)) {
  47.                     System.out.println("Source has stopped publishing or play is complete");
  48.                     client.disconnect();
  49.                 }
  50.             }
  51.         });
  52.         client.setConnectionClosedHandler(new Runnable() {
  53.             public void run() {
  54.                 if (writer != null) {
  55.                     writer.close();
  56.  
  57.                 }
  58.                 System.out.println("Source connection has been closed, proxy will be stopped");
  59.                 System.exit(0);
  60.             }
  61.         });
  62.         client.setExceptionHandler(new ClientExceptionHandler() {
  63.             @Override
  64.             public void handleException(Throwable throwable) {
  65.                 throwable.printStackTrace();
  66.                 System.exit(1);
  67.             }
  68.         });
  69.  
  70.         // connect the consumer
  71.         Map<String, Object> defParams = client.makeDefaultConnectionParams(sourceHost, sourcePort,
  72.                 sourceApp);
  73.         // add pageurl and swfurl
  74.         defParams.put("pageUrl", "");
  75.         defParams.put("swfUrl", "app:/Red5-StreamRelay.swf");
  76.         // indicate for the handshake to generate swf verification data
  77.         client.setSwfVerification(true);
  78.         // connect the client
  79.         client.connect(sourceHost, sourcePort, defParams, new IPendingServiceCallback() {
  80.             public void resultReceived(IPendingServiceCall call) {
  81.                 System.out.println("connectCallback");
  82.                 ObjectMap<?, ?> map = (ObjectMap<?, ?>) call.getResult();
  83.                 String code = (String) map.get("code");
  84.                 if ("NetConnection.Connect.Rejected".equals(code)) {
  85.                     System.out.printf("Rejected: %s\n", map.get("description"));
  86.                     client.disconnect();
  87.                     //proxy.stop();
  88.                 }
  89.                 else if ("NetConnection.Connect.Success".equals(code)) {
  90.                     // 1. Wait for onBWDone
  91.                     timer.schedule(new BandwidthStatusTask(), 2000L);
  92.                     Object result = call.getResult();
  93.                     System.out.println("Red5ClientTest.main()");
  94.                 }
  95.                 else {
  96.                     System.out.printf("Unhandled response code: %s\n", code);
  97.                 }
  98.             }
  99.         });
  100.  
  101.         // keep sleeping main thread while the proxy runs
  102.  
  103.         // kill the timer
  104.         //timer.cancel();
  105.         System.out.println("Stream relay exit");
  106.  
  107.     }
  108.  
  109.     /**
  110.      * Handles result from subscribe call.
  111.      */
  112.     private static final class SubscribeStreamCallBack implements IPendingServiceCallback {
  113.  
  114.         public void resultReceived(IPendingServiceCall call) {
  115.             System.out.println("resultReceived: " + call);
  116.             Object result = call.getResult();
  117.             System.out.println("results came {}" + result);
  118.         }
  119.  
  120.     }
  121.  
  122.     private static final class StreamEventDispatcher implements IEventDispatcher {
  123.  
  124.         public void dispatchEvent(IEvent event) {
  125.             System.out.println("ClientStream.dispachEvent()" + event.toString());
  126.             try {
  127.  
  128.                 //RTMPMessage build = RTMPMessage.build((IRTMPEvent) event);
  129.  
  130.  
  131.  
  132.  
  133.                 IRTMPEvent rtmpEvent = (IRTMPEvent) event;
  134.                 ITag tag = new Tag();
  135.                 tag.setDataType(rtmpEvent.getDataType());
  136.                 if (rtmpEvent instanceof VideoData) {
  137.                     videoTs += rtmpEvent.getTimestamp();
  138.                     tag.setTimestamp(videoTs);
  139.                 }
  140.                 else if (rtmpEvent instanceof AudioData) {
  141.                     audioTs += rtmpEvent.getTimestamp();
  142.                     tag.setTimestamp(audioTs);
  143.                 }
  144.  
  145.  
  146.                 IoBuffer data = ((IStreamData) rtmpEvent).getData().asReadOnlyBuffer();
  147.                 tag.setBodySize(data.limit());
  148.                 tag.setBody(data);
  149.                 try {
  150.                     writer.writeTag(tag);
  151.  
  152.                 } catch (Exception e) {
  153.                     throw new RuntimeException(e);
  154.                 }
  155.                 System.out.println("writting....");    
  156.  
  157.  
  158.             }
  159.             catch (Exception e) {//IOException
  160.                 e.printStackTrace();
  161.             }
  162.         }
  163.  
  164.     }
  165.  
  166.     private static final class BandwidthStatusTask extends TimerTask {
  167.  
  168.         @Override
  169.         public void run() {
  170.             // check for onBWDone
  171.             System.out.println("Bandwidth check done: " + client.isBandwidthCheckDone());
  172.             // cancel this task
  173.             this.cancel();
  174.             // create a task to wait for subscribed
  175.             timer.schedule(new PlayStatusTask(), 1000L);
  176.             // 2. send FCSubscribe
  177.             client.subscribe(new SubscribeStreamCallBack(), new Object[] { sourceStreamName });
  178.         }
  179.  
  180.     }
  181.  
  182.     private static final class PlayStatusTask extends TimerTask {
  183.  
  184.         @Override
  185.         public void run() {
  186.             // checking subscribed
  187.             System.out.println("Subscribed: " + client.isSubscribed());
  188.             // cancel this task
  189.             this.cancel();
  190.             // 3. create stream
  191.             client.createStream(new CreateStreamCallback());
  192.         }
  193.  
  194.     }
  195.  
  196.     /**
  197.      * Creates a "stream" via playback, this is the source stream.
  198.      */
  199.     private static final class CreateStreamCallback implements IPendingServiceCallback {
  200.  
  201.         public void resultReceived(IPendingServiceCall call) {
  202.             System.out.println("resultReceived: " + call);
  203.             int streamId = ((Number) call.getResult()).intValue();
  204.             System.out.println("stream id: " + streamId);
  205.             // send our buffer size request
  206.             if (sourceStreamName.endsWith(".flv") || sourceStreamName.endsWith(".f4v")
  207.                     || sourceStreamName.endsWith(".mp4")) {
  208.                 client.play(streamId, sourceStreamName, 0, -1);
  209.             }
  210.             else {
  211.                 client.play(streamId, sourceStreamName, -1, 0);
  212.             }
  213.         }
  214.  
  215.     }
  216.  
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement