content: /*******************************************************************************
   * Software Name : RCS IMS Stack
   *
   * Copyright (C) 2010-2016 Orange.
   * Copyright (C) 2015 Sony Mobile Communications Inc.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   *
   * NOTE: This file has been modified by Sony Mobile Communications Inc.
   * Modifications are licensed under the License.
   ******************************************************************************/
  
  package com.gsma.rcs.core.ims.protocol.rtp.stream;
  
  import com.gsma.rcs.core.ims.network.NetworkException;
  import com.gsma.rcs.core.ims.protocol.rtp.format.Format;
  import com.gsma.rcs.core.ims.protocol.rtp.media.MediaException;
  import com.gsma.rcs.core.ims.protocol.rtp.media.MediaInput;
  import com.gsma.rcs.core.ims.protocol.rtp.media.MediaSample;
  import com.gsma.rcs.core.ims.protocol.rtp.util.Buffer;
  import com.gsma.rcs.utils.logger.Logger;
  
  /**
   * Media capture stream
   * 
   * @author jexa7410
   */
  public class MediaCaptureStream implements ProcessorInputStream {
      /**
       * Media player
       */
      private MediaInput player;
  
      /**
       * Media format
       */
      private Format format;
  
      /**
       * Sequence number
       */
      protected long seqNo = 0;
  
      /**
       * Input buffer
       */
      protected Buffer buffer = new Buffer();
  
      /**
       * The logger
       */
      private Logger mLogger = Logger.getLogger(this.getClass().getName());
  
      /**
       * Constructor
       * 
       * @param format Input format
       * @param player Media player
       */
      public MediaCaptureStream(Format format, MediaInput player) {
          this.format = format;
          this.player = player;
      }
  
      /**
       * Open the input stream
       * 
       * @throws MediaException
       */
      public void open() throws MediaException {
          player.open();
          if (mLogger.isActivated()) {
              mLogger.debug("Media capture stream opened");
          }
      }
  
      /**
       * Close the input stream
       */
      public void close() {
          player.close();
          if (mLogger.isActivated()) {
              mLogger.debug("Media capture stream closed");
          }
      }
  
      /**
       * Format of the data provided by the source stream
       * 
       * @return Format
       */
      public Format getFormat() {
          return format;
      }
  
      /**
       * returns the MediaInput
       * 
       * @return MediaInput
       */
      public MediaInput getPlayer() {
          return player;
      }
  
      /**
       * Read from the stream
       * 
       * @return Buffer
       * @throws NetworkException
       */
      public Buffer read() throws NetworkException {
          // Read a new sample from the media player
          MediaSample sample = player.readSample();
          if (sample == null) {
              return null;
          }
  
          // Create a buffer
          buffer.setData(sample.getData());
          buffer.setLength(sample.getLength());
          buffer.setFormat(format);
          buffer.setSequenceNumber(seqNo++);
          if (sample.isMarker()) {
              buffer.setFlags(Buffer.FLAG_RTP_MARKER);
          }
          buffer.setTimestamp(sample.getTimestamp());
          return buffer;
      }
  }
  