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;
021
022
023
024import com.avpkit.core.IContainer;
025
026/**
027 * An abstract implementation of all
028 * {@link IMediaCoder} methods, but does not declare {@link IMediaCoder}.
029 * 
030 * <p>
031 * 
032 * Mixin classes can be extended by anyone, but the extending class
033 * gets to decide which, if any, of the interfaces they actually
034 * want to support.
035 * 
036 * </p>
037 * 
038 * @author trebor
039 * @author aclarke
040 */
041
042public abstract class AMediaCoderMixin extends AMediaToolMixin
043{
044  // the container to read from or write to
045  
046  private final IContainer mContainer;
047
048  // true if this media writer should close the container
049
050  private boolean mCloseContainer;
051
052  // the URL which is read or written
053
054  private final String mUrl;
055
056  // all the media reader listeners
057
058  /**
059   * Construct an {@link AMediaCoderMixin}.
060   *
061   * @param url the URL which will be read or written to
062   * @param container the container which be read from or written to
063   */
064  
065  public AMediaCoderMixin(String url, IContainer container)
066  {
067    mUrl = url;
068    mContainer = container.copyReference();
069
070    // it is assuemd that the container should not be closed by the
071    // tool, this may change if open() is laster called 
072
073    setShouldCloseContainer(false);
074  }
075
076  /**
077   * The URL from which the {@link IContainer} is being read or written to.
078   * 
079   * @return the source or destination URL.
080   */
081
082  public String getUrl()
083  {
084    return mUrl;
085  }
086
087  /** 
088   * Get the underlying media {@link IContainer} that the {@link IMediaCoder} is
089   * reading from or writing to.  The returned {@link IContainer} can
090   * be further interrogated for media stream details.
091   *
092   * @return the media container.
093   */
094
095  public IContainer getContainer()
096  {
097    return mContainer == null ? null : mContainer.copyReference();
098  }
099
100  /**
101   * Test if this {@link IMediaCoder} is open.
102   * 
103   * @return true if the media tool is open.
104   */
105
106  public boolean isOpen()
107  {
108    return mContainer.isOpened();
109  }
110
111  /**
112   * Should this {@link IMediaCoder} call {@link IContainer#close()}
113   * when {@link IMediaCoder#close()} is called.
114   * @param value should we close the container
115   */
116  public void setShouldCloseContainer(boolean value)
117  {
118    mCloseContainer = value;
119  }
120
121  /**
122   * Should this {@link IMediaCoder} call {@link IContainer#close()}
123   * when {@link IMediaCoder#close()} is called.
124   * 
125   * @return should we close the container
126   */
127  public boolean getShouldCloseContainer()
128  {
129    return mCloseContainer;
130  }
131}