• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <cstring>
22 #include <ui/ISurface.h>
23 #include <utils/Errors.h>  // for android::status_t
24 #include <utils/RefBase.h>  // for android::sp
25 #include <media/MediaPlayerInterface.h>
26 
27 typedef void (*media_completion_f)(android::status_t status, void *cookie, bool cancelled);
28 
29 // Commands that MediaPlayer sends to the PlayerDriver
30 // TODO: Move this class and subclass in their own .h
31 // TODO: Write a class comment.
32 class PlayerCommand
33 {
34   public:
35     // TODO: Explain these codes.
36     enum Code {
37         // Stops the scheduler. Does not free any resources in the player.
38         PLAYER_QUIT                     = 1,
39         // Load the player capabilities.
40         PLAYER_SETUP                    = 2,
41         // Reset must be called before set data source.
42         PLAYER_SET_DATA_SOURCE          = 3,
43         // TODO: Rename to PLAYER_SET_VIDEO_SINK.
44         PLAYER_SET_VIDEO_SURFACE        = 4,
45         PLAYER_SET_AUDIO_SINK           = 5,
46         // Must be called after a data source has been set or modified.
47         PLAYER_INIT                     = 6,
48         // PLAYER_PREPARE must be called after the source(s) and sink(s) have
49         // been set or after a PLAYER_STOP command.
50         PLAYER_PREPARE                  = 7,
51         PLAYER_START                    = 8,
52         // After a PLAYER_STOP you must issue a PLAYER_PREPARE to replay the
53         // same source/sink combination.
54         PLAYER_STOP                     = 9,
55         PLAYER_PAUSE                    = 10,
56         // Discard the sinks. Does not cancel all pending and running
57         // commands. Does not remove the data source.
58         PLAYER_RESET                    = 11,
59         // TODO: What is that? Loop on the data source?
60         PLAYER_SET_LOOP                 = 12,
61         // TODO: When can this happen? in prepared started and pause?
62         PLAYER_SEEK                     = 13,
63         // TODO: When can this happend?  in prepared started and pause?
64         PLAYER_GET_POSITION             = 14,
65         PLAYER_GET_DURATION             = 15,
66         // TODO: Can this command be issued anytime?
67         PLAYER_GET_STATUS               = 16,
68         // TODO: How is that different from reset? Does it leave the sink
69         // untouched?
70         PLAYER_REMOVE_DATA_SOURCE       = 17,
71         // TODO: clarify the scope of PLAYER_CANCEL_ALL_COMMANDS, does it work
72         // for asynchronous commands only or for synchronous as well?
73         PLAYER_CANCEL_ALL_COMMANDS      = 18,
74         PLAYER_CHECK_LIVE_STREAMING     = 19,
75     };
76 
~PlayerCommand()77     virtual             ~PlayerCommand() {}
code()78     Code                code() const { return mCode; }
callback()79     media_completion_f  callback() { return mCallback; }
cookie()80     void*               cookie() { return mCookie; }
81 
82     // If no callback was supplied, the command runs in synchronous mode.
hasCallback()83     bool                hasCallback() const { return NULL != mCallback; }
complete(android::status_t status,bool cancelled)84     void                complete(android::status_t status, bool cancelled) { mCallback(status, mCookie, cancelled); }
set(media_completion_f cbf,void * cookie)85     void                set(media_completion_f cbf, void* cookie) { mCallback = cbf; mCookie = cookie; }
86 
87     // @return the command code as a string.
88     const char*         toString() const;
89   protected:
PlayerCommand(Code code,media_completion_f cbf,void * cookie)90     PlayerCommand(Code code, media_completion_f cbf, void* cookie) :
91             mCode(code), mCallback(cbf), mCookie(cookie) {}
92   private:
93     PlayerCommand();
94     Code                mCode;
95     media_completion_f  mCallback;
96     void*               mCookie;
97 };
98 
99 class PlayerQuit : public PlayerCommand
100 {
101   public:
PlayerQuit(media_completion_f cbf,void * cookie)102     PlayerQuit(media_completion_f cbf, void* cookie) :
103             PlayerCommand(PLAYER_QUIT, cbf, cookie) {}
104   private:
105     PlayerQuit();
106 };
107 
108 class PlayerSetup : public PlayerCommand
109 {
110   public:
PlayerSetup(media_completion_f cbf,void * cookie)111     PlayerSetup(media_completion_f cbf, void* cookie) :
112             PlayerCommand(PLAYER_SETUP, cbf, cookie) {}
113   private:
114     PlayerSetup();
115 };
116 
117 class PlayerInit : public PlayerCommand
118 {
119   public:
PlayerInit(media_completion_f cbf,void * cookie)120     PlayerInit(media_completion_f cbf, void* cookie) :
121             PlayerCommand(PLAYER_INIT, cbf, cookie) {}
122   private:
123     PlayerInit();
124 };
125 
126 class PlayerPrepare: public PlayerCommand
127 {
128   public:
PlayerPrepare(media_completion_f cbf,void * cookie)129     PlayerPrepare(media_completion_f cbf, void* cookie) :
130             PlayerCommand(PLAYER_PREPARE, cbf, cookie) {}
131   private:
132     PlayerPrepare();
133 };
134 
135 class PlayerStart: public PlayerCommand
136 {
137   public:
PlayerStart(media_completion_f cbf,void * cookie)138     PlayerStart(media_completion_f cbf, void* cookie) :
139             PlayerCommand(PLAYER_START, cbf, cookie) {}
140   private:
141     PlayerStart();
142 };
143 
144 class PlayerStop: public PlayerCommand
145 {
146   public:
PlayerStop(media_completion_f cbf,void * cookie)147     PlayerStop(media_completion_f cbf, void* cookie) :
148             PlayerCommand(PLAYER_STOP, cbf, cookie) {}
149   private:
150     PlayerStop();
151 };
152 
153 class PlayerPause: public PlayerCommand
154 {
155   public:
PlayerPause(media_completion_f cbf,void * cookie)156     PlayerPause(media_completion_f cbf, void* cookie) :
157             PlayerCommand(PLAYER_PAUSE, cbf, cookie) {}
158   private:
159     PlayerPause();
160 };
161 
162 class PlayerReset: public PlayerCommand
163 {
164   public:
PlayerReset(media_completion_f cbf,void * cookie)165     PlayerReset(media_completion_f cbf, void* cookie) :
166             PlayerCommand(PLAYER_RESET, cbf, cookie) {}
167   private:
168     PlayerReset();
169 };
170 
171 class PlayerSetDataSource : public PlayerCommand
172 {
173   public:
PlayerSetDataSource(const char * url,media_completion_f cbf,void * cookie)174     PlayerSetDataSource(const char* url, media_completion_f cbf, void* cookie) :
175             PlayerCommand(PLAYER_SET_DATA_SOURCE, cbf, cookie), mUrl(0) {
176         if (url) mUrl = strdup(url); }
~PlayerSetDataSource()177     ~PlayerSetDataSource() { if (mUrl) free(mUrl); }
url()178     const char*         url() const { return mUrl; }
179   private:
180     PlayerSetDataSource();
181     char*               mUrl;
182 };
183 
184 class PlayerSetVideoSurface : public PlayerCommand
185 {
186   public:
PlayerSetVideoSurface(const android::sp<android::ISurface> & surface,media_completion_f cbf,void * cookie)187     PlayerSetVideoSurface(const android::sp<android::ISurface>& surface, media_completion_f cbf, void* cookie) :
188             PlayerCommand(PLAYER_SET_VIDEO_SURFACE, cbf, cookie), mSurface(surface) {}
~PlayerSetVideoSurface()189     ~PlayerSetVideoSurface() { mSurface.clear(); }
surface()190     android::sp<android::ISurface>        surface() const { return mSurface; }
191   private:
192     PlayerSetVideoSurface();
193     android::sp<android::ISurface>        mSurface;
194 };
195 
196 class PlayerSetAudioSink : public PlayerCommand
197 {
198   public:
PlayerSetAudioSink(const android::sp<android::MediaPlayerInterface::AudioSink> & audioSink,media_completion_f cbf,void * cookie)199     PlayerSetAudioSink(const android::sp<android::MediaPlayerInterface::AudioSink>& audioSink,
200                        media_completion_f cbf, void* cookie) :
201             PlayerCommand(PLAYER_SET_AUDIO_SINK, cbf, cookie), mAudioSink(audioSink) {}
~PlayerSetAudioSink()202     ~PlayerSetAudioSink() { mAudioSink.clear(); }
audioSink()203     android::sp<android::MediaPlayerInterface::AudioSink> audioSink() { return mAudioSink; }
204   private:
205     PlayerSetAudioSink();
206     android::sp<android::MediaPlayerInterface::AudioSink> mAudioSink;
207 };
208 
209 class PlayerSetLoop: public PlayerCommand
210 {
211   public:
PlayerSetLoop(int loop,media_completion_f cbf,void * cookie)212     PlayerSetLoop(int loop, media_completion_f cbf, void* cookie) :
213             PlayerCommand(PLAYER_SET_LOOP, cbf, cookie), mLoop(loop) {}
loop()214     int loop() { return mLoop; }
215   private:
216     PlayerSetLoop();
217     int                 mLoop;
218 };
219 
220 class PlayerSeek : public PlayerCommand
221 {
222   public:
PlayerSeek(int msec,media_completion_f cbf,void * cookie)223     PlayerSeek(int msec, media_completion_f cbf, void* cookie) :
224             PlayerCommand(PLAYER_SEEK, cbf, cookie), mMsec(msec) {}
msec()225     int                 msec() { return mMsec; }
226   private:
227     PlayerSeek();
228     int                 mMsec;
229 };
230 
231 class PlayerGetPosition: public PlayerCommand
232 {
233   public:
PlayerGetPosition(int * msec,media_completion_f cbf,void * cookie)234     PlayerGetPosition(int* msec, media_completion_f cbf, void* cookie) :
235             PlayerCommand(PLAYER_GET_POSITION, cbf, cookie), mMsec(msec) {}
set(int msecs)236     void set(int msecs) { if (mMsec) *mMsec = msecs; }
237   private:
238     PlayerGetPosition();
239     int*                mMsec;
240 };
241 
242 class PlayerGetDuration: public PlayerCommand
243 {
244   public:
PlayerGetDuration(int * msec,media_completion_f cbf,void * cookie)245     PlayerGetDuration(int* msec, media_completion_f cbf, void* cookie) :
246             PlayerCommand(PLAYER_GET_DURATION, cbf, cookie), mMsec(msec) {}
set(int msecs)247     void set(int msecs) { if (mMsec) *mMsec = msecs; }
248   private:
249     PlayerGetDuration();
250     int*                mMsec;
251 };
252 
253 class PlayerCheckLiveStreaming: public PlayerCommand
254 {
255   public:
PlayerCheckLiveStreaming(media_completion_f cbf,void * cookie)256     PlayerCheckLiveStreaming(media_completion_f cbf, void* cookie) :
257             PlayerCommand(PLAYER_CHECK_LIVE_STREAMING, cbf, cookie) {}
258   private:
259     PlayerCheckLiveStreaming();
260 };
261 
262 class PlayerGetStatus: public PlayerCommand
263 {
264   public:
PlayerGetStatus(int * status,media_completion_f cbf,void * cookie)265     PlayerGetStatus(int *status, media_completion_f cbf, void* cookie) :
266             PlayerCommand(PLAYER_GET_STATUS, cbf, cookie), mStatus(status) {}
set(int status)267     void set(int status) { *mStatus = status; }
268   private:
269     PlayerGetStatus();
270     int*                mStatus;
271 };
272 
273 class PlayerRemoveDataSource: public PlayerCommand
274 {
275   public:
PlayerRemoveDataSource(media_completion_f cbf,void * cookie)276     PlayerRemoveDataSource(media_completion_f cbf, void* cookie) :
277             PlayerCommand(PLAYER_REMOVE_DATA_SOURCE, cbf, cookie) {}
278   private:
279     PlayerRemoveDataSource();
280 };
281 
282 class PlayerCancelAllCommands: public PlayerCommand
283 {
284   public:
PlayerCancelAllCommands(media_completion_f cbf,void * cookie)285     PlayerCancelAllCommands(media_completion_f cbf, void* cookie) :
286             PlayerCommand(PLAYER_CANCEL_ALL_COMMANDS, cbf, cookie) {}
287   private:
288     PlayerCancelAllCommands();
289 };
290 
291 
292 #endif // _PLAYERDRIVER_H
293