summaryrefslogtreecommitdiffstats
path: root/camera/OMXCameraAdapter/OMXDCC.cpp
blob: cbaf2abc1a75513d603eddae886b73887cc7be74 (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
/*
 * Copyright (C) Texas Instruments - http://www.ti.com/
 *
 * 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.
 */

/**
* @file OMXDCC.cpp
*
* This file contains functionality for loading the DCC binaries.
*
*/

#include "CameraHal.h"
#include "OMXCameraAdapter.h"
#include "ErrorUtils.h"
#include "OMXDCC.h"
#include <utils/String8.h>
#include <utils/Vector.h>

namespace Ti {
namespace Camera {

#ifndef MOTOROLA_CAMERA
android::String8 DCCHandler::DCCPath("/data/misc/camera/");
#else
android::String8 DCCHandler::DCCPath("/system/etc/omapcam/");
#endif
bool DCCHandler::mDCCLoaded = false;

status_t DCCHandler::loadDCC(OMX_HANDLETYPE hComponent)
{
    OMX_ERRORTYPE dccError = OMX_ErrorNone;

    if (!mDCCLoaded) {
        dccError = initDCC(hComponent);
        if (dccError != OMX_ErrorNone) {
            CAMHAL_LOGE(" Error in DCC Init");
        }

        mDCCLoaded = true;
    }

    return Utils::ErrorUtils::omxToAndroidError(dccError);
}

OMX_ERRORTYPE DCCHandler::initDCC(OMX_HANDLETYPE hComponent)
{
    OMX_TI_PARAM_DCCURIINFO param;
    OMX_PTR ptempbuf;
    OMX_U16 nIndex = 0;
    OMX_ERRORTYPE eError = OMX_ErrorNone;
    int ret;
    OMX_S32 status = 0;
    android::Vector<android::String8 *> dccDirs;
    OMX_U16 i;
    MemoryManager memMgr;
    CameraBuffer *dccBuffer = NULL;
    int dccbuf_size = 0;
    OMX_INIT_STRUCT_PTR(&param, OMX_TI_PARAM_DCCURIINFO);

    // Read the the DCC URI info
    for (nIndex = 0; eError != OMX_ErrorNoMore; nIndex++) {
        param.nIndex = nIndex;
        eError = OMX_GetParameter(hComponent,
                                  ( OMX_INDEXTYPE )OMX_TI_IndexParamDccUriInfo,
                                  &param);

        if (eError == OMX_ErrorNone) {
            CAMHAL_LOGD("DCC URI's %s ", param.sDCCURI);
            android::String8 *dccDir = new android::String8();
            if ( NULL != dccDir ) {
                dccDir->clear();
                dccDir->append(DCCPath);
                dccDir->append((const char *) param.sDCCURI);
                dccDir->append("/");
                dccDirs.add(dccDir);
            } else {
                CAMHAL_LOGE("DCC URI not allocated");
                eError = OMX_ErrorInsufficientResources;
                goto EXIT;
            }
        }
    }

    // setting  back errortype OMX_ErrorNone
    if (eError == OMX_ErrorNoMore) {
        eError = OMX_ErrorNone;
    }

    dccbuf_size = readDCCdir(NULL, dccDirs);
    if(dccbuf_size <= 0) {
        CAMHAL_LOGE("No DCC files found, switching back to default DCC");
        eError = OMX_ErrorInsufficientResources;
        goto EXIT;
    }
    dccbuf_size = ((dccbuf_size + 4095 )/4096)*4096;

    if ( memMgr.initialize() != NO_ERROR ) {
        CAMHAL_LOGE("DCC memory manager initialization failed!!!");
        eError = OMX_ErrorInsufficientResources;
        goto EXIT;
    }

    dccBuffer = memMgr.allocateBufferList(0, 0, NULL, dccbuf_size, 1);
    if ( NULL == dccBuffer ) {
        CAMHAL_LOGE("DCC buffer allocation failed!!!");
        eError = OMX_ErrorInsufficientResources;
        goto EXIT;
    }

    dccbuf_size = readDCCdir(dccBuffer[0].mapped, dccDirs);
    CAMHAL_ASSERT_X(dccbuf_size > 0,"ERROR in copy DCC files into buffer");

    eError = sendDCCBufPtr(hComponent, dccBuffer);

EXIT:

    for (i = 0; i < dccDirs.size(); i++) {
        android::String8 *dccDir = dccDirs.itemAt(0);
        dccDirs.removeAt(0);
        delete dccDir;
    }

    if ( NULL != dccBuffer ) {
        memMgr.freeBufferList(dccBuffer);
    }

     return eError;
}

OMX_ERRORTYPE DCCHandler::sendDCCBufPtr(OMX_HANDLETYPE hComponent,
                                    CameraBuffer *dccBuffer)
{
    OMX_TI_CONFIG_SHAREDBUFFER uribufparam;
    OMX_ERRORTYPE eError = OMX_ErrorNone;
    OMX_INIT_STRUCT_PTR(&uribufparam, OMX_TI_CONFIG_SHAREDBUFFER);

    CAMHAL_ASSERT_X(dccBuffer != NULL,"ERROR invalid DCC buffer");

    uribufparam.nPortIndex = OMX_ALL;
    uribufparam.nSharedBuffSize = dccBuffer->size;
    uribufparam.pSharedBuff = (OMX_U8 *) camera_buffer_get_omx_ptr(dccBuffer);

    eError = OMX_SetParameter(hComponent,
                                ( OMX_INDEXTYPE )OMX_TI_IndexParamDccUriBuffer,
                                &uribufparam);
    if (eError != OMX_ErrorNone) {
        CAMHAL_LOGEB(" Error in SetParam for DCC Uri Buffer 0x%x", eError);
    }

    return eError;
}

size_t DCCHandler::readDCCdir(OMX_PTR buffer,
                          const android::Vector<android::String8 *> &dirPaths)
{
    FILE *pFile;
    OMX_S32 lSize;
    OMX_S32 dcc_buf_size = 0;
    size_t result;
    OMX_STRING filename;
    android::String8 temp;
    const char *dotdot = "..";
    DIR *d;
    struct dirent *dir;
    OMX_U16 i = 0;
    status_t stat = NO_ERROR;
    size_t ret = 0;

    for (i = 0; i < dirPaths.size(); i++) {
        d = opendir(dirPaths.itemAt(i)->string());
        if (d) {
            // read each filename
            while ((dir = readdir(d)) != NULL) {
                filename = dir->d_name;
                temp.clear();
                temp.append(dirPaths.itemAt(i)->string());
                temp.append(filename);
                if ((*filename != *dotdot)) {
                    pFile = fopen(temp.string(), "rb");
                    if (pFile == NULL) {
                        stat = -errno;
                    } else {
                        fseek(pFile, 0, SEEK_END);
                        lSize = ftell(pFile);
                        rewind(pFile);
                        // buffer is not NULL then copy all the DCC profiles into buffer
                        // else return the size of the DCC directory.
                        if (buffer) {
                            // copy file into the buffer:
                            result = fread(buffer, 1, lSize, pFile);
                            if (result != (size_t) lSize) {
                                stat = INVALID_OPERATION;
                            }
                            buffer = buffer + lSize;
                        }
                        // getting the size of the total dcc files available in FS */
                        dcc_buf_size = dcc_buf_size + lSize;
                        // terminate
                        fclose(pFile);
                    }
                }
            }
            closedir(d);
        }
    }

    if (stat == NO_ERROR) {
        ret = dcc_buf_size;
    }

    return ret;
}

} // namespace Camera
} // namespace Ti