1134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber/*
2134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber * Copyright (C) 2009 The Android Open Source Project
3134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber *
4134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber * Licensed under the Apache License, Version 2.0 (the "License");
5134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber * you may not use this file except in compliance with the License.
6134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber * You may obtain a copy of the License at
7134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber *
8134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber *      http://www.apache.org/licenses/LICENSE-2.0
9134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber *
10134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber * Unless required by applicable law or agreed to in writing, software
11134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber * distributed under the License is distributed on an "AS IS" BASIS,
12134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber * See the License for the specific language governing permissions and
14134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber * limitations under the License.
15134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber */
16134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber
17134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber//#define LOG_NDEBUG 0
18134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber#define LOG_TAG "OMXHarness"
19377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT#include <inttypes.h>
20134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber#include <utils/Log.h>
21134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber
22134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber#include "OMXHarness.h"
23134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber
24134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber#include <sys/time.h>
25134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber
26134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber#include <binder/ProcessState.h>
27134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber#include <binder/IServiceManager.h>
28134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber#include <binder/MemoryDealer.h>
291b86fe063badb5f28c467ade39be0f4008688947Andreas Huber#include <media/IMediaHTTPService.h>
301900e77bac4276f247f80fd06d19316cac598f57Marco Nelissen#include <media/IMediaCodecService.h>
31f1d5aa162c02a16b7195a43a9bcea4d592600ac4James Dong#include <media/stagefright/foundation/ADebug.h>
32af5dd7753e62353411cf0daf3b513c38818e9662Andreas Huber#include <media/stagefright/foundation/ALooper.h>
33134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber#include <media/stagefright/DataSource.h>
34134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber#include <media/stagefright/MediaBuffer.h>
35bbba88cb1bdc34705d1477208990a06904c022e7Andreas Huber#include <media/stagefright/MediaDefs.h>
36134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber#include <media/stagefright/MediaErrors.h>
37134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber#include <media/stagefright/MediaExtractor.h>
38134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber#include <media/stagefright/MediaSource.h>
39134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber#include <media/stagefright/MetaData.h>
40708336fec4e4620dafa32c4b4b2dc3230a120961Lajos Molnar#include <media/stagefright/SimpleDecodingSource.h>
413fd200feb657c157125e45e30c2a7262e3c0244dChong Zhang#include <media/OMXBuffer.h>
429c47c97ecac581d66b6febafd156618247e86742Pawin Vongmasa#include <android/hardware/media/omx/1.0/IOmx.h>
4304563aafa0debc95d50951ca944abf37ef2777cePawin Vongmasa#include <media/omx/1.0/WOmx.h>
44134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber
45134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber#define DEFAULT_TIMEOUT         500000
46134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber
47134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Hubernamespace android {
48134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber
49d59b97223424a3974d2ac31cff998d02eecf2eedChong Zhang/////////////////////////////////////////////////////////////////////
50d59b97223424a3974d2ac31cff998d02eecf2eedChong Zhang
51d59b97223424a3974d2ac31cff998d02eecf2eedChong Zhangstruct Harness::CodecObserver : public BnOMXObserver {
52d59b97223424a3974d2ac31cff998d02eecf2eedChong Zhang    CodecObserver(const sp<Harness> &harness, int32_t gen)
53d59b97223424a3974d2ac31cff998d02eecf2eedChong Zhang            : mHarness(harness), mGeneration(gen) {}
54d59b97223424a3974d2ac31cff998d02eecf2eedChong Zhang
55d59b97223424a3974d2ac31cff998d02eecf2eedChong Zhang    void onMessages(const std::list<omx_message> &messages) override;
56d59b97223424a3974d2ac31cff998d02eecf2eedChong Zhang
57d59b97223424a3974d2ac31cff998d02eecf2eedChong Zhangprivate:
58d59b97223424a3974d2ac31cff998d02eecf2eedChong Zhang    sp<Harness> mHarness;
59d59b97223424a3974d2ac31cff998d02eecf2eedChong Zhang    int32_t mGeneration;
60d59b97223424a3974d2ac31cff998d02eecf2eedChong Zhang};
61d59b97223424a3974d2ac31cff998d02eecf2eedChong Zhang
62d59b97223424a3974d2ac31cff998d02eecf2eedChong Zhangvoid Harness::CodecObserver::onMessages(const std::list<omx_message> &messages) {
63d59b97223424a3974d2ac31cff998d02eecf2eedChong Zhang    mHarness->handleMessages(mGeneration, messages);
64d59b97223424a3974d2ac31cff998d02eecf2eedChong Zhang}
65d59b97223424a3974d2ac31cff998d02eecf2eedChong Zhang
66d59b97223424a3974d2ac31cff998d02eecf2eedChong Zhang/////////////////////////////////////////////////////////////////////
67d59b97223424a3974d2ac31cff998d02eecf2eedChong Zhang
68134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas HuberHarness::Harness()
699c47c97ecac581d66b6febafd156618247e86742Pawin Vongmasa    : mInitCheck(NO_INIT), mUseTreble(false) {
70134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber    mInitCheck = initOMX();
71134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber}
72134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber
73134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas HuberHarness::~Harness() {
74134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber}
75134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber
76134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huberstatus_t Harness::initCheck() const {
77134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber    return mInitCheck;
78134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber}
79134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber
80134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huberstatus_t Harness::initOMX() {
81d4e9ca49a3863beeb0027f51e2331e4c5ec2402bPawin Vongmasa    if (property_get_bool("persist.media.treble_omx", true)) {
829c47c97ecac581d66b6febafd156618247e86742Pawin Vongmasa        using namespace ::android::hardware::media::omx::V1_0;
839c47c97ecac581d66b6febafd156618247e86742Pawin Vongmasa        sp<IOmx> tOmx = IOmx::getService();
849c47c97ecac581d66b6febafd156618247e86742Pawin Vongmasa        if (tOmx == nullptr) {
859c47c97ecac581d66b6febafd156618247e86742Pawin Vongmasa            return NO_INIT;
869c47c97ecac581d66b6febafd156618247e86742Pawin Vongmasa        }
879c47c97ecac581d66b6febafd156618247e86742Pawin Vongmasa        mOMX = new utils::LWOmx(tOmx);
889c47c97ecac581d66b6febafd156618247e86742Pawin Vongmasa        mUseTreble = true;
899c47c97ecac581d66b6febafd156618247e86742Pawin Vongmasa    } else {
909c47c97ecac581d66b6febafd156618247e86742Pawin Vongmasa        sp<IServiceManager> sm = defaultServiceManager();
919c47c97ecac581d66b6febafd156618247e86742Pawin Vongmasa        sp<IBinder> binder = sm->getService(String16("media.codec"));
929c47c97ecac581d66b6febafd156618247e86742Pawin Vongmasa        sp<IMediaCodecService> service = interface_cast<IMediaCodecService>(binder);
939c47c97ecac581d66b6febafd156618247e86742Pawin Vongmasa        mOMX = service->getOMX();
949c47c97ecac581d66b6febafd156618247e86742Pawin Vongmasa        mUseTreble = false;
959c47c97ecac581d66b6febafd156618247e86742Pawin Vongmasa    }
96134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber
97134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber    return mOMX != 0 ? OK : NO_INIT;
98134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber}
99134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber
100d59b97223424a3974d2ac31cff998d02eecf2eedChong Zhangvoid Harness::handleMessages(int32_t gen, const std::list<omx_message> &messages) {
101134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber    Mutex::Autolock autoLock(mLock);
10226a48f304a8754d655e554178ffb6d7ba4c5aac3Lajos Molnar    for (std::list<omx_message>::const_iterator it = messages.cbegin(); it != messages.cend(); ) {
10326a48f304a8754d655e554178ffb6d7ba4c5aac3Lajos Molnar        mMessageQueue.push_back(*it++);
104d59b97223424a3974d2ac31cff998d02eecf2eedChong Zhang        mLastMsgGeneration = gen;
10526a48f304a8754d655e554178ffb6d7ba4c5aac3Lajos Molnar    }
106134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber    mMessageAddedCondition.signal();
107134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber}
108134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber
109d59b97223424a3974d2ac31cff998d02eecf2eedChong Zhangstatus_t Harness::dequeueMessageForNode(omx_message *msg, int64_t timeoutUs) {
110d59b97223424a3974d2ac31cff998d02eecf2eedChong Zhang    return dequeueMessageForNodeIgnoringBuffers(NULL, NULL, msg, timeoutUs);
111134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber}
112134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber
113134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber// static
114134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huberbool Harness::handleBufferMessage(
115134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber        const omx_message &msg,
116134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber        Vector<Buffer> *inputBuffers,
117134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber        Vector<Buffer> *outputBuffers) {
118134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber    switch (msg.type) {
119134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber        case omx_message::EMPTY_BUFFER_DONE:
120134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber        {
121134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber            if (inputBuffers) {
122134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber                for (size_t i = 0; i < inputBuffers->size(); ++i) {
123134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber                    if ((*inputBuffers)[i].mID == msg.u.buffer_data.buffer) {
124134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber                        inputBuffers->editItemAt(i).mFlags &= ~kBufferBusy;
125134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber                        return true;
126134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber                    }
127134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber                }
128134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber                CHECK(!"should not be here");
129134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber            }
130134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber            break;
131134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber        }
132134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber
133134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber        case omx_message::FILL_BUFFER_DONE:
134134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber        {
135134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber            if (outputBuffers) {
136134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber                for (size_t i = 0; i < outputBuffers->size(); ++i) {
137134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber                    if ((*outputBuffers)[i].mID == msg.u.buffer_data.buffer) {
138134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber                        outputBuffers->editItemAt(i).mFlags &= ~kBufferBusy;
139134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber                        return true;
140134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber                    }
141134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber                }
142134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber                CHECK(!"should not be here");
143134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber            }
144134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber            break;
145134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber        }
146134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber
147134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber        default:
148134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber            break;
149134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber    }
150134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber
151134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber    return false;
152134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber}
153134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber
154134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huberstatus_t Harness::dequeueMessageForNodeIgnoringBuffers(
155134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber        Vector<Buffer> *inputBuffers,
156134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber        Vector<Buffer> *outputBuffers,
157134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber        omx_message *msg, int64_t timeoutUs) {
158af5dd7753e62353411cf0daf3b513c38818e9662Andreas Huber    int64_t finishBy = ALooper::GetNowUs() + timeoutUs;
159134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber
160134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber    for (;;) {
161134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber        Mutex::Autolock autoLock(mLock);
162d59b97223424a3974d2ac31cff998d02eecf2eedChong Zhang        // Messages are queued in batches, if the last batch queued is
163d59b97223424a3974d2ac31cff998d02eecf2eedChong Zhang        // from a node that already expired, discard those messages.
164d59b97223424a3974d2ac31cff998d02eecf2eedChong Zhang        if (mLastMsgGeneration < mCurGeneration) {
165d59b97223424a3974d2ac31cff998d02eecf2eedChong Zhang            mMessageQueue.clear();
166d59b97223424a3974d2ac31cff998d02eecf2eedChong Zhang        }
167