/**
 * Copyright (C) 2022 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 <nuplayer/NuPlayerStreamListener.h>
#include <stdlib.h>

#include "../includes/common.h"

const size_t kBufferSize = 1024;

using namespace android;

bool isTestInProgress = false;

struct sigaction new_action, old_action;

void sigabrt_handler(int signum, siginfo_t *info, void *context) {
    if (isTestInProgress && info->si_signo == SIGABRT) {
        (*old_action.sa_sigaction)(signum, info, context);
        return;
    }
    exit(EXIT_FAILURE);
}

class StreamSource : public IStreamSource {
public:
    void setListener(const sp<IStreamListener> &listener __attribute__((unused))) {}
    void setBuffers(const Vector<sp<IMemory>> &buffers __attribute__((unused))) {}
    void onBufferAvailable(size_t index __attribute__((unused))) {}

protected:
    IBinder *onAsBinder() { return (IBinder *)malloc(kBufferSize); }
};

int main() {
    sigemptyset(&new_action.sa_mask);
    new_action.sa_flags = SA_SIGINFO;
    new_action.sa_sigaction = sigabrt_handler;
    sigaction(SIGABRT, &new_action, &old_action);

    const sp<StreamSource> source = new StreamSource();
    FAIL_CHECK(source != nullptr);
    isTestInProgress = true;
    sp<NuPlayer::NuPlayerStreamListener> listener =
            new NuPlayer::NuPlayerStreamListener(source, nullptr);
    isTestInProgress = false;
    return EXIT_SUCCESS;
}