File: Ap4HintTrackReader.cpp

package info (click to toggle)
kodi-inputstream-adaptive 2.6.14%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,036 kB
  • sloc: cpp: 53,019; ansic: 492; makefile: 10
file content (342 lines) | stat: -rw-r--r-- 12,191 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*****************************************************************
|
|    AP4 - Hint Track Reader
|
|    Copyright 2002-2008 Axiomatic Systems, LLC
|
|
|    This file is part of Bento4/AP4 (MP4 Atom Processing Library).
|
|    Unless you have obtained Bento4 under a difference license,
|    this version of Bento4 is Bento4|GPL.
|    Bento4|GPL is free software; you can redistribute it and/or modify
|    it under the terms of the GNU General Public License as published by
|    the Free Software Foundation; either version 2, or (at your option)
|    any later version.
|
|    Bento4|GPL is distributed in the hope that it will be useful,
|    but WITHOUT ANY WARRANTY; without even the implied warranty of
|    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
|    GNU General Public License for more details.
|
|    You should have received a copy of the GNU General Public License
|    along with Bento4|GPL; see the file COPYING.  If not, write to the
|    Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
|    02111-1307, USA.
|
****************************************************************/

/*----------------------------------------------------------------------
|   includes
+---------------------------------------------------------------------*/
#include <stdlib.h>
#include <time.h>
#include "Ap4HintTrackReader.h"
#include "Ap4DataBuffer.h"
#include "Ap4Track.h"
#include "Ap4Movie.h"
#include "Ap4SdpAtom.h"
#include "Ap4RtpHint.h"
#include "Ap4TrakAtom.h"
#include "Ap4TrefTypeAtom.h"
#include "Ap4TimsAtom.h"
#include "Ap4Utils.h"

/*----------------------------------------------------------------------
|   AP4_HintTrackReader::AP4_HintTrackReader
+---------------------------------------------------------------------*/
AP4_HintTrackReader::AP4_HintTrackReader(AP4_Track& hint_track, 
                                         AP4_Movie& movie,
                                         AP4_UI32   ssrc) :
    m_HintTrack(hint_track),
    m_MediaTrack(NULL),
    m_MediaTimeScale(0),
    m_RtpSampleData(NULL),
    m_Ssrc(ssrc),
    m_SampleIndex(0),
    m_PacketIndex(0),
    m_RtpSequenceStart(0),
    m_RtpTimeStampStart(0),
    m_RtpTimeScale(0)
{
    // get the media track
    AP4_TrakAtom* hint_trak_atom = hint_track.UseTrakAtom();
    AP4_Atom* atom = hint_trak_atom->FindChild("tref/hint");
    if (atom != NULL) {
        AP4_UI32 media_track_id = AP4_DYNAMIC_CAST(AP4_TrefTypeAtom, atom)->GetTrackIds()[0];
        m_MediaTrack = movie.GetTrack(media_track_id);

        // get the media time scale
        m_MediaTimeScale = m_MediaTrack->GetMediaTimeScale();
    }

    // initiate random generator
    srand((int)time(NULL));

    // rtp sequence start init TODO!!
    m_RtpSequenceStart = (AP4_UI16)(rand()&0xFFFF);

    // rtp timestamp start init TODO!!
    m_RtpTimeStampStart = rand();

    // rtp time scale
    atom = hint_trak_atom->FindChild("mdia/minf/stbl/rtp /tims");
    if (atom) {
        AP4_TimsAtom* tims = AP4_DYNAMIC_CAST(AP4_TimsAtom, atom);
        m_RtpTimeScale = tims->GetTimeScale();
    }

    // generate a random ssrc if = 0
    if (m_Ssrc == 0) {
        m_Ssrc = rand();
    }

    // get the first sample
    GetRtpSample(0);
}

/*----------------------------------------------------------------------
|   AP4_HintTrackReader::Create
+---------------------------------------------------------------------*/
AP4_Result
AP4_HintTrackReader::Create(AP4_Track&            hint_track, 
                            AP4_Movie&            movie, 
                            AP4_UI32              ssrc,
                            AP4_HintTrackReader*& reader)
{
    // default value
    reader = NULL;
    
    // check the type
    if (hint_track.GetType() != AP4_Track::TYPE_HINT) {
        return AP4_ERROR_INVALID_TRACK_TYPE;
    }
    
    // create a new object
    reader = new AP4_HintTrackReader(hint_track, movie, ssrc);
    
    return AP4_SUCCESS;
}

/*----------------------------------------------------------------------
|   AP4_HintTrackReader::~AP4_HintTrackReader
+---------------------------------------------------------------------*/
AP4_HintTrackReader::~AP4_HintTrackReader()
{
    delete m_RtpSampleData;
}

/*----------------------------------------------------------------------
|   AP4_HintTrackReader::GetRtpSample
+---------------------------------------------------------------------*/
AP4_Result
AP4_HintTrackReader::GetRtpSample(AP4_Ordinal index)
{
    // get the sample
    AP4_Result result = m_HintTrack.GetSample(index, m_CurrentHintSample);
    if (AP4_FAILED(result)) return result;

    // renew the sample data
    delete m_RtpSampleData;
    AP4_ByteStream& rtp_data_stream = *m_CurrentHintSample.GetDataStream();
    rtp_data_stream.Seek(m_CurrentHintSample.GetOffset());
    m_RtpSampleData = new AP4_RtpSampleData(rtp_data_stream,
                                            m_CurrentHintSample.GetSize());

    // reinit the packet index
    m_PacketIndex = 0;

    // release the stream
    rtp_data_stream.Release();

    return AP4_SUCCESS;
}

/*----------------------------------------------------------------------
|   AP4_HintTrackReader::GetCurrentTimeStampMs
+---------------------------------------------------------------------*/
AP4_UI32
AP4_HintTrackReader::GetCurrentTimeStampMs()
{
    return (AP4_UI32)AP4_ConvertTime(m_CurrentHintSample.GetCts(), 
                                     m_HintTrack.GetMediaTimeScale(),
                                     1000);
}

/*----------------------------------------------------------------------
|   AP4_HintTrackReader::Rewind
+---------------------------------------------------------------------*/
AP4_Result
AP4_HintTrackReader::Rewind()
{
    m_SampleIndex = 0;
    return GetRtpSample(m_SampleIndex);
}

/*----------------------------------------------------------------------
|   AP4_HintTrackReader::GetSdpText
+---------------------------------------------------------------------*/
AP4_Result
AP4_HintTrackReader::GetSdpText(AP4_String& sdp_text)
{
    AP4_Atom* sdp_atom = m_HintTrack.UseTrakAtom()->FindChild("udta/hnti/sdp ");
    if (sdp_atom == NULL) return AP4_FAILURE;

    // C cast is OK because we know the type of the atom
    sdp_text = AP4_DYNAMIC_CAST(AP4_SdpAtom, sdp_atom)->GetSdpText(); 
    return AP4_SUCCESS;
}

/*----------------------------------------------------------------------
|   AP4_HintTrackReader::SeekToTimeStampMs
+---------------------------------------------------------------------*/
AP4_Result
AP4_HintTrackReader::SeekToTimeStampMs(AP4_UI32  desired_ts_ms, 
                                       AP4_UI32& actual_ts_ms)
{
    // get the sample index
    AP4_Cardinal index;
    AP4_Result result = m_HintTrack.GetSampleIndexForTimeStampMs(desired_ts_ms, index);
    if (AP4_FAILED(result)) return result;

    // get the current sample based on the index and renew the sample data
    result = GetRtpSample(index);
    if (AP4_FAILED(result)) return result;

    // set the actual ts
    actual_ts_ms = GetCurrentTimeStampMs();
    return AP4_SUCCESS;
}

/*----------------------------------------------------------------------
|   AP4_HintTrackReader::GetNextPacket
+---------------------------------------------------------------------*/
AP4_Result
AP4_HintTrackReader::GetNextPacket(AP4_DataBuffer& packet_data, 
                                   AP4_UI32&       ts_ms)
{
    AP4_Result result = AP4_SUCCESS;

    // get the next rtp sample if needed
    AP4_List<AP4_RtpPacket>* packets = &m_RtpSampleData->GetPackets();
    while (m_PacketIndex == packets->ItemCount()) { // while: handle the 0 packet case
        result = GetRtpSample(++m_SampleIndex);
        if (AP4_FAILED(result)) return result;
        packets = &m_RtpSampleData->GetPackets();
    }

    // get the packet
    AP4_RtpPacket* packet;
    result = packets->Get(m_PacketIndex++, packet);
    if (AP4_FAILED(result)) return result;

    // build it
    result = BuildRtpPacket(packet, packet_data);
    if (AP4_FAILED(result)) return result;

    // set the time stamp
    ts_ms = GetCurrentTimeStampMs();

    return result;
}

/*----------------------------------------------------------------------
|   AP4_HintTrackReader::BuildRtpPacket
+---------------------------------------------------------------------*/
AP4_Result
AP4_HintTrackReader::BuildRtpPacket(AP4_RtpPacket*  packet, 
                                    AP4_DataBuffer& packet_data)
{
    // set the data size
    AP4_Result result = packet_data.SetDataSize(packet->GetConstructedDataSize());
    if (AP4_FAILED(result)) return result;

    // now write
    AP4_ByteStream* stream = new AP4_MemoryByteStream(packet_data); 

    // header + ssrc
    stream->WriteUI08(0x80 | (packet->GetPBit() << 5) | (packet->GetXBit() << 4));
    stream->WriteUI08((packet->GetMBit() << 7) | packet->GetPayloadType());
    stream->WriteUI16(m_RtpSequenceStart + packet->GetSequenceSeed());
    stream->WriteUI32(m_RtpTimeStampStart + (AP4_UI32)m_CurrentHintSample.GetCts() + packet->GetTimeStampOffset());
    stream->WriteUI32(m_Ssrc);

    AP4_List<AP4_RtpConstructor>::Item* constructors_it 
        = packet->GetConstructors().FirstItem();
    while (constructors_it != NULL) {
        AP4_RtpConstructor* constructor = constructors_it->GetData();

        // add data to the packet according to the constructor
        switch (constructor->GetType()) {
            case AP4_RTP_CONSTRUCTOR_TYPE_NOOP:
                // nothing to do here
                break;
            case AP4_RTP_CONSTRUCTOR_TYPE_IMMEDIATE:
                result = WriteImmediateRtpData(
                    static_cast<AP4_ImmediateRtpConstructor*>(constructor), stream);
                if (AP4_FAILED(result)) return result;
                break;
            case AP4_RTP_CONSTRUCTOR_TYPE_SAMPLE:
                result = WriteSampleRtpData(
                    static_cast<AP4_SampleRtpConstructor*>(constructor), stream);
                if (AP4_FAILED(result)) return result;
                break;
            case AP4_RTP_CONSTRUCTOR_TYPE_SAMPLE_DESC:
                return AP4_ERROR_NOT_SUPPORTED;
            default:
                // unknown constructor type
                return AP4_FAILURE;
        }

        // iterate
        constructors_it = constructors_it->GetNext();
    }

    // release the stream
    stream->Release();

    return result;
}

/*----------------------------------------------------------------------
|   AP4_HintTrackReader::WriteImmediateRtpData
+---------------------------------------------------------------------*/
AP4_Result
AP4_HintTrackReader::WriteImmediateRtpData(AP4_ImmediateRtpConstructor* constructor, 
                                           AP4_ByteStream*              data_stream)
{
    const AP4_DataBuffer& data_buffer = constructor->GetData();
    return data_stream->Write(data_buffer.GetData(), data_buffer.GetDataSize());
}

/*----------------------------------------------------------------------
|   AP4_HintTrackReader::WriteSampleRtpData
+---------------------------------------------------------------------*/
AP4_Result
AP4_HintTrackReader::WriteSampleRtpData(AP4_SampleRtpConstructor* constructor, 
                                        AP4_ByteStream*           data_stream)
{
    AP4_Track* referenced_track = NULL;
    if (constructor->GetTrackRefIndex() == 0xFF) {
        // data is in the hint track
        referenced_track = &m_HintTrack;
    } else {
        // check if we have a media track
        if (m_MediaTrack == NULL) return AP4_FAILURE;
        referenced_track = m_MediaTrack;
    }

    // write the sample data
    AP4_Sample sample;
    AP4_Result result = referenced_track->GetSample(constructor->GetSampleNum()-1, // adjust
                                                    sample);
    if (AP4_FAILED(result)) return result;
    AP4_DataBuffer buffer(constructor->GetLength());
    result = sample.ReadData(
        buffer, constructor->GetLength(), constructor->GetSampleOffset());
    if (AP4_FAILED(result)) return result;

    // write the data
    return data_stream->Write(buffer.GetData(), buffer.GetDataSize());
}