OSDN Git Service

RIO-8910: Fix for MP3 parser returning incorrect value for bitrate key
[android-x86/external-opencore.git] / android / playerdriver.h
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 _PLAYERDRIVER_H
19 #define _PLAYERDRIVER_H
20
21
22 #include <cstring>
23 #include <ui/ISurface.h>
24 #include <utils/Errors.h>  // for android::status_t
25 #include <utils/RefBase.h>  // for android::sp
26 #include <media/MediaPlayerInterface.h>
27
28 #include <utils/List.h>
29 #include <cutils/properties.h>
30 #include <media/PVPlayer.h>
31
32 #include "oscl_base.h"
33 #include "oscl_exception.h"
34 #include "oscl_scheduler.h"
35 #include "oscl_scheduler_ao.h"
36 #include "oscl_exception.h"
37 #include "oscl_mem_basic_functions.h"  // oscl_memcpy
38 #include "oscl_mem.h"
39 #include "oscl_mem_audit.h"
40 #include "oscl_error.h"
41 #include "oscl_utf8conv.h"
42 #include "oscl_string_utils.h"
43
44 #include "media_clock_converter.h"
45
46 #include "pv_player_factory.h"
47 #include "pv_player_interface.h"
48 #include "pv_engine_observer.h"
49 #include "pvmi_media_io_fileoutput.h"
50 #include "pv_player_datasourceurl.h"
51 #include "pv_player_datasinkpvmfnode.h"
52 #include "pvmf_errorinfomessage_extension.h"
53 #include "pvmf_basic_errorinfomessage.h"
54 #include "pvmf_fileformat_events.h"
55 #include "pvmf_mp4ffparser_events.h"
56 #include "pvmf_duration_infomessage.h"
57 #include "android_surface_output.h"
58 #include "android_audio_output.h"
59 #include "android_audio_stream.h"
60 #include "pv_media_output_node_factory.h"
61 #include "pvmf_format_type.h"  // for PVMFFormatType
62 #include "pvmf_node_interface.h"
63 #include "pvmf_source_context_data.h"
64 #include "pvmf_download_data_source.h"
65 //#include "pvmf_return_codes.h"  // for PVMFStatus
66 #include "OMX_Core.h"
67 #include "pv_omxcore.h"
68
69 // color converter
70 #include "cczoomrotation16.h"
71
72 //for KMJ DRM Plugin
73 #include "pvmf_local_data_source.h"
74 #include "pvmf_recognizer_registry.h"
75
76 class  PVPlayerExtensionHandler;
77
78 typedef void (*media_completion_f)(android::status_t status, void *cookie, bool cancelled);
79
80 // Commands that MediaPlayer sends to the PlayerDriver
81 // TODO: Move this class and subclass in their own .h
82 // TODO: Write a class comment.
83 class PlayerCommand
84 {
85   public:
86     // TODO: Explain these codes.
87     enum Code {
88         // Stops the scheduler. Does not free any resources in the player.
89         PLAYER_QUIT                     = 1,
90         // Load the player capabilities.
91         PLAYER_SETUP                    = 2,
92         // Reset must be called before set data source.
93         PLAYER_SET_DATA_SOURCE          = 3,
94         // TODO: Rename to PLAYER_SET_VIDEO_SINK.
95         PLAYER_SET_VIDEO_SURFACE        = 4,
96         PLAYER_SET_AUDIO_SINK           = 5,
97         // Must be called after a data source has been set or modified.
98         PLAYER_INIT                     = 6,
99         // PLAYER_PREPARE must be called after the source(s) and sink(s) have
100         // been set or after a PLAYER_STOP command.
101         PLAYER_PREPARE                  = 7,
102         PLAYER_START                    = 8,
103         // After a PLAYER_STOP you must issue a PLAYER_PREPARE to replay the
104         // same source/sink combination.
105         PLAYER_STOP                     = 9,
106         PLAYER_PAUSE                    = 10,
107         // Discard the sinks. Does not cancel all pending and running
108         // commands. Does not remove the data source.
109         PLAYER_RESET                    = 11,
110         // TODO: What is that? Loop on the data source?
111         PLAYER_SET_LOOP                 = 12,
112         // TODO: When can this happen? in prepared started and pause?
113         PLAYER_SEEK                     = 13,
114         // TODO: When can this happend?  in prepared started and pause?
115         PLAYER_GET_POSITION             = 14,
116         PLAYER_GET_DURATION             = 15,
117         // TODO: Can this command be issued anytime?
118         PLAYER_GET_STATUS               = 16,
119         // TODO: How is that different from reset? Does it leave the sink
120         // untouched?
121         PLAYER_REMOVE_DATA_SOURCE       = 17,
122         // TODO: clarify the scope of PLAYER_CANCEL_ALL_COMMANDS, does it work
123         // for asynchronous commands only or for synchronous as well?
124         PLAYER_CANCEL_ALL_COMMANDS      = 18,
125         PLAYER_EXTENSION_COMMAND        = 19,
126         PLAYER_CHECK_LIVE_STREAMING     = 20,
127     };
128
129     virtual             ~PlayerCommand() {}
130     Code                code() const { return mCode; }
131     media_completion_f  callback() { return mCallback; }
132     void*               cookie() { return mCookie; }
133
134     // If no callback was supplied, the command runs in synchronous mode.
135     bool                hasCallback() const { return NULL != mCallback; }
136     void                complete(android::status_t status, bool cancelled) { mCallback(status, mCookie, cancelled); }
137     void                set(media_completion_f cbf, void* cookie) { mCallback = cbf; mCookie = cookie; }
138
139     // @return the command code as a string.
140     const char*         toString() const;
141   protected:
142     PlayerCommand(Code code, media_completion_f cbf, void* cookie) :
143             mCode(code), mCallback(cbf), mCookie(cookie) {}
144   private:
145     PlayerCommand();
146     Code                mCode;
147     media_completion_f  mCallback;
148     void*               mCookie;
149 };
150
151 class PlayerQuit : public PlayerCommand
152 {
153   public:
154     PlayerQuit(media_completion_f cbf, void* cookie) :
155             PlayerCommand(PLAYER_QUIT, cbf, cookie) {}
156   private:
157     PlayerQuit();
158 };
159
160 class PlayerSetup : public PlayerCommand
161 {
162   public:
163     PlayerSetup(media_completion_f cbf, void* cookie) :
164             PlayerCommand(PLAYER_SETUP, cbf, cookie) {}
165   private:
166     PlayerSetup();
167 };
168
169 class PlayerInit : public PlayerCommand
170 {
171   public:
172     PlayerInit(media_completion_f cbf, void* cookie) :
173             PlayerCommand(PLAYER_INIT, cbf, cookie) {}
174   private:
175     PlayerInit();
176 };
177
178 class PlayerPrepare: public PlayerCommand
179 {
180   public:
181     PlayerPrepare(media_completion_f cbf, void* cookie) :
182             PlayerCommand(PLAYER_PREPARE, cbf, cookie) {}
183   private:
184     PlayerPrepare();
185 };
186
187 class PlayerStart: public PlayerCommand
188 {
189   public:
190     PlayerStart(media_completion_f cbf, void* cookie) :
191             PlayerCommand(PLAYER_START, cbf, cookie) {}
192   private:
193     PlayerStart();
194 };
195
196 class PlayerStop: public PlayerCommand
197 {
198   public:
199     PlayerStop(media_completion_f cbf, void* cookie) :
200             PlayerCommand(PLAYER_STOP, cbf, cookie) {}
201   private:
202     PlayerStop();
203 };
204
205 class PlayerPause: public PlayerCommand
206 {
207   public:
208     PlayerPause(media_completion_f cbf, void* cookie) :
209             PlayerCommand(PLAYER_PAUSE, cbf, cookie) {}
210   private:
211     PlayerPause();
212 };
213
214 class PlayerReset: public PlayerCommand
215 {
216   public:
217     PlayerReset(media_completion_f cbf, void* cookie) :
218             PlayerCommand(PLAYER_RESET, cbf, cookie) {}
219   private:
220     PlayerReset();
221 };
222
223 class PlayerSetDataSource : public PlayerCommand
224 {
225   public:
226     PlayerSetDataSource(const char* url, media_completion_f cbf, void* cookie) :
227             PlayerCommand(PLAYER_SET_DATA_SOURCE, cbf, cookie), mUrl(0) {
228         if (url) mUrl = strdup(url); }
229     ~PlayerSetDataSource() { if (mUrl) free(mUrl); }
230     const char*         url() const { return mUrl; }
231   private:
232     PlayerSetDataSource();
233     char*               mUrl;
234 };
235
236 class PlayerSetVideoSurface : public PlayerCommand
237 {
238   public:
239     PlayerSetVideoSurface(const android::sp<android::ISurface>& surface, media_completion_f cbf, void* cookie) :
240             PlayerCommand(PLAYER_SET_VIDEO_SURFACE, cbf, cookie), mSurface(surface) {}
241     ~PlayerSetVideoSurface() { mSurface.clear(); }
242     android::sp<android::ISurface>        surface() const { return mSurface; }
243   private:
244     PlayerSetVideoSurface();
245     android::sp<android::ISurface>        mSurface;
246 };
247
248 class PlayerSetAudioSink : public PlayerCommand
249 {
250   public:
251     PlayerSetAudioSink(const android::sp<android::MediaPlayerInterface::AudioSink>& audioSink,
252                        media_completion_f cbf, void* cookie) :
253             PlayerCommand(PLAYER_SET_AUDIO_SINK, cbf, cookie), mAudioSink(audioSink) {}
254     ~PlayerSetAudioSink() { mAudioSink.clear(); }
255     android::sp<android::MediaPlayerInterface::AudioSink> audioSink() { return mAudioSink; }
256   private:
257     PlayerSetAudioSink();
258     android::sp<android::MediaPlayerInterface::AudioSink> mAudioSink;
259 };
260
261 class PlayerSetLoop: public PlayerCommand
262 {
263   public:
264     PlayerSetLoop(int loop, media_completion_f cbf, void* cookie) :
265             PlayerCommand(PLAYER_SET_LOOP, cbf, cookie), mLoop(loop) {}
266     int loop() { return mLoop; }
267   private:
268     PlayerSetLoop();
269     int                 mLoop;
270 };
271
272 class PlayerSeek : public PlayerCommand
273 {
274   public:
275     PlayerSeek(int msec, media_completion_f cbf, void* cookie) :
276             PlayerCommand(PLAYER_SEEK, cbf, cookie), mMsec(msec) {}
277     int                 msec() { return mMsec; }
278   private:
279     PlayerSeek();
280     int                 mMsec;
281 };
282
283 class PlayerGetPosition: public PlayerCommand
284 {
285   public:
286     PlayerGetPosition(int* msec, media_completion_f cbf, void* cookie) :
287             PlayerCommand(PLAYER_GET_POSITION, cbf, cookie), mMsec(msec) {}
288     void set(int msecs) { if (mMsec) *mMsec = msecs; }
289   private:
290     PlayerGetPosition();
291     int*                mMsec;
292 };
293
294 class PlayerGetDuration: public PlayerCommand
295 {
296   public:
297     PlayerGetDuration(int* msec, media_completion_f cbf, void* cookie) :
298             PlayerCommand(PLAYER_GET_DURATION, cbf, cookie), mMsec(msec) {}
299     void set(int msecs) { if (mMsec) *mMsec = msecs; }
300   private:
301     PlayerGetDuration();
302     int*                mMsec;
303 };
304
305 class PlayerCheckLiveStreaming: public PlayerCommand
306 {
307   public:
308     PlayerCheckLiveStreaming(media_completion_f cbf, void* cookie) :
309     PlayerCommand(PLAYER_CHECK_LIVE_STREAMING, cbf, cookie) {}
310   private:
311     PlayerCheckLiveStreaming();
312 };
313
314 class PlayerGetStatus: public PlayerCommand
315 {
316   public:
317     PlayerGetStatus(int *status, media_completion_f cbf, void* cookie) :
318             PlayerCommand(PLAYER_GET_STATUS, cbf, cookie), mStatus(status) {}
319     void set(int status) { *mStatus = status; }
320   private:
321     PlayerGetStatus();
322     int*                mStatus;
323 };
324
325 class PlayerRemoveDataSource: public PlayerCommand
326 {
327   public:
328     PlayerRemoveDataSource(media_completion_f cbf, void* cookie) :
329             PlayerCommand(PLAYER_REMOVE_DATA_SOURCE, cbf, cookie) {}
330   private:
331     PlayerRemoveDataSource();
332 };
333
334 class PlayerCancelAllCommands: public PlayerCommand
335 {
336   public:
337     PlayerCancelAllCommands(media_completion_f cbf, void* cookie) :
338             PlayerCommand(PLAYER_CANCEL_ALL_COMMANDS, cbf, cookie) {}
339   private:
340     PlayerCancelAllCommands();
341 };
342
343 class PlayerExtensionCommand: public PlayerCommand
344 {
345   public:
346     PlayerExtensionCommand(const Parcel& aData, Parcel& aReply, media_completion_f cbf, void* cookie) :
347             PlayerCommand(PLAYER_EXTENSION_COMMAND, cbf, cookie),mData(aData),mReply(aReply),mCompletionHandle(NULL) {}
348     Parcel& getReplyParcel(){ return mReply;}
349     const Parcel& getDataParcel(){ return mData;}
350     void setCompletionHandle(int32 aHandle) {mCompletionHandle=aHandle;}
351     int32 getCompletionHandle() { return mCompletionHandle; }
352   private:
353     PlayerExtensionCommand();
354     const Parcel&       mData;
355     Parcel&             mReply;
356     int32               mCompletionHandle;
357 };
358
359 class PlayerDriver :
360         public OsclActiveObject,
361         public PVCommandStatusObserver,
362         public PVInformationalEventObserver,
363         public PVErrorEventObserver
364 {
365   public:
366     PlayerDriver(PVPlayer* pvPlayer);
367     ~PlayerDriver();
368
369     PlayerCommand* dequeueCommand();
370     status_t enqueueCommand(PlayerCommand* code);
371
372     // Dequeues a code from MediaPlayer and gives it to PVPlayer.
373     void Run();
374
375     // Handlers for the various commands we can accept.
376     void commandFailed(PlayerCommand* command) ;
377     void handleSetup(PlayerSetup* command);
378     void handleSetDataSource(PlayerSetDataSource* command);
379     void handleSetVideoSurface(PlayerSetVideoSurface* command);
380     void handleSetAudioSink(PlayerSetAudioSink* command);
381     void handleInit(PlayerInit* command);
382     void handlePrepare(PlayerPrepare* command);
383     void handleStart(PlayerStart* command);
384     void handleStop(PlayerStop* command);
385     void handlePause(PlayerPause* command);
386     void handleSeek(PlayerSeek* command);
387     void handleCancelAllCommands(PlayerCancelAllCommands* command);
388     void handleRemoveDataSource(PlayerRemoveDataSource* command);
389     void handleReset(PlayerReset* command);
390     void handleQuit(PlayerQuit* command);
391     void handleGetPosition(PlayerGetPosition* command);
392     void handleGetDuration(PlayerGetDuration* command);
393     void handleGetStatus(PlayerGetStatus* command);
394     void handleCheckLiveStreaming(PlayerCheckLiveStreaming* cmd);
395     void handleExtensionCommand(PlayerExtensionCommand* command);
396
397     PVMFFormatType getFormatType();
398     void CommandCompleted(const PVCmdResponse& aResponse);
399     void HandleErrorEvent(const PVAsyncErrorEvent& aEvent);
400     void HandleInformationalEvent(const PVAsyncInformationalEvent& aEvent);
401
402   private:
403     // PVPlayerExtensionHandler needs  to access mPlayer and FinishSyncCommand()
404     // for calling PVPlayerEngine APIs and completing the commands.
405     friend class PVPlayerExtensionHandler;
406     // Finish up a non-async code in such a way that
407     // the event loop will keep running.
408     void FinishSyncCommand(PlayerCommand* command);
409
410     void handleGetDurationComplete(PlayerGetDuration* cmd);
411     void handleCheckLiveStreamingComplete(PlayerCheckLiveStreaming* cmd);
412
413     int setupHttpStreamPre();
414     int setupHttpStreamPost();
415
416
417     // Starts the PV scheduler thread.
418     static int startPlayerThread(void *cookie);
419     int playerThread();
420
421     // Callback for synchronous commands.
422     static void syncCompletion(status_t s, void *cookie, bool cancelled);
423
424     PVPlayer                *mPvPlayer;
425     PVPlayerInterface       *mPlayer;
426     PVPlayerDataSourceURL   *mDataSource;
427
428     PVPlayerDataSink        *mAudioSink;
429     PVMFNodeInterface       *mAudioNode;
430     AndroidAudioMIO         *mAudioOutputMIO;
431
432     PVPlayerDataSink        *mVideoSink;
433     PVMFNodeInterface       *mVideoNode;
434     PvmiMIOControl          *mVideoOutputMIO;
435
436     PvmiCapabilityAndConfig *mPlayerCapConfig;
437
438     OSCL_wHeapString<OsclMemAllocator> mDownloadFilename;
439     OSCL_HeapString<OsclMemAllocator> mDownloadProxy;
440     OSCL_wHeapString<OsclMemAllocator> mDownloadConfigFilename;
441     PVMFSourceContextData   *mDownloadContextData;
442     PVMFSourceContextData   *mLocalContextData;
443
444     PVPMetadataList mMetaKeyList;
445     Oscl_Vector<PvmiKvp,OsclMemAllocator> mMetaValueList;
446     int mNumMetaValues;
447     PVPMetadataList mCheckLiveKey;
448     Oscl_Vector<PvmiKvp,OsclMemAllocator> mCheckLiveValue;
449     int mCheckLiveMetaValues;
450
451     // Semaphore used for synchronous commands.
452     OsclSemaphore           *mSyncSem;
453     // Status cached by syncCompletion for synchronous commands.
454     status_t                mSyncStatus;
455
456     // Command queue and its lock.
457     List<PlayerCommand*>    mCommandQueue;
458     Mutex                   mQueueLock;
459
460     bool                    mIsLooping;
461     bool                    mDoLoop;
462     bool                    mDataReadyReceived;
463     bool                    mPrepareDone;
464     bool                    mEndOfData;
465     int                     mRecentSeek;
466     bool                    mSeekComp;
467     bool                    mSeekPending;
468     bool                    mIsLiveStreaming;
469     bool                    mEmulation;
470     void*                   mLibHandle;
471     PVPlayerExtensionHandler* mExtensionHandler;
472 };
473
474 #endif // _PLAYERDRIVER_H