SourceStream.cpp 3.74 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * SourceStream.cpp
 *****************************************************************************
 * Copyright © 2015 - VideoLAN and VLC Authors
 *
 * 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 24

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

25 26 27
#include "SourceStream.hpp"

#include "../ChunksSource.hpp"
28
#include "../http/Chunk.h"
29 30 31
#include <vlc_stream.h>
#include <vlc_demux.h>

32
using namespace adaptive;
33

34
ChunksSourceStream::ChunksSourceStream(vlc_object_t *p_obj_, ChunksSource *source_)
35 36 37
{
    p_block = NULL;
    b_eof = false;
38
    p_obj = p_obj_;
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    source = source_;
}

ChunksSourceStream::~ChunksSourceStream()
{
    Reset();
}

void ChunksSourceStream::Reset()
{
    if(p_block)
        block_Release(p_block);
    p_block = NULL;
    b_eof = false;
}

55
stream_t * ChunksSourceStream::makeStream()
56
{
57
    stream_t *p_stream = vlc_stream_CommonNew( p_obj, delete_Callback );
58 59 60 61 62 63 64 65 66
    if(p_stream)
    {
        p_stream->pf_control = control_Callback;
        p_stream->pf_read = read_Callback;
        p_stream->pf_readdir = NULL;
        p_stream->pf_seek = seek_Callback;
        p_stream->p_sys = reinterpret_cast<stream_sys_t*>(this);
    }
    return p_stream;
67 68 69 70 71 72 73 74 75
}

ssize_t ChunksSourceStream::Read(uint8_t *buf, size_t size)
{
    size_t i_copied = 0;
    size_t i_toread = size;

    while(i_toread && !b_eof)
    {
76
        if(!p_block && !(p_block = source->readNextBlock()))
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
        {
            b_eof = true;
            break;
        }

        if(p_block->i_buffer > i_toread)
        {
            if(buf)
                memcpy(buf + i_copied, p_block->p_buffer, i_toread);
            i_copied += i_toread;
            p_block->p_buffer += i_toread;
            p_block->i_buffer -= i_toread;
            i_toread = 0;
        }
        else
        {
            if(buf)
                memcpy(buf + i_copied, p_block->p_buffer, p_block->i_buffer);
            i_copied += p_block->i_buffer;
            i_toread -= p_block->i_buffer;
            block_Release(p_block);
            p_block = NULL;
        }
    }

    return i_copied;
}

ssize_t ChunksSourceStream::read_Callback(stream_t *s, void *buf, size_t size)
{
    ChunksSourceStream *me = reinterpret_cast<ChunksSourceStream *>(s->p_sys);
    return me->Read(reinterpret_cast<uint8_t *>(buf), size);
}

int ChunksSourceStream::seek_Callback(stream_t *, uint64_t)
{
    return VLC_EGENERIC;
}

int ChunksSourceStream::control_Callback(stream_t *, int i_query, va_list args)
{
    switch( i_query )
    {
        case STREAM_GET_SIZE:
            *(va_arg( args, uint64_t * )) = 0;
            return VLC_SUCCESS;

        case STREAM_CAN_SEEK:
        case STREAM_CAN_FASTSEEK:
        case STREAM_CAN_PAUSE:
        case STREAM_CAN_CONTROL_PACE:
            *va_arg( args, bool * ) = false;
            return VLC_SUCCESS;

        case STREAM_GET_PTS_DELAY:
            *(va_arg( args, uint64_t * )) = DEFAULT_PTS_DELAY;
            return VLC_SUCCESS;

        default:
            return VLC_EGENERIC;
    }
}

void ChunksSourceStream::delete_Callback(stream_t *)
{
}