1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "SurroundViewStreamHandler.h"
18 
19 #include <utils/Log.h>
20 
21 using std::lock_guard;
22 
SurroundViewServiceHandler(sp<ISurroundViewSession> pSession)23 SurroundViewServiceHandler::SurroundViewServiceHandler(sp<ISurroundViewSession> pSession) :
24     mSession(pSession),
25     mReceiveFramesCount(0),
26     mDoNotReturnFrames(false) {
27     // Nothing but member initialization
28 }
29 
notify(SvEvent svEvent)30 Return<void> SurroundViewServiceHandler::notify(SvEvent svEvent) {
31     ALOGD("SurroundViewServiceHandler::notify %d", svEvent);
32 
33     lock_guard<mutex> lock(mLock);
34     switch (svEvent) {
35         case SvEvent::STREAM_STARTED:
36         case SvEvent::CONFIG_UPDATED:
37         case SvEvent::STREAM_STOPPED:
38         case SvEvent::FRAME_DROPPED:
39         case SvEvent::TIMEOUT:
40             mReceivedEvents.emplace_back(svEvent);
41             break;
42         default:
43             ALOGI("[SurroundViewLog] Received other event");
44     }
45 
46     return android::hardware::Void();
47 }
48 
receiveFrames(const SvFramesDesc & svFramesDesc)49 Return<void> SurroundViewServiceHandler::receiveFrames(const SvFramesDesc& svFramesDesc) {
50     ALOGD("SurroundViewServiceHandler::receiveFrames");
51 
52     lock_guard<mutex> lock(mLock);
53     unsigned long timestampNs = svFramesDesc.timestampNs;
54     unsigned sequenceId = svFramesDesc.sequenceId;
55     ALOGD("receiveFrames count: %d", mReceiveFramesCount);
56     ALOGD("timestampNs: %lu, sequenceId: %u", timestampNs, sequenceId);
57     if (mReceiveFramesCount != 0
58         && (mLastReceivedFrames.timestampNs >= svFramesDesc.timestampNs
59             || mLastReceivedFrames.sequenceId >= svFramesDesc.sequenceId)) {
60         mAllFramesValid = false;
61         ALOGD("The incoming frames are with invalid timestamp or sequenceId!");
62     }
63 
64     for (int i=0; i<svFramesDesc.svBuffers.size(); i++) {
65         if (svFramesDesc.svBuffers[i].hardwareBuffer.nativeHandle == nullptr) {
66             mAllFramesValid = false;
67             ALOGD("The incoming frames are with invalid nativeHandle!");
68             break;
69         }
70     }
71 
72     mReceiveFramesCount++;
73 
74     // Store all the information except for the handle
75     mLastReceivedFrames.timestampNs = svFramesDesc.timestampNs;
76     mLastReceivedFrames.sequenceId = svFramesDesc.sequenceId;
77     mLastReceivedFrames.svBuffers.resize(svFramesDesc.svBuffers.size());
78     for (int i=0; i<svFramesDesc.svBuffers.size(); i++) {
79         mLastReceivedFrames.svBuffers[i].viewId = svFramesDesc.svBuffers[i].viewId;
80         mLastReceivedFrames.svBuffers[i].hardwareBuffer.description =
81             svFramesDesc.svBuffers[i].hardwareBuffer.description;
82     }
83 
84     if (!mDoNotReturnFrames) {
85         mSession->doneWithFrames(svFramesDesc);
86     }
87 
88     return android::hardware::Void();
89 }
90 
checkEventReceived(SvEvent svEvent)91 bool SurroundViewServiceHandler::checkEventReceived(SvEvent svEvent) {
92     ALOGD("SurroundViewServiceHandler::checkEventReceived");
93     int size = mReceivedEvents.size(); // work around
94     ALOGD("Received event number: %d", size);
95     auto iter = find(mReceivedEvents.begin(), mReceivedEvents.end(), svEvent);
96     return iter != mReceivedEvents.end();
97 }
98 
getLastReceivedFrames()99 SvFramesDesc SurroundViewServiceHandler::getLastReceivedFrames() {
100     return mLastReceivedFrames;
101 }
102 
getReceiveFramesCount()103 int SurroundViewServiceHandler::getReceiveFramesCount() {
104     return mReceiveFramesCount;
105 }
106 
areAllFramesValid()107 bool SurroundViewServiceHandler::areAllFramesValid() {
108     return mAllFramesValid;
109 }
110 
setDoNotReturnFrames(bool flag)111 void SurroundViewServiceHandler::setDoNotReturnFrames(bool flag) {
112     mDoNotReturnFrames = flag;
113 }
114