Home | History | Annotate | Download | only in media
      1 /*
      2  * Copyright (C) 2008 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 #ifndef JETPLAYER_H_
     18 #define JETPLAYER_H_
     19 
     20 #include <utils/threads.h>
     21 
     22 #include <libsonivox/jet.h>
     23 #include <libsonivox/eas_types.h>
     24 #include <media/AudioTrack.h>
     25 
     26 
     27 namespace android {
     28 
     29 typedef void (*jetevent_callback)(int eventType, int val1, int val2, void *cookie);
     30 
     31 class JetPlayer {
     32 
     33 public:
     34 
     35     // to keep in sync with the JetPlayer class constants
     36     // defined in frameworks/base/media/java/android/media/JetPlayer.java
     37     static const int JET_EVENT                   = 1;
     38     static const int JET_USERID_UPDATE           = 2;
     39     static const int JET_NUMQUEUEDSEGMENT_UPDATE = 3;
     40     static const int JET_PAUSE_UPDATE            = 4;
     41 
     42     JetPlayer(void *javaJetPlayer,
     43             int maxTracks = 32,
     44             int trackBufferSize = 1200);
     45     ~JetPlayer();
     46     int init();
     47     int release();
     48 
     49     int loadFromFile(const char* url);
     50     int loadFromFD(const int fd, const long long offset, const long long length);
     51     int closeFile();
     52     int play();
     53     int pause();
     54     int queueSegment(int segmentNum, int libNum, int repeatCount, int transpose,
     55             EAS_U32 muteFlags, EAS_U8 userID);
     56     int setMuteFlags(EAS_U32 muteFlags, bool sync);
     57     int setMuteFlag(int trackNum, bool muteFlag, bool sync);
     58     int triggerClip(int clipId);
     59     int clearQueue();
     60 
     61     void setEventCallback(jetevent_callback callback);
     62 
     63     int getMaxTracks() { return mMaxTracks; };
     64 
     65 
     66 private:
     67     int                 render();
     68     void                fireUpdateOnStatusChange();
     69     void                fireEventsFromJetQueue();
     70 
     71     JetPlayer() {} // no default constructor
     72     void dump();
     73     void dumpJetStatus(S_JET_STATUS* pJetStatus);
     74 
     75     jetevent_callback   mEventCallback;
     76 
     77     void*               mJavaJetPlayerRef;
     78     Mutex               mMutex; // mutex to sync the render and playback thread with the JET calls
     79     pid_t               mTid;
     80     Condition           mCondition;
     81     volatile bool       mRender;
     82     bool                mPaused;
     83 
     84     EAS_STATE           mState;
     85     int*                mMemFailedVar;
     86 
     87     int                 mMaxTracks; // max number of MIDI tracks, usually 32
     88     EAS_DATA_HANDLE     mEasData;
     89     EAS_FILE_LOCATOR    mEasJetFileLoc;
     90     EAS_PCM*            mAudioBuffer;// EAS renders the MIDI data into this buffer,
     91     AudioTrack*         mAudioTrack; // and we play it in this audio track
     92     int                 mTrackBufferSize;
     93     S_JET_STATUS        mJetStatus;
     94     S_JET_STATUS        mPreviousJetStatus;
     95 
     96     char                mJetFilePath[PATH_MAX];
     97 
     98     class JetPlayerThread : public Thread {
     99     public:
    100         JetPlayerThread(JetPlayer *player) : mPlayer(player) {
    101         }
    102 
    103     protected:
    104         virtual ~JetPlayerThread() {}
    105 
    106     private:
    107         JetPlayer *mPlayer;
    108 
    109         bool threadLoop() {
    110             int result;
    111             result = mPlayer->render();
    112             return false;
    113         }
    114 
    115         JetPlayerThread(const JetPlayerThread &);
    116         JetPlayerThread &operator=(const JetPlayerThread &);
    117     };
    118 
    119     sp<JetPlayerThread> mThread;
    120 
    121 }; // end class JetPlayer
    122 
    123 } // end namespace android
    124 
    125 
    126 
    127 #endif /*JETPLAYER_H_*/
    128