Home | History | Annotate | Download | only in android
      1 /*
      2 **
      3 ** Copyright 2008, The Android Open Source Project
      4 **
      5 ** Licensed under the Apache License, Version 2.0 (the "License");
      6 ** you may not use this file except in compliance with the License.
      7 ** You may obtain a copy of the License at
      8 **
      9 **     http://www.apache.org/licenses/LICENSE-2.0
     10 **
     11 ** Unless required by applicable law or agreed to in writing, software
     12 ** distributed under the License is distributed on an "AS IS" BASIS,
     13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 ** See the License for the specific language governing permissions and
     15 ** limitations under the License.
     16 */
     17 
     18 #ifndef ANDROID_METADATADRIVER_H
     19 #define ANDROID_METADATADRIVER_H
     20 
     21 #include <media/mediametadataretriever.h>
     22 
     23 #include "pv_frame_metadata_interface.h"
     24 #include "pv_frame_metadata_factory.h"
     25 #include "pv_engine_observer.h"
     26 #include "pv_player_datasourceurl.h"
     27 #include "pvmi_kvp_util.h"
     28 #include "oscl_mem.h"
     29 #include "oscl_mem_audit.h"
     30 #include "oscl_error.h"
     31 #include "oscl_snprintf.h"
     32 
     33 #include "oscl_scheduler.h"
     34 #include "oscl_utf8conv.h"
     35 #include "oscl_scheduler_ao.h"
     36 #include "cczoomrotation16.h"  // for color converter
     37 #include "OMX_Core.h"
     38 #include "pv_omxcore.h"
     39 
     40 #define BEST_THUMBNAIL_MODE 1
     41 
     42 #if BEST_THUMBNAIL_MODE
     43 #include "pvmf_local_data_source.h"
     44 #endif
     45 
     46 namespace android {
     47 
     48 class VideoFrame;
     49 class MediaAlbumArt;
     50 
     51 class MetadataDriver:
     52 public OsclActiveObject,
     53 public PVCommandStatusObserver,
     54 public PVInformationalEventObserver,
     55 public PVErrorEventObserver
     56 {
     57 public:
     58     // @param mode The intended mode of operations:
     59     // can be any of the following:
     60     // METADATA_MODE_NOOP: Experimental - just add and remove data source.
     61     // METADATA_MODE_FRAME_CAPTURE_ONLY: For capture frame/thumbnail only.
     62     // METADATA_MODE_METADATA_RETRIEVAL_ONLY: For meta data retrieval only.
     63     // METADATA_MODE_FRAME_CAPTURE_AND_METADATA_RETRIEVAL: For both frame
     64     //     capture and meta data retrieval.
     65     explicit MetadataDriver(uint32 mode = 0x03);
     66     ~MetadataDriver();
     67 
     68     // Call this before setDataSource() so that the intended mode of
     69     // operation becomes effective.
     70     status_t setMode(int mode) {
     71         if (mode < METADATA_MODE_NOOP ||
     72             mode > METADATA_MODE_FRAME_CAPTURE_AND_METADATA_RETRIEVAL) {
     73             return BAD_VALUE;
     74         }
     75         mMode = mode;
     76         return NO_ERROR;
     77     }
     78 
     79     // Returns the current mode of operation.
     80     status_t getMode(int* mode) const { *mode = mMode; return NO_ERROR; }
     81 
     82     // This call may be time consuming.
     83     // Returns OK if no operation failed; otherwise, it returns UNKNOWN_ERROR.
     84     status_t setDataSource(const char* srcUrl);
     85 
     86     // This call may be time consuming.
     87     // Returns OK if no operation failed; otherwise, it returns UNKNOWN_ERROR.
     88     // The caller _retains_ ownership of "fd".
     89     status_t setDataSourceFd(int fd, int64_t offset, int64_t length);
     90 
     91     // Captures a representative frame. Returns NULL if failure.
     92     VideoFrame *captureFrame();
     93 
     94     // Returns the metadata value if it exists; return NULL if the metadata
     95     // value does not exit or if operation failure occurs.
     96     const char* extractMetadata(int keyCode);
     97 
     98     // Returns the optional graphic embedded/stored within the given data
     99     // source. Returns NULL if no graphic information is found.
    100     MediaAlbumArt* extractAlbumArt();
    101 
    102     // These callback handlers implement PVCommandStatusObserver,
    103     // PVInformationalEventObserver, PVErrorEventObserver, and OsclTimerObject.
    104     // They should never be called directly, although the access modifier
    105     // is public.
    106     void Run();
    107     void CommandCompleted(const PVCmdResponse& aResponse);
    108     void HandleErrorEvent(const PVAsyncErrorEvent& aEvent);
    109     void HandleInformationalEvent(const PVAsyncInformationalEvent& aEvent);
    110 
    111 private:
    112     // Internal states. The existence of the optional states depend on the
    113     // value of mMode.
    114     enum MetadataDriverState {
    115         STATE_CREATE,
    116         STATE_ADD_DATA_SOURCE,
    117         STATE_GET_METADATA_KEYS,        // Optional.
    118         STATE_GET_METADATA_VALUES,      // Depends on STATE_GET_METADATA_KEYS.
    119         STATE_GET_FRAME,                // Optional.
    120         STATE_REMOVE_DATA_SOURCE,
    121         STATE_CLEANUP_AND_COMPLETE,
    122     };
    123 
    124     // We support get metadata, or get frame, or get both, or get neigther.
    125     static const uint32 GET_METADATA_ONLY    = METADATA_MODE_METADATA_RETRIEVAL_ONLY;
    126     static const uint32 GET_FRAME_ONLY       = METADATA_MODE_FRAME_CAPTURE_ONLY;
    127     static const uint32 MAX_VIDEO_FRAME_SIZE = 1280 * 720 * 4;  // Big enough?
    128     static const uint32 MAX_METADATA_STRING_LENGTH = 128;
    129     static const uint32 MAX_STRING_LENGTH = 512;
    130     static const uint32 NUM_METADATA_KEYS = 22;
    131     static const char* METADATA_KEYS[NUM_METADATA_KEYS];
    132     static const char* ALBUM_ART_KEY;
    133 
    134     status_t doSetDataSource(const char* srcUrl);
    135     status_t doExtractAlbumArt();
    136     status_t extractExternalAlbumArt(const char* url);
    137     status_t extractEmbeddedAlbumArt(const PvmfApicStruct* apic);
    138     void doColorConversion();
    139     void trimKeys();
    140     bool containsSupportedKey(const OSCL_HeapString<OsclMemAllocator>& str) const;
    141     bool isCommandSuccessful(const PVCmdResponse& aResponse) const;
    142     void handleCommandFailure();
    143     void handleCreate();
    144     void handleCleanUp();
    145     void handleAddDataSource();
    146     void handleRemoveDataSource();
    147     void handleGetMetadataKeys();
    148     void handleGetMetadataValues();
    149     void handleGetFrame();
    150     void cacheMetadataRetrievalResults();
    151     void clearCache();
    152     status_t extractMetadata(const char* key, char* value, uint32 valueLength);
    153     static int startDriverThread(void *cookie);
    154     int retrieverThread();
    155     void closeSharedFdIfNecessary();
    156 
    157     OsclSemaphore* mSyncSem;
    158 
    159     uint32 mMode;
    160     MetadataDriverState mState;
    161     PVCommandId mCmdId;
    162     bool mIsSetDataSourceSuccessful;
    163     uint32 mContextObject;
    164     uint32 mContextObjectRefValue;
    165     PVFrameAndMetadataInterface *mUtil;
    166 
    167     // Required for setting/removing data source
    168     PVPlayerDataSourceURL *mDataSource;
    169 #if BEST_THUMBNAIL_MODE
    170     PVMFLocalDataSource* mLocalDataSource;
    171 #endif
    172     OSCL_wHeapString<OsclMemAllocator> mDataSourceUrl;
    173 
    174     // Required for frame retrieval
    175     PVFrameBufferProperty mFrameBufferProp;
    176     PVFrameSelector mFrameSelector;
    177     uint32 mFrameBufferSize;
    178     uint8 mFrameBuffer[MAX_VIDEO_FRAME_SIZE];
    179     VideoFrame* mVideoFrame;
    180 
    181     // Required for meta data retrieval
    182     Oscl_Vector<PvmiKvp,OsclMemAllocator> mMetadataValueList;
    183     PVPMetadataList mMetadataKeyList;
    184     PVPMetadataList mActualMetadataKeyList;
    185     int32 mNumMetadataValues;
    186 
    187     // Keep all these copies because of bug 1201885, otherwise, we can
    188     // get these out of mMetadataValueList
    189     char mMetadataValues[NUM_METADATA_KEYS][MAX_METADATA_STRING_LENGTH];
    190     MediaAlbumArt *mMediaAlbumArt;
    191 
    192     // If sourcing from a file descriptor, this holds a dup of it to prevent
    193     // it from going away while we pass around the sharedfd: URI.
    194     int mSharedFd;
    195 };
    196 
    197 }; // namespace android
    198 
    199 #endif // ANDROID_METADATADRIVER_H
    200