Package javax.media

Examples of javax.media.ResourceUnavailableException


        AVCODEC.avcodec_init()// TODO: everything seems to be fine if we don't call this...
      }
      catch (Throwable t)
      { 
        logger.log(Level.WARNING, "" + t, t);
        throw new ResourceUnavailableException("avcodec_init or av_register_all failed");
      }
     
      // not sure what the consequences of such a mismatch are, but it is worth logging a warning:
      if (AVCODEC.avcodec_version() != AVCodecLibrary.LIBAVCODEC_VERSION_INT)
        logger.warning("ffmpeg-java and ffmpeg versions do not match: avcodec_version=" + AVCODEC.avcodec_version() + " LIBAVCODEC_VERSION_INT=" + AVCodecLibrary.LIBAVCODEC_VERSION_INT);
     
      String urlStr;
     
      final String protocol = source.getLocator().getProtocol();
      // TODO: ffmpeg appears to support multiple file protocols, for example: file: pipe: udp: rtp: tcp: http:
      // we should also allow those.
      // TODO: would be best to query this dynamically from ffmpeg

      // we don't really use the DataSource, we just grab its URL.  So arbitrary data sources won't work.
      // otherwise, we register a custom URLHandler with ffmpeg, which calls us back to get the data.
 
      if (protocol.equals("file") || protocol.equals("http")) {
        // just use the URL from the datasource
       
        // FMJ supports relative file URLs, but FFMPEG does not.  So we'll rewrite the URL here:
        // TODO: perhaps we should only do this if FFMPEG has a problem (av_open_input_file returns nonzero).
        if (protocol.equals("file"))
          // ffmpeg has problems with windows file-URLs like file:///c:/
          urlStr = URLUtils.extractValidPathFromFileUrl(source.getLocator().toExternalForm());
        else
          urlStr = source.getLocator().toExternalForm();
      }
      else
      // use the real java datasource, via callbacks.
        CallbackURLProtocolMgr.register(AVFORMAT);
       
        // TODO: do this in start?
        final String callbackURL = CallbackURLProtocolMgr.addCallbackURLProtocolHandler(new PullDataSourceCallbackURLProtocolHandler(source));
       
        // TODO: we need to remove the handler when we are done.
       
        urlStr = callbackURL;
      }
     
      final PointerByReference ppFormatCtx = new PointerByReference();
     
      // Open video file
      final int ret = AVFORMAT.av_open_input_file(ppFormatCtx, urlStr, null, 0, null);
      if (ret != 0) {
        throw new ResourceUnavailableException("av_open_input_file failed: " + ret); // Couldn't open file
      }
     
     
      formatCtx = new AVFormatContext(ppFormatCtx.getValue());
      //System.out.println(new String(formatCtx.filename));
     
     
      // Retrieve stream information
      if (AVFORMAT.av_find_stream_info(formatCtx) < 0)
          throw new ResourceUnavailableException("Couldn't find stream information"); // Couldn't find stream information
   
      AVFORMAT.dump_format(formatCtx, 0, urlStr, 0);
   
     
      VideoTrack videoTrack = null;
      AudioTrack audioTrack = null;
        for (int i = 0; i < formatCtx.nb_streams; i++)
        {   final AVStream stream = new AVStream(formatCtx.getStreams()[i]);
          final AVCodecContext codecCtx = new AVCodecContext(stream.codec);
            if (codecCtx.codec_id == 0)
            {  logger.info("Codec id is zero (no codec) - skipping stream " + i);
              continue;
            }
            if (codecCtx.codec_type == AVCodecLibrary.CODEC_TYPE_VIDEO && videoTrack == null)
            {
              videoTrack = new VideoTrack(i, stream, codecCtx);
            }
          else if (codecCtx.codec_type == AVCodecLibrary.CODEC_TYPE_AUDIO && audioTrack == null)
          {
            try
            {
              audioTrack = new AudioTrack(i, stream, codecCtx);
            }
            catch (ResourceUnavailableException e)
            { 
              if (!PROCEED_IF_NO_AUDIO_CODEC)
                throw e;
              logger.log(Level.WARNING, "Skipping audio track: " + e, e);
            }
          }
          else
          //throw new ResourceUnavailableException("Unknown track codec type " + codecCtx.codec_type + " for track " + i);
          }
         
        }
       
        if (audioTrack == null && videoTrack == null)
          throw new ResourceUnavailableException("No audio or video track found");
        else if (audioTrack != null && videoTrack != null)
          tracks = new PullSourceStreamTrack[] {videoTrack, audioTrack};
        else if (audioTrack != null)
          tracks = new PullSourceStreamTrack[] {audioTrack};
        else
View Full Code Here


        {

          // Find the decoder for the video stream
          this.codec = AVCODEC.avcodec_find_decoder(codecCtx.codec_id);
          if (codec == null)
              throw new ResourceUnavailableException("Codec not found for codec_id " + codecCtx.codec_id + " (0x" + Integer.toHexString(codecCtx.codec_id) + ")"); // Codec not found - see AVCodecLibrary.CODEC_ID constants
         
          // Open codec
          if (AVCODEC.avcodec_open(codecCtx, codec) < 0)
             throw new ResourceUnavailableException("Could not open codec"); // Could not open codec
       
       
          // Allocate video frame
          srcFrame = AVCODEC.avcodec_alloc_frame();
          if (srcFrame == null)
            throw new ResourceUnavailableException("Could not allocate frame");
         
          // Allocate an AVFrame structure
          dstFrame = AVCODEC.avcodec_alloc_frame();
          if (dstFrame == null)
            throw new ResourceUnavailableException("Could not allocate frame");
         
          // set format
          float frameRate = (float)getFPS(stream, codecCtx);
          // we let ffmpeg convert our data to our preferred pixel format
          dstPixFmt = ListFormats.getPreferedPixelFormat();
View Full Code Here

     
          // Find the decoder for the video stream
          this.codec = AVCODEC.avcodec_find_decoder(codecCtx.codec_id);
          if (codec == null)
              throw new ResourceUnavailableException("Codec not found for codec_id " + codecCtx.codec_id + " (0x" + Integer.toHexString(codecCtx.codec_id) + ")"); // Codec not found - see AVCodecLibrary.CODEC_ID constants
         
          // Open codec
          if (AVCODEC.avcodec_open(codecCtx, codec) < 0)
             throw new ResourceUnavailableException("Could not open codec"); // Could not open codec
       
          // actually appears to be used as a short array.
          bufferSize = AVCodecLibrary.AVCODEC_MAX_AUDIO_FRAME_SIZE;
          buffer = AVUTIL.av_malloc(bufferSize);
        
View Full Code Here

          /* look for further theora headers */
          while (theora_p != 0 && (theora_p < 3) && ((ret = to.packetout(op))) != 0)
          {
            if (ret < 0)
            {
              throw new ResourceUnavailableException("Error parsing Theora stream headers; corrupt stream?");
            }
            if (ti.decodeHeader(tc, op) != 0)
            {
              throw new ResourceUnavailableException("Error parsing Theora stream headers; corrupt stream?");
            }
            theora_p++;
            if (theora_p == 3)
              break;
          }

          /* look for more vorbis header packets */
          while (vorbis_p != 0 && (vorbis_p < 3) && ((ret = vo.packetout(op))) != 0)
          {
            if (ret < 0)
            {
              throw new ResourceUnavailableException("Error parsing Vorbis stream headers; corrupt stream?");
            }

            if (vi.synthesis_headerin(vc, op) != 0)
            {
              throw new ResourceUnavailableException("Error parsing Vorbis stream headers; corrupt stream?");
            }
            vorbis_p++;
            if (vorbis_p == 3)
              break;
          }

          /* The header pages/packets will arrive before anything else we
             care about, or the stream is not obeying spec */

          if (oy.pageout(og) > 0)
          {
            queue_page(og); /* demux into the appropriate stream */
          } else
          {
            final int ret2 = buffer_data(oy); /* someone needs more data */
            if (ret2 <= 0)
            {
              throw new ResourceUnavailableException("End of file while searching for codec headers.");
            }
          }
        }

        //System.out.println("Initializing decoders...");

        /* and now we have it all.  initialize decoders */
        if (theora_p != 0)
        {
          td.decodeInit(ti);
          final double fps = (double) ti.fps_numerator / (double) ti.fps_denominator;
          logger.info("Ogg logical stream " + Integer.toHexString(getSerialNo(to)) + " is Theora " + ti.width + "x" + ti.height + " " + fps + " fps");
          // TODO: jheora doesn't have pixelformat as a field of ti:
          //      switch(ti.pixelformat){
          //        case TheoraLibrary.OC_PF_420: System.out.printf(" 4:2:0 video\n"); break;
          //        case TheoraLibrary.OC_PF_422: System.out.printf(" 4:2:2 video\n"); break;
          //        case TheoraLibrary.OC_PF_444: System.out.printf(" 4:4:4 video\n"); break;
          //        case TheoraLibrary.OC_PF_RSVD:
          //        default:
          //          System.out.printf(" video\n  (UNKNOWN Chroma sampling!)\n");
          //    break;
          //      }
          if (ti.width != ti.frame_width || ti.height != ti.frame_height)
          {  logger.warning("  Frame content is " + ti.frame_width + "x" + ti.frame_height + " with offset (" +  ti.offset_x + "," + ti.offset_y + ").");
            // TODO: we need to handle cropping properly.
          }
          report_colorspace(ti);
          dump_comments(tc);
        } else
        {
          /* tear down the partial theora setup */
          ti.clear();
          // tc.clear();
        }
        if (vorbis_p != 0)
        {
          vd.synthesis_init(vi);
          vb.init(vd);
          logger.info("Ogg logical stream " + Integer.toHexString(getSerialNo(vo)) + " is Vorbis " + vi.channels + " channel " + vi.rate + " Hz audio.");

        } else
        {
          /* tear down the partial vorbis setup */
          vi.clear();
          // vc.clear();
        }
       
        stateflag = 0; /* playback has not begun */

        VideoTrack videoTrack = null;
        AudioTrack audioTrack = null;

        if (theora_p != 0)
        {
          videoTrack = new VideoTrack();
        }
        if (vorbis_p != 0)
        {
          audioTrack = new AudioTrack();
        }

        if (audioTrack == null && videoTrack == null)
          throw new ResourceUnavailableException("No audio or video track found");
        else if (audioTrack != null && videoTrack != null)
          tracks = new PullSourceStreamTrack[] { videoTrack, audioTrack };
        else if (audioTrack != null)
          tracks = new PullSourceStreamTrack[] { audioTrack };
        else
          tracks = new PullSourceStreamTrack[] { videoTrack };

      } catch (IOException e)
      {
        logger.log(Level.WARNING, "" + e, e);
        throw new ResourceUnavailableException("" + e);
      }
    }

   
    super.open();
View Full Code Here

          /* look for further theora headers */
          while (theora_p != 0 && (theora_p < 3) && ((ret = OGG.ogg_stream_packetout(to, op))) != 0)
          {
            if (ret < 0)
            {
              throw new ResourceUnavailableException("Error parsing Theora stream headers; corrupt stream?");
            }
            if (THEORA.theora_decode_header(ti, tc, op) != 0)
            {
              throw new ResourceUnavailableException("Error parsing Theora stream headers; corrupt stream?");
            }
            theora_p++;
            if (theora_p == 3)
              break;
          }

          /* look for more vorbis header packets */
          while (vorbis_p != 0 && (vorbis_p < 3) && ((ret = OGG.ogg_stream_packetout(vo, op))) != 0)
          {
            if (ret < 0)
            {
              throw new ResourceUnavailableException("Error parsing Vorbis stream headers; corrupt stream?");
            }

            if (VORBIS.vorbis_synthesis_headerin(vi, vc, op) != 0)
            {
              throw new ResourceUnavailableException("Error parsing Vorbis stream headers; corrupt stream?");
            }
            vorbis_p++;
            if (vorbis_p == 3)
              break;
          }

          /*
           * The header pages/packets will arrive before anything else
           * we care about, or the stream is not obeying spec
           */

          if (OGG.ogg_sync_pageout(oy, og) > 0)
          {
            queue_page(og); /* demux into the appropriate stream */
          } else
          {
            final int ret2 = buffer_data(oy); /* someone needs more data  */
            if (ret2 <= 0)
            {
              throw new ResourceUnavailableException("End of file while searching for codec headers.");
            }
          }
        }

        /* and now we have it all. initialize decoders */
        if (theora_p != 0)
        {
          THEORA.theora_decode_init(td, ti);
          final double fps = (double) ti.fps_numerator / (double) ti.fps_denominator;
          logger.info("Ogg logical stream " + Integer.toHexString( to.serialno.intValue()) + " is Theora " + ti.width + "x" + ti.height + " " + fps + " fps");
          switch (ti.pixelformat)
          {
          case TheoraLibrary.OC_PF_420:
            logger.info(" 4:2:0 video");
            break;
          case TheoraLibrary.OC_PF_422:
            logger.info(" 4:2:2 video");
            break;
          case TheoraLibrary.OC_PF_444:
            logger.info(" 4:4:4 video");
            break;
          case TheoraLibrary.OC_PF_RSVD:
          default:
            logger.info(" video\n  (UNKNOWN Chroma sampling!)");
            break;
          }
          if (ti.width != ti.frame_width || ti.height != ti.frame_height)
          {  logger.warning("  Frame content is " + ti.frame_width + "x" + ti.frame_height + " with offset (" +  ti.offset_x + "," + ti.offset_y + ").");
            // TODO: we need to handle cropping properly.
          }
          report_colorspace(ti);
          dump_comments(tc);
        } else
        {
          /* tear down the partial theora setup */
          THEORA.theora_info_clear(ti);
          THEORA.theora_comment_clear(tc);
        }
        if (vorbis_p != 0)
        {
          VORBIS.vorbis_synthesis_init(vd, vi);
          VORBIS.vorbis_block_init(vd, vb);
          logger.info("Ogg logical stream " + Integer.toHexString(vo.serialno.intValue()) + " is Vorbis " + vi.channels + " channel " + vi.rate.intValue() + " Hz audio.");
        } else
        {
          /* tear down the partial vorbis setup */
          VORBIS.vorbis_info_clear(vi);
          VORBIS.vorbis_comment_clear(vc);
        }

        stateflag = 0; /* playback has not begun */

        VideoTrack videoTrack = null;
        AudioTrack audioTrack = null;

        if (theora_p != 0)
        {
          videoTrack = new VideoTrack();
        }
        if (vorbis_p != 0)
        {
          audioTrack = new AudioTrack();
        }

        if (audioTrack == null && videoTrack == null)
          throw new ResourceUnavailableException("No audio or video track found");
        else if (audioTrack != null && videoTrack != null)
          tracks = new PullSourceStreamTrack[] { videoTrack, audioTrack };
        else if (audioTrack != null)
          tracks = new PullSourceStreamTrack[] { audioTrack };
        else
          tracks = new PullSourceStreamTrack[] { videoTrack };

      } catch (IOException e)
      {
        logger.log(Level.WARNING, "" + e, e);
        throw new ResourceUnavailableException("" + e);
      }
    }

    super.open();

View Full Code Here

      {
        outputHeader(getOutputStream());
      } catch (IOException e)
      {
        logger.log(Level.WARNING, "" + e, e);
        throw new ResourceUnavailableException("" + e);
      }
      headerWritten = true;
    }
  }
View Full Code Here

      {
        outputHeader(getOutputStream());
      } catch (IOException e)
      {
        logger.log(Level.WARNING, "" + e, e);
        throw new ResourceUnavailableException("" + e);
      }
      headerWritten = true;
    }
  }
View Full Code Here

      {
        outputHeader(getOutputStream());
      } catch (IOException e)
      {
        logger.log(Level.WARNING, "" + e, e);
        throw new ResourceUnavailableException("" + e);
      }
      headerWritten = true;
    }
  }
View Full Code Here

      }
   
    } catch (IOException e)
    {
      logger.log(Level.WARNING, "" + e, e);
      throw new ResourceUnavailableException("" + e);
    }

    super.open();

  }
View Full Code Here

     
      // read first frame to determine format
      final Buffer buffer = new Buffer();
      readFrame(buffer);
      if (buffer.isDiscard() || buffer.isEOM())
        throw new ResourceUnavailableException("Unable to read first frame");
      // TODO: catch runtime exception too?
     
      // parse jpeg
      final java.awt.Image image;
      try
      {
        image = ImageIO.read(new ByteArrayInputStream((byte []) buffer.getData(), buffer.getOffset(), buffer.getLength()));
      } catch (IOException e)
      {
        logger.log(Level.WARNING, "" + e, e);
        throw new ResourceUnavailableException("Error reading image: " + e);
      }
     
      if (image == null)
      {
        logger.log(Level.WARNING, "Failed to read image (ImageIO.read returned null).");
        throw new ResourceUnavailableException();
      }
     
      if (frameContentType.equals("image/jpeg"))
        format = new JPEGFormat(new Dimension(image.getWidth(null), image.getHeight(null)), Format.NOT_SPECIFIED, Format.byteArray, -1.f, Format.NOT_SPECIFIED, Format.NOT_SPECIFIED);
      else if(frameContentType.equals("image/gif"))
        format = new GIFFormat(new Dimension(image.getWidth(null), image.getHeight(null)), Format.NOT_SPECIFIED, Format.byteArray, -1.f);
      else if(frameContentType.equals("image/png"))
        format = new PNGFormat(new Dimension(image.getWidth(null), image.getHeight(null)), Format.NOT_SPECIFIED, Format.byteArray, -1.f);
      else
        throw new ResourceUnavailableException("Unsupported frame content type: " + frameContentType);
      // TODO: this discards first image.  save and return first time readFrame is called.
       
    }
View Full Code Here

TOP

Related Classes of javax.media.ResourceUnavailableException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.