summaryrefslogtreecommitdiffstats
path: root/Common/ViperBuffer.cpp
blob: 27ec8b5e0576f220a318608956856bbde57dbd36 (plain)
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
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "ViperBuffer.h"
ViperBuffer::ViperBuffer(QObject* parent) :
    QIODevice(parent)
{
    readMax = 32768;
    readBuffer = (uint8_t*)malloc(sizeof(uint8_t)*readMax);
    pthread_mutex_init(&(this->monitorMutex), NULL);
    qByteArrayVector.reserve(2);
    qByteArrayVector.push_back(new QByteArray());
    qByteArrayVector.push_back(new QByteArray());
    indexReadBuffer = 0;
    indexWriteBuffer = 0;
}

ViperBuffer::~ViperBuffer()
{
    pthread_mutex_destroy(&(this->monitorMutex));
    free(readBuffer);
}

bool ViperBuffer::isSequential() const
{
    return true;
}

bool ViperBuffer::open(OpenMode mode)
{
    setOpenMode(mode);
    return true;
}

void ViperBuffer::close()
{
    qByteArrayVector.clear();
    setOpenMode(NotOpen);
}

void ViperBuffer::clear()
{
    qByteArrayVector.at(0)->clear();
    qByteArrayVector.at(1)->clear();
    indexReadBuffer = 0;
    indexWriteBuffer = 0;
}

qint64 ViperBuffer::readData(char* data, qint64 maxSize)
{
    pthread_mutex_lock(&(this->monitorMutex));
    if ((maxSize = qMin(maxSize, qint64(qByteArrayVector.at(indexReadBuffer)->size()))) <= 0)
    {
        pthread_mutex_unlock(&(this->monitorMutex));
        return qint64(0);
    }
    memcpy(data, qByteArrayVector.at(indexReadBuffer)->constData(), maxSize);
    qByteArrayVector.at(indexReadBuffer)->remove(0,maxSize);
    pthread_mutex_unlock(&(this->monitorMutex));
    return maxSize;

}

qint64 ViperBuffer::writeData(libdash::framework::input::MediaObject* media)
{
    pthread_mutex_lock(&(this->monitorMutex));
    int ret = 0;
    int total = 0;
    ret = media->ReadInitSegment(readBuffer,readMax);
    total += ret;
    this->writeData((const char *)readBuffer, ret);

    ret = media->Read(readBuffer,readMax);
    while(ret)
    {
        total += ret;
        this->writeData((const char *)readBuffer, ret);
        ret = media->Read(readBuffer,readMax);
    }
    pthread_mutex_unlock(&(this->monitorMutex));
    return total;
}

qint64 ViperBuffer::writeData(const char* data, qint64 maxSize)
{
    qByteArrayVector.at(indexWriteBuffer)->append(data, maxSize);
    return maxSize;
}


void ViperBuffer::writeToNextBuffer()
{

    pthread_mutex_lock(&(this->monitorMutex));
    indexWriteBuffer = (indexWriteBuffer + 1 ) % 2;
    pthread_mutex_unlock(&(this->monitorMutex));

}

void ViperBuffer::readFromNextBuffer()
{
    pthread_mutex_lock(&(this->monitorMutex));
    indexReadBuffer = (indexReadBuffer + 1 ) % 2;
    pthread_mutex_unlock(&(this->monitorMutex));

}