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.core;
021
022import com.avpkit.core.IAudioSamples;
023import com.avpkit.core.ICodec;
024import com.avpkit.core.IContainerFormat;
025import com.avpkit.core.IPixelFormat;
026import com.avpkit.core.IRational;
027import com.avpkit.core.ITimeValue;
028import com.avpkit.core.IAudioSamples.Format;
029
030/**
031 * An implementation of {@link ISimpleMediaFile}.
032 * 
033 * Provides a generic bag of attributes about a SimpleMediaFile.
034 */
035public class SimpleMediaFile implements ISimpleMediaFile
036{
037  private String mURL = null;
038  private boolean mHasAudio = true;
039  private int mAudioBitRate = 64000;
040  private boolean mAudioBitRateKnown = false;
041  private int mAudioChannels = 1;
042  private boolean mAudioChannelsKnown = false;
043  private int mAudioSampleRate = 44100;
044  private boolean mAudioSampleRateKnown = false;
045  private IRational mAudioTimeBase = null;
046  private ICodec.ID mAudioCodec = ICodec.ID.AV_CODEC_ID_NONE;
047  private IAudioSamples.Format mAudioSamplesFormat = IAudioSamples.Format.FMT_S16;
048  
049  private boolean mHasVideo = true;
050  private ICodec.ID mVideoCodec = ICodec.ID.AV_CODEC_ID_NONE;
051  private int mVideoHeight = 1;
052  private boolean mVideoHeightKnown = false;
053  
054  private int mVideoWidth = 1;
055  private boolean mVideoWidthKnown = false;
056  
057  private int mVideoBitRate = 320000;
058  private boolean mVideoBitRateKnown = false;
059  private IRational mVideoTimeBase = null;
060  private IRational mVideoFrameRate = null;
061  private int mVideoGOPS = 15;
062  private boolean mVideoGOPSKnown=false;
063  private int mVideoGlobalQuality = 0;
064  private boolean mVideoGlobalQualityKnown=false;
065  private IPixelFormat.Type mPixelFormat = IPixelFormat.Type.YUV420P;
066  private boolean mVideoPixelFormatKnown = false;
067  private IContainerFormat mContainerFormat = null;
068  private ITimeValue mDuration = null;
069
070
071  /**
072   * Create a new {@link SimpleMediaFile}.
073   */
074  public SimpleMediaFile()
075  {
076  }
077  /**
078   * Create a new {@link SimpleMediaFile} from an existing {@link SimpleMediaFile}.
079   * 
080   * All values are shallow copied.
081   * 
082   * @param src The value to copy.
083   * 
084   * @throws IllegalArgumentException if src is null
085   */
086  public SimpleMediaFile(ISimpleMediaFile src)
087  {
088    if (src == null)
089      throw new IllegalArgumentException("cannot pass null src");
090    setHasAudio(src.hasAudio());
091    setAudioBitRate(src.getAudioBitRate());
092    setAudioChannels(src.getAudioChannels());
093    setAudioSampleRate(src.getAudioSampleRate());
094    setAudioSampleFormat(src.getAudioSampleFormat());
095    setHasVideo(src.hasVideo());
096    setAudioCodec(src.getAudioCodec());
097    setVideoCodec(src.getVideoCodec());
098    setVideoHeight(src.getVideoHeight());
099    setVideoWidth(src.getVideoWidth());
100    IRational srcRational = src.getVideoTimeBase();
101    setVideoTimeBase(srcRational);
102    srcRational = src.getVideoFrameRate();
103    setVideoFrameRate(srcRational);
104    srcRational = src.getAudioTimeBase();
105    setAudioTimeBase(srcRational);
106    setVideoNumPicturesInGroupOfPictures(src.getVideoNumPicturesInGroupOfPictures());
107    setVideoGlobalQuality(src.getVideoGlobalQuality());
108    setVideoPixelFormat(src.getVideoPixelFormat());
109    setContainerFormat(src.getContainerFormat());
110    setDuration(src.getDuration());
111    
112    setURL(src.getURL());
113    
114    // set the "known" flags last.
115    setAudioBitRateKnown(src.isAudioBitRateKnown());
116    setAudioChannelsKnown(src.isAudioChannelsKnown());
117    setAudioSampleRateKnown(src.isAudioSampleRateKnown());
118    
119    setVideoHeightKnown(src.isVideoHeightKnown());
120    setVideoWidthKnown(src.isVideoWidthKnown());
121    setVideoNumPicturesInGroupOfPicturesKnown(src.isVideoNumPicturesInGroupOfPicturesKnown());
122    setVideoPixelFormatKnown(src.isVideoPixelFormatKnown());
123    setVideoGlobalQualityKnown(src.isVideoGlobalQualityKnown());
124    
125  }
126  
127  /**
128   * {@inheritDoc}
129   */
130  public int getAudioBitRate()
131  {
132    return mAudioBitRate;
133  }
134
135  /**
136   * {@inheritDoc}
137   */
138  public int getAudioChannels()
139  {
140    return mAudioChannels;
141  }
142
143  /**
144   * {@inheritDoc}
145   */
146  public int getAudioSampleRate()
147  {
148    return mAudioSampleRate;
149  }
150
151  /**
152   * {@inheritDoc}
153   */
154  public void setAudioBitRate(int bitRate)
155  {
156    mAudioBitRate = bitRate;
157    setAudioBitRateKnown(true);
158  }
159
160  /**
161   * {@inheritDoc}
162   */
163  public void setAudioChannels(int numChannels)
164  {
165    if (numChannels < 1 || numChannels > 2)
166      throw new IllegalArgumentException("only supports 1 or 2 channels");
167    
168    mAudioChannels = numChannels;
169    setAudioChannelsKnown(true);
170  }
171
172  /**
173   * {@inheritDoc}
174   */
175  public void setAudioSampleRate(int sampleRate)
176  {
177    mAudioSampleRate = sampleRate;
178    setAudioSampleRateKnown(true);
179  }
180
181  /**
182   * {@inheritDoc}
183   */
184  public ICodec.ID getAudioCodec()
185  {
186    return mAudioCodec;
187  }
188
189  /**
190   * {@inheritDoc}
191   */
192  public void setAudioCodec(ICodec.ID audioCodec)
193  {
194    mAudioCodec = audioCodec;
195  }
196  /**
197   * {@inheritDoc}
198   */
199  public ICodec.ID getVideoCodec()
200  {
201    return mVideoCodec;
202  }
203  /**
204   * {@inheritDoc}
205   */
206  public int getVideoHeight()
207  {
208    return mVideoHeight;
209  }
210  /**
211   * {@inheritDoc}
212   */
213  public int getVideoWidth()
214  {
215    return mVideoWidth;
216  }
217  /**
218   * {@inheritDoc}
219   */
220  public int getVideoBitRate()
221  {
222    return mVideoBitRate;
223  }
224  /**
225   * {@inheritDoc}
226   */
227  public void setVideoCodec(ICodec.ID videoCodec)
228  {
229    mVideoCodec = videoCodec;
230  }
231  /**
232   * {@inheritDoc}
233   */
234  public void setVideoHeight(int height)
235  {
236    mVideoHeight = height;
237    setVideoHeightKnown(true);
238  }
239  /**
240   * {@inheritDoc}
241   */
242  public void setVideoWidth(int width)
243  {
244    mVideoWidth = width;
245    setVideoWidthKnown(true);
246  }
247  /**
248   * {@inheritDoc}
249   */
250  public IRational getVideoTimeBase()
251  {
252    return mVideoTimeBase ;
253  }
254  /**
255   * {@inheritDoc}
256   */
257  public void setVideoTimeBase(IRational timeBase)
258  {
259    mVideoTimeBase = timeBase;
260  }
261  /**
262   * {@inheritDoc}
263   */
264  public void setVideoBitRate(int bitRate)
265  {
266    mVideoBitRate = bitRate;
267    setVideoBitRateKnown(true);
268  }
269  /**
270   * {@inheritDoc}
271   */
272  public IPixelFormat.Type getVideoPixelFormat()
273  {
274    return mPixelFormat;
275  }
276  /**
277   * {@inheritDoc}
278   */
279  public void setVideoPixelFormat(IPixelFormat.Type pixelFormat)
280  {
281    mPixelFormat = pixelFormat;
282    setVideoPixelFormatKnown(true);
283  }
284  /**
285   * {@inheritDoc}
286   */
287  public IRational getVideoFrameRate()
288  {
289    return mVideoFrameRate;
290  }
291
292  /**
293   * {@inheritDoc}
294   */
295  public int getVideoGlobalQuality()
296  {
297    return mVideoGlobalQuality;
298  }
299
300  /**
301   * {@inheritDoc}
302   */
303  public int getVideoNumPicturesInGroupOfPictures()
304  {
305    return mVideoGOPS;
306  }
307
308  /**
309   * {@inheritDoc}
310   */
311  public void setVideoFrameRate(IRational frameRate)
312  {
313    mVideoFrameRate = frameRate;
314  }
315
316  /**
317   * {@inheritDoc}
318   */
319  public void setVideoGlobalQuality(int quality)
320  {
321    mVideoGlobalQuality = quality;
322    setVideoGlobalQualityKnown(true);
323  }
324
325  /**
326   * {@inheritDoc}
327   */
328  public void setVideoNumPicturesInGroupOfPictures(int gops)
329  {
330    mVideoGOPS = gops;
331    setVideoNumPicturesInGroupOfPicturesKnown(true);
332  }
333
334  /**
335   * {@inheritDoc}
336   */
337  public boolean hasAudio()
338  {
339    return mHasAudio;
340  }
341
342  /**
343   * {@inheritDoc}
344   */
345  public boolean hasVideo()
346  {
347    return mHasVideo;
348  }
349
350  /**
351   * {@inheritDoc}
352   */
353  public void setHasAudio(boolean hasAudio)
354  {
355    mHasAudio = hasAudio;
356  }
357
358  /**
359   * {@inheritDoc}
360   */
361  public void setHasVideo(boolean hasVideo)
362  {
363    mHasVideo = hasVideo;    
364  }
365  /**
366   * {@inheritDoc}
367   */
368  public IContainerFormat getContainerFormat()
369  {
370    return mContainerFormat;
371  }
372  /**
373   * {@inheritDoc}
374   */
375  public void setContainerFormat(IContainerFormat aFormat)
376  {
377    mContainerFormat = aFormat;
378  }
379  /**
380   * {@inheritDoc}
381   */
382  public IRational getAudioTimeBase()
383  {
384    return mAudioTimeBase;
385  }
386  /**
387   * {@inheritDoc}
388   */
389  public void setAudioTimeBase(IRational aTimeBase)
390  {
391    mAudioTimeBase = aTimeBase;
392  }
393  /**
394   * {@inheritDoc}
395   */
396  public void setAudioBitRateKnown(boolean audioBitRateKnown)
397  {
398    mAudioBitRateKnown = audioBitRateKnown;
399  }
400
401  /**
402   * {@inheritDoc}
403   */
404  public boolean isAudioBitRateKnown()
405  {
406    return mAudioBitRateKnown;
407  }
408  /**
409   * {@inheritDoc}
410   */
411  public void setAudioChannelsKnown(boolean audioChannelsKnown)
412  {
413    mAudioChannelsKnown = audioChannelsKnown;
414  }
415  /**
416   * {@inheritDoc}
417   */
418  public boolean isAudioChannelsKnown()
419  {
420    return mAudioChannelsKnown;
421  }
422  /**
423   * {@inheritDoc}
424   */
425  public void setAudioSampleRateKnown(boolean audioSampleRateKnown)
426  {
427    mAudioSampleRateKnown = audioSampleRateKnown;
428  }
429  /**
430   * {@inheritDoc}
431   */
432  public boolean isAudioSampleRateKnown()
433  {
434    return mAudioSampleRateKnown;
435  }
436  /**
437   * {@inheritDoc}
438   */
439  public void setVideoHeightKnown(boolean videoHeightKnown)
440  {
441    mVideoHeightKnown = videoHeightKnown;
442  }
443  /**
444   * {@inheritDoc}
445   */
446  public boolean isVideoHeightKnown()
447  {
448    return mVideoHeightKnown;
449  }
450  /**
451   * {@inheritDoc}
452   */
453  public void setVideoWidthKnown(boolean videoWidthKnown)
454  {
455    mVideoWidthKnown = videoWidthKnown;
456  }
457  /**
458   * {@inheritDoc}
459   */
460  public boolean isVideoWidthKnown()
461  {
462    return mVideoWidthKnown;
463  }
464  /**
465   * {@inheritDoc}
466   */
467  public void setVideoBitRateKnown(boolean videoBitRateKnown)
468  {
469    mVideoBitRateKnown = videoBitRateKnown;
470  }
471  /**
472   * {@inheritDoc}
473   */
474  public boolean isVideoBitRateKnown()
475  {
476    return mVideoBitRateKnown;
477  }
478  /**
479   * {@inheritDoc}
480   */
481  public void setVideoNumPicturesInGroupOfPicturesKnown(boolean videoGOPSKnown)
482  {
483    mVideoGOPSKnown = videoGOPSKnown;
484  }
485  /**
486   * {@inheritDoc}
487   */
488  public boolean isVideoNumPicturesInGroupOfPicturesKnown()
489  {
490    return mVideoGOPSKnown;
491  }
492  /**
493   * {@inheritDoc}
494   */
495  public void setVideoGlobalQualityKnown(boolean videoGlobalQualityKnown)
496  {
497    mVideoGlobalQualityKnown = videoGlobalQualityKnown;
498  }
499  /**
500   * {@inheritDoc}
501   */
502  public boolean isVideoGlobalQualityKnown()
503  {
504    return mVideoGlobalQualityKnown;
505  }
506  /**
507   * {@inheritDoc}
508   */
509  public void setVideoPixelFormatKnown(boolean videoPixelFormatKnown)
510  {
511    mVideoPixelFormatKnown = videoPixelFormatKnown;
512  }
513  /**
514   * {@inheritDoc}
515   */
516  public boolean isVideoPixelFormatKnown()
517  {
518    return mVideoPixelFormatKnown;
519  }
520
521  /**
522   * {@inheritDoc}
523   */
524  public void setDuration(ITimeValue duration)
525  {
526    mDuration = duration;
527  }
528
529  /**
530   * {@inheritDoc}
531   */
532  public ITimeValue getDuration()
533  {
534    return mDuration;
535  }
536  
537  /**
538   * {@inheritDoc}
539   */
540  public void setURL(String uRL)
541  {
542    mURL = uRL;
543  }
544  
545  /**
546   * {@inheritDoc}
547   */
548  public String getURL()
549  {
550    return mURL;
551  }
552  
553  /**
554   * {@inheritDoc}
555   */
556  public Format getAudioSampleFormat()
557  {
558    return mAudioSamplesFormat;
559  }
560  
561  /**
562   * {@inheritDoc}
563   */
564  public void setAudioSampleFormat(Format aFormat)
565  {
566    if (aFormat == null)
567      throw new IllegalArgumentException("cannot set to null format");
568    mAudioSamplesFormat = aFormat;
569  }
570
571}