Home | History | Annotate | Download | only in libstagefright
      1 /*
      2  * Copyright (C) 2010 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 "AHierarchicalStateMachine"
     19 #include <utils/Log.h>
     20 
     21 #include <media/stagefright/AHierarchicalStateMachine.h>
     22 
     23 #include <media/stagefright/foundation/ADebug.h>
     24 #include <media/stagefright/foundation/AMessage.h>
     25 #include <utils/Vector.h>
     26 
     27 namespace android {
     28 
     29 AState::AState(const sp<AState> &parentState)
     30     : mParentState(parentState) {
     31 }
     32 
     33 AState::~AState() {
     34 }
     35 
     36 sp<AState> AState::parentState() {
     37     return mParentState;
     38 }
     39 
     40 void AState::stateEntered() {
     41 }
     42 
     43 void AState::stateExited() {
     44 }
     45 
     46 ////////////////////////////////////////////////////////////////////////////////
     47 
     48 AHierarchicalStateMachine::AHierarchicalStateMachine() {
     49 }
     50 
     51 AHierarchicalStateMachine::~AHierarchicalStateMachine() {
     52 }
     53 
     54 void AHierarchicalStateMachine::handleMessage(const sp<AMessage> &msg) {
     55     sp<AState> save = mState;
     56 
     57     sp<AState> cur = mState;
     58     while (cur != NULL && !cur->onMessageReceived(msg)) {
     59         // If you claim not to have handled the message you shouldn't
     60         // have called setState...
     61         CHECK(save == mState);
     62 
     63         cur = cur->parentState();
     64     }
     65 
     66     if (cur != NULL) {
     67         return;
     68     }
     69 
     70     ALOGW("Warning message %s unhandled in root state.",
     71          msg->debugString().c_str());
     72 }
     73 
     74 void AHierarchicalStateMachine::changeState(const sp<AState> &state) {
     75     if (state == mState) {
     76         // Quick exit for the easy case.
     77         return;
     78     }
     79 
     80     Vector<sp<AState> > A;
     81     sp<AState> cur = mState;
     82     for (;;) {
     83         A.push(cur);
     84         if (cur == NULL) {
     85             break;
     86         }
     87         cur = cur->parentState();
     88     }
     89 
     90     Vector<sp<AState> > B;
     91     cur = state;
     92     for (;;) {
     93         B.push(cur);
     94         if (cur == NULL) {
     95             break;
     96         }
     97         cur = cur->parentState();
     98     }
     99 
    100     // Remove the common tail.
    101     while (A.size() > 0 && B.size() > 0 && A.top() == B.top()) {
    102         A.pop();
    103         B.pop();
    104     }
    105 
    106     mState = state;
    107 
    108     for (size_t i = 0; i < A.size(); ++i) {
    109         A.editItemAt(i)->stateExited();
    110     }
    111 
    112     for (size_t i = B.size(); i > 0;) {
    113         i--;
    114         B.editItemAt(i)->stateEntered();
    115     }
    116 }
    117 
    118 }  // namespace android
    119