aboutsummaryrefslogtreecommitdiff
path: root/pvmi/pvmf/src/pvmi_fd_data_stream.cpp
blob: 3e9b62c13fefc29049f81358a6457f8468e646a3 (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
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
/* ------------------------------------------------------------------
 * Copyright (C) 1998-2010 PacketVideo
 *
 * 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.
 * -------------------------------------------------------------------
 */
#ifndef PVMI_DATA_STREAM_H_INCLUDED
#include "pvmi_data_stream.h"
#endif


// Local constants
#define PVMI_FD_DATASTREAM_GETINFO_SUCCESS 0
#define PVMI_FD_DATASTREAM_GETINFO_FSTAT_FAIL 1

// Macros associated with the state flag
#define PVMI_FD_DS_INVALID_STATE_FLAG   1
#define PVMI_FD_DS_CLOSE_FLAG       2
#define RESET_FD_DS_STATE(x) x=0
#define SET_INVALID_FD_DS_STATE(x) x |= PVMI_FD_DS_INVALID_STATE_FLAG
#define SET_CLOSE_FD_DS_FLAG(x) x |= PVMI_FD_DS_CLOSE_FLAG
#define CHECK_FD_DS_CLOSE_FLAG(x) (x & PVMI_FD_DS_CLOSE_FLAG)
#define CHECK_FD_DS_INVALID_STATE(x) (x & PVMI_FD_DS_INVALID_STATE_FLAG)

PvmiDataStream::PvmiDataStream(int input_fd)
{
    RESET_FD_DS_STATE(iDataStreamState);
    if ((iFd = input_fd) == -1)
    {
        SET_INVALID_FD_DS_STATE(iDataStreamState);
        return;
    }

    // get information on the file
    if (PVMI_FD_DATASTREAM_GETINFO_SUCCESS != GetFileInfo())
    {
        SET_INVALID_FD_DS_STATE(iDataStreamState);
    }

    ipDSObserver = NULL;
    iTmpBuffer = NULL;
    iTmpSize = 0;
}

PvmiDataStream::PvmiDataStream(const char *filename)
{
    RESET_FD_DS_STATE(iDataStreamState);
    if ((iFd = open(filename, O_RDONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) == -1)
    {
        SET_INVALID_FD_DS_STATE(iDataStreamState);
        return;
    }

    // get information on the file
    if (PVMI_FD_DATASTREAM_GETINFO_SUCCESS != GetFileInfo())
    {
        close(iFd);
        SET_INVALID_FD_DS_STATE(iDataStreamState);
    }

    SET_CLOSE_FD_DS_FLAG(iDataStreamState);

    ipDSObserver = NULL;
    iTmpBuffer = NULL;
    iTmpSize = 0;
}

void PvmiDataStream::SetCallbackHandler(PvmiBasicDataStreamObs* aDSObserver)
{
    ipDSObserver = aDSObserver;
}

int PvmiDataStream::GetFileInfo()
{
    struct stat fd_stat;
    if (-1 == fstat(iFd, &fd_stat))
    {
        // problem getting file information
        return PVMI_FD_DATASTREAM_GETINFO_FSTAT_FAIL;
    }
    iRegFile = S_ISREG(fd_stat.st_mode);
    iFileSize = fd_stat.st_size;
    iBlkSize = fd_stat.st_blksize;

    return PVMI_FD_DATASTREAM_GETINFO_SUCCESS;
}

PvmiDataStream::~PvmiDataStream()
{
    if (CHECK_FD_DS_CLOSE_FLAG(iFd))
    {
        close(iFd);
    }
}

PvmiDataStreamStatus PvmiDataStream::GetFileSize(OsclOffsetT& size)
{
    if (CHECK_FD_DS_INVALID_STATE(iDataStreamState))
    {
        size = -1;
        return PVDS_INVALID_STATE;
    }
    size = iFileSize;
    return PVDS_SUCCESS;
}

PvmiDataStreamStatus PvmiDataStream::Read(PvmiDataStreamSession aSessionId,
        void* aBuffer,
        OsclSizeT& aSize)
{
    return InternalRead(aSessionId, aBuffer, aSize, 0, false);
}


PvmiDataStreamStatus PvmiDataStream::ReadAtOffset(PvmiDataStreamSession aSessionId,
        void* aBuffer,
        OsclSizeT& aSize,
        OsclOffsetT aOffset)
{
    return InternalRead(aSessionId, aBuffer, aSize, aOffset, true);
}


PvmiDataStreamStatus PvmiDataStream::InternalRead(PvmiDataStreamSession aSessionId,
        void* aBuffer,
        OsclSizeT& aSize,
        OsclOffsetT aOffset,
        bool aUseOffset)


{
    if (CHECK_FD_DS_INVALID_STATE(iDataStreamState))
    {
        aSize = -1;
        return PVDS_INVALID_STATE;
    }

    OsclSizeT size;
    OsclOffsetT CurrentOffset;
    PvmiDataStreamStatus status;

    if (aUseOffset)
    {
        CurrentOffset = GetCurrentPointerPosition(0);
        status = Seek(0, aOffset, PVDS_SEEK_SET);
        if (PVDS_SUCCESS != status)
            return PVDS_FAILURE;

        size = read(iFd, aBuffer, aSize);

        status = Seek(0, CurrentOffset, PVDS_SEEK_SET);
        if (PVDS_SUCCESS != status)
            return status;     // seeking shouldn't be a issue, yet if it does, then it should exit.
    }
    else
    {
        size = read(iFd, aBuffer, aSize);
    }

    aSize = size;

    switch (size)
    {
        case -1:
            status = PVDS_FAILURE;
            break;

        case 0:
            status = PVDS_END_OF_STREAM;
            break;

        default:
            status = PVDS_SUCCESS;
            break;
    }

    return status;
}

PvmiDataStreamStatus PvmiDataStream::Seek(PvmiDataStreamSession aSessionID,
        OsclOffsetT& aOffset,
        PvmiDataStreamSeekType aSeektype)
{
    if (CHECK_FD_DS_INVALID_STATE(iDataStreamState))
    {
        return PVDS_INVALID_STATE;
    }

    int seekType = SEEK_CUR;

    switch (aSeektype)
    {
        case PVDS_SEEK_SET:
            seekType = SEEK_SET;
            break;
        case PVDS_SEEK_CUR:
            seekType = SEEK_CUR;
            break;
        case PVDS_SEEK_END:
            seekType = SEEK_END;
            break;
        default:
            break;
    }

    OsclOffsetT offset = lseek(iFd, aOffset, seekType);

    if (-1 == offset)
    {
        return PVDS_FAILURE;
    }

    aOffset = offset;

    return PVDS_SUCCESS;
}

OsclOffsetT PvmiDataStream::GetCurrentPointerPosition(PvmiDataStreamSession aSessionID)
{
    return lseek(iFd, 0, SEEK_CUR);
}

PvmiDataStreamStatus PvmiDataStream::Write(PvmiDataStreamSession aSessionID,
        void* aBuffer,
        OsclSizeT aSize)
{
    return InternalWrite(aBuffer, aSize);
}

PvmiDataStreamStatus PvmiDataStream::InternalWrite(void* aBuffer, OsclSizeT aSize)
{
    if (CHECK_FD_DS_INVALID_STATE(iDataStreamState))
    {
        return PVDS_INVALID_STATE;
    }

    OsclSizeT size;
    size = write(iFd, aBuffer, aSize);

    if (size != aSize) return PVDS_FAILURE;

    return PVDS_SUCCESS;

}

void PvmiDataStream::DataAvailable()
{
    // Data is available now, do the read and return the buffer
    PvmiDataStreamStatus status = PVDS_SUCCESS;
    status = InternalRead(0, iTmpBuffer, iTmpSize, 0, false);
    ipDSObserver->NotifyDataAvailable(iTmpSize, status);
}