blob: bbc53f3fc3ca240c425f56b49b40e57ffa671678 [file] [log] [blame]
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001/*
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// Proxy for media player implementations
19
20//#define LOG_NDEBUG 0
21#define LOG_TAG "MediaPlayerService"
22#include <utils/Log.h>
23
24#include <sys/types.h>
25#include <sys/stat.h>
Gloria Wang7cf180c2011-02-19 18:37:57 -080026#include <sys/time.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080027#include <dirent.h>
28#include <unistd.h>
29
30#include <string.h>
Mathias Agopian6f74b0c2009-06-03 17:32:49 -070031
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080032#include <cutils/atomic.h>
Nicolas Catania14d27472009-07-13 14:37:49 -070033#include <cutils/properties.h> // for property_get
Mathias Agopian6f74b0c2009-06-03 17:32:49 -070034
35#include <utils/misc.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080036
37#include <android_runtime/ActivityManager.h>
Mathias Agopian6f74b0c2009-06-03 17:32:49 -070038
Mathias Agopian75624082009-05-19 19:08:10 -070039#include <binder/IPCThreadState.h>
40#include <binder/IServiceManager.h>
41#include <binder/MemoryHeapBase.h>
42#include <binder/MemoryBase.h>
Jamie Gennis7dae00b2011-10-26 18:36:31 -070043#include <gui/SurfaceTextureClient.h>
Nicolas Catania1d187f12009-05-12 23:25:55 -070044#include <utils/Errors.h> // for status_t
45#include <utils/String8.h>
Marco Nelissen10dbb8e2009-09-20 10:42:13 -070046#include <utils/SystemClock.h>
Nicolas Catania1d187f12009-05-12 23:25:55 -070047#include <utils/Vector.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080048#include <cutils/properties.h>
49
50#include <media/MediaPlayerInterface.h>
51#include <media/mediarecorder.h>
52#include <media/MediaMetadataRetrieverInterface.h>
nikoa64c8c72009-07-20 15:07:26 -070053#include <media/Metadata.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080054#include <media/AudioTrack.h>
James Dong8635b7b2011-03-14 17:01:38 -070055#include <media/MemoryLeakTrackUtil.h>
Eric Laurent9cb839a2011-09-27 09:48:56 -070056#include <media/stagefright/MediaErrors.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080057
Dima Zavin64760242011-05-11 14:15:23 -070058#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070059
Gloria Wang7cf180c2011-02-19 18:37:57 -080060#include <private/android_filesystem_config.h>
61
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080062#include "MediaRecorderClient.h"
63#include "MediaPlayerService.h"
64#include "MetadataRetrieverClient.h"
65
66#include "MidiFile.h"
Nicolas Catania14d27472009-07-13 14:37:49 -070067#include "TestPlayerStub.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070068#include "StagefrightPlayer.h"
Andreas Huberf9334412010-12-15 15:17:42 -080069#include "nuplayer/NuPlayerDriver.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070070
Andreas Huber20111aa2009-07-14 16:56:47 -070071#include <OMX.h>
Nicolas Catania14d27472009-07-13 14:37:49 -070072
John Grossman761defc2012-02-09 15:09:05 -080073namespace android {
74sp<MediaPlayerBase> createAAH_TXPlayer();
75sp<MediaPlayerBase> createAAH_RXPlayer();
76}
77
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070078namespace {
nikoa64c8c72009-07-20 15:07:26 -070079using android::media::Metadata;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070080using android::status_t;
81using android::OK;
82using android::BAD_VALUE;
83using android::NOT_ENOUGH_DATA;
84using android::Parcel;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070085
86// Max number of entries in the filter.
87const int kMaxFilterSize = 64; // I pulled that out of thin air.
88
nikoa64c8c72009-07-20 15:07:26 -070089// FIXME: Move all the metadata related function in the Metadata.cpp
nikod608a812009-07-16 16:39:53 -070090
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070091
92// Unmarshall a filter from a Parcel.
93// Filter format in a parcel:
94//
95// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
96// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
97// | number of entries (n) |
98// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
99// | metadata type 1 |
100// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
101// | metadata type 2 |
102// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
103// ....
104// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105// | metadata type n |
106// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
107//
108// @param p Parcel that should start with a filter.
109// @param[out] filter On exit contains the list of metadata type to be
110// filtered.
111// @param[out] status On exit contains the status code to be returned.
112// @return true if the parcel starts with a valid filter.
113bool unmarshallFilter(const Parcel& p,
nikoa64c8c72009-07-20 15:07:26 -0700114 Metadata::Filter *filter,
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700115 status_t *status)
116{
Nicolas Catania48290382009-07-10 13:53:06 -0700117 int32_t val;
118 if (p.readInt32(&val) != OK)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700119 {
Steve Block29357bc2012-01-06 19:20:56 +0000120 ALOGE("Failed to read filter's length");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700121 *status = NOT_ENOUGH_DATA;
122 return false;
123 }
124
Nicolas Catania48290382009-07-10 13:53:06 -0700125 if( val > kMaxFilterSize || val < 0)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700126 {
Steve Block29357bc2012-01-06 19:20:56 +0000127 ALOGE("Invalid filter len %d", val);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700128 *status = BAD_VALUE;
129 return false;
130 }
131
Nicolas Catania48290382009-07-10 13:53:06 -0700132 const size_t num = val;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700133
134 filter->clear();
Nicolas Catania48290382009-07-10 13:53:06 -0700135 filter->setCapacity(num);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700136
nikoa64c8c72009-07-20 15:07:26 -0700137 size_t size = num * sizeof(Metadata::Type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700138
Nicolas Catania48290382009-07-10 13:53:06 -0700139
140 if (p.dataAvail() < size)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700141 {
Steve Block29357bc2012-01-06 19:20:56 +0000142 ALOGE("Filter too short expected %d but got %d", size, p.dataAvail());
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700143 *status = NOT_ENOUGH_DATA;
144 return false;
145 }
146
nikoa64c8c72009-07-20 15:07:26 -0700147 const Metadata::Type *data =
148 static_cast<const Metadata::Type*>(p.readInplace(size));
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700149
Nicolas Catania48290382009-07-10 13:53:06 -0700150 if (NULL == data)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700151 {
Steve Block29357bc2012-01-06 19:20:56 +0000152 ALOGE("Filter had no data");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700153 *status = BAD_VALUE;
154 return false;
155 }
156
157 // TODO: The stl impl of vector would be more efficient here
158 // because it degenerates into a memcpy on pod types. Try to
159 // replace later or use stl::set.
Nicolas Catania48290382009-07-10 13:53:06 -0700160 for (size_t i = 0; i < num; ++i)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700161 {
Nicolas Catania48290382009-07-10 13:53:06 -0700162 filter->add(*data);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700163 ++data;
164 }
165 *status = OK;
166 return true;
167}
168
Nicolas Catania48290382009-07-10 13:53:06 -0700169// @param filter Of metadata type.
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700170// @param val To be searched.
171// @return true if a match was found.
nikoa64c8c72009-07-20 15:07:26 -0700172bool findMetadata(const Metadata::Filter& filter, const int32_t val)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700173{
174 // Deal with empty and ANY right away
175 if (filter.isEmpty()) return false;
nikoa64c8c72009-07-20 15:07:26 -0700176 if (filter[0] == Metadata::kAny) return true;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700177
Nicolas Catania48290382009-07-10 13:53:06 -0700178 return filter.indexOf(val) >= 0;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700179}
180
181} // anonymous namespace
182
183
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800184namespace android {
185
Dave Burked681bbb2011-08-30 14:39:17 +0100186static bool checkPermission(const char* permissionString) {
187#ifndef HAVE_ANDROID_OS
188 return true;
189#endif
190 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
191 bool ok = checkCallingPermission(String16(permissionString));
Steve Block29357bc2012-01-06 19:20:56 +0000192 if (!ok) ALOGE("Request requires %s", permissionString);
Dave Burked681bbb2011-08-30 14:39:17 +0100193 return ok;
194}
195
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800196// TODO: Temp hack until we can register players
197typedef struct {
198 const char *extension;
199 const player_type playertype;
200} extmap;
201extmap FILE_EXTS [] = {
202 {".mid", SONIVOX_PLAYER},
203 {".midi", SONIVOX_PLAYER},
204 {".smf", SONIVOX_PLAYER},
205 {".xmf", SONIVOX_PLAYER},
206 {".imy", SONIVOX_PLAYER},
207 {".rtttl", SONIVOX_PLAYER},
208 {".rtx", SONIVOX_PLAYER},
209 {".ota", SONIVOX_PLAYER},
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800210};
211
212// TODO: Find real cause of Audio/Video delay in PV framework and remove this workaround
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800213/* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4;
214/* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false;
215
216void MediaPlayerService::instantiate() {
217 defaultServiceManager()->addService(
218 String16("media.player"), new MediaPlayerService());
219}
220
221MediaPlayerService::MediaPlayerService()
222{
Steve Block3856b092011-10-20 11:56:00 +0100223 ALOGV("MediaPlayerService created");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800224 mNextConnId = 1;
Gloria Wang9ee159b2011-02-24 14:51:45 -0800225
226 mBatteryAudio.refCount = 0;
227 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
228 mBatteryAudio.deviceOn[i] = 0;
229 mBatteryAudio.lastTime[i] = 0;
230 mBatteryAudio.totalTime[i] = 0;
231 }
232 // speaker is on by default
233 mBatteryAudio.deviceOn[SPEAKER] = 1;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800234}
235
236MediaPlayerService::~MediaPlayerService()
237{
Steve Block3856b092011-10-20 11:56:00 +0100238 ALOGV("MediaPlayerService destroyed");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800239}
240
241sp<IMediaRecorder> MediaPlayerService::createMediaRecorder(pid_t pid)
242{
Gloria Wangdac6a312009-10-29 15:46:37 -0700243 sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid);
244 wp<MediaRecorderClient> w = recorder;
245 Mutex::Autolock lock(mLock);
246 mMediaRecorderClients.add(w);
Steve Block3856b092011-10-20 11:56:00 +0100247 ALOGV("Create new media recorder client from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800248 return recorder;
249}
250
Gloria Wangdac6a312009-10-29 15:46:37 -0700251void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client)
252{
253 Mutex::Autolock lock(mLock);
254 mMediaRecorderClients.remove(client);
Steve Block3856b092011-10-20 11:56:00 +0100255 ALOGV("Delete media recorder client");
Gloria Wangdac6a312009-10-29 15:46:37 -0700256}
257
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800258sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever(pid_t pid)
259{
260 sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
Steve Block3856b092011-10-20 11:56:00 +0100261 ALOGV("Create new media retriever from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800262 return retriever;
263}
264
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800265sp<IMediaPlayer> MediaPlayerService::create(pid_t pid, const sp<IMediaPlayerClient>& client,
Dave Burked681bbb2011-08-30 14:39:17 +0100266 int audioSessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800267{
268 int32_t connId = android_atomic_inc(&mNextConnId);
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700269
270 sp<Client> c = new Client(
271 this, pid, connId, client, audioSessionId,
272 IPCThreadState::self()->getCallingUid());
273
Steve Block3856b092011-10-20 11:56:00 +0100274 ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid,
Dave Burked681bbb2011-08-30 14:39:17 +0100275 IPCThreadState::self()->getCallingUid());
276
277 wp<Client> w = c;
278 {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800279 Mutex::Autolock lock(mLock);
280 mClients.add(w);
281 }
Andreas Hubere2b10282010-11-23 11:41:34 -0800282 return c;
283}
284
Andreas Huber318ad9c2009-10-15 13:46:54 -0700285sp<IOMX> MediaPlayerService::getOMX() {
286 Mutex::Autolock autoLock(mLock);
287
288 if (mOMX.get() == NULL) {
289 mOMX = new OMX;
290 }
291
292 return mOMX;
Andreas Huber20111aa2009-07-14 16:56:47 -0700293}
294
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800295status_t MediaPlayerService::AudioCache::dump(int fd, const Vector<String16>& args) const
296{
297 const size_t SIZE = 256;
298 char buffer[SIZE];
299 String8 result;
300
301 result.append(" AudioCache\n");
302 if (mHeap != 0) {
303 snprintf(buffer, 255, " heap base(%p), size(%d), flags(%d), device(%s)\n",
304 mHeap->getBase(), mHeap->getSize(), mHeap->getFlags(), mHeap->getDevice());
305 result.append(buffer);
306 }
307 snprintf(buffer, 255, " msec per frame(%f), channel count(%d), format(%d), frame count(%ld)\n",
308 mMsecsPerFrame, mChannelCount, mFormat, mFrameCount);
309 result.append(buffer);
310 snprintf(buffer, 255, " sample rate(%d), size(%d), error(%d), command complete(%s)\n",
311 mSampleRate, mSize, mError, mCommandComplete?"true":"false");
312 result.append(buffer);
313 ::write(fd, result.string(), result.size());
314 return NO_ERROR;
315}
316
317status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
318{
319 const size_t SIZE = 256;
320 char buffer[SIZE];
321 String8 result;
322
323 result.append(" AudioOutput\n");
324 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n",
325 mStreamType, mLeftVolume, mRightVolume);
326 result.append(buffer);
327 snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
Eric Laurentdb354e52012-03-05 17:27:11 -0800328 mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800329 result.append(buffer);
Eric Laurent2beeb502010-07-16 07:43:46 -0700330 snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
331 mAuxEffectId, mSendLevel);
332 result.append(buffer);
333
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800334 ::write(fd, result.string(), result.size());
335 if (mTrack != 0) {
336 mTrack->dump(fd, args);
337 }
338 return NO_ERROR;
339}
340
341status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args) const
342{
343 const size_t SIZE = 256;
344 char buffer[SIZE];
345 String8 result;
346 result.append(" Client\n");
347 snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n",
348 mPid, mConnId, mStatus, mLoop?"true": "false");
349 result.append(buffer);
350 write(fd, result.string(), result.size());
Andreas Hubera0b1d4b2011-06-07 15:52:25 -0700351 if (mPlayer != NULL) {
352 mPlayer->dump(fd, args);
353 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800354 if (mAudioOutput != 0) {
355 mAudioOutput->dump(fd, args);
356 }
357 write(fd, "\n", 1);
358 return NO_ERROR;
359}
360
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800361status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
362{
363 const size_t SIZE = 256;
364 char buffer[SIZE];
365 String8 result;
366 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
367 snprintf(buffer, SIZE, "Permission Denial: "
368 "can't dump MediaPlayerService from pid=%d, uid=%d\n",
369 IPCThreadState::self()->getCallingPid(),
370 IPCThreadState::self()->getCallingUid());
371 result.append(buffer);
372 } else {
373 Mutex::Autolock lock(mLock);
374 for (int i = 0, n = mClients.size(); i < n; ++i) {
375 sp<Client> c = mClients[i].promote();
376 if (c != 0) c->dump(fd, args);
377 }
James Dongb9141222010-07-08 11:16:11 -0700378 if (mMediaRecorderClients.size() == 0) {
379 result.append(" No media recorder client\n\n");
380 } else {
381 for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
382 sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
James Donge579e282011-10-18 22:29:20 -0700383 if (c != 0) {
384 snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid);
385 result.append(buffer);
386 write(fd, result.string(), result.size());
387 result = "\n";
388 c->dump(fd, args);
389 }
James Dongb9141222010-07-08 11:16:11 -0700390 }
Gloria Wangdac6a312009-10-29 15:46:37 -0700391 }
392
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800393 result.append(" Files opened and/or mapped:\n");
Glenn Kasten0512ab52011-05-04 17:58:57 -0700394 snprintf(buffer, SIZE, "/proc/%d/maps", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800395 FILE *f = fopen(buffer, "r");
396 if (f) {
397 while (!feof(f)) {
398 fgets(buffer, SIZE, f);
Dave Sparks02fa8342010-09-27 16:55:18 -0700399 if (strstr(buffer, " /mnt/sdcard/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800400 strstr(buffer, " /system/sounds/") ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700401 strstr(buffer, " /data/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800402 strstr(buffer, " /system/media/")) {
403 result.append(" ");
404 result.append(buffer);
405 }
406 }
407 fclose(f);
408 } else {
409 result.append("couldn't open ");
410 result.append(buffer);
411 result.append("\n");
412 }
413
Glenn Kasten0512ab52011-05-04 17:58:57 -0700414 snprintf(buffer, SIZE, "/proc/%d/fd", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800415 DIR *d = opendir(buffer);
416 if (d) {
417 struct dirent *ent;
418 while((ent = readdir(d)) != NULL) {
419 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
Glenn Kasten0512ab52011-05-04 17:58:57 -0700420 snprintf(buffer, SIZE, "/proc/%d/fd/%s", gettid(), ent->d_name);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800421 struct stat s;
422 if (lstat(buffer, &s) == 0) {
423 if ((s.st_mode & S_IFMT) == S_IFLNK) {
424 char linkto[256];
425 int len = readlink(buffer, linkto, sizeof(linkto));
426 if(len > 0) {
427 if(len > 255) {
428 linkto[252] = '.';
429 linkto[253] = '.';
430 linkto[254] = '.';
431 linkto[255] = 0;
432 } else {
433 linkto[len] = 0;
434 }
Dave Sparks02fa8342010-09-27 16:55:18 -0700435 if (strstr(linkto, "/mnt/sdcard/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800436 strstr(linkto, "/system/sounds/") == linkto ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700437 strstr(linkto, "/data/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800438 strstr(linkto, "/system/media/") == linkto) {
439 result.append(" ");
440 result.append(buffer);
441 result.append(" -> ");
442 result.append(linkto);
443 result.append("\n");
444 }
445 }
446 } else {
447 result.append(" unexpected type for ");
448 result.append(buffer);
449 result.append("\n");
450 }
451 }
452 }
453 }
454 closedir(d);
455 } else {
456 result.append("couldn't open ");
457 result.append(buffer);
458 result.append("\n");
459 }
460
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800461 bool dumpMem = false;
462 for (size_t i = 0; i < args.size(); i++) {
463 if (args[i] == String16("-m")) {
464 dumpMem = true;
465 }
466 }
467 if (dumpMem) {
James Dong8635b7b2011-03-14 17:01:38 -0700468 dumpMemoryAddresses(fd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800469 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800470 }
471 write(fd, result.string(), result.size());
472 return NO_ERROR;
473}
474
475void MediaPlayerService::removeClient(wp<Client> client)
476{
477 Mutex::Autolock lock(mLock);
478 mClients.remove(client);
479}
480
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700481MediaPlayerService::Client::Client(
482 const sp<MediaPlayerService>& service, pid_t pid,
483 int32_t connId, const sp<IMediaPlayerClient>& client,
484 int audioSessionId, uid_t uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800485{
Steve Block3856b092011-10-20 11:56:00 +0100486 ALOGV("Client(%d) constructor", connId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800487 mPid = pid;
488 mConnId = connId;
489 mService = service;
490 mClient = client;
491 mLoop = false;
492 mStatus = NO_INIT;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700493 mAudioSessionId = audioSessionId;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700494 mUID = uid;
John Grossmanc795b642012-02-22 15:38:35 -0800495 mRetransmitEndpointValid = false;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700496
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800497#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000498 ALOGD("create Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800499 mAntagonizer = new Antagonizer(notify, this);
500#endif
501}
502
503MediaPlayerService::Client::~Client()
504{
Steve Block3856b092011-10-20 11:56:00 +0100505 ALOGV("Client(%d) destructor pid = %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800506 mAudioOutput.clear();
507 wp<Client> client(this);
508 disconnect();
509 mService->removeClient(client);
510}
511
512void MediaPlayerService::Client::disconnect()
513{
Steve Block3856b092011-10-20 11:56:00 +0100514 ALOGV("disconnect(%d) from pid %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800515 // grab local reference and clear main reference to prevent future
516 // access to object
517 sp<MediaPlayerBase> p;
518 {
519 Mutex::Autolock l(mLock);
520 p = mPlayer;
521 }
Dave Sparks795fa582009-03-24 17:57:12 -0700522 mClient.clear();
Andreas Huber20111aa2009-07-14 16:56:47 -0700523
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800524 mPlayer.clear();
525
526 // clear the notification to prevent callbacks to dead client
527 // and reset the player. We assume the player will serialize
528 // access to itself if necessary.
529 if (p != 0) {
530 p->setNotifyCallback(0, 0);
531#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000532 ALOGD("kill Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800533 mAntagonizer->kill();
534#endif
535 p->reset();
536 }
537
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700538 disconnectNativeWindow();
539
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800540 IPCThreadState::self()->flushCommands();
541}
542
Andreas Huber47f59cf2009-08-07 09:30:32 -0700543static player_type getDefaultPlayerType() {
Andreas Huber608d77b2010-06-23 16:40:57 -0700544 return STAGEFRIGHT_PLAYER;
Andreas Huber47f59cf2009-08-07 09:30:32 -0700545}
546
James Dong148c1a22009-09-06 14:29:45 -0700547player_type getPlayerType(int fd, int64_t offset, int64_t length)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800548{
549 char buf[20];
550 lseek(fd, offset, SEEK_SET);
551 read(fd, buf, sizeof(buf));
552 lseek(fd, offset, SEEK_SET);
553
554 long ident = *((long*)buf);
555
556 // Ogg vorbis?
557 if (ident == 0x5367674f) // 'OggS'
Andreas Huber608d77b2010-06-23 16:40:57 -0700558 return STAGEFRIGHT_PLAYER;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800559
560 // Some kind of MIDI?
561 EAS_DATA_HANDLE easdata;
562 if (EAS_Init(&easdata) == EAS_SUCCESS) {
563 EAS_FILE locator;
564 locator.path = NULL;
565 locator.fd = fd;
566 locator.offset = offset;
567 locator.length = length;
568 EAS_HANDLE eashandle;
569 if (EAS_OpenFile(easdata, &locator, &eashandle) == EAS_SUCCESS) {
570 EAS_CloseFile(easdata, eashandle);
571 EAS_Shutdown(easdata);
572 return SONIVOX_PLAYER;
573 }
574 EAS_Shutdown(easdata);
575 }
576
Andreas Huber47f59cf2009-08-07 09:30:32 -0700577 return getDefaultPlayerType();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800578}
579
James Dong148c1a22009-09-06 14:29:45 -0700580player_type getPlayerType(const char* url)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800581{
Nicolas Catania14d27472009-07-13 14:37:49 -0700582 if (TestPlayerStub::canBeUsed(url)) {
583 return TEST_PLAYER;
584 }
585
Andreas Huber0a73d812011-03-21 16:28:04 -0700586 if (!strncasecmp("http://", url, 7)
587 || !strncasecmp("https://", url, 8)) {
Andreas Hubered8d14f2011-02-16 09:05:38 -0800588 size_t len = strlen(url);
589 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
590 return NU_PLAYER;
591 }
Andreas Huber5bc087c2010-12-23 10:27:40 -0800592
Andreas Hubered8d14f2011-02-16 09:05:38 -0800593 if (strstr(url,"m3u8")) {
594 return NU_PLAYER;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800595 }
596 }
597
Andreas Huber2bfdd422011-10-11 15:24:07 -0700598 if (!strncasecmp("rtsp://", url, 7)) {
599 return NU_PLAYER;
600 }
601
John Grossman761defc2012-02-09 15:09:05 -0800602 if (!strncasecmp("aahRX://", url, 8)) {
603 return AAH_RX_PLAYER;
604 }
605
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800606 // use MidiFile for MIDI extensions
607 int lenURL = strlen(url);
608 for (int i = 0; i < NELEM(FILE_EXTS); ++i) {
609 int len = strlen(FILE_EXTS[i].extension);
610 int start = lenURL - len;
611 if (start > 0) {
Atsushi Enofc1c7b92010-03-19 23:18:02 +0900612 if (!strncasecmp(url + start, FILE_EXTS[i].extension, len)) {
Andreas Huber608d77b2010-06-23 16:40:57 -0700613 return FILE_EXTS[i].playertype;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800614 }
615 }
616 }
617
Andreas Huber47f59cf2009-08-07 09:30:32 -0700618 return getDefaultPlayerType();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800619}
620
John Grossmanc795b642012-02-22 15:38:35 -0800621player_type MediaPlayerService::Client::getPlayerType(int fd,
622 int64_t offset,
623 int64_t length)
624{
625 // Until re-transmit functionality is added to the existing core android
626 // players, we use the special AAH TX player whenever we were configured
627 // for retransmission.
628 if (mRetransmitEndpointValid) {
629 return AAH_TX_PLAYER;
630 }
631
632 return android::getPlayerType(fd, offset, length);
633}
634
635player_type MediaPlayerService::Client::getPlayerType(const char* url)
636{
637 // Until re-transmit functionality is added to the existing core android
638 // players, we use the special AAH TX player whenever we were configured
639 // for retransmission.
640 if (mRetransmitEndpointValid) {
641 return AAH_TX_PLAYER;
642 }
643
644 return android::getPlayerType(url);
645}
646
647player_type MediaPlayerService::Client::getPlayerType(
648 const sp<IStreamSource> &source) {
649 // Until re-transmit functionality is added to the existing core android
650 // players, we use the special AAH TX player whenever we were configured
651 // for retransmission.
652 if (mRetransmitEndpointValid) {
653 return AAH_TX_PLAYER;
654 }
655
656 return NU_PLAYER;
657}
658
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800659static sp<MediaPlayerBase> createPlayer(player_type playerType, void* cookie,
660 notify_callback_f notifyFunc)
661{
662 sp<MediaPlayerBase> p;
663 switch (playerType) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800664 case SONIVOX_PLAYER:
Steve Block3856b092011-10-20 11:56:00 +0100665 ALOGV(" create MidiFile");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800666 p = new MidiFile();
667 break;
Andreas Huber20111aa2009-07-14 16:56:47 -0700668 case STAGEFRIGHT_PLAYER:
Steve Block3856b092011-10-20 11:56:00 +0100669 ALOGV(" create StagefrightPlayer");
Andreas Huber20111aa2009-07-14 16:56:47 -0700670 p = new StagefrightPlayer;
671 break;
Andreas Huberf9334412010-12-15 15:17:42 -0800672 case NU_PLAYER:
Steve Block3856b092011-10-20 11:56:00 +0100673 ALOGV(" create NuPlayer");
Andreas Huberf9334412010-12-15 15:17:42 -0800674 p = new NuPlayerDriver;
675 break;
Nicolas Catania14d27472009-07-13 14:37:49 -0700676 case TEST_PLAYER:
Steve Block3856b092011-10-20 11:56:00 +0100677 ALOGV("Create Test Player stub");
Nicolas Catania14d27472009-07-13 14:37:49 -0700678 p = new TestPlayerStub();
679 break;
John Grossman761defc2012-02-09 15:09:05 -0800680 case AAH_RX_PLAYER:
681 ALOGV(" create A@H RX Player");
682 p = createAAH_RXPlayer();
683 break;
684 case AAH_TX_PLAYER:
685 ALOGV(" create A@H TX Player");
686 p = createAAH_TXPlayer();
687 break;
James Dong30d713a2010-11-10 18:42:40 -0800688 default:
Steve Block29357bc2012-01-06 19:20:56 +0000689 ALOGE("Unknown player type: %d", playerType);
James Dong30d713a2010-11-10 18:42:40 -0800690 return NULL;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800691 }
692 if (p != NULL) {
693 if (p->initCheck() == NO_ERROR) {
694 p->setNotifyCallback(cookie, notifyFunc);
695 } else {
696 p.clear();
697 }
698 }
699 if (p == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000700 ALOGE("Failed to create player object");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800701 }
702 return p;
703}
704
705sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
706{
707 // determine if we have the right player type
708 sp<MediaPlayerBase> p = mPlayer;
709 if ((p != NULL) && (p->playerType() != playerType)) {
Steve Block3856b092011-10-20 11:56:00 +0100710 ALOGV("delete player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800711 p.clear();
712 }
713 if (p == NULL) {
714 p = android::createPlayer(playerType, this, notify);
715 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700716
Jason Simmonsdb29e522011-08-12 13:46:55 -0700717 if (p != NULL) {
718 p->setUID(mUID);
719 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700720
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800721 return p;
722}
723
John Grossmanc795b642012-02-22 15:38:35 -0800724sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre(
725 player_type playerType)
726{
727 ALOGV("player type = %d", playerType);
728
729 // create the right type of player
730 sp<MediaPlayerBase> p = createPlayer(playerType);
731 if (p == NULL) {
732 return p;
733 }
734
735 if (!p->hardwareOutput()) {
736 mAudioOutput = new AudioOutput(mAudioSessionId);
737 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
738 }
739
740 return p;
741}
742
743void MediaPlayerService::Client::setDataSource_post(
744 const sp<MediaPlayerBase>& p,
745 status_t status)
746{
747 ALOGV(" setDataSource");
748 mStatus = status;
749 if (mStatus != OK) {
750 ALOGE(" error: %d", mStatus);
751 return;
752 }
753
754 // Set the re-transmission endpoint if one was chosen.
755 if (mRetransmitEndpointValid) {
756 mStatus = p->setRetransmitEndpoint(&mRetransmitEndpoint);
757 if (mStatus != NO_ERROR) {
758 ALOGE("setRetransmitEndpoint error: %d", mStatus);
759 }
760 }
761
762 if (mStatus == OK) {
763 mPlayer = p;
764 }
765}
766
Andreas Huber2db84552010-01-28 11:19:57 -0800767status_t MediaPlayerService::Client::setDataSource(
768 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800769{
Steve Block3856b092011-10-20 11:56:00 +0100770 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800771 if (url == NULL)
772 return UNKNOWN_ERROR;
773
Dave Burked681bbb2011-08-30 14:39:17 +0100774 if ((strncmp(url, "http://", 7) == 0) ||
775 (strncmp(url, "https://", 8) == 0) ||
776 (strncmp(url, "rtsp://", 7) == 0)) {
777 if (!checkPermission("android.permission.INTERNET")) {
778 return PERMISSION_DENIED;
779 }
780 }
781
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800782 if (strncmp(url, "content://", 10) == 0) {
783 // get a filedescriptor for the content Uri and
784 // pass it to the setDataSource(fd) method
785
786 String16 url16(url);
787 int fd = android::openContentProviderFile(url16);
788 if (fd < 0)
789 {
Steve Block29357bc2012-01-06 19:20:56 +0000790 ALOGE("Couldn't open fd for %s", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800791 return UNKNOWN_ERROR;
792 }
793 setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
794 close(fd);
795 return mStatus;
796 } else {
797 player_type playerType = getPlayerType(url);
John Grossmanc795b642012-02-22 15:38:35 -0800798 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
799 if (p == NULL) {
800 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800801 }
802
John Grossmanc795b642012-02-22 15:38:35 -0800803 setDataSource_post(p, p->setDataSource(url, headers));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800804 return mStatus;
805 }
806}
807
808status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
809{
Steve Block3856b092011-10-20 11:56:00 +0100810 ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800811 struct stat sb;
812 int ret = fstat(fd, &sb);
813 if (ret != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000814 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800815 return UNKNOWN_ERROR;
816 }
817
Steve Block3856b092011-10-20 11:56:00 +0100818 ALOGV("st_dev = %llu", sb.st_dev);
819 ALOGV("st_mode = %u", sb.st_mode);
820 ALOGV("st_uid = %lu", sb.st_uid);
821 ALOGV("st_gid = %lu", sb.st_gid);
822 ALOGV("st_size = %llu", sb.st_size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800823
824 if (offset >= sb.st_size) {
Steve Block29357bc2012-01-06 19:20:56 +0000825 ALOGE("offset error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800826 ::close(fd);
827 return UNKNOWN_ERROR;
828 }
829 if (offset + length > sb.st_size) {
830 length = sb.st_size - offset;
Steve Block3856b092011-10-20 11:56:00 +0100831 ALOGV("calculated length = %lld", length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800832 }
833
John Grossmanc795b642012-02-22 15:38:35 -0800834 // Until re-transmit functionality is added to the existing core android
835 // players, we use the special AAH TX player whenever we were configured for
836 // retransmission.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800837 player_type playerType = getPlayerType(fd, offset, length);
John Grossmanc795b642012-02-22 15:38:35 -0800838 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
839 if (p == NULL) {
840 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800841 }
842
843 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800844 setDataSource_post(p, p->setDataSource(fd, offset, length));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800845 return mStatus;
846}
847
Andreas Hubere2b10282010-11-23 11:41:34 -0800848status_t MediaPlayerService::Client::setDataSource(
849 const sp<IStreamSource> &source) {
850 // create the right type of player
John Grossmanc795b642012-02-22 15:38:35 -0800851 // Until re-transmit functionality is added to the existing core android
852 // players, we use the special AAH TX player whenever we were configured for
853 // retransmission.
854 player_type playerType = getPlayerType(source);
855 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
Andreas Hubere2b10282010-11-23 11:41:34 -0800856 if (p == NULL) {
857 return NO_INIT;
858 }
859
Andreas Hubere2b10282010-11-23 11:41:34 -0800860 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800861 setDataSource_post(p, p->setDataSource(source));
Andreas Hubere2b10282010-11-23 11:41:34 -0800862 return mStatus;
863}
864
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700865void MediaPlayerService::Client::disconnectNativeWindow() {
866 if (mConnectedWindow != NULL) {
867 status_t err = native_window_api_disconnect(mConnectedWindow.get(),
868 NATIVE_WINDOW_API_MEDIA);
869
870 if (err != OK) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000871 ALOGW("native_window_api_disconnect returned an error: %s (%d)",
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700872 strerror(-err), err);
873 }
874 }
875 mConnectedWindow.clear();
876}
877
Glenn Kasten11731182011-02-08 17:26:17 -0800878status_t MediaPlayerService::Client::setVideoSurfaceTexture(
879 const sp<ISurfaceTexture>& surfaceTexture)
880{
Steve Block3856b092011-10-20 11:56:00 +0100881 ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, surfaceTexture.get());
Glenn Kasten11731182011-02-08 17:26:17 -0800882 sp<MediaPlayerBase> p = getPlayer();
883 if (p == 0) return UNKNOWN_ERROR;
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700884
885 sp<IBinder> binder(surfaceTexture == NULL ? NULL :
886 surfaceTexture->asBinder());
887 if (mConnectedWindowBinder == binder) {
888 return OK;
889 }
890
891 sp<ANativeWindow> anw;
892 if (surfaceTexture != NULL) {
893 anw = new SurfaceTextureClient(surfaceTexture);
894 status_t err = native_window_api_connect(anw.get(),
895 NATIVE_WINDOW_API_MEDIA);
896
897 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000898 ALOGE("setVideoSurfaceTexture failed: %d", err);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700899 // Note that we must do the reset before disconnecting from the ANW.
900 // Otherwise queue/dequeue calls could be made on the disconnected
901 // ANW, which may result in errors.
902 reset();
903
904 disconnectNativeWindow();
905
906 return err;
907 }
908 }
909
910 // Note that we must set the player's new SurfaceTexture before
911 // disconnecting the old one. Otherwise queue/dequeue calls could be made
912 // on the disconnected ANW, which may result in errors.
913 status_t err = p->setVideoSurfaceTexture(surfaceTexture);
914
915 disconnectNativeWindow();
916
917 mConnectedWindow = anw;
918
919 if (err == OK) {
920 mConnectedWindowBinder = binder;
921 } else {
922 disconnectNativeWindow();
923 }
924
925 return err;
Glenn Kasten11731182011-02-08 17:26:17 -0800926}
927
Nicolas Catania1d187f12009-05-12 23:25:55 -0700928status_t MediaPlayerService::Client::invoke(const Parcel& request,
929 Parcel *reply)
930{
931 sp<MediaPlayerBase> p = getPlayer();
932 if (p == NULL) return UNKNOWN_ERROR;
933 return p->invoke(request, reply);
934}
935
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700936// This call doesn't need to access the native player.
937status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
938{
939 status_t status;
nikoa64c8c72009-07-20 15:07:26 -0700940 media::Metadata::Filter allow, drop;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700941
Nicolas Catania48290382009-07-10 13:53:06 -0700942 if (unmarshallFilter(filter, &allow, &status) &&
943 unmarshallFilter(filter, &drop, &status)) {
944 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700945
946 mMetadataAllow = allow;
947 mMetadataDrop = drop;
948 }
949 return status;
950}
951
Nicolas Catania48290382009-07-10 13:53:06 -0700952status_t MediaPlayerService::Client::getMetadata(
953 bool update_only, bool apply_filter, Parcel *reply)
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700954{
nikoa64c8c72009-07-20 15:07:26 -0700955 sp<MediaPlayerBase> player = getPlayer();
956 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania48290382009-07-10 13:53:06 -0700957
nikod608a812009-07-16 16:39:53 -0700958 status_t status;
959 // Placeholder for the return code, updated by the caller.
960 reply->writeInt32(-1);
961
nikoa64c8c72009-07-20 15:07:26 -0700962 media::Metadata::Filter ids;
Nicolas Catania48290382009-07-10 13:53:06 -0700963
964 // We don't block notifications while we fetch the data. We clear
965 // mMetadataUpdated first so we don't lose notifications happening
966 // during the rest of this call.
967 {
968 Mutex::Autolock lock(mLock);
969 if (update_only) {
nikod608a812009-07-16 16:39:53 -0700970 ids = mMetadataUpdated;
Nicolas Catania48290382009-07-10 13:53:06 -0700971 }
972 mMetadataUpdated.clear();
973 }
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700974
nikoa64c8c72009-07-20 15:07:26 -0700975 media::Metadata metadata(reply);
Nicolas Catania48290382009-07-10 13:53:06 -0700976
nikoa64c8c72009-07-20 15:07:26 -0700977 metadata.appendHeader();
978 status = player->getMetadata(ids, reply);
nikod608a812009-07-16 16:39:53 -0700979
980 if (status != OK) {
nikoa64c8c72009-07-20 15:07:26 -0700981 metadata.resetParcel();
Steve Block29357bc2012-01-06 19:20:56 +0000982 ALOGE("getMetadata failed %d", status);
nikod608a812009-07-16 16:39:53 -0700983 return status;
984 }
985
986 // FIXME: Implement filtering on the result. Not critical since
987 // filtering takes place on the update notifications already. This
988 // would be when all the metadata are fetch and a filter is set.
989
nikod608a812009-07-16 16:39:53 -0700990 // Everything is fine, update the metadata length.
nikoa64c8c72009-07-20 15:07:26 -0700991 metadata.updateLength();
nikod608a812009-07-16 16:39:53 -0700992 return OK;
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700993}
994
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800995status_t MediaPlayerService::Client::prepareAsync()
996{
Steve Block3856b092011-10-20 11:56:00 +0100997 ALOGV("[%d] prepareAsync", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800998 sp<MediaPlayerBase> p = getPlayer();
999 if (p == 0) return UNKNOWN_ERROR;
1000 status_t ret = p->prepareAsync();
1001#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +00001002 ALOGD("start Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001003 if (ret == NO_ERROR) mAntagonizer->start();
1004#endif
1005 return ret;
1006}
1007
1008status_t MediaPlayerService::Client::start()
1009{
Steve Block3856b092011-10-20 11:56:00 +01001010 ALOGV("[%d] start", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001011 sp<MediaPlayerBase> p = getPlayer();
1012 if (p == 0) return UNKNOWN_ERROR;
1013 p->setLooping(mLoop);
1014 return p->start();
1015}
1016
1017status_t MediaPlayerService::Client::stop()
1018{
Steve Block3856b092011-10-20 11:56:00 +01001019 ALOGV("[%d] stop", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001020 sp<MediaPlayerBase> p = getPlayer();
1021 if (p == 0) return UNKNOWN_ERROR;
1022 return p->stop();
1023}
1024
1025status_t MediaPlayerService::Client::pause()
1026{
Steve Block3856b092011-10-20 11:56:00 +01001027 ALOGV("[%d] pause", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001028 sp<MediaPlayerBase> p = getPlayer();
1029 if (p == 0) return UNKNOWN_ERROR;
1030 return p->pause();
1031}
1032
1033status_t MediaPlayerService::Client::isPlaying(bool* state)
1034{
1035 *state = false;
1036 sp<MediaPlayerBase> p = getPlayer();
1037 if (p == 0) return UNKNOWN_ERROR;
1038 *state = p->isPlaying();
Steve Block3856b092011-10-20 11:56:00 +01001039 ALOGV("[%d] isPlaying: %d", mConnId, *state);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001040 return NO_ERROR;
1041}
1042
1043status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
1044{
Steve Block3856b092011-10-20 11:56:00 +01001045 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001046 sp<MediaPlayerBase> p = getPlayer();
1047 if (p == 0) return UNKNOWN_ERROR;
1048 status_t ret = p->getCurrentPosition(msec);
1049 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01001050 ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001051 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001052 ALOGE("getCurrentPosition returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001053 }
1054 return ret;
1055}
1056
1057status_t MediaPlayerService::Client::getDuration(int *msec)
1058{
Steve Block3856b092011-10-20 11:56:00 +01001059 ALOGV("getDuration");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001060 sp<MediaPlayerBase> p = getPlayer();
1061 if (p == 0) return UNKNOWN_ERROR;
1062 status_t ret = p->getDuration(msec);
1063 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01001064 ALOGV("[%d] getDuration = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001065 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001066 ALOGE("getDuration returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001067 }
1068 return ret;
1069}
1070
1071status_t MediaPlayerService::Client::seekTo(int msec)
1072{
Steve Block3856b092011-10-20 11:56:00 +01001073 ALOGV("[%d] seekTo(%d)", mConnId, msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001074 sp<MediaPlayerBase> p = getPlayer();
1075 if (p == 0) return UNKNOWN_ERROR;
1076 return p->seekTo(msec);
1077}
1078
1079status_t MediaPlayerService::Client::reset()
1080{
Steve Block3856b092011-10-20 11:56:00 +01001081 ALOGV("[%d] reset", mConnId);
John Grossmanc795b642012-02-22 15:38:35 -08001082 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001083 sp<MediaPlayerBase> p = getPlayer();
1084 if (p == 0) return UNKNOWN_ERROR;
1085 return p->reset();
1086}
1087
Glenn Kastenfff6d712012-01-12 16:38:12 -08001088status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001089{
Steve Block3856b092011-10-20 11:56:00 +01001090 ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001091 // TODO: for hardware output, call player instead
1092 Mutex::Autolock l(mLock);
1093 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
1094 return NO_ERROR;
1095}
1096
1097status_t MediaPlayerService::Client::setLooping(int loop)
1098{
Steve Block3856b092011-10-20 11:56:00 +01001099 ALOGV("[%d] setLooping(%d)", mConnId, loop);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001100 mLoop = loop;
1101 sp<MediaPlayerBase> p = getPlayer();
1102 if (p != 0) return p->setLooping(loop);
1103 return NO_ERROR;
1104}
1105
1106status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
1107{
Steve Block3856b092011-10-20 11:56:00 +01001108 ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
John Grossman761defc2012-02-09 15:09:05 -08001109
1110 // for hardware output, call player instead
1111 sp<MediaPlayerBase> p = getPlayer();
1112 {
1113 Mutex::Autolock l(mLock);
1114 if (p != 0 && p->hardwareOutput()) {
1115 MediaPlayerHWInterface* hwp =
1116 reinterpret_cast<MediaPlayerHWInterface*>(p.get());
1117 return hwp->setVolume(leftVolume, rightVolume);
1118 } else {
1119 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
1120 return NO_ERROR;
1121 }
1122 }
1123
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001124 return NO_ERROR;
1125}
1126
Eric Laurent2beeb502010-07-16 07:43:46 -07001127status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
1128{
Steve Block3856b092011-10-20 11:56:00 +01001129 ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001130 Mutex::Autolock l(mLock);
1131 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
1132 return NO_ERROR;
1133}
1134
1135status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
1136{
Steve Block3856b092011-10-20 11:56:00 +01001137 ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001138 Mutex::Autolock l(mLock);
1139 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1140 return NO_ERROR;
1141}
Nicolas Catania48290382009-07-10 13:53:06 -07001142
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001143status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
Steve Block3856b092011-10-20 11:56:00 +01001144 ALOGV("[%d] setParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001145 sp<MediaPlayerBase> p = getPlayer();
1146 if (p == 0) return UNKNOWN_ERROR;
1147 return p->setParameter(key, request);
1148}
1149
1150status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
Steve Block3856b092011-10-20 11:56:00 +01001151 ALOGV("[%d] getParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001152 sp<MediaPlayerBase> p = getPlayer();
1153 if (p == 0) return UNKNOWN_ERROR;
1154 return p->getParameter(key, reply);
1155}
1156
John Grossmanc795b642012-02-22 15:38:35 -08001157status_t MediaPlayerService::Client::setRetransmitEndpoint(
1158 const struct sockaddr_in* endpoint) {
1159
1160 if (NULL != endpoint) {
1161 uint32_t a = ntohl(endpoint->sin_addr.s_addr);
1162 uint16_t p = ntohs(endpoint->sin_port);
1163 ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId,
1164 (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p);
1165 } else {
1166 ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId);
1167 }
1168
1169 sp<MediaPlayerBase> p = getPlayer();
1170
1171 // Right now, the only valid time to set a retransmit endpoint is before
1172 // player selection has been made (since the presence or absence of a
1173 // retransmit endpoint is going to determine which player is selected during
1174 // setDataSource).
1175 if (p != 0) return INVALID_OPERATION;
1176
1177 if (NULL != endpoint) {
1178 mRetransmitEndpoint = *endpoint;
1179 mRetransmitEndpointValid = true;
1180 } else {
1181 mRetransmitEndpointValid = false;
1182 }
1183
1184 return NO_ERROR;
1185}
1186
Gloria Wangb483c472011-04-11 17:23:27 -07001187void MediaPlayerService::Client::notify(
1188 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001189{
1190 Client* client = static_cast<Client*>(cookie);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001191
1192 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001193 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001194 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-07-10 13:53:06 -07001195
1196 if(client->shouldDropMetadata(metadata_type)) {
1197 return;
1198 }
1199
1200 // Update the list of metadata that have changed. getMetadata
1201 // also access mMetadataUpdated and clears it.
1202 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001203 }
Steve Block3856b092011-10-20 11:56:00 +01001204 ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
Gloria Wangb483c472011-04-11 17:23:27 -07001205 client->mClient->notify(msg, ext1, ext2, obj);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001206}
1207
Nicolas Catania48290382009-07-10 13:53:06 -07001208
nikoa64c8c72009-07-20 15:07:26 -07001209bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001210{
Nicolas Catania48290382009-07-10 13:53:06 -07001211 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001212
Nicolas Catania48290382009-07-10 13:53:06 -07001213 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001214 return true;
1215 }
1216
Nicolas Catania48290382009-07-10 13:53:06 -07001217 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001218 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001219 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001220 return true;
1221 }
1222}
1223
Nicolas Catania48290382009-07-10 13:53:06 -07001224
nikoa64c8c72009-07-20 15:07:26 -07001225void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-07-10 13:53:06 -07001226 Mutex::Autolock lock(mLock);
1227 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1228 mMetadataUpdated.add(metadata_type);
1229 }
1230}
1231
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001232#if CALLBACK_ANTAGONIZER
1233const int Antagonizer::interval = 10000; // 10 msecs
1234
1235Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1236 mExit(false), mActive(false), mClient(client), mCb(cb)
1237{
1238 createThread(callbackThread, this);
1239}
1240
1241void Antagonizer::kill()
1242{
1243 Mutex::Autolock _l(mLock);
1244 mActive = false;
1245 mExit = true;
1246 mCondition.wait(mLock);
1247}
1248
1249int Antagonizer::callbackThread(void* user)
1250{
Steve Blockb8a80522011-12-20 16:23:08 +00001251 ALOGD("Antagonizer started");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001252 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1253 while (!p->mExit) {
1254 if (p->mActive) {
Steve Block3856b092011-10-20 11:56:00 +01001255 ALOGV("send event");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001256 p->mCb(p->mClient, 0, 0, 0);
1257 }
1258 usleep(interval);
1259 }
1260 Mutex::Autolock _l(p->mLock);
1261 p->mCondition.signal();
Steve Blockb8a80522011-12-20 16:23:08 +00001262 ALOGD("Antagonizer stopped");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001263 return 0;
1264}
1265#endif
1266
1267static size_t kDefaultHeapSize = 1024 * 1024; // 1MB
1268
Glenn Kastene1c39622012-01-04 09:36:37 -08001269sp<IMemory> MediaPlayerService::decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, audio_format_t* pFormat)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001270{
Steve Block3856b092011-10-20 11:56:00 +01001271 ALOGV("decode(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001272 sp<MemoryBase> mem;
1273 sp<MediaPlayerBase> player;
1274
1275 // Protect our precious, precious DRMd ringtones by only allowing
1276 // decoding of http, but not filesystem paths or content Uris.
1277 // If the application wants to decode those, it should open a
1278 // filedescriptor for them and use that.
1279 if (url != NULL && strncmp(url, "http://", 7) != 0) {
Steve Blockb8a80522011-12-20 16:23:08 +00001280 ALOGD("Can't decode %s by path, use filedescriptor instead", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001281 return mem;
1282 }
1283
1284 player_type playerType = getPlayerType(url);
Steve Block3856b092011-10-20 11:56:00 +01001285 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001286
1287 // create the right type of player
1288 sp<AudioCache> cache = new AudioCache(url);
1289 player = android::createPlayer(playerType, cache.get(), cache->notify);
1290 if (player == NULL) goto Exit;
1291 if (player->hardwareOutput()) goto Exit;
1292
1293 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1294
1295 // set data source
1296 if (player->setDataSource(url) != NO_ERROR) goto Exit;
1297
Steve Block3856b092011-10-20 11:56:00 +01001298 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001299 player->prepareAsync();
1300
Steve Block3856b092011-10-20 11:56:00 +01001301 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001302 if (cache->wait() != NO_ERROR) goto Exit;
1303
Steve Block3856b092011-10-20 11:56:00 +01001304 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001305 player->start();
1306
Steve Block3856b092011-10-20 11:56:00 +01001307 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001308 cache->wait();
1309 // in case of error, return what was successfully decoded.
1310 if (cache->size() == 0) {
1311 goto Exit;
1312 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001313
1314 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1315 *pSampleRate = cache->sampleRate();
1316 *pNumChannels = cache->channelCount();
Glenn Kastene1c39622012-01-04 09:36:37 -08001317 *pFormat = cache->format();
Steve Block3856b092011-10-20 11:56:00 +01001318 ALOGV("return memory @ %p, sampleRate=%u, channelCount = %d, format = %d", mem->pointer(), *pSampleRate, *pNumChannels, *pFormat);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001319
1320Exit:
1321 if (player != 0) player->reset();
1322 return mem;
1323}
1324
Glenn Kastene1c39622012-01-04 09:36:37 -08001325sp<IMemory> MediaPlayerService::decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, audio_format_t* pFormat)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001326{
Steve Block3856b092011-10-20 11:56:00 +01001327 ALOGV("decode(%d, %lld, %lld)", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001328 sp<MemoryBase> mem;
1329 sp<MediaPlayerBase> player;
1330
1331 player_type playerType = getPlayerType(fd, offset, length);
Steve Block3856b092011-10-20 11:56:00 +01001332 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001333
1334 // create the right type of player
1335 sp<AudioCache> cache = new AudioCache("decode_fd");
1336 player = android::createPlayer(playerType, cache.get(), cache->notify);
1337 if (player == NULL) goto Exit;
1338 if (player->hardwareOutput()) goto Exit;
1339
1340 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1341
1342 // set data source
1343 if (player->setDataSource(fd, offset, length) != NO_ERROR) goto Exit;
1344
Steve Block3856b092011-10-20 11:56:00 +01001345 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001346 player->prepareAsync();
1347
Steve Block3856b092011-10-20 11:56:00 +01001348 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001349 if (cache->wait() != NO_ERROR) goto Exit;
1350
Steve Block3856b092011-10-20 11:56:00 +01001351 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001352 player->start();
1353
Steve Block3856b092011-10-20 11:56:00 +01001354 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001355 cache->wait();
1356 // in case of error, return what was successfully decoded.
1357 if (cache->size() == 0) {
1358 goto Exit;
1359 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001360
1361 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1362 *pSampleRate = cache->sampleRate();
1363 *pNumChannels = cache->channelCount();
1364 *pFormat = cache->format();
Steve Block3856b092011-10-20 11:56:00 +01001365 ALOGV("return memory @ %p, sampleRate=%u, channelCount = %d, format = %d", mem->pointer(), *pSampleRate, *pNumChannels, *pFormat);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001366
1367Exit:
1368 if (player != 0) player->reset();
1369 ::close(fd);
1370 return mem;
1371}
1372
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001373
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001374#undef LOG_TAG
1375#define LOG_TAG "AudioSink"
Eric Laurenta514bdb2010-06-21 09:27:30 -07001376MediaPlayerService::AudioOutput::AudioOutput(int sessionId)
Andreas Huber20111aa2009-07-14 16:56:47 -07001377 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001378 mCallbackCookie(NULL),
1379 mSessionId(sessionId) {
Steve Block3856b092011-10-20 11:56:00 +01001380 ALOGV("AudioOutput(%d)", sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001381 mTrack = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -07001382 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001383 mLeftVolume = 1.0;
1384 mRightVolume = 1.0;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001385 mPlaybackRatePermille = 1000;
1386 mSampleRateHz = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001387 mMsecsPerFrame = 0;
Eric Laurent2beeb502010-07-16 07:43:46 -07001388 mAuxEffectId = 0;
1389 mSendLevel = 0.0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001390 setMinBufferCount();
1391}
1392
1393MediaPlayerService::AudioOutput::~AudioOutput()
1394{
1395 close();
1396}
1397
1398void MediaPlayerService::AudioOutput::setMinBufferCount()
1399{
1400 char value[PROPERTY_VALUE_MAX];
1401 if (property_get("ro.kernel.qemu", value, 0)) {
1402 mIsOnEmulator = true;
1403 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1404 }
1405}
1406
1407bool MediaPlayerService::AudioOutput::isOnEmulator()
1408{
1409 setMinBufferCount();
1410 return mIsOnEmulator;
1411}
1412
1413int MediaPlayerService::AudioOutput::getMinBufferCount()
1414{
1415 setMinBufferCount();
1416 return mMinBufferCount;
1417}
1418
1419ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1420{
1421 if (mTrack == 0) return NO_INIT;
1422 return mTrack->frameCount() * frameSize();
1423}
1424
1425ssize_t MediaPlayerService::AudioOutput::frameCount() const
1426{
1427 if (mTrack == 0) return NO_INIT;
1428 return mTrack->frameCount();
1429}
1430
1431ssize_t MediaPlayerService::AudioOutput::channelCount() const
1432{
1433 if (mTrack == 0) return NO_INIT;
1434 return mTrack->channelCount();
1435}
1436
1437ssize_t MediaPlayerService::AudioOutput::frameSize() const
1438{
1439 if (mTrack == 0) return NO_INIT;
1440 return mTrack->frameSize();
1441}
1442
1443uint32_t MediaPlayerService::AudioOutput::latency () const
1444{
Eric Laurentdb354e52012-03-05 17:27:11 -08001445 if (mTrack == 0) return 0;
1446 return mTrack->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001447}
1448
1449float MediaPlayerService::AudioOutput::msecsPerFrame() const
1450{
1451 return mMsecsPerFrame;
1452}
1453
Eric Laurent342e9cf2010-01-19 17:37:09 -08001454status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position)
1455{
1456 if (mTrack == 0) return NO_INIT;
1457 return mTrack->getPosition(position);
1458}
1459
Andreas Huber20111aa2009-07-14 16:56:47 -07001460status_t MediaPlayerService::AudioOutput::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001461 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1462 audio_format_t format, int bufferCount,
Andreas Huber20111aa2009-07-14 16:56:47 -07001463 AudioCallback cb, void *cookie)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001464{
Andreas Huber20111aa2009-07-14 16:56:47 -07001465 mCallback = cb;
1466 mCallbackCookie = cookie;
1467
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001468 // Check argument "bufferCount" against the mininum buffer count
1469 if (bufferCount < mMinBufferCount) {
Steve Blockb8a80522011-12-20 16:23:08 +00001470 ALOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001471 bufferCount = mMinBufferCount;
1472
1473 }
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001474 ALOGV("open(%u, %d, 0x%x, %d, %d, %d)", sampleRate, channelCount, channelMask,
1475 format, bufferCount, mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001476 if (mTrack) close();
1477 int afSampleRate;
1478 int afFrameCount;
1479 int frameCount;
1480
1481 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1482 return NO_INIT;
1483 }
1484 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1485 return NO_INIT;
1486 }
1487
1488 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
Andreas Huber20111aa2009-07-14 16:56:47 -07001489
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001490 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
1491 channelMask = audio_channel_mask_from_count(channelCount);
1492 if (0 == channelMask) {
1493 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
1494 return NO_INIT;
1495 }
1496 }
1497
Andreas Huber20111aa2009-07-14 16:56:47 -07001498 AudioTrack *t;
1499 if (mCallback != NULL) {
1500 t = new AudioTrack(
Eric Laurentc2f1f072009-07-17 12:17:14 -07001501 mStreamType,
1502 sampleRate,
1503 format,
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001504 channelMask,
Eric Laurentc2f1f072009-07-17 12:17:14 -07001505 frameCount,
Glenn Kasten18868c52012-03-07 09:15:37 -08001506 AUDIO_POLICY_OUTPUT_FLAG_NONE,
Eric Laurentc2f1f072009-07-17 12:17:14 -07001507 CallbackWrapper,
Eric Laurenta514bdb2010-06-21 09:27:30 -07001508 this,
1509 0,
1510 mSessionId);
Andreas Huber20111aa2009-07-14 16:56:47 -07001511 } else {
1512 t = new AudioTrack(
Eric Laurentc2f1f072009-07-17 12:17:14 -07001513 mStreamType,
1514 sampleRate,
1515 format,
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001516 channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -07001517 frameCount,
Glenn Kasten18868c52012-03-07 09:15:37 -08001518 AUDIO_POLICY_OUTPUT_FLAG_NONE,
Eric Laurenta514bdb2010-06-21 09:27:30 -07001519 NULL,
1520 NULL,
1521 0,
1522 mSessionId);
Andreas Huber20111aa2009-07-14 16:56:47 -07001523 }
1524
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001525 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
Steve Block29357bc2012-01-06 19:20:56 +00001526 ALOGE("Unable to create audio track");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001527 delete t;
1528 return NO_INIT;
1529 }
1530
Steve Block3856b092011-10-20 11:56:00 +01001531 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001532 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001533
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001534 mSampleRateHz = sampleRate;
1535 mMsecsPerFrame = mPlaybackRatePermille / (float) sampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001536 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001537
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001538 status_t res = t->setSampleRate(mPlaybackRatePermille * mSampleRateHz / 1000);
1539 if (res != NO_ERROR) {
1540 return res;
1541 }
Eric Laurent2beeb502010-07-16 07:43:46 -07001542 t->setAuxEffectSendLevel(mSendLevel);
1543 return t->attachAuxEffect(mAuxEffectId);;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001544}
1545
1546void MediaPlayerService::AudioOutput::start()
1547{
Steve Block3856b092011-10-20 11:56:00 +01001548 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001549 if (mTrack) {
1550 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001551 mTrack->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001552 mTrack->start();
1553 }
1554}
1555
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001556
1557
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001558ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size)
1559{
Andreas Huber20111aa2009-07-14 16:56:47 -07001560 LOG_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
1561
Steve Block3856b092011-10-20 11:56:00 +01001562 //ALOGV("write(%p, %u)", buffer, size);
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001563 if (mTrack) {
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001564 ssize_t ret = mTrack->write(buffer, size);
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001565 return ret;
1566 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001567 return NO_INIT;
1568}
1569
1570void MediaPlayerService::AudioOutput::stop()
1571{
Steve Block3856b092011-10-20 11:56:00 +01001572 ALOGV("stop");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001573 if (mTrack) mTrack->stop();
1574}
1575
1576void MediaPlayerService::AudioOutput::flush()
1577{
Steve Block3856b092011-10-20 11:56:00 +01001578 ALOGV("flush");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001579 if (mTrack) mTrack->flush();
1580}
1581
1582void MediaPlayerService::AudioOutput::pause()
1583{
Steve Block3856b092011-10-20 11:56:00 +01001584 ALOGV("pause");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001585 if (mTrack) mTrack->pause();
1586}
1587
1588void MediaPlayerService::AudioOutput::close()
1589{
Steve Block3856b092011-10-20 11:56:00 +01001590 ALOGV("close");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001591 delete mTrack;
1592 mTrack = 0;
1593}
1594
1595void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1596{
Steve Block3856b092011-10-20 11:56:00 +01001597 ALOGV("setVolume(%f, %f)", left, right);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001598 mLeftVolume = left;
1599 mRightVolume = right;
1600 if (mTrack) {
1601 mTrack->setVolume(left, right);
1602 }
1603}
1604
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001605status_t MediaPlayerService::AudioOutput::setPlaybackRatePermille(int32_t ratePermille)
1606{
1607 ALOGV("setPlaybackRatePermille(%d)", ratePermille);
1608 status_t res = NO_ERROR;
1609 if (mTrack) {
1610 res = mTrack->setSampleRate(ratePermille * mSampleRateHz / 1000);
1611 } else {
1612 res = NO_INIT;
1613 }
1614 mPlaybackRatePermille = ratePermille;
1615 if (mSampleRateHz != 0) {
1616 mMsecsPerFrame = mPlaybackRatePermille / (float) mSampleRateHz;
1617 }
1618 return res;
1619}
1620
Eric Laurent2beeb502010-07-16 07:43:46 -07001621status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1622{
Steve Block3856b092011-10-20 11:56:00 +01001623 ALOGV("setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001624 mSendLevel = level;
1625 if (mTrack) {
1626 return mTrack->setAuxEffectSendLevel(level);
1627 }
1628 return NO_ERROR;
1629}
1630
1631status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1632{
Steve Block3856b092011-10-20 11:56:00 +01001633 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001634 mAuxEffectId = effectId;
1635 if (mTrack) {
1636 return mTrack->attachAuxEffect(effectId);
1637 }
1638 return NO_ERROR;
1639}
1640
Andreas Huber20111aa2009-07-14 16:56:47 -07001641// static
1642void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastend217a8c2011-06-01 15:20:35 -07001643 int event, void *cookie, void *info) {
Steve Block3856b092011-10-20 11:56:00 +01001644 //ALOGV("callbackwrapper");
Andreas Huber20111aa2009-07-14 16:56:47 -07001645 if (event != AudioTrack::EVENT_MORE_DATA) {
1646 return;
1647 }
1648
1649 AudioOutput *me = (AudioOutput *)cookie;
1650 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
1651
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001652 size_t actualSize = (*me->mCallback)(
Andreas Huber20111aa2009-07-14 16:56:47 -07001653 me, buffer->raw, buffer->size, me->mCallbackCookie);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001654
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001655 if (actualSize == 0 && buffer->size > 0) {
1656 // We've reached EOS but the audio track is not stopped yet,
1657 // keep playing silence.
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08001658
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001659 memset(buffer->raw, 0, buffer->size);
1660 actualSize = buffer->size;
1661 }
1662
1663 buffer->size = actualSize;
Andreas Huber20111aa2009-07-14 16:56:47 -07001664}
1665
Eric Laurent8c563ed2010-10-07 18:23:03 -07001666int MediaPlayerService::AudioOutput::getSessionId()
1667{
1668 return mSessionId;
1669}
1670
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001671#undef LOG_TAG
1672#define LOG_TAG "AudioCache"
1673MediaPlayerService::AudioCache::AudioCache(const char* name) :
1674 mChannelCount(0), mFrameCount(1024), mSampleRate(0), mSize(0),
1675 mError(NO_ERROR), mCommandComplete(false)
1676{
1677 // create ashmem heap
1678 mHeap = new MemoryHeapBase(kDefaultHeapSize, 0, name);
1679}
1680
1681uint32_t MediaPlayerService::AudioCache::latency () const
1682{
1683 return 0;
1684}
1685
1686float MediaPlayerService::AudioCache::msecsPerFrame() const
1687{
1688 return mMsecsPerFrame;
1689}
1690
Eric Laurent342e9cf2010-01-19 17:37:09 -08001691status_t MediaPlayerService::AudioCache::getPosition(uint32_t *position)
1692{
1693 if (position == 0) return BAD_VALUE;
1694 *position = mSize;
1695 return NO_ERROR;
1696}
1697
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001698////////////////////////////////////////////////////////////////////////////////
1699
1700struct CallbackThread : public Thread {
1701 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1702 MediaPlayerBase::AudioSink::AudioCallback cb,
1703 void *cookie);
1704
1705protected:
1706 virtual ~CallbackThread();
1707
1708 virtual bool threadLoop();
1709
1710private:
1711 wp<MediaPlayerBase::AudioSink> mSink;
1712 MediaPlayerBase::AudioSink::AudioCallback mCallback;
1713 void *mCookie;
1714 void *mBuffer;
1715 size_t mBufferSize;
1716
1717 CallbackThread(const CallbackThread &);
1718 CallbackThread &operator=(const CallbackThread &);
1719};
1720
1721CallbackThread::CallbackThread(
1722 const wp<MediaPlayerBase::AudioSink> &sink,
1723 MediaPlayerBase::AudioSink::AudioCallback cb,
1724 void *cookie)
1725 : mSink(sink),
1726 mCallback(cb),
1727 mCookie(cookie),
1728 mBuffer(NULL),
1729 mBufferSize(0) {
1730}
1731
1732CallbackThread::~CallbackThread() {
1733 if (mBuffer) {
1734 free(mBuffer);
1735 mBuffer = NULL;
1736 }
1737}
1738
1739bool CallbackThread::threadLoop() {
1740 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
1741 if (sink == NULL) {
1742 return false;
1743 }
1744
1745 if (mBuffer == NULL) {
1746 mBufferSize = sink->bufferSize();
1747 mBuffer = malloc(mBufferSize);
1748 }
1749
1750 size_t actualSize =
1751 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie);
1752
1753 if (actualSize > 0) {
1754 sink->write(mBuffer, actualSize);
1755 }
1756
1757 return true;
1758}
1759
1760////////////////////////////////////////////////////////////////////////////////
1761
Andreas Huber20111aa2009-07-14 16:56:47 -07001762status_t MediaPlayerService::AudioCache::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001763 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1764 audio_format_t format, int bufferCount,
Andreas Huber20111aa2009-07-14 16:56:47 -07001765 AudioCallback cb, void *cookie)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001766{
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001767 ALOGV("open(%u, %d, 0x%x, %d, %d)", sampleRate, channelCount, channelMask, format, bufferCount);
Dave Sparks8eb80112009-12-09 20:20:26 -08001768 if (mHeap->getHeapID() < 0) {
1769 return NO_INIT;
1770 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001771
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001772 mSampleRate = sampleRate;
1773 mChannelCount = (uint16_t)channelCount;
Glenn Kastene1c39622012-01-04 09:36:37 -08001774 mFormat = format;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001775 mMsecsPerFrame = 1.e3 / (float) sampleRate;
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001776
1777 if (cb != NULL) {
1778 mCallbackThread = new CallbackThread(this, cb, cookie);
1779 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001780 return NO_ERROR;
1781}
1782
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001783void MediaPlayerService::AudioCache::start() {
1784 if (mCallbackThread != NULL) {
1785 mCallbackThread->run("AudioCache callback");
1786 }
1787}
1788
1789void MediaPlayerService::AudioCache::stop() {
1790 if (mCallbackThread != NULL) {
1791 mCallbackThread->requestExitAndWait();
1792 }
1793}
1794
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001795ssize_t MediaPlayerService::AudioCache::write(const void* buffer, size_t size)
1796{
Steve Block3856b092011-10-20 11:56:00 +01001797 ALOGV("write(%p, %u)", buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001798 if ((buffer == 0) || (size == 0)) return size;
1799
1800 uint8_t* p = static_cast<uint8_t*>(mHeap->getBase());
1801 if (p == NULL) return NO_INIT;
1802 p += mSize;
Steve Block3856b092011-10-20 11:56:00 +01001803 ALOGV("memcpy(%p, %p, %u)", p, buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001804 if (mSize + size > mHeap->getSize()) {
Steve Block29357bc2012-01-06 19:20:56 +00001805 ALOGE("Heap size overflow! req size: %d, max size: %d", (mSize + size), mHeap->getSize());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001806 size = mHeap->getSize() - mSize;
1807 }
1808 memcpy(p, buffer, size);
1809 mSize += size;
1810 return size;
1811}
1812
1813// call with lock held
1814status_t MediaPlayerService::AudioCache::wait()
1815{
1816 Mutex::Autolock lock(mLock);
Dave Sparks4bbc0ba2010-03-01 19:29:58 -08001817 while (!mCommandComplete) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001818 mSignal.wait(mLock);
1819 }
1820 mCommandComplete = false;
1821
1822 if (mError == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01001823 ALOGV("wait - success");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001824 } else {
Steve Block3856b092011-10-20 11:56:00 +01001825 ALOGV("wait - error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001826 }
1827 return mError;
1828}
1829
Gloria Wangb483c472011-04-11 17:23:27 -07001830void MediaPlayerService::AudioCache::notify(
1831 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001832{
Steve Block3856b092011-10-20 11:56:00 +01001833 ALOGV("notify(%p, %d, %d, %d)", cookie, msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001834 AudioCache* p = static_cast<AudioCache*>(cookie);
1835
1836 // ignore buffering messages
Dave Sparks8eb80112009-12-09 20:20:26 -08001837 switch (msg)
1838 {
1839 case MEDIA_ERROR:
Steve Block29357bc2012-01-06 19:20:56 +00001840 ALOGE("Error %d, %d occurred", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001841 p->mError = ext1;
Dave Sparks8eb80112009-12-09 20:20:26 -08001842 break;
1843 case MEDIA_PREPARED:
Steve Block3856b092011-10-20 11:56:00 +01001844 ALOGV("prepared");
Dave Sparks8eb80112009-12-09 20:20:26 -08001845 break;
1846 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +01001847 ALOGV("playback complete");
Dave Sparks8eb80112009-12-09 20:20:26 -08001848 break;
1849 default:
Steve Block3856b092011-10-20 11:56:00 +01001850 ALOGV("ignored");
Dave Sparks8eb80112009-12-09 20:20:26 -08001851 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001852 }
1853
1854 // wake up thread
Dave Sparksfe4c6f02010-03-02 12:56:37 -08001855 Mutex::Autolock lock(p->mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001856 p->mCommandComplete = true;
1857 p->mSignal.signal();
1858}
1859
Eric Laurent8c563ed2010-10-07 18:23:03 -07001860int MediaPlayerService::AudioCache::getSessionId()
1861{
1862 return 0;
1863}
1864
Gloria Wang7cf180c2011-02-19 18:37:57 -08001865void MediaPlayerService::addBatteryData(uint32_t params)
1866{
1867 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08001868
1869 int32_t time = systemTime() / 1000000L;
1870
1871 // change audio output devices. This notification comes from AudioFlinger
1872 if ((params & kBatteryDataSpeakerOn)
1873 || (params & kBatteryDataOtherAudioDeviceOn)) {
1874
1875 int deviceOn[NUM_AUDIO_DEVICES];
1876 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1877 deviceOn[i] = 0;
1878 }
1879
1880 if ((params & kBatteryDataSpeakerOn)
1881 && (params & kBatteryDataOtherAudioDeviceOn)) {
1882 deviceOn[SPEAKER_AND_OTHER] = 1;
1883 } else if (params & kBatteryDataSpeakerOn) {
1884 deviceOn[SPEAKER] = 1;
1885 } else {
1886 deviceOn[OTHER_AUDIO_DEVICE] = 1;
1887 }
1888
1889 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1890 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
1891
1892 if (mBatteryAudio.refCount > 0) { // if playing audio
1893 if (!deviceOn[i]) {
1894 mBatteryAudio.lastTime[i] += time;
1895 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1896 mBatteryAudio.lastTime[i] = 0;
1897 } else {
1898 mBatteryAudio.lastTime[i] = 0 - time;
1899 }
1900 }
1901
1902 mBatteryAudio.deviceOn[i] = deviceOn[i];
1903 }
1904 }
1905 return;
1906 }
1907
1908 // an sudio stream is started
1909 if (params & kBatteryDataAudioFlingerStart) {
1910 // record the start time only if currently no other audio
1911 // is being played
1912 if (mBatteryAudio.refCount == 0) {
1913 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1914 if (mBatteryAudio.deviceOn[i]) {
1915 mBatteryAudio.lastTime[i] -= time;
1916 }
1917 }
1918 }
1919
1920 mBatteryAudio.refCount ++;
1921 return;
1922
1923 } else if (params & kBatteryDataAudioFlingerStop) {
1924 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001925 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001926 return;
1927 }
1928
1929 // record the stop time only if currently this is the only
1930 // audio being played
1931 if (mBatteryAudio.refCount == 1) {
1932 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1933 if (mBatteryAudio.deviceOn[i]) {
1934 mBatteryAudio.lastTime[i] += time;
1935 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1936 mBatteryAudio.lastTime[i] = 0;
1937 }
1938 }
1939 }
1940
1941 mBatteryAudio.refCount --;
1942 return;
1943 }
1944
Gloria Wang7cf180c2011-02-19 18:37:57 -08001945 int uid = IPCThreadState::self()->getCallingUid();
1946 if (uid == AID_MEDIA) {
1947 return;
1948 }
1949 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08001950
1951 if (index < 0) { // create a new entry for this UID
1952 BatteryUsageInfo info;
1953 info.audioTotalTime = 0;
1954 info.videoTotalTime = 0;
1955 info.audioLastTime = 0;
1956 info.videoLastTime = 0;
1957 info.refCount = 0;
1958
Gloria Wang9ee159b2011-02-24 14:51:45 -08001959 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00001960 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001961 return;
1962 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08001963 }
1964
1965 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
1966
1967 if (params & kBatteryDataCodecStarted) {
1968 if (params & kBatteryDataTrackAudio) {
1969 info.audioLastTime -= time;
1970 info.refCount ++;
1971 }
1972 if (params & kBatteryDataTrackVideo) {
1973 info.videoLastTime -= time;
1974 info.refCount ++;
1975 }
1976 } else {
1977 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001978 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08001979 return;
1980 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001981 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08001982 mBatteryData.removeItem(uid);
1983 return;
1984 }
1985
1986 if (params & kBatteryDataTrackAudio) {
1987 info.audioLastTime += time;
1988 info.refCount --;
1989 }
1990 if (params & kBatteryDataTrackVideo) {
1991 info.videoLastTime += time;
1992 info.refCount --;
1993 }
1994
1995 // no stream is being played by this UID
1996 if (info.refCount == 0) {
1997 info.audioTotalTime += info.audioLastTime;
1998 info.audioLastTime = 0;
1999 info.videoTotalTime += info.videoLastTime;
2000 info.videoLastTime = 0;
2001 }
2002 }
2003}
2004
2005status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
2006 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002007
2008 // audio output devices usage
2009 int32_t time = systemTime() / 1000000L; //in ms
2010 int32_t totalTime;
2011
2012 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2013 totalTime = mBatteryAudio.totalTime[i];
2014
2015 if (mBatteryAudio.deviceOn[i]
2016 && (mBatteryAudio.lastTime[i] != 0)) {
2017 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
2018 totalTime += tmpTime;
2019 }
2020
2021 reply->writeInt32(totalTime);
2022 // reset the total time
2023 mBatteryAudio.totalTime[i] = 0;
2024 }
2025
2026 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08002027 BatteryUsageInfo info;
2028 int size = mBatteryData.size();
2029
2030 reply->writeInt32(size);
2031 int i = 0;
2032
2033 while (i < size) {
2034 info = mBatteryData.valueAt(i);
2035
2036 reply->writeInt32(mBatteryData.keyAt(i)); //UID
2037 reply->writeInt32(info.audioTotalTime);
2038 reply->writeInt32(info.videoTotalTime);
2039
2040 info.audioTotalTime = 0;
2041 info.videoTotalTime = 0;
2042
2043 // remove the UID entry where no stream is being played
2044 if (info.refCount <= 0) {
2045 mBatteryData.removeItemsAt(i);
2046 size --;
2047 i --;
2048 }
2049 i++;
2050 }
2051 return NO_ERROR;
2052}
nikoa64c8c72009-07-20 15:07:26 -07002053} // namespace android