001/*******************************************************************************
002 * Copyright (c) 2024, 2026, Olivier Ayache.  All rights reserved.
003 *
004 * This file is part of AVPKit.
005 *
006 * AVPKit is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU Lesser General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * AVPKit is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with AVPKit.  If not, see <http://www.gnu.org/licenses/>.
018 *******************************************************************************/
019
020package com.avpkit.mediatool.event;
021
022import java.util.concurrent.TimeUnit;
023
024import com.avpkit.mediatool.IMediaGenerator;
025import com.avpkit.core.Global;
026import com.avpkit.core.IMediaData;
027
028/**
029 * An abstract implementation of {@link IRawMediaEvent}, but does not declare
030 * {@link IRawMediaEvent}.
031 * 
032 * @author aclarke
033 * 
034 */
035public abstract class ARawMediaMixin extends AStreamMixin
036{
037  private final IMediaData mMediaData;
038  private final Object mJavaData;
039  private final long mTimeStamp;
040  private final TimeUnit mTimeUnit;
041
042  /**
043   * Create an {@link ARawMediaMixin}.
044   * 
045   * @param source the source
046   * @param picture a picture
047   * @param image an image
048   * @param timeStamp a time stamp value. If image is null this value is
049   *        ignored.
050   * @param timeUnit a time unit for timeStamp. If image is null this value is
051   *        ignored.
052   * @param streamIndex the stream index this media is associated with
053   * or null if none.
054   * @throws IllegalArgumentException if both picture and image are null.
055   */
056  public ARawMediaMixin(IMediaGenerator source, IMediaData picture,
057      Object image, long timeStamp, TimeUnit timeUnit, Integer streamIndex)
058  {
059    super(source, streamIndex);
060    if (image == null && picture == null)
061      throw new IllegalArgumentException();
062    mMediaData = picture;
063    mJavaData = image;
064    if (image == null)
065    {
066      timeStamp = picture.getTimeStamp();
067      timeUnit = TimeUnit.MICROSECONDS;
068    }
069    mTimeStamp = timeStamp;
070    if (timeUnit == null)
071      throw new IllegalArgumentException();
072    mTimeUnit = timeUnit;
073  }
074
075  /**
076   * Implementation of {@link IRawMediaEvent#getMediaData()}.
077   * @see com.avpkit.mediatool.event.IRawMediaEvent#getMediaData()
078   */
079  public IMediaData getMediaData()
080  {
081    return mMediaData;
082  }
083
084  /**
085   * Implementation of {@link IRawMediaEvent#getJavaData()}.
086   * @see com.avpkit.mediatool.event.IRawMediaEvent#getJavaData()
087   */
088  public Object getJavaData()
089  {
090    return mJavaData;
091  }
092
093  /**
094   * Implementation of {@link IRawMediaEvent#getTimeStamp()}.
095   * @see com.avpkit.mediatool.event.IRawMediaEvent#getTimeStamp()
096   */
097  public Long getTimeStamp()
098  {
099    return getTimeStamp(TimeUnit.MICROSECONDS);
100  }
101
102  /**
103   * Implementation of {@link IRawMediaEvent#getTimeStamp(TimeUnit)}.
104   * @see
105   * com.avpkit.mediatool.event.IRawMediaEvent#getTimeStamp(java.util.concurrent.TimeUnit)
106   */
107  public Long getTimeStamp(TimeUnit unit)
108  {
109    if (unit == null)
110      throw new IllegalArgumentException();
111    if (mTimeStamp == Global.NO_PTS)
112      return null;
113    return unit.convert(mTimeStamp, mTimeUnit);
114  }
115
116  /**
117   * Implementation of {@link IRawMediaEvent#getTimeUnit()}.
118   * @see com.avpkit.mediatool.event.IRawMediaEvent#getTimeUnit()
119   */
120  public TimeUnit getTimeUnit()
121  {
122    return mTimeUnit;
123  }
124
125}