summaryrefslogtreecommitdiff
path: root/automotive/evs/1.1/default/EvsCamera.cpp
blob: 520670a5d622ef364699e1bd0a04124188d85829 (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
/*
 * 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.
 */

#include "EvsCamera.h"
#include "ConfigManager.h"
#include "EvsEnumerator.h"

#include <ui/GraphicBufferAllocator.h>
#include <ui/GraphicBufferMapper.h>
#include <utils/SystemClock.h>

namespace {

// Arbitrary limit on number of graphics buffers allowed to be allocated
// Safeguards against unreasonable resource consumption and provides a testable limit
constexpr unsigned kMaxBuffersInFlight = 100;

// Minimum number of buffers to run a video stream
constexpr int kMinimumBuffersInFlight = 1;

// Colors for the colorbar test pattern in ABGR format
constexpr uint32_t kColors[] = {
        0xFFFFFFFF,  // white
        0xFF00FFFF,  // yellow
        0xFFFFFF00,  // cyan
        0xFF00FF00,  // green
        0xFFFF00FF,  // fuchsia
        0xFF0000FF,  // red
        0xFFFF0000,  // blue
        0xFF000000,  // black
};
constexpr uint32_t kNumColors = sizeof(kColors) / sizeof(kColors[0]);

}  // namespace

namespace android::hardware::automotive::evs::V1_1::implementation {

using V1_0::EvsResult;

EvsCamera::EvsCamera(const char* id, std::unique_ptr<ConfigManager::CameraInfo>& camInfo)
    : mFramesAllowed(0), mFramesInUse(0), mStreamState(STOPPED), mCameraInfo(camInfo) {
    ALOGD("%s", __FUNCTION__);

    /* set a camera id */
    mDescription.v1.cameraId = id;

    /* set camera metadata */
    mDescription.metadata.setToExternal((uint8_t*)camInfo->characteristics,
                                        get_camera_metadata_size(camInfo->characteristics));
}

EvsCamera::~EvsCamera() {
    ALOGD("%s", __FUNCTION__);
    forceShutdown();
}

// This gets called if another caller "steals" ownership of the camera
void EvsCamera::forceShutdown() {
    ALOGD("%s", __FUNCTION__);

    // Make sure our output stream is cleaned up
    // (It really should be already)
    stopVideoStream();

    // Claim the lock while we work on internal state
    std::lock_guard<std::mutex> lock(mAccessLock);

    // Drop all the graphics buffers we've been using
    if (mBuffers.size() > 0) {
        GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
        for (auto&& rec : mBuffers) {
            if (rec.inUse) {
                ALOGE("Error - releasing buffer despite remote ownership");
            }
            alloc.free(rec.handle);
            rec.handle = nullptr;
        }
        mBuffers.clear();
    }

    // Put this object into an unrecoverable error state since somebody else
    // is going to own the underlying camera now
    mStreamState = DEAD;
}

// Methods from ::android::hardware::automotive::evs::V1_0::IEvsCamera follow.
Return<void> EvsCamera::getCameraInfo(getCameraInfo_cb _hidl_cb) {
    ALOGD("%s", __FUNCTION__);

    // Send back our self description
    _hidl_cb(mDescription.v1);
    return {};
}

Return<EvsResult> EvsCamera::setMaxFramesInFlight(uint32_t bufferCount) {
    ALOGD("%s, bufferCount = %u", __FUNCTION__, bufferCount);

    std::lock_guard<std::mutex> lock(mAccessLock);

    // If we've been displaced by another owner of the camera, then we can't do anything else
    if (mStreamState == DEAD) {
        ALOGE("ignoring setMaxFramesInFlight call when camera has been lost.");
        return EvsResult::OWNERSHIP_LOST;
    }

    // We cannot function without at least one video buffer to send data
    if (bufferCount < 1) {
        ALOGE("Ignoring setMaxFramesInFlight with less than one buffer requested");
        return EvsResult::INVALID_ARG;
    }

    // Update our internal state
    if (setAvailableFrames_Locked(bufferCount)) {
        return EvsResult::OK;
    } else {
        return EvsResult::BUFFER_NOT_AVAILABLE;
    }
}

Return<EvsResult> EvsCamera::startVideoStream(const ::android::sp<V1_0::IEvsCameraStream>& stream) {
    ALOGD("%s", __FUNCTION__);

    std::lock_guard<std::mutex> lock(mAccessLock);

    // If we've been displaced by another owner of the camera, then we can't do anything else
    if (mStreamState == DEAD) {
        ALOGE("ignoring startVideoStream call when camera has been lost.");
        return EvsResult::OWNERSHIP_LOST;
    }

    if (mStreamState != STOPPED) {
        ALOGE("ignoring startVideoStream call when a stream is already running.");
        return EvsResult::STREAM_ALREADY_RUNNING;
    }

    // If the client never indicated otherwise, configure ourselves for a single streaming buffer
    if (mFramesAllowed < kMinimumBuffersInFlight) {
        if (!setAvailableFrames_Locked(kMinimumBuffersInFlight)) {
            ALOGE("Failed to start stream because we couldn't get a graphics buffer");
            return EvsResult::BUFFER_NOT_AVAILABLE;
        }
    }

    // Record the user's callback for use when we have a frame ready
    mStream = IEvsCameraStream::castFrom(stream).withDefault(nullptr);
    if (!mStream) {
        ALOGE("Default implementation does not support v1.0 IEvsCameraStream");
        return EvsResult::INVALID_ARG;
    }

    // Start the frame generation thread
    mStreamState = RUNNING;
    mCaptureThread = std::thread([this]() { generateFrames(); });

    return EvsResult::OK;
}

Return<void> EvsCamera::doneWithFrame(const V1_0::BufferDesc& buffer) {
    std::lock_guard<std::mutex> lock(mAccessLock);
    returnBufferLocked(buffer.bufferId, buffer.memHandle);

    return {};
}

Return<void> EvsCamera::stopVideoStream() {
    ALOGD("%s", __FUNCTION__);

    std::unique_lock<std::mutex> lock(mAccessLock);

    if (mStreamState != RUNNING) {
        return {};
    }

    // Tell the GenerateFrames loop we want it to stop
    mStreamState = STOPPING;

    // Block outside the mutex until the "stop" flag has been acknowledged
    // We won't send any more frames, but the client might still get some already in flight
    ALOGD("Waiting for stream thread to end...");
    lock.unlock();
    if (mCaptureThread.joinable()) {
        mCaptureThread.join();
    }
    lock.lock();

    mStreamState = STOPPED;
    mStream = nullptr;
    ALOGD("Stream marked STOPPED.");

    return {};
}

Return<int32_t> EvsCamera::getExtendedInfo(uint32_t opaqueIdentifier) {
    ALOGD("%s", __FUNCTION__);

    std::lock_guard<std::mutex> lock(mAccessLock);
    const auto it = mExtInfo.find(opaqueIdentifier);
    if (it == mExtInfo.end()) {
        // Return zero by default as required by the spec
        return 0;
    } else {
        return it->second[0];
    }
}

Return<EvsResult> EvsCamera::setExtendedInfo([[maybe_unused]] uint32_t opaqueIdentifier,
                                             [[maybe_unused]] int32_t opaqueValue) {
    ALOGD("%s", __FUNCTION__);

    std::lock_guard<std::mutex> lock(mAccessLock);

    // If we've been displaced by another owner of the camera, then we can't do anything else
    if (mStreamState == DEAD) {
        ALOGE("ignoring setExtendedInfo call when camera has been lost.");
        return EvsResult::OWNERSHIP_LOST;
    }

    mExtInfo.insert_or_assign(opaqueIdentifier, opaqueValue);
    return EvsResult::OK;
}

// Methods from ::android::hardware::automotive::evs::V1_1::IEvsCamera follow.
Return<void> EvsCamera::getCameraInfo_1_1(getCameraInfo_1_1_cb _hidl_cb) {
    ALOGD("%s", __FUNCTION__);

    // Send back our self description
    _hidl_cb(mDescription);
    return {};
}

Return<void> EvsCamera::getPhysicalCameraInfo([[maybe_unused]] const hidl_string& id,
                                              getCameraInfo_1_1_cb _hidl_cb) {
    ALOGD("%s", __FUNCTION__);

    // This works exactly same as getCameraInfo_1_1() in default implementation.
    _hidl_cb(mDescription);
    return {};
}

Return<EvsResult> EvsCamera::doneWithFrame_1_1(const hidl_vec<BufferDesc>& buffers) {
    ALOGD("%s", __FUNCTION__);

    std::lock_guard<std::mutex> lock(mAccessLock);
    for (auto&& buffer : buffers) {
        returnBufferLocked(buffer.bufferId, buffer.buffer.nativeHandle);
    }
    return EvsResult::OK;
}

Return<EvsResult> EvsCamera::pauseVideoStream() {
    ALOGD("%s", __FUNCTION__);
    // Default implementation does not support this.
    return EvsResult::UNDERLYING_SERVICE_ERROR;
}

Return<EvsResult> EvsCamera::resumeVideoStream() {
    ALOGD("%s", __FUNCTION__);
    // Default implementation does not support this.
    return EvsResult::UNDERLYING_SERVICE_ERROR;
}

Return<EvsResult> EvsCamera::setMaster() {
    ALOGD("%s", __FUNCTION__);
    // Default implementation does not expect multiple subscribers and therefore
    // return a success code always.
    return EvsResult::OK;
}

Return<EvsResult> EvsCamera::forceMaster(const sp<V1_0::IEvsDisplay>&) {
    ALOGD("%s", __FUNCTION__);
    // Default implementation does not expect multiple subscribers and therefore
    // return a success code always.
    return EvsResult::OK;
}

Return<EvsResult> EvsCamera::unsetMaster() {
    ALOGD("%s", __FUNCTION__);
    // Default implementation does not expect multiple subscribers and therefore
    // return a success code always.
    return EvsResult::OK;
}

Return<void> EvsCamera::getParameterList(getParameterList_cb _hidl_cb) {
    ALOGD("%s", __FUNCTION__);
    hidl_vec<CameraParam> hidlCtrls;
    hidlCtrls.resize(mCameraInfo->controls.size());
    unsigned idx = 0;
    for (auto& [cid, cfg] : mCameraInfo->controls) {
        hidlCtrls[idx++] = cid;
    }

    _hidl_cb(hidlCtrls);
    return {};
}

Return<void> EvsCamera::getIntParameterRange(CameraParam id, getIntParameterRange_cb _hidl_cb) {
    ALOGD("%s", __FUNCTION__);
    auto it = mCameraInfo->controls.find(id);
    if (it == mCameraInfo->controls.end()) {
        _hidl_cb(0, 0, 0);
    } else {
        _hidl_cb(std::get<0>(it->second), std::get<1>(it->second), std::get<2>(it->second));
    }
    return {};
}

Return<void> EvsCamera::setIntParameter(CameraParam id, int32_t value,
                                        setIntParameter_cb _hidl_cb) {
    ALOGD("%s", __FUNCTION__);
    mParams.insert_or_assign(id, value);
    _hidl_cb(EvsResult::OK, {value});
    return {};
}

Return<void> EvsCamera::getIntParameter([[maybe_unused]] CameraParam id,
                                        getIntParameter_cb _hidl_cb) {
    ALOGD("%s", __FUNCTION__);
    auto it = mParams.find(id);
    std::vector<int32_t> values;
    if (it == mParams.end()) {
        _hidl_cb(EvsResult::INVALID_ARG, values);
    } else {
        values.push_back(it->second);
        _hidl_cb(EvsResult::OK, values);
    }
    return {};
}

Return<EvsResult> EvsCamera::setExtendedInfo_1_1(uint32_t opaqueIdentifier,
                                                 const hidl_vec<uint8_t>& opaqueValue) {
    ALOGD("%s", __FUNCTION__);
    mExtInfo.insert_or_assign(opaqueIdentifier, opaqueValue);
    return EvsResult::OK;
}

Return<void> EvsCamera::getExtendedInfo_1_1(uint32_t opaqueIdentifier,
                                            getExtendedInfo_1_1_cb _hidl_cb) {
    ALOGD("%s", __FUNCTION__);
    auto status = EvsResult::OK;
    hidl_vec<uint8_t> value;
    const auto it = mExtInfo.find(opaqueIdentifier);
    if (it == mExtInfo.end()) {
        status = EvsResult::INVALID_ARG;
    } else {
        value = it->second;
    }
    _hidl_cb(status, value);
    return {};
}

Return<void> EvsCamera::importExternalBuffers([[maybe_unused]] const hidl_vec<BufferDesc>& buffers,
                                              importExternalBuffers_cb _hidl_cb) {
    auto numBuffersToAdd = buffers.size();
    if (numBuffersToAdd < 1) {
        ALOGD("No buffers to add");
        _hidl_cb(EvsResult::OK, mFramesAllowed);
        return {};
    }

    {
        std::scoped_lock<std::mutex> lock(mAccessLock);

        if (numBuffersToAdd > (kMaxBuffersInFlight - mFramesAllowed)) {
            numBuffersToAdd -= (kMaxBuffersInFlight - mFramesAllowed);
            ALOGW("Exceed the limit on number of buffers. %" PRIu64 " buffers will be added only.",
                  static_cast<uint64_t>(numBuffersToAdd));
        }

        GraphicBufferMapper& mapper = GraphicBufferMapper::get();
        const auto before = mFramesAllowed;
        for (auto i = 0; i < numBuffersToAdd; ++i) {
            // TODO: reject if external buffer is configured differently.
            auto& b = buffers[i];
            const AHardwareBuffer_Desc* pDesc =
                    reinterpret_cast<const AHardwareBuffer_Desc*>(&b.buffer.description);

            // Import a buffer to add
            buffer_handle_t memHandle = nullptr;
            status_t result =
                    mapper.importBuffer(b.buffer.nativeHandle, pDesc->width, pDesc->height, 1,
                                        pDesc->format, pDesc->usage, pDesc->stride, &memHandle);
            if (result != android::NO_ERROR || !memHandle) {
                ALOGW("Failed to import a buffer %d", b.bufferId);
                continue;
            }

            auto stored = false;
            for (auto&& rec : mBuffers) {
                if (rec.handle == nullptr) {
                    // Use this existing entry
                    rec.handle = memHandle;
                    rec.inUse = false;

                    stored = true;
                    break;
                }
            }

            if (!stored) {
                // Add a BufferRecord wrapping this handle to our set of available buffers
                mBuffers.emplace_back(memHandle);
            }

            ++mFramesAllowed;
        }

        _hidl_cb(EvsResult::OK, mFramesAllowed - before);
        return {};
    }
}

bool EvsCamera::setAvailableFrames_Locked(unsigned bufferCount) {
    if (bufferCount < kMinimumBuffersInFlight) {
        ALOGE("Ignoring request to set buffer count below the minimum number of buffers to run a "
              "video stream");
        return false;
    }
    if (bufferCount > kMaxBuffersInFlight) {
        ALOGE("Rejecting buffer request in excess of internal limit");
        return false;
    }

    // Is an increase required?
    if (mFramesAllowed < bufferCount) {
        // An increase is required
        unsigned needed = bufferCount - mFramesAllowed;
        ALOGI("Allocating %d buffers for camera frames", needed);

        unsigned added = increaseAvailableFrames_Locked(needed);
        if (added != needed) {
            // If we didn't add all the frames we needed, then roll back to the previous state
            ALOGE("Rolling back to previous frame queue size");
            decreaseAvailableFrames_Locked(added);
            return false;
        }
    } else if (mFramesAllowed > bufferCount) {
        // A decrease is required
        unsigned framesToRelease = mFramesAllowed - bufferCount;
        ALOGI("Returning %d camera frame buffers", framesToRelease);

        unsigned released = decreaseAvailableFrames_Locked(framesToRelease);
        if (released != framesToRelease) {
            // This shouldn't happen with a properly behaving client because the client
            // should only make this call after returning sufficient outstanding buffers
            // to allow a clean resize.
            ALOGE("Buffer queue shrink failed -- too many buffers currently in use?");
        }
    }

    return true;
}

unsigned EvsCamera::increaseAvailableFrames_Locked(unsigned numToAdd) {
    // Acquire the graphics buffer allocator
    GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());

    unsigned added = 0;

    while (added < numToAdd) {
        buffer_handle_t memHandle = nullptr;
        status_t result = alloc.allocate(mWidth, mHeight, mFormat, 1, mUsage, &memHandle, &mStride,
                                         0, "EvsCamera");
        if (result != NO_ERROR) {
            ALOGE("Error %d allocating %d x %d graphics buffer", result, mWidth, mHeight);
            break;
        }
        if (!memHandle) {
            ALOGE("We didn't get a buffer handle back from the allocator");
            break;
        }

        // Find a place to store the new buffer
        bool stored = false;
        for (auto&& rec : mBuffers) {
            if (rec.handle == nullptr) {
                // Use this existing entry
                rec.handle = memHandle;
                rec.inUse = false;
                stored = true;
                break;
            }
        }
        if (!stored) {
            // Add a BufferRecord wrapping this handle to our set of available buffers
            mBuffers.push_back(std::move(BufferRecord(memHandle)));
        }

        mFramesAllowed++;
        added++;
    }

    return added;
}

unsigned EvsCamera::decreaseAvailableFrames_Locked(unsigned numToRemove) {
    // Acquire the graphics buffer allocator
    GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());

    unsigned removed = 0;

    for (auto&& rec : mBuffers) {
        // Is this record not in use, but holding a buffer that we can free?
        if ((rec.inUse == false) && (rec.handle != nullptr)) {
            // Release buffer and update the record so we can recognize it as "empty"
            alloc.free(rec.handle);
            rec.handle = nullptr;

            mFramesAllowed--;
            removed++;

            if (removed == numToRemove) {
                break;
            }
        }
    }

    return removed;
}

// This is the asynchronous frame generation thread that runs in parallel with the
// main serving thread.  There is one for each active camera instance.
void EvsCamera::generateFrames() {
    ALOGD("Frame generation loop started");

    unsigned idx;
    while (true) {
        bool timeForFrame = false;
        nsecs_t startTime = systemTime(SYSTEM_TIME_MONOTONIC);

        // Lock scope for updating shared state
        {
            std::lock_guard<std::mutex> lock(mAccessLock);

            if (mStreamState != RUNNING) {
                // Break out of our main thread loop
                break;
            }

            // Are we allowed to issue another buffer?
            if (mFramesInUse >= mFramesAllowed) {
                // Can't do anything right now -- skip this frame
                ALOGW("Skipped a frame because too many are in flight\n");
            } else {
                // Identify an available buffer to fill
                for (idx = 0; idx < mBuffers.size(); idx++) {
                    if (!mBuffers[idx].inUse) {
                        if (mBuffers[idx].handle != nullptr) {
                            // Found an available record, so stop looking
                            break;
                        }
                    }
                }
                if (idx >= mBuffers.size()) {
                    // This shouldn't happen since we already checked mFramesInUse vs mFramesAllowed
                    ALOGE("Failed to find an available buffer slot\n");
                } else {
                    // We're going to make the frame busy
                    mBuffers[idx].inUse = true;
                    mFramesInUse++;
                    timeForFrame = true;
                }
            }
        }

        if (timeForFrame) {
            // Assemble the buffer description we'll transmit below
            BufferDesc newBuffer = {};
            AHardwareBuffer_Desc* pDesc =
                    reinterpret_cast<AHardwareBuffer_Desc*>(&newBuffer.buffer.description);
            pDesc->width = mWidth;
            pDesc->height = mHeight;
            pDesc->layers = 1;
            pDesc->format = mFormat;
            pDesc->usage = mUsage;
            pDesc->stride = mStride;
            newBuffer.buffer.nativeHandle = mBuffers[idx].handle;
            newBuffer.pixelSize = sizeof(uint32_t);
            newBuffer.bufferId = idx;
            newBuffer.deviceId = mDescription.v1.cameraId;
            newBuffer.timestamp = elapsedRealtimeNano() * 1e+3;  // timestamps is in microseconds

            // Write test data into the image buffer
            fillTestFrame(newBuffer);

            // Issue the (asynchronous) callback to the client -- can't be holding the lock
            auto result = mStream->deliverFrame_1_1({newBuffer});
            if (result.isOk()) {
                ALOGD("Delivered %p as id %d", newBuffer.buffer.nativeHandle.getNativeHandle(),
                      newBuffer.bufferId);
            } else {
                // This can happen if the client dies and is likely unrecoverable.
                // To avoid consuming resources generating failing calls, we stop sending
                // frames.  Note, however, that the stream remains in the "STREAMING" state
                // until cleaned up on the main thread.
                ALOGE("Frame delivery call failed in the transport layer.");

                // Since we didn't actually deliver it, mark the frame as available
                std::lock_guard<std::mutex> lock(mAccessLock);
                mBuffers[idx].inUse = false;
                mFramesInUse--;

                break;
            }
        }

        // We arbitrarily choose to generate frames at 15 fps to ensure we pass the 10fps test
        // requirement
        static const int kTargetFrameRate = 15;
        static const nsecs_t kTargetFrameIntervalUs = 1000 * 1000 / kTargetFrameRate;
        const nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
        const nsecs_t elapsedTimeUs = (now - startTime) / 1000;
        const nsecs_t sleepDurationUs = kTargetFrameIntervalUs - elapsedTimeUs;
        if (sleepDurationUs > 0) {
            usleep(sleepDurationUs);
        }
    }

    // If we've been asked to stop, send an event to signal the actual end of stream
    EvsEventDesc event = {
            .aType = EvsEventType::STREAM_STOPPED,
    };
    if (!mStream->notify(event).isOk()) {
        ALOGE("Error delivering end of stream marker");
    }

    return;
}

void EvsCamera::fillTestFrame(const BufferDesc& buff) {
    // Lock our output buffer for writing
    uint32_t* pixels = nullptr;
    const AHardwareBuffer_Desc* pDesc =
            reinterpret_cast<const AHardwareBuffer_Desc*>(&buff.buffer.description);
    GraphicBufferMapper& mapper = GraphicBufferMapper::get();
    mapper.lock(buff.buffer.nativeHandle,
                GRALLOC_USAGE_SW_WRITE_OFTEN | GRALLOC_USAGE_SW_READ_NEVER,
                android::Rect(pDesc->width, pDesc->height), (void**)&pixels);

    // If we failed to lock the pixel buffer, we're about to crash, but log it first
    if (!pixels) {
        ALOGE("Camera failed to gain access to image buffer for writing");
        return;
    }

    // Fill in the test pixels; the colorbar in ABGR format
    for (unsigned row = 0; row < pDesc->height; row++) {
        for (unsigned col = 0; col < pDesc->width; col++) {
            const uint32_t index = col * kNumColors / pDesc->width;
            pixels[col] = kColors[index];
        }
        // Point to the next row
        // NOTE:  stride retrieved from gralloc is in units of pixels
        pixels = pixels + pDesc->stride;
    }

    // Release our output buffer
    mapper.unlock(buff.buffer.nativeHandle);
}

void EvsCamera::fillTestFrame(const V1_0::BufferDesc& buff) {
    BufferDesc newBuffer = {
            .buffer.nativeHandle = buff.memHandle,
            .pixelSize = buff.pixelSize,
            .bufferId = buff.bufferId,
    };
    AHardwareBuffer_Desc* pDesc =
            reinterpret_cast<AHardwareBuffer_Desc*>(&newBuffer.buffer.description);
    *pDesc = {
            buff.width,   // width
            buff.height,  // height
            1,            // layers, always 1 for EVS
            buff.format,  // One of AHardwareBuffer_Format
            buff.usage,   // Combination of AHardwareBuffer_UsageFlags
            buff.stride,  // Row stride in pixels
            0,            // Reserved
            0             // Reserved
    };
    return fillTestFrame(newBuffer);
}

void EvsCamera::returnBufferLocked(const uint32_t bufferId, const buffer_handle_t memHandle) {
    if (memHandle == nullptr) {
        ALOGE("ignoring doneWithFrame called with null handle");
    } else if (bufferId >= mBuffers.size()) {
        ALOGE("ignoring doneWithFrame called with invalid bufferId %d (max is %zu)", bufferId,
              mBuffers.size() - 1);
    } else if (!mBuffers[bufferId].inUse) {
        ALOGE("ignoring doneWithFrame called on frame %d which is already free", bufferId);
    } else {
        // Mark the frame as available
        mBuffers[bufferId].inUse = false;
        mFramesInUse--;

        // If this frame's index is high in the array, try to move it down
        // to improve locality after mFramesAllowed has been reduced.
        if (bufferId >= mFramesAllowed) {
            // Find an empty slot lower in the array (which should always exist in this case)
            for (auto&& rec : mBuffers) {
                if (rec.handle == nullptr) {
                    rec.handle = mBuffers[bufferId].handle;
                    mBuffers[bufferId].handle = nullptr;
                    break;
                }
            }
        }
    }
}

sp<EvsCamera> EvsCamera::Create(const char* deviceName) {
    std::unique_ptr<ConfigManager::CameraInfo> nullCamInfo = nullptr;

    return Create(deviceName, nullCamInfo);
}

sp<EvsCamera> EvsCamera::Create(const char* deviceName,
                                std::unique_ptr<ConfigManager::CameraInfo>& camInfo,
                                [[maybe_unused]] const Stream* streamCfg) {
    sp<EvsCamera> evsCamera = new EvsCamera(deviceName, camInfo);
    if (evsCamera == nullptr) {
        return nullptr;
    }

    // Use the first resolution from the list for the testing
    // TODO(b/214835237): Uses a given Stream configuration to choose the best
    // stream configuration.
    auto it = camInfo->streamConfigurations.begin();
    evsCamera->mWidth = it->second[1];
    evsCamera->mHeight = it->second[2];
    evsCamera->mDescription.v1.vendorFlags = 0xFFFFFFFF;  // Arbitrary test value

    evsCamera->mFormat = HAL_PIXEL_FORMAT_RGBA_8888;
    evsCamera->mUsage = GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_CAMERA_WRITE |
                        GRALLOC_USAGE_SW_READ_RARELY | GRALLOC_USAGE_SW_WRITE_RARELY;

    return evsCamera;
}

}  // namespace android::hardware::automotive::evs::V1_1::implementation