HLSStreams.cpp 5.25 KB
Newer Older
1
2
3
/*
 * HLSStreams.cpp
 *****************************************************************************
4
 * Copyright (C) 2015 - VideoLAN and VLC authors
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/
20
21
22
23
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

24
25
#include "HLSStreams.hpp"
#include <vlc_demux.h>
26
#include <vlc_meta.h>
27

28
29
#include "../mpeg/timestamps.h"

30
31
extern "C"
{
32
33
    #include "../../meta_engine/ID3Tag.h"
    #include "../../meta_engine/ID3Meta.h"
34
35
}

36
37
using namespace hls;

38
39
HLSStream::HLSStream(demux_t *demux)
    : AbstractStream(demux)
40
{
41
    b_id3_timestamps_offset_set = false;
42
43
44
45
46
47
48
49
    p_meta = vlc_meta_New();
    b_meta_updated = false;
}

HLSStream::~HLSStream()
{
    if(p_meta)
        vlc_meta_Delete(p_meta);
50
51
}

52
void HLSStream::setMetadataTimeOffset(vlc_tick_t i_offset)
53
{
54
    if(i_offset >= 0)
55
    {
56
57
58
        if(!b_id3_timestamps_offset_set)
            fakeEsOut()->setAssociatedTimestamp(i_offset);
        b_id3_timestamps_offset_set = true;
59
    }
60
61
    else
    {
62
        fakeEsOut()->setAssociatedTimestamp(-1);
63
64
        b_id3_timestamps_offset_set = false;
    }
65
66
67
68
69
70
71
72
}

bool HLSStream::setPosition(vlc_tick_t ts , bool b)
{
    bool ok = AbstractStream::setPosition(ts ,b);
    if(b && ok)
        b_id3_timestamps_offset_set = false;
    return ok;
73
74
}

75
int HLSStream::ParseID3PrivTag(const uint8_t *p_payload, size_t i_payload)
76
{
77
78
79
    if(i_payload == 53 &&
       !memcmp( p_payload, "com.apple.streaming.transportStreamTimestamp", 45))
    {
80
        setMetadataTimeOffset(FROM_SCALE_NZ(GetQWBE(&p_payload[45])));
81
82
    }
    return VLC_SUCCESS;
83
84
}

85
int HLSStream::ParseID3Tag(uint32_t i_tag, const uint8_t *p_payload, size_t i_payload)
86
{
87
88
89
90
    if(i_tag == VLC_FOURCC('P','R','I','V'))
        (void) ParseID3PrivTag(p_payload, i_payload);
    else
        (void) ID3HandleTag(p_payload, i_payload, i_tag, p_meta, &b_meta_updated);
91
    return VLC_SUCCESS;
92
93
}

94
95
96
97
98
99
int HLSStream::ID3TAG_Parse_Handler(uint32_t i_tag, const uint8_t *p_payload, size_t i_payload, void *p_priv)
{
    HLSStream *hlsstream = static_cast<HLSStream *>(p_priv);
    return hlsstream->ParseID3Tag(i_tag, p_payload, i_payload);
}

100
block_t * HLSStream::checkBlock(block_t *p_block, bool b_first)
101
{
102
    if(b_first && p_block)
103
    {
104
        while(p_block->i_buffer >= 10 && ID3TAG_IsTag(p_block->p_buffer, false))
105
106
107
        {
            size_t i_size = ID3TAG_Parse( p_block->p_buffer, p_block->i_buffer,
                                          ID3TAG_Parse_Handler, static_cast<void *>(this) );
108
109
            if(i_size >= p_block->i_buffer || i_size == 0)
                break;
110
111
112
113
            /* Skip ID3 for demuxer */
            p_block->p_buffer += i_size;
            p_block->i_buffer -= i_size;
        }
114
115
    }

116
117
118
    if( b_meta_updated )
    {
        b_meta_updated = false;
119
        AbstractCommand *command = fakeEsOut()->commandsFactory()->createEsOutMetaCommand( fakeesout, -1, p_meta );
120
        if( command )
121
            fakeEsOut()->commandsQueue()->Schedule( command );
122
123
    }

124
    return p_block;
125
126
}

127
AbstractDemuxer *HLSStream::newDemux(vlc_object_t *p_obj, const StreamFormat &format,
128
129
                                     es_out_t *out, AbstractSourceStream *source) const
{
130
    AbstractDemuxer *ret = nullptr;
131
    switch(format)
132
    {
133
        case StreamFormat::Type::PackedAAC:
134
135
        case StreamFormat::Type::PackedMP3:
        case StreamFormat::Type::PackedAC3:
136
            ret = new Demuxer(p_obj, "es", out, source);
137
138
            break;

139
        case StreamFormat::Type::MPEG2TS:
140
            ret = new Demuxer(p_obj, "ts", out, source);
141
            if(ret)
142
                ret->setBitstreamSwitchCompatible(false); /* HLS and unique PAT/PMT versions */
143
144
            break;

145
        case StreamFormat::Type::MP4:
146
            ret = AbstractStream::newDemux(p_obj, format, out, source);
147
148
            break;

149
/* Disabled until we can handle empty segments/cue and absolute time
150
        case StreamFormat::Type::WebVTT:
151
            ret = new Demuxer(p_obj, "webvttstream", out, source);
152
153
154
            if(ret)
                ret->setRestartsOnEachSegment(true);
            break;
155
*/
156
157

        default:
158
159
        case StreamFormat::Type::Unknown:
        case StreamFormat::Type::Unsupported:
160
161
162
163
164
            break;
    }
    return ret;
}

165
AbstractStream * HLSStreamFactory::create(demux_t *realdemux, const StreamFormat &format,
166
                               SegmentTracker *tracker, AbstractConnectionManager *manager) const
167
{
168
    HLSStream *stream = new (std::nothrow) HLSStream(realdemux);
169
    if(stream && !stream->init(format, tracker, manager))
170
    {
171
        delete stream;
172
        return nullptr;
173
174
    }
    return stream;
175
}