Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bf620125 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Automerger Merge Worker
Browse files

Merge "NdkMediaCodec: fix onFrameRendered message parsing" into tm-qpr-dev am:...

Merge "NdkMediaCodec: fix onFrameRendered message parsing" into tm-qpr-dev am: 8c4e619c am: f6bc4970

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/av/+/19437098



Change-Id: I7f221a3a4b7ad00e16eedc7f5f7d0c775efdc194
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents 912a06b1 f6bc4970
Loading
Loading
Loading
Loading
+52 −19
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@
 * limitations under the License.
 */

#include <charconv>
#include <inttypes.h>
#include <mutex>
#include <set>
@@ -324,29 +325,61 @@ void CodecHandler::onMessageReceived(const sp<AMessage> &msg) {
            }

            AMessage::Type type;
            int64_t mediaTimeUs, systemNano;
            size_t index = 0;

            // TODO. This code has dependency with MediaCodec::CreateFramesRenderedMessage.
            for (size_t ix = 0; ix < data->countEntries(); ix++) {
                AString name = data->getEntryNameAt(ix, &type);
                if (name.startsWith(AStringPrintf("%zu-media-time-us", index).c_str())) {
                    AMessage::ItemData data = msg->getEntryAt(index);
                    data.find(&mediaTimeUs);
                } else if (name.startsWith(AStringPrintf("%zu-system-nano", index).c_str())) {
                    AMessage::ItemData data = msg->getEntryAt(index);
                    data.find(&systemNano);
            size_t n = data->countEntries();

            thread_local std::vector<std::optional<int64_t>> mediaTimesInUs;
            thread_local std::vector<std::optional<int64_t>> systemTimesInNs;
            mediaTimesInUs.resize(n);
            systemTimesInNs.resize(n);
            std::fill_n(mediaTimesInUs.begin(), n, std::nullopt);
            std::fill_n(systemTimesInNs.begin(), n, std::nullopt);
            for (size_t i = 0; i < n; i++) {
                AString name = data->getEntryNameAt(i, &type);
                if (name.endsWith("-media-time-us")) {
                    int64_t mediaTimeUs;
                    AMessage::ItemData itemData = data->getEntryAt(i);
                    itemData.find(&mediaTimeUs);

                    int index = -1;
                    std::from_chars_result result = std::from_chars(
                            name.c_str(), name.c_str() + name.find("-"), index);
                    if (result.ec == std::errc() && 0 <= index && index < n) {
                        mediaTimesInUs[index] = mediaTimeUs;
                    } else {
                        std::error_code ec = std::make_error_code(result.ec);
                        ALOGE("Unexpected media time index: #%d with value %lldus (err=%d %s)",
                              index, (long long)mediaTimeUs, ec.value(), ec.message().c_str());
                    }
                } else if (name.endsWith("-system-nano")) {
                    int64_t systemNano;
                    AMessage::ItemData itemData = data->getEntryAt(i);
                    itemData.find(&systemNano);

                    int index = -1;
                    std::from_chars_result result = std::from_chars(
                            name.c_str(), name.c_str() + name.find("-"), index);
                    if (result.ec == std::errc() && 0 <= index && index < n) {
                        systemTimesInNs[index] = systemNano;
                    } else {
                        std::error_code ec = std::make_error_code(result.ec);
                        ALOGE("Unexpected system time index: #%d with value %lldns (err=%d %s)",
                              index, (long long)systemNano, ec.value(), ec.message().c_str());
                    }
                }
            }

            Mutex::Autolock _l(mCodec->mFrameRenderedCallbackLock);
            if (mCodec->mFrameRenderedCallback != NULL) {
                for (size_t i = 0; i < n; ++i) {
                    if (mediaTimesInUs[i] && systemTimesInNs[i]) {
                        mCodec->mFrameRenderedCallback(
                                mCodec,
                                mCodec->mFrameRenderedCallbackUserData,
                                mediaTimeUs,
                                systemNano);
                                mediaTimesInUs[i].value(),
                                systemTimesInNs[i].value());
                    } else {
                        break;
                    }

                    index++;
                }
            }
            break;