Home | History | Annotate | Download | only in foundation
      1 /*
      2  * Copyright (C) 2018 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 // #define LOG_NDEBUG 0
     18 #define LOG_TAG "AudioPresentationInfo"
     19 
     20 #include <vector>
     21 
     22 #include "AudioPresentationInfo.h"
     23 
     24 #include <utils/Log.h>
     25 
     26 namespace android {
     27 
     28 void serializeAudioPresentations(const AudioPresentationCollection& presentations,
     29         std::ostream* serializedOutput) {
     30     uint32_t numPresentations = presentations.size();
     31     serializedOutput->write(reinterpret_cast<char*>(&numPresentations), sizeof(numPresentations));
     32     for (const auto& ap : presentations) {
     33         if (ap.mVersion == PRESENTATION_VERSION_1) {
     34             serializedOutput->write(
     35                     const_cast<char*>(reinterpret_cast<const char*>(&ap.mVersion)),
     36                     sizeof(ap.mVersion));
     37             serializedOutput->write(
     38                     const_cast<char*>(reinterpret_cast<const char*>(&ap.mPresentationId)),
     39                     sizeof(ap.mPresentationId));
     40             serializedOutput->write(
     41                     const_cast<char*>(reinterpret_cast<const char*>(&ap.mProgramId)),
     42                     sizeof(ap.mProgramId));
     43 
     44             uint32_t numLabels = ap.mLabels.size();
     45             serializedOutput->write(
     46                     const_cast<char*>(reinterpret_cast<const char*>(&numLabels)),
     47                     sizeof(numLabels));
     48             for (const auto& label : ap.mLabels) {
     49                 uint32_t labelKeySize = label.first.size();
     50                 serializedOutput->write(
     51                         const_cast<char*>(reinterpret_cast<const char*>(&labelKeySize)),
     52                         sizeof(labelKeySize));
     53                 serializedOutput->write(label.first.c_str(), labelKeySize);
     54 
     55                 uint32_t labelValSize = label.second.size();
     56                 serializedOutput->write(
     57                         const_cast<char*>(reinterpret_cast<const char*>(&labelValSize)),
     58                         sizeof(labelValSize));
     59                 serializedOutput->write(label.second.c_str(), labelValSize);
     60             }
     61 
     62             uint32_t langSize = ap.mLanguage.size();
     63             serializedOutput->write(
     64                     const_cast<char*>(reinterpret_cast<const char*>(&langSize)),
     65                     sizeof(langSize));
     66             serializedOutput->write(ap.mLanguage.c_str(), langSize);
     67 
     68             serializedOutput->write(
     69                     const_cast<char*>(reinterpret_cast<const char*>(&ap.mMasteringIndication)),
     70                     sizeof(ap.mMasteringIndication));
     71             serializedOutput->write(
     72                     const_cast<char*>(reinterpret_cast<const char*>(&ap.mAudioDescriptionAvailable)),
     73                     sizeof(ap.mAudioDescriptionAvailable));
     74             serializedOutput->write(
     75                     const_cast<char*>(reinterpret_cast<const char*>(&ap.mSpokenSubtitlesAvailable)),
     76                     sizeof(ap.mSpokenSubtitlesAvailable));
     77             serializedOutput->write(
     78                     const_cast<char*>(reinterpret_cast<const char*>(&ap.mDialogueEnhancementAvailable)),
     79                     sizeof(ap.mDialogueEnhancementAvailable));
     80         }
     81     }
     82 }
     83 
     84 status_t deserializeAudioPresentations(std::istream* serializedInput,
     85         AudioPresentationCollection *presentations) {
     86     uint32_t numPresentations;
     87     serializedInput->read(reinterpret_cast<char*>(&numPresentations), sizeof(numPresentations));
     88     for (uint32_t i = 0; i < numPresentations; ++i) {
     89         uint32_t version;
     90         serializedInput->read(reinterpret_cast<char*>(&version), sizeof(version));
     91         if (version == PRESENTATION_VERSION_1) {
     92             AudioPresentationV1 ap;
     93             serializedInput->read(
     94                     reinterpret_cast<char*>(&ap.mPresentationId),
     95                     sizeof(ap.mPresentationId));
     96             serializedInput->read(reinterpret_cast<char*>(&ap.mProgramId), sizeof(ap.mProgramId));
     97 
     98             uint32_t numLabels;
     99             serializedInput->read(reinterpret_cast<char*>(&numLabels), sizeof(numLabels));
    100             for (uint32_t j = 0; j < numLabels; ++j) {
    101                 uint32_t labelKeySize;
    102                 serializedInput->read(reinterpret_cast<char*>(&labelKeySize), sizeof(labelKeySize));
    103                 std::vector<char> labelKey(labelKeySize);
    104                 serializedInput->read(labelKey.data(), labelKeySize);
    105 
    106                 uint32_t labelValSize;
    107                 serializedInput->read(reinterpret_cast<char*>(&labelValSize), sizeof(labelValSize));
    108                 std::vector<char> labelVal(labelValSize);
    109                 serializedInput->read(labelVal.data(), labelValSize);
    110                 ap.mLabels.emplace(
    111                         std::string(reinterpret_cast<char*>(labelKey.data()), labelKeySize),
    112                         std::string(reinterpret_cast<char*>(labelVal.data()), labelValSize));
    113             }
    114             uint32_t languageSize;
    115             serializedInput->read(reinterpret_cast<char*>(&languageSize), sizeof(languageSize));
    116             std::vector<char> language(languageSize);
    117             serializedInput->read(language.data(), languageSize);
    118             ap.mLanguage = std::string(reinterpret_cast<char*>(language.data()), languageSize);
    119             serializedInput->read(reinterpret_cast<char*>(&ap.mMasteringIndication),
    120                                  sizeof(ap.mMasteringIndication));
    121             serializedInput->read(reinterpret_cast<char*>(&ap.mAudioDescriptionAvailable),
    122                                  sizeof(ap.mAudioDescriptionAvailable));
    123             serializedInput->read(reinterpret_cast<char*>(&ap.mSpokenSubtitlesAvailable),
    124                                  sizeof(ap.mSpokenSubtitlesAvailable));
    125             serializedInput->read(reinterpret_cast<char*>(&ap.mDialogueEnhancementAvailable),
    126                                  sizeof(ap.mDialogueEnhancementAvailable));
    127             presentations->push_back(std::move(ap));
    128         } else {
    129             ALOGE("Audio presentation info version is not supported");
    130             return INVALID_OPERATION;
    131         }
    132     }
    133     return OK;
    134 }
    135 
    136 }  // namespace android
    137