summaryrefslogtreecommitdiff
path: root/media/eco/ECOService.cpp
blob: e95dee500da8497d2b3ba90f58266abae4c41bee (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
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * 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.
 */

//#define LOG_NDEBUG 0
#define LOG_TAG "ECOService"

#include "eco/ECOService.h"

#include <binder/BinderService.h>
#include <cutils/atomic.h>
#include <inttypes.h>
#include <pthread.h>
#include <stdio.h>
#include <sys/types.h>

#include <algorithm>
#include <climits>
#include <cstring>
#include <ctime>
#include <string>

#include "eco/ECODebug.h"

namespace android {
namespace media {
namespace eco {

ECOService::ECOService() : BnECOService() {
    ALOGD("ECOService created");
    updateLogLevel();
}

/*virtual*/ ::android::binder::Status ECOService::obtainSession(
        int32_t width, int32_t height, bool isCameraRecording,
        ::android::sp<::android::media::eco::IECOSession>* _aidl_return) {
    ECOLOGI("ECOService::obtainSession w: %d, h: %d, isCameraRecording: %d", width, height,
            isCameraRecording);

    bool disable = property_get_bool(kDisableEcoServiceProperty, false);
    if (disable) {
        ECOLOGE("ECOService:: Failed to obtainSession as ECOService is disable");
        return STATUS_ERROR(ERROR_UNSUPPORTED, "ECOService is disable");
    }

    if (width <= 0) {
        return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Width can not be <= 0");
    }

    if (height <= 0) {
        return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Height can not be <= 0");
    }

    SessionConfig newCfg(width, height, isCameraRecording);

    ECOLOGD("session count before is %zu", mSessionConfigToSessionMap.size());

    Mutex::Autolock lock(mServiceLock);
    bool foundSession = false;
    // Instead of looking up the map directly, take the chance to scan the map and evict all the
    // invalid sessions.
    SanitizeSession([&](MapIterType iter) {
        if (iter->first == newCfg) {
            sp<ECOSession> session = iter->second.promote();
            foundSession = true;
            *_aidl_return = session;
        }
    });

    if (foundSession) {
        return binder::Status::ok();
    }

    // Create a new session and add it to the record.
    sp<ECOSession> newSession = ECOSession::createECOSession(width, height, isCameraRecording);
    if (newSession == nullptr) {
        ECOLOGE("ECOService failed to create ECOSession w: %d, h: %d, isCameraRecording: %d", width,
                height, isCameraRecording);
        return STATUS_ERROR(ERROR_UNSUPPORTED, "Failed to create eco session");
    }
    *_aidl_return = newSession;
    // Insert the new session into the map.
    mSessionConfigToSessionMap[newCfg] = newSession;
    ECOLOGD("session count after is %zu", mSessionConfigToSessionMap.size());

    return binder::Status::ok();
}

/*virtual*/ ::android::binder::Status ECOService::getNumOfSessions(int32_t* _aidl_return) {
    Mutex::Autolock lock(mServiceLock);
    SanitizeSession(std::function<void(MapIterType it)>());  // empty callback
    *_aidl_return = mSessionConfigToSessionMap.size();
    return binder::Status::ok();
}

/*virtual*/ ::android::binder::Status ECOService::getSessions(
        ::std::vector<::android::sp<::android::IBinder>>* _aidl_return) {
    // Clear all the entries in the vector.
    _aidl_return->clear();

    Mutex::Autolock lock(mServiceLock);
    SanitizeSession([&](MapIterType iter) {
        sp<ECOSession> session = iter->second.promote();
        _aidl_return->push_back(IInterface::asBinder(session));
    });
    return binder::Status::ok();
}

inline bool isEmptySession(const android::wp<ECOSession>& entry) {
    sp<ECOSession> session = entry.promote();
    return session == nullptr;
}

void ECOService::SanitizeSession(
        const std::function<void(std::unordered_map<SessionConfig, wp<ECOSession>,
                                                    SessionConfigHash>::iterator it)>& callback) {
    for (auto it = mSessionConfigToSessionMap.begin(), end = mSessionConfigToSessionMap.end();
         it != end;) {
        if (isEmptySession(it->second)) {
            it = mSessionConfigToSessionMap.erase(it);
        } else {
            if (callback != nullptr) {
                callback(it);
            };
            it++;
        }
    }
}

/*virtual*/ void ECOService::binderDied(const wp<IBinder>& /*who*/) {}

status_t ECOService::dump(int fd, const Vector<String16>& args) {
    Mutex::Autolock lock(mServiceLock);
    dprintf(fd, "\n== ECO Service info: ==\n\n");
    dprintf(fd, "Number of ECOServices: %zu\n", mSessionConfigToSessionMap.size());
    for (auto it = mSessionConfigToSessionMap.begin(), end = mSessionConfigToSessionMap.end();
         it != end; it++) {
        sp<ECOSession> session = it->second.promote();
        if (session != nullptr) {
            session->dump(fd, args);
        }
    }

    return NO_ERROR;
}

}  // namespace eco
}  // namespace media
}  // namespace android