// ------------------------
  // Handler for timer
  // ------------------------
  public void actionPerformed(ActionEvent e) {

    // if the current image nb is less than the length of the video
    if (imagenb < VIDEO_LENGTH) {
      // update current imagenb
      imagenb++;

      try {
        // get next frame to send from the video, as well as its size
        int image_length = video.getnextframe(buf);

        // Builds an RTPpacket object containing the frame
        RTPpacket rtp_packet =
            new RTPpacket(MJPEG_TYPE, imagenb, imagenb * FRAME_PERIOD, buf, image_length);

        // get to total length of the full rtp packet to send
        int packet_length = rtp_packet.getlength();

        // retrieve the packet bitstream and store it in an array of bytes
        byte[] packet_bits = new byte[packet_length];
        rtp_packet.getpacket(packet_bits);

        // send the packet as a DatagramPacket over the UDP socket
        senddp = new DatagramPacket(packet_bits, packet_length, ClientIPAddr, RTP_dest_port);
        RTPsocket.send(senddp);

        // System.out.println("Send frame #"+imagenb);
        // print the header bitstream
        rtp_packet.printheader();

        // update GUI
        label.setText("Send frame #" + imagenb);
      } catch (Exception ex) {
        System.out.println("Exception caught: " + ex);
        System.exit(0);
      }
    } else {
      // if we have reached the end of the video file, stop the timer
      timer.stop();
    }
  }
示例#2
0
  @Override
  public void actionPerformed(ActionEvent e) {

    // if the current image nb is less than the length of the video
    if (frameNumber < VIDEO_LENGTH) {
      // update current imagenb
      frameNumber++;

      try {
        // get next frame to send from the video, as well as its size
        int imageLength = video.getnextframe(buffer);

        // Builds an RTPpacket object containing the frame
        RTPpacket rtpPacket =
            new RTPpacket(MJPEG_TYPE, frameNumber, frameNumber * FRAME_PERIOD, buffer, imageLength);

        // get to total length of the full rtp packet to send
        int packetLength = rtpPacket.getLength();

        // retrieve the packet bitstream and store it in an array of bytes
        byte[] packetBits = new byte[packetLength];
        rtpPacket.getPacket(packetBits);

        //				System.out.println("Send frame #" + imagenb);
        rtpTransport.send(packetBits, packetLength);

        // update GUI
        this.setChanged();
        this.notifyObservers(Update.FRAME);
      } catch (IOException ex) {
        RTSPServer.log("I/O exception sending RTSP packet: %s\n", ex.getMessage());
        return;
      }
    } else {
      // if we have reached the end of the video file, stop the timer
      timer.stop();
    }
  }
示例#3
0
    protected Void doInBackground(Void... params) {

      // send the setup message to the serversend_RTSP_request in order to receive the live stream

      Socket socket = null;
      Log.e("here", "here");
      // get video filename to request:
      try {
        socket = new Socket(ServerHost, RTSP_server_port);
        Log.e("socket established", "socket established");
        RTSPsocket = socket;
        Log.e("here12", "here12");
        RTSPBufferedReader = new BufferedReader(new InputStreamReader(RTSPsocket.getInputStream()));
        Log.e("here13", "here13");
        RTSPBufferedWriter =
            new BufferedWriter(new OutputStreamWriter(RTSPsocket.getOutputStream()));
      } catch (Exception ee) {
        Log.e("Exception", "Exception");
        if (handler == null) Log.e("handler", "null");
        else Log.e("handler", "not null");
        showToast("server is not reachable");
        Log.e("destroy", "destroy the activity");
        onPause();
        onDestroy();
        if (socket != null) {
          try {
            socket.close();
          } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
        }
        return null;
      }

      assert RTSPBufferedReader != null;
      assert RTSPBufferedWriter != null;
      Log.e("here1", "here1");
      // Init non-blocking RTPsocket that will be used to receive data

      try {
        // construct a new DatagramSocket to receive RTP packets from the server, on port
        // RTP_RCV_PORT
        RTPsocket = new DatagramSocket(RTP_RCV_PORT);
        RTPsocket.setSoTimeout(10000);
      } catch (SocketException se) {
        System.out.println("Socket exception: " + se);
        Log.e("DatagramSocket", "Exception");
      }

      Log.e("here2", "here2");

      RTSPSeqNb = 1;
      send_RTSP_request("SETUP", VideoFileName, RTSPSeqNb);
      if (parse_server_response() == 200) {
        RTSPSeqNb++;
        send_RTSP_request("PLAY", VideoFileName, RTSPSeqNb);
        if (parse_server_response() == 200) {
          byte[] buf = new byte[100000];
          Log.e("here3", "here3");

          long prevTime = System.currentTimeMillis();

          while (!stop) {
            // Construct a DatagramPacket to receive data from the UDP socket
            rcvdp = new DatagramPacket(buf, buf.length);
            Log.e("here4", "here4");
            try {
              // receive the DP from the socket:
              RTPsocket.receive(rcvdp);
              Log.e("here5", "here5");

              long currentTime = System.currentTimeMillis();
              if (((currentTime - prevTime)) > 3000L) {
                Log.e("play", "play");
                prevTime = currentTime;
                RTSPSeqNb++;
                send_RTSP_request("PLAY", VideoFileName, RTSPSeqNb);
                /*
                int m = RTSPSeqNb;
                StringBuffer sb = new StringBuffer();
                while(m != 0) {
                	sb.insert(0, m % 10);
                	m = m / 10;
                }
                String seq = new String(sb);
                */
                Log.e("play send", "play send");
                Log.e("play send", "" + RTSPSeqNb);
              }
              // create an RTPpacket object from the DP
              RTPpacket rtp_packet = new RTPpacket(rcvdp.getData(), rcvdp.getLength());
              Log.e("here6", "here6");
              // get the payload bitstream from the RTPpacket object
              int payload_length = rtp_packet.getpayload_length();
              Log.e("here7", "here7");
              byte[] payload = new byte[payload_length];
              rtp_packet.getpayload(payload);
              Log.e("here8", "here8");

              imageToDraw = BitmapFactory.decodeByteArray(payload, 0, payload.length);
              /*
              for(int i = 0; i < payload.length; i++) {
              	Log.e(Integer.toString(i), Byte.toString(payload[i]));
              }*/
              Log.e("payload length", Integer.toString(payload.length));
              Log.e("first", Byte.toString(payload[0]));
            } catch (SocketTimeoutException e) {
              Log.e("timeout", "timeout");
              if (handler == null) Log.e("handler", "null");
              else Log.e("handler", "not null");
              showToast("network broken");
              Log.e("destroy", "destroy the activity");
              onPause();
              onDestroy();
            } catch (IOException ioe) {
              ioe.printStackTrace();
              Log.e("IOException", "IOException");
            }
          }
        }
      }
      Log.e("return", "return");
      return null;
    }