The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | #include <stdint.h> |
| 19 | #include <sys/types.h> |
| 20 | |
| 21 | #include <utils/Parcel.h> |
| 22 | |
| 23 | #include <media/IMediaPlayer.h> |
| 24 | #include <ui/ISurface.h> |
| 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | enum { |
| 29 | DISCONNECT = IBinder::FIRST_CALL_TRANSACTION, |
| 30 | SET_VIDEO_SURFACE, |
| 31 | PREPARE_ASYNC, |
| 32 | START, |
| 33 | STOP, |
| 34 | IS_PLAYING, |
| 35 | PAUSE, |
| 36 | SEEK_TO, |
| 37 | GET_CURRENT_POSITION, |
| 38 | GET_DURATION, |
| 39 | RESET, |
| 40 | SET_AUDIO_STREAM_TYPE, |
| 41 | SET_LOOPING, |
| 42 | SET_VOLUME |
| 43 | }; |
| 44 | |
| 45 | class BpMediaPlayer: public BpInterface<IMediaPlayer> |
| 46 | { |
| 47 | public: |
| 48 | BpMediaPlayer(const sp<IBinder>& impl) |
| 49 | : BpInterface<IMediaPlayer>(impl) |
| 50 | { |
| 51 | } |
| 52 | |
| 53 | // disconnect from media player service |
| 54 | void disconnect() |
| 55 | { |
| 56 | Parcel data, reply; |
| 57 | data.writeInterfaceToken(IMediaPlayer::getInterfaceDescriptor()); |
| 58 | remote()->transact(DISCONNECT, data, &reply); |
| 59 | } |
| 60 | |
| 61 | status_t setVideoSurface(const sp<ISurface>& surface) |
| 62 | { |
| 63 | Parcel data, reply; |
| 64 | data.writeInterfaceToken(IMediaPlayer::getInterfaceDescriptor()); |
| 65 | data.writeStrongBinder(surface->asBinder()); |
| 66 | remote()->transact(SET_VIDEO_SURFACE, data, &reply); |
| 67 | return reply.readInt32(); |
| 68 | } |
| 69 | |
| 70 | status_t prepareAsync() |
| 71 | { |
| 72 | Parcel data, reply; |
| 73 | data.writeInterfaceToken(IMediaPlayer::getInterfaceDescriptor()); |
| 74 | remote()->transact(PREPARE_ASYNC, data, &reply); |
| 75 | return reply.readInt32(); |
| 76 | } |
| 77 | |
| 78 | status_t start() |
| 79 | { |
| 80 | Parcel data, reply; |
| 81 | data.writeInterfaceToken(IMediaPlayer::getInterfaceDescriptor()); |
| 82 | remote()->transact(START, data, &reply); |
| 83 | return reply.readInt32(); |
| 84 | } |
| 85 | |
| 86 | status_t stop() |
| 87 | { |
| 88 | Parcel data, reply; |
| 89 | data.writeInterfaceToken(IMediaPlayer::getInterfaceDescriptor()); |
| 90 | remote()->transact(STOP, data, &reply); |
| 91 | return reply.readInt32(); |
| 92 | } |
| 93 | |
| 94 | status_t isPlaying(bool* state) |
| 95 | { |
| 96 | Parcel data, reply; |
| 97 | data.writeInterfaceToken(IMediaPlayer::getInterfaceDescriptor()); |
| 98 | remote()->transact(IS_PLAYING, data, &reply); |
| 99 | *state = reply.readInt32(); |
| 100 | return reply.readInt32(); |
| 101 | } |
| 102 | |
| 103 | status_t pause() |
| 104 | { |
| 105 | Parcel data, reply; |
| 106 | data.writeInterfaceToken(IMediaPlayer::getInterfaceDescriptor()); |
| 107 | remote()->transact(PAUSE, data, &reply); |
| 108 | return reply.readInt32(); |
| 109 | } |
| 110 | |
| 111 | status_t seekTo(int msec) |
| 112 | { |
| 113 | Parcel data, reply; |
| 114 | data.writeInterfaceToken(IMediaPlayer::getInterfaceDescriptor()); |
| 115 | data.writeInt32(msec); |
| 116 | remote()->transact(SEEK_TO, data, &reply); |
| 117 | return reply.readInt32(); |
| 118 | } |
| 119 | |
| 120 | status_t getCurrentPosition(int* msec) |
| 121 | { |
| 122 | Parcel data, reply; |
| 123 | data.writeInterfaceToken(IMediaPlayer::getInterfaceDescriptor()); |
| 124 | remote()->transact(GET_CURRENT_POSITION, data, &reply); |
| 125 | *msec = reply.readInt32(); |
| 126 | return reply.readInt32(); |
| 127 | } |
| 128 | |
| 129 | status_t getDuration(int* msec) |
| 130 | { |
| 131 | Parcel data, reply; |
| 132 | data.writeInterfaceToken(IMediaPlayer::getInterfaceDescriptor()); |
| 133 | remote()->transact(GET_DURATION, data, &reply); |
| 134 | *msec = reply.readInt32(); |
| 135 | return reply.readInt32(); |
| 136 | } |
| 137 | |
| 138 | status_t reset() |
| 139 | { |
| 140 | Parcel data, reply; |
| 141 | data.writeInterfaceToken(IMediaPlayer::getInterfaceDescriptor()); |
| 142 | remote()->transact(RESET, data, &reply); |
| 143 | return reply.readInt32(); |
| 144 | } |
| 145 | |
| 146 | status_t setAudioStreamType(int type) |
| 147 | { |
| 148 | Parcel data, reply; |
| 149 | data.writeInterfaceToken(IMediaPlayer::getInterfaceDescriptor()); |
| 150 | data.writeInt32(type); |
| 151 | remote()->transact(SET_AUDIO_STREAM_TYPE, data, &reply); |
| 152 | return reply.readInt32(); |
| 153 | } |
| 154 | |
| 155 | status_t setLooping(int loop) |
| 156 | { |
| 157 | Parcel data, reply; |
| 158 | data.writeInterfaceToken(IMediaPlayer::getInterfaceDescriptor()); |
| 159 | data.writeInt32(loop); |
| 160 | remote()->transact(SET_LOOPING, data, &reply); |
| 161 | return reply.readInt32(); |
| 162 | } |
| 163 | |
| 164 | status_t setVolume(float leftVolume, float rightVolume) |
| 165 | { |
| 166 | Parcel data, reply; |
| 167 | data.writeFloat(leftVolume); |
| 168 | data.writeFloat(rightVolume); |
| 169 | remote()->transact(SET_VOLUME, data, &reply); |
| 170 | return reply.readInt32(); |
| 171 | } |
| 172 | }; |
| 173 | |
| 174 | IMPLEMENT_META_INTERFACE(MediaPlayer, "android.hardware.IMediaPlayer"); |
| 175 | |
| 176 | // ---------------------------------------------------------------------- |
| 177 | |
| 178 | #define CHECK_INTERFACE(interface, data, reply) \ |
| 179 | do { if (!data.enforceInterface(interface::getInterfaceDescriptor())) { \ |
| 180 | LOGW("Call incorrectly routed to " #interface); \ |
| 181 | return PERMISSION_DENIED; \ |
| 182 | } } while (0) |
| 183 | |
| 184 | status_t BnMediaPlayer::onTransact( |
| 185 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 186 | { |
| 187 | switch(code) { |
| 188 | case DISCONNECT: { |
| 189 | CHECK_INTERFACE(IMediaPlayer, data, reply); |
| 190 | disconnect(); |
| 191 | return NO_ERROR; |
| 192 | } break; |
| 193 | case SET_VIDEO_SURFACE: { |
| 194 | CHECK_INTERFACE(IMediaPlayer, data, reply); |
| 195 | sp<ISurface> surface = interface_cast<ISurface>(data.readStrongBinder()); |
| 196 | reply->writeInt32(setVideoSurface(surface)); |
| 197 | return NO_ERROR; |
| 198 | } break; |
| 199 | case PREPARE_ASYNC: { |
| 200 | CHECK_INTERFACE(IMediaPlayer, data, reply); |
| 201 | reply->writeInt32(prepareAsync()); |
| 202 | return NO_ERROR; |
| 203 | } break; |
| 204 | case START: { |
| 205 | CHECK_INTERFACE(IMediaPlayer, data, reply); |
| 206 | reply->writeInt32(start()); |
| 207 | return NO_ERROR; |
| 208 | } break; |
| 209 | case STOP: { |
| 210 | CHECK_INTERFACE(IMediaPlayer, data, reply); |
| 211 | reply->writeInt32(stop()); |
| 212 | return NO_ERROR; |
| 213 | } break; |
| 214 | case IS_PLAYING: { |
| 215 | CHECK_INTERFACE(IMediaPlayer, data, reply); |
| 216 | bool state; |
| 217 | status_t ret = isPlaying(&state); |
| 218 | reply->writeInt32(state); |
| 219 | reply->writeInt32(ret); |
| 220 | return NO_ERROR; |
| 221 | } break; |
| 222 | case PAUSE: { |
| 223 | CHECK_INTERFACE(IMediaPlayer, data, reply); |
| 224 | reply->writeInt32(pause()); |
| 225 | return NO_ERROR; |
| 226 | } break; |
| 227 | case SEEK_TO: { |
| 228 | CHECK_INTERFACE(IMediaPlayer, data, reply); |
| 229 | reply->writeInt32(seekTo(data.readInt32())); |
| 230 | return NO_ERROR; |
| 231 | } break; |
| 232 | case GET_CURRENT_POSITION: { |
| 233 | CHECK_INTERFACE(IMediaPlayer, data, reply); |
| 234 | int msec; |
| 235 | status_t ret = getCurrentPosition(&msec); |
| 236 | reply->writeInt32(msec); |
| 237 | reply->writeInt32(ret); |
| 238 | return NO_ERROR; |
| 239 | } break; |
| 240 | case GET_DURATION: { |
| 241 | CHECK_INTERFACE(IMediaPlayer, data, reply); |
| 242 | int msec; |
| 243 | status_t ret = getDuration(&msec); |
| 244 | reply->writeInt32(msec); |
| 245 | reply->writeInt32(ret); |
| 246 | return NO_ERROR; |
| 247 | } break; |
| 248 | case RESET: { |
| 249 | CHECK_INTERFACE(IMediaPlayer, data, reply); |
| 250 | reply->writeInt32(reset()); |
| 251 | return NO_ERROR; |
| 252 | } break; |
| 253 | case SET_AUDIO_STREAM_TYPE: { |
| 254 | CHECK_INTERFACE(IMediaPlayer, data, reply); |
| 255 | reply->writeInt32(setAudioStreamType(data.readInt32())); |
| 256 | return NO_ERROR; |
| 257 | } break; |
| 258 | case SET_LOOPING: { |
| 259 | CHECK_INTERFACE(IMediaPlayer, data, reply); |
| 260 | reply->writeInt32(setLooping(data.readInt32())); |
| 261 | return NO_ERROR; |
| 262 | } break; |
| 263 | case SET_VOLUME: { |
| 264 | reply->writeInt32(setVolume(data.readFloat(), data.readFloat())); |
| 265 | return NO_ERROR; |
| 266 | } break; |
| 267 | default: |
| 268 | return BBinder::onTransact(code, data, reply, flags); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | // ---------------------------------------------------------------------------- |
| 273 | |
| 274 | }; // namespace android |
| 275 | |