3 ** Copyright 2008, The Android Open Source Project
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
9 ** http://www.apache.org/licenses/LICENSE-2.0
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.
18 #ifndef _PLAYERDRIVER_H
19 #define _PLAYERDRIVER_H
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>
28 #include <utils/List.h>
29 #include <cutils/properties.h>
30 #include <media/PVPlayer.h>
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
39 #include "oscl_mem_audit.h"
40 #include "oscl_error.h"
41 #include "oscl_utf8conv.h"
42 #include "oscl_string_utils.h"
44 #include "media_clock_converter.h"
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
67 #include "pv_omxcore.h"
70 #include "cczoomrotation16.h"
73 #include "pvmf_local_data_source.h"
74 #include "pvmf_recognizer_registry.h"
76 class PVPlayerExtensionHandler;
78 typedef void (*media_completion_f)(android::status_t status, void *cookie, bool cancelled);
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.
86 // TODO: Explain these codes.
88 // Stops the scheduler. Does not free any resources in the player.
90 // Load the player capabilities.
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.
99 // PLAYER_PREPARE must be called after the source(s) and sink(s) have
100 // been set or after a PLAYER_STOP command.
103 // After a PLAYER_STOP you must issue a PLAYER_PREPARE to replay the
104 // same source/sink combination.
107 // Discard the sinks. Does not cancel all pending and running
108 // commands. Does not remove the data source.
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?
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
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,
129 virtual ~PlayerCommand() {}
130 Code code() const { return mCode; }
131 media_completion_f callback() { return mCallback; }
132 void* cookie() { return mCookie; }
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; }
139 // @return the command code as a string.
140 const char* toString() const;
142 PlayerCommand(Code code, media_completion_f cbf, void* cookie) :
143 mCode(code), mCallback(cbf), mCookie(cookie) {}
147 media_completion_f mCallback;
151 class PlayerQuit : public PlayerCommand
154 PlayerQuit(media_completion_f cbf, void* cookie) :
155 PlayerCommand(PLAYER_QUIT, cbf, cookie) {}
160 class PlayerSetup : public PlayerCommand
163 PlayerSetup(media_completion_f cbf, void* cookie) :
164 PlayerCommand(PLAYER_SETUP, cbf, cookie) {}
169 class PlayerInit : public PlayerCommand
172 PlayerInit(media_completion_f cbf, void* cookie) :
173 PlayerCommand(PLAYER_INIT, cbf, cookie) {}
178 class PlayerPrepare: public PlayerCommand
181 PlayerPrepare(media_completion_f cbf, void* cookie) :
182 PlayerCommand(PLAYER_PREPARE, cbf, cookie) {}
187 class PlayerStart: public PlayerCommand
190 PlayerStart(media_completion_f cbf, void* cookie) :
191 PlayerCommand(PLAYER_START, cbf, cookie) {}
196 class PlayerStop: public PlayerCommand
199 PlayerStop(media_completion_f cbf, void* cookie) :
200 PlayerCommand(PLAYER_STOP, cbf, cookie) {}
205 class PlayerPause: public PlayerCommand
208 PlayerPause(media_completion_f cbf, void* cookie) :
209 PlayerCommand(PLAYER_PAUSE, cbf, cookie) {}
214 class PlayerReset: public PlayerCommand
217 PlayerReset(media_completion_f cbf, void* cookie) :
218 PlayerCommand(PLAYER_RESET, cbf, cookie) {}
223 class PlayerSetDataSource : public PlayerCommand
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; }
232 PlayerSetDataSource();
236 class PlayerSetVideoSurface : public PlayerCommand
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; }
244 PlayerSetVideoSurface();
245 android::sp<android::ISurface> mSurface;
248 class PlayerSetAudioSink : public PlayerCommand
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; }
257 PlayerSetAudioSink();
258 android::sp<android::MediaPlayerInterface::AudioSink> mAudioSink;
261 class PlayerSetLoop: public PlayerCommand
264 PlayerSetLoop(int loop, media_completion_f cbf, void* cookie) :
265 PlayerCommand(PLAYER_SET_LOOP, cbf, cookie), mLoop(loop) {}
266 int loop() { return mLoop; }
272 class PlayerSeek : public PlayerCommand
275 PlayerSeek(int msec, media_completion_f cbf, void* cookie) :
276 PlayerCommand(PLAYER_SEEK, cbf, cookie), mMsec(msec) {}
277 int msec() { return mMsec; }
283 class PlayerGetPosition: public PlayerCommand
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; }
294 class PlayerGetDuration: public PlayerCommand
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; }
305 class PlayerCheckLiveStreaming: public PlayerCommand
308 PlayerCheckLiveStreaming(media_completion_f cbf, void* cookie) :
309 PlayerCommand(PLAYER_CHECK_LIVE_STREAMING, cbf, cookie) {}
311 PlayerCheckLiveStreaming();
314 class PlayerGetStatus: public PlayerCommand
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; }
325 class PlayerRemoveDataSource: public PlayerCommand
328 PlayerRemoveDataSource(media_completion_f cbf, void* cookie) :
329 PlayerCommand(PLAYER_REMOVE_DATA_SOURCE, cbf, cookie) {}
331 PlayerRemoveDataSource();
334 class PlayerCancelAllCommands: public PlayerCommand
337 PlayerCancelAllCommands(media_completion_f cbf, void* cookie) :
338 PlayerCommand(PLAYER_CANCEL_ALL_COMMANDS, cbf, cookie) {}
340 PlayerCancelAllCommands();
343 class PlayerExtensionCommand: public PlayerCommand
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; }
353 PlayerExtensionCommand();
356 int32 mCompletionHandle;
360 public OsclActiveObject,
361 public PVCommandStatusObserver,
362 public PVInformationalEventObserver,
363 public PVErrorEventObserver
366 PlayerDriver(PVPlayer* pvPlayer);
369 PlayerCommand* dequeueCommand();
370 status_t enqueueCommand(PlayerCommand* code);
372 // Dequeues a code from MediaPlayer and gives it to PVPlayer.
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);
397 PVMFFormatType getFormatType();
398 void CommandCompleted(const PVCmdResponse& aResponse);
399 void HandleErrorEvent(const PVAsyncErrorEvent& aEvent);
400 void HandleInformationalEvent(const PVAsyncInformationalEvent& aEvent);
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);
410 void handleGetDurationComplete(PlayerGetDuration* cmd);
411 void handleCheckLiveStreamingComplete(PlayerCheckLiveStreaming* cmd);
413 int setupHttpStreamPre();
414 int setupHttpStreamPost();
417 // Starts the PV scheduler thread.
418 static int startPlayerThread(void *cookie);
421 // Callback for synchronous commands.
422 static void syncCompletion(status_t s, void *cookie, bool cancelled);
425 PVPlayerInterface *mPlayer;
426 PVPlayerDataSourceURL *mDataSource;
428 PVPlayerDataSink *mAudioSink;
429 PVMFNodeInterface *mAudioNode;
430 AndroidAudioMIO *mAudioOutputMIO;
432 PVPlayerDataSink *mVideoSink;
433 PVMFNodeInterface *mVideoNode;
434 PvmiMIOControl *mVideoOutputMIO;
436 PvmiCapabilityAndConfig *mPlayerCapConfig;
438 OSCL_wHeapString<OsclMemAllocator> mDownloadFilename;
439 OSCL_HeapString<OsclMemAllocator> mDownloadProxy;
440 OSCL_wHeapString<OsclMemAllocator> mDownloadConfigFilename;
441 PVMFSourceContextData *mDownloadContextData;
442 PVMFSourceContextData *mLocalContextData;
444 PVPMetadataList mMetaKeyList;
445 Oscl_Vector<PvmiKvp,OsclMemAllocator> mMetaValueList;
447 PVPMetadataList mCheckLiveKey;
448 Oscl_Vector<PvmiKvp,OsclMemAllocator> mCheckLiveValue;
449 int mCheckLiveMetaValues;
451 // Semaphore used for synchronous commands.
452 OsclSemaphore *mSyncSem;
453 // Status cached by syncCompletion for synchronous commands.
454 status_t mSyncStatus;
456 // Command queue and its lock.
457 List<PlayerCommand*> mCommandQueue;
462 bool mDataReadyReceived;
468 bool mIsLiveStreaming;
471 PVPlayerExtensionHandler* mExtensionHandler;
474 #endif // _PLAYERDRIVER_H