AHierarchicalStateMachine.cpp revision f933441648ef6a71dee783d733aac17b9508b452
1f933441648ef6a71dee783d733aac17b9508b452Andreas Huber#include <media/stagefright/foundation/AHierarchicalStateMachine.h>
2f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3f933441648ef6a71dee783d733aac17b9508b452Andreas Huber#include <media/stagefright/foundation/ADebug.h>
4f933441648ef6a71dee783d733aac17b9508b452Andreas Huber#include <media/stagefright/foundation/AMessage.h>
5f933441648ef6a71dee783d733aac17b9508b452Andreas Huber#include <utils/Vector.h>
6f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
7f933441648ef6a71dee783d733aac17b9508b452Andreas Hubernamespace android {
8f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
9f933441648ef6a71dee783d733aac17b9508b452Andreas HuberAState::AState(const sp<AState> &parentState)
10f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    : mParentState(parentState) {
11f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
12f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
13f933441648ef6a71dee783d733aac17b9508b452Andreas HuberAState::~AState() {
14f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
15f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
16f933441648ef6a71dee783d733aac17b9508b452Andreas Hubersp<AState> AState::parentState() {
17f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    return mParentState;
18f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
19f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
20f933441648ef6a71dee783d733aac17b9508b452Andreas Hubervoid AState::stateEntered() {
21f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
22f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
23f933441648ef6a71dee783d733aac17b9508b452Andreas Hubervoid AState::stateExited() {
24f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
25f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
26f933441648ef6a71dee783d733aac17b9508b452Andreas Huber////////////////////////////////////////////////////////////////////////////////
27f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
28f933441648ef6a71dee783d733aac17b9508b452Andreas HuberAHierarchicalStateMachine::AHierarchicalStateMachine() {
29f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
30f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
31f933441648ef6a71dee783d733aac17b9508b452Andreas HuberAHierarchicalStateMachine::~AHierarchicalStateMachine() {
32f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
33f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
34f933441648ef6a71dee783d733aac17b9508b452Andreas Hubervoid AHierarchicalStateMachine::onMessageReceived(const sp<AMessage> &msg) {
35f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    sp<AState> save = mState;
36f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
37f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    sp<AState> cur = mState;
38f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    while (cur != NULL && !cur->onMessageReceived(msg)) {
39f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        // If you claim not to have handled the message you shouldn't
40f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        // have called setState...
41f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        CHECK(save == mState);
42f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
43f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        cur = cur->parentState();
44f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
45f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
46f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    if (cur != NULL) {
47f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        return;
48f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
49f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
50f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    LOGW("Warning message %s unhandled in root state.",
51f933441648ef6a71dee783d733aac17b9508b452Andreas Huber         msg->debugString().c_str());
52f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
53f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
54f933441648ef6a71dee783d733aac17b9508b452Andreas Hubervoid AHierarchicalStateMachine::changeState(const sp<AState> &state) {
55f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    if (state == mState) {
56f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        // Quick exit for the easy case.
57f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        return;
58f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
59f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
60f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    Vector<sp<AState> > A;
61f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    sp<AState> cur = mState;
62f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    for (;;) {
63f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        A.push(cur);
64f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        if (cur == NULL) {
65f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            break;
66f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
67f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        cur = cur->parentState();
68f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
69f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
70f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    Vector<sp<AState> > B;
71f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    cur = state;
72f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    for (;;) {
73f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        B.push(cur);
74f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        if (cur == NULL) {
75f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            break;
76f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
77f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        cur = cur->parentState();
78f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
79f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
80f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    // Remove the common tail.
81f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    while (A.size() > 0 && B.size() > 0 && A.top() == B.top()) {
82f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        A.pop();
83f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        B.pop();
84f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
85f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
86f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    mState = state;
87f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
88f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    for (size_t i = 0; i < A.size(); ++i) {
89f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        A.editItemAt(i)->stateExited();
90f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
91f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
92f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    for (size_t i = B.size(); i-- > 0;) {
93f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        B.editItemAt(i)->stateEntered();
94f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
95f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
96f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
97f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}  // namespace android
98