1 /*****************************************************************
2 |
3 |    AP4 - Track Objects
4 |
5 |    Copyright 2002-2008 Axiomatic Systems, LLC
6 |
7 |
8 |    This file is part of Bento4/AP4 (MP4 Atom Processing Library).
9 |
10 |    Unless you have obtained Bento4 under a difference license,
11 |    this version of Bento4 is Bento4|GPL.
12 |    Bento4|GPL is free software; you can redistribute it and/or modify
13 |    it under the terms of the GNU General Public License as published by
14 |    the Free Software Foundation; either version 2, or (at your option)
15 |    any later version.
16 |
17 |    Bento4|GPL is distributed in the hope that it will be useful,
18 |    but WITHOUT ANY WARRANTY; without even the implied warranty of
19 |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 |    GNU General Public License for more details.
21 |
22 |    You should have received a copy of the GNU General Public License
23 |    along with Bento4|GPL; see the file COPYING.  If not, write to the
24 |    Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 |    02111-1307, USA.
26 |
27  ****************************************************************/
28 
29 #ifndef _AP4_TRAK_H_
30 #define _AP4_TRAK_H_
31 
32 /*----------------------------------------------------------------------
33 |   includes
34 +---------------------------------------------------------------------*/
35 #include "Ap4Types.h"
36 #include "Ap4Array.h"
37 
38 /*----------------------------------------------------------------------
39 |   forward declarations
40 +---------------------------------------------------------------------*/
41 class AP4_StblAtom;
42 class AP4_ByteStream;
43 class AP4_Sample;
44 class AP4_DataBuffer;
45 class AP4_TrakAtom;
46 class AP4_MoovAtom;
47 class AP4_SampleDescription;
48 class AP4_SampleTable;
49 
50 /*----------------------------------------------------------------------
51 |   constants
52 +---------------------------------------------------------------------*/
53 const AP4_UI32 AP4_TRACK_DEFAULT_MOVIE_TIMESCALE = 1000;
54 
55 const AP4_UI32 AP4_TRACK_FLAG_ENABLED    = 0x0001;
56 const AP4_UI32 AP4_TRACK_FLAG_IN_MOVIE   = 0x0002;
57 const AP4_UI32 AP4_TRACK_FLAG_IN_PREVIEW = 0x0004;
58 
59 /*----------------------------------------------------------------------
60 |   AP4_Track
61 +---------------------------------------------------------------------*/
62 class AP4_Track {
63  public:
64     // types
65     typedef enum {
66         TYPE_UNKNOWN   = 0,
67         TYPE_AUDIO     = 1,
68         TYPE_VIDEO     = 2,
69         TYPE_SYSTEM    = 3,
70         TYPE_HINT      = 4,
71         TYPE_TEXT      = 5,
72         TYPE_JPEG      = 6,
73         TYPE_RTP       = 7,
74         TYPE_SUBTITLES = 8
75     } Type;
76 
77     // methods
78     AP4_Track(Type             type,
79               AP4_SampleTable* sample_table,     // ownership is transfered to the AP4_Track object
80               AP4_UI32         track_id,
81               AP4_UI32         movie_time_scale, // 0 = use default
82               AP4_UI64         track_duration,   // in the movie timescale
83               AP4_UI32         media_time_scale,
84               AP4_UI64         media_duration,   // in the media timescale
85               const char*      language,
86               AP4_UI32         width,            // in 16.16 fixed point
87               AP4_UI32         height,           // in 16.16 fixed point
88               AP4_UI64         creation_time = 0,
89               AP4_UI64         modification_time = 0);
90     AP4_Track(AP4_SampleTable* sample_table,     // ownership is transferred to the AP4_Track object
91               AP4_UI32         track_id,
92               AP4_UI32         movie_time_scale, // 0 = use default
93               AP4_UI64         track_duration,   // in the movie timescale
94               AP4_UI32         media_time_scale,
95               AP4_UI64         media_duration,   // in the media timescale
96               const AP4_Track* track_prototype); // prototype for the type and other track fields
97     AP4_Track(AP4_TrakAtom&   atom,
98               AP4_ByteStream& sample_stream,
99               AP4_UI32        movie_time_scale);
100     virtual ~AP4_Track();
101 
102     /**
103      * Clone a track. This is useful if you want to create a track from
104      * a non-synthetic track (parsed from a file for example) and
105      * write it out
106      */
107     AP4_Track* Clone(AP4_Result* result = NULL);
108 
109     AP4_UI32     GetFlags() const;
110     AP4_Result   SetFlags(AP4_UI32 flags);
GetType()111     AP4_Track::Type GetType() const { return m_Type; }
112     AP4_UI32     GetHandlerType() const;
113     AP4_UI64     GetDuration() const;   // in the timescale of the movie
114     AP4_UI32     GetDurationMs() const; // in milliseconds
115     AP4_UI32     GetWidth() const;      // in 16.16 fixed point
116     AP4_UI32     GetHeight() const;     // in 16.16 fixed point
117     AP4_Cardinal GetSampleCount() const;
118     AP4_Result   GetSample(AP4_Ordinal index, AP4_Sample& sample);
119     AP4_Result   ReadSample(AP4_Ordinal     index,
120                             AP4_Sample&     sample,
121                             AP4_DataBuffer& data);
122     AP4_Result   GetSampleIndexForTimeStampMs(AP4_UI32     ts_ms,
123                                               AP4_Ordinal& index);
124     AP4_Ordinal  GetNearestSyncSampleIndex(AP4_Ordinal index, bool before=true);
125     AP4_SampleDescription* GetSampleDescription(AP4_Ordinal index);
126     AP4_Cardinal           GetSampleDescriptionCount();
GetSampleTable()127     AP4_SampleTable*       GetSampleTable() { return m_SampleTable; }
128     AP4_UI32      GetId() const;
129     AP4_Result    SetId(AP4_UI32 track_id);
GetTrakAtom()130     const AP4_TrakAtom* GetTrakAtom() const { return m_TrakAtom; }
UseTrakAtom()131     AP4_TrakAtom* UseTrakAtom()             { return m_TrakAtom; }
132     AP4_Result    SetMovieTimeScale(AP4_UI32 time_scale);
GetMovieTimeScale()133     AP4_UI32      GetMovieTimeScale() const { return m_MovieTimeScale; }
134     AP4_UI32      GetMediaTimeScale() const;
135     AP4_UI64      GetMediaDuration() const; // in the timescale of the media
136     const char*   GetTrackName() const;
137     const char*   GetTrackLanguage() const;
138     AP4_Result    SetTrackLanguage(const char* language);
139     AP4_Result    Attach(AP4_MoovAtom* moov);
140 
141  protected:
142     // members
143     AP4_TrakAtom*    m_TrakAtom;
144     bool             m_TrakAtomIsOwned;
145     Type             m_Type;
146     AP4_SampleTable* m_SampleTable;
147     bool             m_SampleTableIsOwned;
148     AP4_UI32         m_MovieTimeScale;
149 };
150 
151 #endif // _AP4_TRAK_H_
152