1 /* ------------------------------------------------------------------ 2 * Copyright (C) 2008 PacketVideo 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 13 * express or implied. 14 * See the License for the specific language governing permissions 15 * and limitations under the License. 16 * ------------------------------------------------------------------- 17 */ 18 19 #ifndef ANDROID_SURFACE_OUTPUT_H_INCLUDED 20 #define ANDROID_SURFACE_OUTPUT_H_INCLUDED 21 22 #include "pvmi_mio_control.h" 23 #include "pvmi_media_transfer.h" 24 #include "oscl_scheduler_ao.h" 25 #include "pvmi_media_io_observer.h" 26 #include "oscl_file_io.h" 27 #include "pvmi_config_and_capability.h" 28 #include "oscl_string_containers.h" 29 #include "pvmi_media_io_clock_extension.h" 30 31 #ifdef PERFORMANCE_MEASUREMENTS_ENABLED 32 #include "pvprofile.h" 33 #endif 34 35 // FIXME: Move to OMAP library 36 // Linux and Kernel Includes for Frame Buffer 37 #include <fcntl.h> 38 #include <stdint.h> 39 #include <stdlib.h> 40 #include <stdio.h> 41 #include <string.h> 42 #include <unistd.h> 43 #include <sys/types.h> 44 #include <sys/stat.h> 45 #include <sys/ioctl.h> 46 #include <sys/time.h> 47 //#include <linux/fb.h> 48 //#include <linux/videodev.h> 49 50 // SurfaceFlinger 51 #include <ui/ISurface.h> 52 53 // interprocess shared memory support 54 #include <binder/MemoryBase.h> 55 #include <binder/MemoryHeapBase.h> 56 57 // color converter 58 #include "cczoomrotation16.h" 59 60 // define bits, mask and validity check for video parameters 61 #define VIDEO_PARAMETERS_INVALID 0 62 #define VIDEO_SUBFORMAT_VALID (1 << 0) 63 #define DISPLAY_HEIGHT_VALID (1 << 1) 64 #define DISPLAY_WIDTH_VALID (1 << 2) 65 #define VIDEO_HEIGHT_VALID (1 << 3) 66 #define VIDEO_WIDTH_VALID (1 << 4) 67 #define VIDEO_PARAMETERS_MASK (VIDEO_SUBFORMAT_VALID | DISPLAY_HEIGHT_VALID | \ 68 DISPLAY_WIDTH_VALID | VIDEO_HEIGHT_VALID | VIDEO_WIDTH_VALID) 69 #define VIDEO_PARAMETERS_VALID (VIDEO_SUBFORMAT_VALID | DISPLAY_HEIGHT_VALID | \ 70 DISPLAY_WIDTH_VALID | VIDEO_HEIGHT_VALID | VIDEO_WIDTH_VALID) 71 72 namespace android { 73 class PVPlayer; 74 } 75 76 class PVLogger; 77 class PVMFMediaClock; 78 class AndroidSurfaceOutput; 79 80 using namespace android; 81 82 // FIXME: Not used? 83 // typedef void (*frame_decoded_f)(void *cookie, int width, int height, int pitch, int format, uint8* data); 84 85 // This class implements the reference media IO for file output. 86 // This class constitutes the Media IO component 87 88 class AndroidSurfaceOutput : public OsclTimerObject 89 ,public PvmiMIOControl 90 ,public PvmiMediaTransfer 91 ,public PvmiCapabilityAndConfig 92 { 93 public: 94 AndroidSurfaceOutput(); 95 96 // parameter initialization 97 virtual status_t set(android::PVPlayer* pvPlayer, const sp<ISurface>& surface, bool emulation); 98 virtual status_t setVideoSurface(const sp<ISurface>& surface); 99 100 // For frame buffer 101 virtual bool initCheck(); 102 virtual PVMFStatus writeFrameBuf(uint8* aData, uint32 aDataLen, const PvmiMediaXferHeader& data_header_info); 103 virtual void postLastFrame(); 104 virtual void closeFrameBuf(); 105 106 virtual ~AndroidSurfaceOutput(); 107 108 bool GetVideoSize(int *w, int *h); 109 110 // APIs from PvmiMIOControl 111 112 PVMFStatus connect(PvmiMIOSession& aSession, PvmiMIOObserver* aObserver); 113 114 PVMFStatus disconnect(PvmiMIOSession aSession); 115 116 PVMFCommandId QueryUUID(const PvmfMimeString& aMimeType, Oscl_Vector<PVUuid, OsclMemAllocator>& aUuids, 117 bool aExactUuidsOnly=false, const OsclAny* aContext=NULL); 118 119 PVMFCommandId QueryInterface(const PVUuid& aUuid, PVInterface*& aInterfacePtr, const OsclAny* aContext=NULL); 120 121 PvmiMediaTransfer* createMediaTransfer(PvmiMIOSession& aSession, PvmiKvp* read_formats=NULL, int32 read_flags=0, 122 PvmiKvp* write_formats=NULL, int32 write_flags=0); 123 124 void deleteMediaTransfer(PvmiMIOSession& aSession, PvmiMediaTransfer* media_transfer); 125 126 void processWriteResponseQueue(int numFramesToHold); 127 128 PVMFCommandId Init(const OsclAny* aContext=NULL); 129 130 PVMFCommandId Reset(const OsclAny* aContext=NULL); 131 132 PVMFCommandId Start(const OsclAny* aContext=NULL); 133 134 PVMFCommandId Pause(const OsclAny* aContext=NULL); 135 136 PVMFCommandId Flush(const OsclAny* aContext=NULL); 137 138 PVMFCommandId DiscardData(const OsclAny* aContext=NULL); 139 140 PVMFCommandId DiscardData( PVMFTimestamp aTimestamp=0, const OsclAny* aContext=NULL); 141 142 PVMFCommandId Stop(const OsclAny* aContext=NULL); 143 144 PVMFCommandId CancelAllCommands(const OsclAny* aContext=NULL); 145 146 PVMFCommandId CancelCommand(PVMFCommandId aCmdId, const OsclAny* aContext=NULL); 147 148 void ThreadLogon(); 149 150 void ThreadLogoff(); 151 152 // APIs from PvmiMediaTransfer 153 154 void setPeer(PvmiMediaTransfer* aPeer); 155 156 void useMemoryAllocators(OsclMemAllocator* write_alloc=NULL); 157 158 PVMFCommandId writeAsync(uint8 format_type, int32 format_index, 159 uint8* data, uint32 data_len, 160 const PvmiMediaXferHeader& data_header_info, 161 OsclAny* aContext=NULL); 162 163 void writeComplete(PVMFStatus aStatus, 164 PVMFCommandId write_cmd_id, 165 OsclAny* aContext); 166 167 PVMFCommandId readAsync(uint8* data, uint32 max_data_len, 168 OsclAny* aContext=NULL, 169 int32* formats=NULL, uint16 num_formats=0); 170 171 void readComplete(PVMFStatus aStatus, PVMFCommandId read_cmd_id, int32 format_index, 172 const PvmiMediaXferHeader& data_header_info, OsclAny* aContext); 173 174 void statusUpdate(uint32 status_flags); 175 176 void cancelCommand(PVMFCommandId command_id); 177 178 void cancelAllCommands(); 179 180 // Pure virtuals from PvmiCapabilityAndConfig 181 182 void setObserver (PvmiConfigAndCapabilityCmdObserver* aObserver); 183 184 PVMFStatus getParametersSync(PvmiMIOSession aSession, PvmiKeyType aIdentifier, 185 PvmiKvp*& aParameters, int& num_parameter_elements, PvmiCapabilityContext aContext); 186 187 PVMFStatus releaseParameters(PvmiMIOSession aSession, PvmiKvp* aParameters, int num_elements); 188 189 void createContext(PvmiMIOSession aSession, PvmiCapabilityContext& aContext); 190 191 void setContextParameters(PvmiMIOSession aSession, PvmiCapabilityContext& aContext, 192 PvmiKvp* aParameters, int num_parameter_elements); 193 194 void DeleteContext(PvmiMIOSession aSession, PvmiCapabilityContext& aContext); 195 196 void setParametersSync(PvmiMIOSession aSession, PvmiKvp* aParameters, 197 int num_elements, PvmiKvp * & aRet_kvp); 198 199 PVMFCommandId setParametersAsync(PvmiMIOSession aSession, PvmiKvp* aParameters, 200 int num_elements, PvmiKvp*& aRet_kvp, OsclAny* context=NULL); 201 202 uint32 getCapabilityMetric (PvmiMIOSession aSession); 203 204 PVMFStatus verifyParametersSync (PvmiMIOSession aSession, PvmiKvp* aParameters, int num_elements); 205 206 207 protected: 208 void initData(); 209 void resetVideoParameterFlags(); 210 bool checkVideoParameterFlags(); 211 212 // From OsclTimerObject 213 void Run(); 214 215 void Reschedule(); 216 217 void Cleanup(); 218 void ResetData(); 219 220 PvmiMediaTransfer* iPeer; 221 222 // The PvmiMIOControl class observer. 223 PvmiMIOObserver* iObserver; 224 225 //for generating command IDs 226 uint32 iCommandCounter; 227 228 //State 229 enum PVRefFOState 230 { 231 STATE_IDLE, 232 STATE_LOGGED_ON, 233 STATE_INITIALIZED, 234 STATE_STARTED, 235 STATE_PAUSED 236 }; 237 PVRefFOState iState; 238 239 //Control command handling. 240 class CommandResponse 241 { 242 public: CommandResponse(PVMFStatus s,PVMFCommandId id,const OsclAny * ctx)243 CommandResponse(PVMFStatus s,PVMFCommandId id,const OsclAny* ctx) 244 :iStatus(s),iCmdId(id),iContext(ctx) 245 {} 246 247 PVMFStatus iStatus; 248 PVMFCommandId iCmdId; 249 const OsclAny* iContext; 250 }; 251 Oscl_Vector<CommandResponse,OsclMemAllocator> iCommandResponseQueue; 252 void QueueCommandResponse(CommandResponse&); 253 254 //Write command handling 255 class WriteResponse 256 { 257 public: WriteResponse(PVMFStatus s,PVMFCommandId id,const OsclAny * ctx,const PVMFTimestamp & ts)258 WriteResponse(PVMFStatus s,PVMFCommandId id,const OsclAny* ctx,const PVMFTimestamp& ts) 259 :iStatus(s),iCmdId(id),iContext(ctx),iTimestamp(ts) 260 {} 261 262 PVMFStatus iStatus; 263 PVMFCommandId iCmdId; 264 const OsclAny* iContext; 265 PVMFTimestamp iTimestamp; 266 }; 267 Oscl_Vector<WriteResponse,OsclMemAllocator> iWriteResponseQueue; 268 269 // Output file parameters 270 OSCL_wHeapString<OsclMemAllocator> iOutputFileName; 271 Oscl_FileServer iFs; 272 bool iFsConnected; 273 Oscl_File iOutputFile; 274 bool iFileOpened; 275 276 bool iEosReceived; 277 278 // Video parameters 279 uint32 iVideoParameterFlags; 280 OSCL_HeapString<OsclMemAllocator> iVideoFormatString; 281 PVMFFormatType iVideoFormat; 282 int32 iVideoHeight; 283 int32 iVideoWidth; 284 int32 iVideoDisplayHeight; 285 int32 iVideoDisplayWidth; 286 287 // hardware specific 288 PVMFFormatType iVideoSubFormat; 289 bool iVideoSubFormatValid; 290 291 //For logging 292 PVLogger* iLogger; 293 294 //For implementing the write flow control 295 bool CheckWriteBusy(uint32); 296 297 unsigned long iFrameNumber; 298 299 // software color conversion for software codecs 300 ColorConvertBase* iColorConverter; 301 302 android::PVPlayer* mPvPlayer; 303 bool mInitialized; 304 bool mEmulation; 305 sp<ISurface> mSurface; 306 307 // frame buffer support 308 static const int kBufferCount = 2; 309 int mFrameBufferIndex; 310 ISurface::BufferHeap mBufferHeap; 311 size_t mFrameBuffers[kBufferCount]; 312 313 void convertFrame(void* src, void* dst, size_t len); 314 //This bool is set true when all necassary parameters have been received. 315 bool iIsMIOConfigured; 316 317 #ifdef PERFORMANCE_MEASUREMENTS_ENABLED 318 PVProfile PVOmapVideoProfile; 319 #endif 320 321 }; 322 323 #endif // ANDROID_SURFACE_OUTPUT_H_INCLUDED 324 325