blob: 5fe446f646d08ab9563ef52edfb007eae2e542d6 [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
Mathias Agopian75624082009-05-19 19:08:10 -070037#include <binder/IPCThreadState.h>
38#include <binder/IServiceManager.h>
39#include <binder/MemoryHeapBase.h>
40#include <binder/MemoryBase.h>
Jamie Gennis7dae00b2011-10-26 18:36:31 -070041#include <gui/SurfaceTextureClient.h>
Nicolas Catania1d187f12009-05-12 23:25:55 -070042#include <utils/Errors.h> // for status_t
43#include <utils/String8.h>
Marco Nelissen10dbb8e2009-09-20 10:42:13 -070044#include <utils/SystemClock.h>
Nicolas Catania1d187f12009-05-12 23:25:55 -070045#include <utils/Vector.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080046
47#include <media/MediaPlayerInterface.h>
48#include <media/mediarecorder.h>
49#include <media/MediaMetadataRetrieverInterface.h>
nikoa64c8c72009-07-20 15:07:26 -070050#include <media/Metadata.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080051#include <media/AudioTrack.h>
James Dong8635b7b2011-03-14 17:01:38 -070052#include <media/MemoryLeakTrackUtil.h>
Eric Laurent9cb839a2011-09-27 09:48:56 -070053#include <media/stagefright/MediaErrors.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080054
Dima Zavin64760242011-05-11 14:15:23 -070055#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070056
Gloria Wang7cf180c2011-02-19 18:37:57 -080057#include <private/android_filesystem_config.h>
58
James Dong559bf282012-03-28 10:29:14 -070059#include "ActivityManager.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080060#include "MediaRecorderClient.h"
61#include "MediaPlayerService.h"
62#include "MetadataRetrieverClient.h"
John Grossman44a7e422012-06-21 17:29:24 -070063#include "MediaPlayerFactory.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080064
65#include "MidiFile.h"
Nicolas Catania14d27472009-07-13 14:37:49 -070066#include "TestPlayerStub.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070067#include "StagefrightPlayer.h"
Andreas Huberf9334412010-12-15 15:17:42 -080068#include "nuplayer/NuPlayerDriver.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070069
Andreas Huber20111aa2009-07-14 16:56:47 -070070#include <OMX.h>
Nicolas Catania14d27472009-07-13 14:37:49 -070071
Andreas Hubered3e3e02012-03-26 11:13:27 -070072#include "Crypto.h"
Andreas Huber35213f12012-08-29 11:41:50 -070073#include "RemoteDisplay.h"
Andreas Hubered3e3e02012-03-26 11:13:27 -070074
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070075namespace {
nikoa64c8c72009-07-20 15:07:26 -070076using android::media::Metadata;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070077using android::status_t;
78using android::OK;
79using android::BAD_VALUE;
80using android::NOT_ENOUGH_DATA;
81using android::Parcel;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070082
83// Max number of entries in the filter.
84const int kMaxFilterSize = 64; // I pulled that out of thin air.
85
nikoa64c8c72009-07-20 15:07:26 -070086// FIXME: Move all the metadata related function in the Metadata.cpp
nikod608a812009-07-16 16:39:53 -070087
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070088
89// Unmarshall a filter from a Parcel.
90// Filter format in a parcel:
91//
92// 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
93// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
94// | number of entries (n) |
95// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
96// | metadata type 1 |
97// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
98// | metadata type 2 |
99// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
100// ....
101// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
102// | metadata type n |
103// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
104//
105// @param p Parcel that should start with a filter.
106// @param[out] filter On exit contains the list of metadata type to be
107// filtered.
108// @param[out] status On exit contains the status code to be returned.
109// @return true if the parcel starts with a valid filter.
110bool unmarshallFilter(const Parcel& p,
nikoa64c8c72009-07-20 15:07:26 -0700111 Metadata::Filter *filter,
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700112 status_t *status)
113{
Nicolas Catania48290382009-07-10 13:53:06 -0700114 int32_t val;
115 if (p.readInt32(&val) != OK)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700116 {
Steve Block29357bc2012-01-06 19:20:56 +0000117 ALOGE("Failed to read filter's length");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700118 *status = NOT_ENOUGH_DATA;
119 return false;
120 }
121
Nicolas Catania48290382009-07-10 13:53:06 -0700122 if( val > kMaxFilterSize || val < 0)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700123 {
Steve Block29357bc2012-01-06 19:20:56 +0000124 ALOGE("Invalid filter len %d", val);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700125 *status = BAD_VALUE;
126 return false;
127 }
128
Nicolas Catania48290382009-07-10 13:53:06 -0700129 const size_t num = val;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700130
131 filter->clear();
Nicolas Catania48290382009-07-10 13:53:06 -0700132 filter->setCapacity(num);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700133
nikoa64c8c72009-07-20 15:07:26 -0700134 size_t size = num * sizeof(Metadata::Type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700135
Nicolas Catania48290382009-07-10 13:53:06 -0700136
137 if (p.dataAvail() < size)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700138 {
Steve Block29357bc2012-01-06 19:20:56 +0000139 ALOGE("Filter too short expected %d but got %d", size, p.dataAvail());
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700140 *status = NOT_ENOUGH_DATA;
141 return false;
142 }
143
nikoa64c8c72009-07-20 15:07:26 -0700144 const Metadata::Type *data =
145 static_cast<const Metadata::Type*>(p.readInplace(size));
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700146
Nicolas Catania48290382009-07-10 13:53:06 -0700147 if (NULL == data)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700148 {
Steve Block29357bc2012-01-06 19:20:56 +0000149 ALOGE("Filter had no data");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700150 *status = BAD_VALUE;
151 return false;
152 }
153
154 // TODO: The stl impl of vector would be more efficient here
155 // because it degenerates into a memcpy on pod types. Try to
156 // replace later or use stl::set.
Nicolas Catania48290382009-07-10 13:53:06 -0700157 for (size_t i = 0; i < num; ++i)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700158 {
Nicolas Catania48290382009-07-10 13:53:06 -0700159 filter->add(*data);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700160 ++data;
161 }
162 *status = OK;
163 return true;
164}
165
Nicolas Catania48290382009-07-10 13:53:06 -0700166// @param filter Of metadata type.
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700167// @param val To be searched.
168// @return true if a match was found.
nikoa64c8c72009-07-20 15:07:26 -0700169bool findMetadata(const Metadata::Filter& filter, const int32_t val)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700170{
171 // Deal with empty and ANY right away
172 if (filter.isEmpty()) return false;
nikoa64c8c72009-07-20 15:07:26 -0700173 if (filter[0] == Metadata::kAny) return true;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700174
Nicolas Catania48290382009-07-10 13:53:06 -0700175 return filter.indexOf(val) >= 0;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700176}
177
178} // anonymous namespace
179
180
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800181namespace android {
182
Dave Burked681bbb2011-08-30 14:39:17 +0100183static bool checkPermission(const char* permissionString) {
184#ifndef HAVE_ANDROID_OS
185 return true;
186#endif
187 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
188 bool ok = checkCallingPermission(String16(permissionString));
Steve Block29357bc2012-01-06 19:20:56 +0000189 if (!ok) ALOGE("Request requires %s", permissionString);
Dave Burked681bbb2011-08-30 14:39:17 +0100190 return ok;
191}
192
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800193// 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 -0800194/* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4;
195/* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false;
196
197void MediaPlayerService::instantiate() {
198 defaultServiceManager()->addService(
199 String16("media.player"), new MediaPlayerService());
200}
201
202MediaPlayerService::MediaPlayerService()
203{
Steve Block3856b092011-10-20 11:56:00 +0100204 ALOGV("MediaPlayerService created");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800205 mNextConnId = 1;
Gloria Wang9ee159b2011-02-24 14:51:45 -0800206
207 mBatteryAudio.refCount = 0;
208 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
209 mBatteryAudio.deviceOn[i] = 0;
210 mBatteryAudio.lastTime[i] = 0;
211 mBatteryAudio.totalTime[i] = 0;
212 }
213 // speaker is on by default
214 mBatteryAudio.deviceOn[SPEAKER] = 1;
John Grossman44a7e422012-06-21 17:29:24 -0700215
216 MediaPlayerFactory::registerBuiltinFactories();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800217}
218
219MediaPlayerService::~MediaPlayerService()
220{
Steve Block3856b092011-10-20 11:56:00 +0100221 ALOGV("MediaPlayerService destroyed");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800222}
223
224sp<IMediaRecorder> MediaPlayerService::createMediaRecorder(pid_t pid)
225{
Gloria Wangdac6a312009-10-29 15:46:37 -0700226 sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid);
227 wp<MediaRecorderClient> w = recorder;
228 Mutex::Autolock lock(mLock);
229 mMediaRecorderClients.add(w);
Steve Block3856b092011-10-20 11:56:00 +0100230 ALOGV("Create new media recorder client from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800231 return recorder;
232}
233
Gloria Wangdac6a312009-10-29 15:46:37 -0700234void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client)
235{
236 Mutex::Autolock lock(mLock);
237 mMediaRecorderClients.remove(client);
Steve Block3856b092011-10-20 11:56:00 +0100238 ALOGV("Delete media recorder client");
Gloria Wangdac6a312009-10-29 15:46:37 -0700239}
240
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800241sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever(pid_t pid)
242{
243 sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
Steve Block3856b092011-10-20 11:56:00 +0100244 ALOGV("Create new media retriever from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800245 return retriever;
246}
247
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800248sp<IMediaPlayer> MediaPlayerService::create(pid_t pid, const sp<IMediaPlayerClient>& client,
Dave Burked681bbb2011-08-30 14:39:17 +0100249 int audioSessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800250{
251 int32_t connId = android_atomic_inc(&mNextConnId);
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700252
253 sp<Client> c = new Client(
254 this, pid, connId, client, audioSessionId,
255 IPCThreadState::self()->getCallingUid());
256
Steve Block3856b092011-10-20 11:56:00 +0100257 ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid,
Dave Burked681bbb2011-08-30 14:39:17 +0100258 IPCThreadState::self()->getCallingUid());
259
260 wp<Client> w = c;
261 {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800262 Mutex::Autolock lock(mLock);
263 mClients.add(w);
264 }
Andreas Hubere2b10282010-11-23 11:41:34 -0800265 return c;
266}
267
Andreas Huber318ad9c2009-10-15 13:46:54 -0700268sp<IOMX> MediaPlayerService::getOMX() {
269 Mutex::Autolock autoLock(mLock);
270
271 if (mOMX.get() == NULL) {
272 mOMX = new OMX;
273 }
274
275 return mOMX;
Andreas Huber20111aa2009-07-14 16:56:47 -0700276}
277
Andreas Hubered3e3e02012-03-26 11:13:27 -0700278sp<ICrypto> MediaPlayerService::makeCrypto() {
Andreas Huber1bd139a2012-04-03 14:19:20 -0700279 return new Crypto;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700280}
281
Andreas Huber35213f12012-08-29 11:41:50 -0700282status_t MediaPlayerService::enableRemoteDisplay(bool enable) {
283 Mutex::Autolock autoLock(mLock);
284
285 if (enable && mRemoteDisplay == NULL) {
286 mRemoteDisplay = new RemoteDisplay;
287
288 status_t err = mRemoteDisplay->start();
289
290 if (err != OK) {
291 mRemoteDisplay.clear();
292 return err;
293 }
294
295 return OK;
296 } else if (!enable && mRemoteDisplay != NULL) {
297 mRemoteDisplay->stop();
298 mRemoteDisplay.clear();
299 }
300
301 return OK;
302}
303
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800304status_t MediaPlayerService::AudioCache::dump(int fd, const Vector<String16>& args) const
305{
306 const size_t SIZE = 256;
307 char buffer[SIZE];
308 String8 result;
309
310 result.append(" AudioCache\n");
311 if (mHeap != 0) {
312 snprintf(buffer, 255, " heap base(%p), size(%d), flags(%d), device(%s)\n",
313 mHeap->getBase(), mHeap->getSize(), mHeap->getFlags(), mHeap->getDevice());
314 result.append(buffer);
315 }
316 snprintf(buffer, 255, " msec per frame(%f), channel count(%d), format(%d), frame count(%ld)\n",
317 mMsecsPerFrame, mChannelCount, mFormat, mFrameCount);
318 result.append(buffer);
319 snprintf(buffer, 255, " sample rate(%d), size(%d), error(%d), command complete(%s)\n",
320 mSampleRate, mSize, mError, mCommandComplete?"true":"false");
321 result.append(buffer);
322 ::write(fd, result.string(), result.size());
323 return NO_ERROR;
324}
325
326status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
327{
328 const size_t SIZE = 256;
329 char buffer[SIZE];
330 String8 result;
331
332 result.append(" AudioOutput\n");
333 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n",
334 mStreamType, mLeftVolume, mRightVolume);
335 result.append(buffer);
336 snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
Eric Laurentdb354e52012-03-05 17:27:11 -0800337 mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800338 result.append(buffer);
Eric Laurent2beeb502010-07-16 07:43:46 -0700339 snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
340 mAuxEffectId, mSendLevel);
341 result.append(buffer);
342
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800343 ::write(fd, result.string(), result.size());
344 if (mTrack != 0) {
345 mTrack->dump(fd, args);
346 }
347 return NO_ERROR;
348}
349
350status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args) const
351{
352 const size_t SIZE = 256;
353 char buffer[SIZE];
354 String8 result;
355 result.append(" Client\n");
356 snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n",
357 mPid, mConnId, mStatus, mLoop?"true": "false");
358 result.append(buffer);
359 write(fd, result.string(), result.size());
Andreas Hubera0b1d4b2011-06-07 15:52:25 -0700360 if (mPlayer != NULL) {
361 mPlayer->dump(fd, args);
362 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800363 if (mAudioOutput != 0) {
364 mAudioOutput->dump(fd, args);
365 }
366 write(fd, "\n", 1);
367 return NO_ERROR;
368}
369
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800370status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
371{
372 const size_t SIZE = 256;
373 char buffer[SIZE];
374 String8 result;
375 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
376 snprintf(buffer, SIZE, "Permission Denial: "
377 "can't dump MediaPlayerService from pid=%d, uid=%d\n",
378 IPCThreadState::self()->getCallingPid(),
379 IPCThreadState::self()->getCallingUid());
380 result.append(buffer);
381 } else {
382 Mutex::Autolock lock(mLock);
383 for (int i = 0, n = mClients.size(); i < n; ++i) {
384 sp<Client> c = mClients[i].promote();
385 if (c != 0) c->dump(fd, args);
386 }
James Dongb9141222010-07-08 11:16:11 -0700387 if (mMediaRecorderClients.size() == 0) {
388 result.append(" No media recorder client\n\n");
389 } else {
390 for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
391 sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
James Donge579e282011-10-18 22:29:20 -0700392 if (c != 0) {
393 snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid);
394 result.append(buffer);
395 write(fd, result.string(), result.size());
396 result = "\n";
397 c->dump(fd, args);
398 }
James Dongb9141222010-07-08 11:16:11 -0700399 }
Gloria Wangdac6a312009-10-29 15:46:37 -0700400 }
401
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800402 result.append(" Files opened and/or mapped:\n");
Glenn Kasten0512ab52011-05-04 17:58:57 -0700403 snprintf(buffer, SIZE, "/proc/%d/maps", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800404 FILE *f = fopen(buffer, "r");
405 if (f) {
406 while (!feof(f)) {
407 fgets(buffer, SIZE, f);
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700408 if (strstr(buffer, " /storage/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800409 strstr(buffer, " /system/sounds/") ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700410 strstr(buffer, " /data/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800411 strstr(buffer, " /system/media/")) {
412 result.append(" ");
413 result.append(buffer);
414 }
415 }
416 fclose(f);
417 } else {
418 result.append("couldn't open ");
419 result.append(buffer);
420 result.append("\n");
421 }
422
Glenn Kasten0512ab52011-05-04 17:58:57 -0700423 snprintf(buffer, SIZE, "/proc/%d/fd", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800424 DIR *d = opendir(buffer);
425 if (d) {
426 struct dirent *ent;
427 while((ent = readdir(d)) != NULL) {
428 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
Glenn Kasten0512ab52011-05-04 17:58:57 -0700429 snprintf(buffer, SIZE, "/proc/%d/fd/%s", gettid(), ent->d_name);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800430 struct stat s;
431 if (lstat(buffer, &s) == 0) {
432 if ((s.st_mode & S_IFMT) == S_IFLNK) {
433 char linkto[256];
434 int len = readlink(buffer, linkto, sizeof(linkto));
435 if(len > 0) {
436 if(len > 255) {
437 linkto[252] = '.';
438 linkto[253] = '.';
439 linkto[254] = '.';
440 linkto[255] = 0;
441 } else {
442 linkto[len] = 0;
443 }
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700444 if (strstr(linkto, "/storage/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800445 strstr(linkto, "/system/sounds/") == linkto ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700446 strstr(linkto, "/data/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800447 strstr(linkto, "/system/media/") == linkto) {
448 result.append(" ");
449 result.append(buffer);
450 result.append(" -> ");
451 result.append(linkto);
452 result.append("\n");
453 }
454 }
455 } else {
456 result.append(" unexpected type for ");
457 result.append(buffer);
458 result.append("\n");
459 }
460 }
461 }
462 }
463 closedir(d);
464 } else {
465 result.append("couldn't open ");
466 result.append(buffer);
467 result.append("\n");
468 }
469
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800470 bool dumpMem = false;
471 for (size_t i = 0; i < args.size(); i++) {
472 if (args[i] == String16("-m")) {
473 dumpMem = true;
474 }
475 }
476 if (dumpMem) {
James Dong8635b7b2011-03-14 17:01:38 -0700477 dumpMemoryAddresses(fd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800478 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800479 }
480 write(fd, result.string(), result.size());
481 return NO_ERROR;
482}
483
484void MediaPlayerService::removeClient(wp<Client> client)
485{
486 Mutex::Autolock lock(mLock);
487 mClients.remove(client);
488}
489
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700490MediaPlayerService::Client::Client(
491 const sp<MediaPlayerService>& service, pid_t pid,
492 int32_t connId, const sp<IMediaPlayerClient>& client,
493 int audioSessionId, uid_t uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800494{
Steve Block3856b092011-10-20 11:56:00 +0100495 ALOGV("Client(%d) constructor", connId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800496 mPid = pid;
497 mConnId = connId;
498 mService = service;
499 mClient = client;
500 mLoop = false;
501 mStatus = NO_INIT;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700502 mAudioSessionId = audioSessionId;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700503 mUID = uid;
John Grossmanc795b642012-02-22 15:38:35 -0800504 mRetransmitEndpointValid = false;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700505
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800506#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000507 ALOGD("create Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800508 mAntagonizer = new Antagonizer(notify, this);
509#endif
510}
511
512MediaPlayerService::Client::~Client()
513{
Steve Block3856b092011-10-20 11:56:00 +0100514 ALOGV("Client(%d) destructor pid = %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800515 mAudioOutput.clear();
516 wp<Client> client(this);
517 disconnect();
518 mService->removeClient(client);
519}
520
521void MediaPlayerService::Client::disconnect()
522{
Steve Block3856b092011-10-20 11:56:00 +0100523 ALOGV("disconnect(%d) from pid %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800524 // grab local reference and clear main reference to prevent future
525 // access to object
526 sp<MediaPlayerBase> p;
527 {
528 Mutex::Autolock l(mLock);
529 p = mPlayer;
530 }
Dave Sparks795fa582009-03-24 17:57:12 -0700531 mClient.clear();
Andreas Huber20111aa2009-07-14 16:56:47 -0700532
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800533 mPlayer.clear();
534
535 // clear the notification to prevent callbacks to dead client
536 // and reset the player. We assume the player will serialize
537 // access to itself if necessary.
538 if (p != 0) {
539 p->setNotifyCallback(0, 0);
540#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000541 ALOGD("kill Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800542 mAntagonizer->kill();
543#endif
544 p->reset();
545 }
546
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700547 disconnectNativeWindow();
548
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800549 IPCThreadState::self()->flushCommands();
550}
551
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800552sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
553{
554 // determine if we have the right player type
555 sp<MediaPlayerBase> p = mPlayer;
556 if ((p != NULL) && (p->playerType() != playerType)) {
Steve Block3856b092011-10-20 11:56:00 +0100557 ALOGV("delete player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800558 p.clear();
559 }
560 if (p == NULL) {
John Grossman44a7e422012-06-21 17:29:24 -0700561 p = MediaPlayerFactory::createPlayer(playerType, this, notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800562 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700563
Jason Simmonsdb29e522011-08-12 13:46:55 -0700564 if (p != NULL) {
565 p->setUID(mUID);
566 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700567
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800568 return p;
569}
570
John Grossmanc795b642012-02-22 15:38:35 -0800571sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre(
572 player_type playerType)
573{
574 ALOGV("player type = %d", playerType);
575
576 // create the right type of player
577 sp<MediaPlayerBase> p = createPlayer(playerType);
578 if (p == NULL) {
579 return p;
580 }
581
582 if (!p->hardwareOutput()) {
583 mAudioOutput = new AudioOutput(mAudioSessionId);
584 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
585 }
586
587 return p;
588}
589
590void MediaPlayerService::Client::setDataSource_post(
591 const sp<MediaPlayerBase>& p,
592 status_t status)
593{
594 ALOGV(" setDataSource");
595 mStatus = status;
596 if (mStatus != OK) {
597 ALOGE(" error: %d", mStatus);
598 return;
599 }
600
601 // Set the re-transmission endpoint if one was chosen.
602 if (mRetransmitEndpointValid) {
603 mStatus = p->setRetransmitEndpoint(&mRetransmitEndpoint);
604 if (mStatus != NO_ERROR) {
605 ALOGE("setRetransmitEndpoint error: %d", mStatus);
606 }
607 }
608
609 if (mStatus == OK) {
610 mPlayer = p;
611 }
612}
613
Andreas Huber2db84552010-01-28 11:19:57 -0800614status_t MediaPlayerService::Client::setDataSource(
615 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800616{
Steve Block3856b092011-10-20 11:56:00 +0100617 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800618 if (url == NULL)
619 return UNKNOWN_ERROR;
620
Dave Burked681bbb2011-08-30 14:39:17 +0100621 if ((strncmp(url, "http://", 7) == 0) ||
622 (strncmp(url, "https://", 8) == 0) ||
623 (strncmp(url, "rtsp://", 7) == 0)) {
624 if (!checkPermission("android.permission.INTERNET")) {
625 return PERMISSION_DENIED;
626 }
627 }
628
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800629 if (strncmp(url, "content://", 10) == 0) {
630 // get a filedescriptor for the content Uri and
631 // pass it to the setDataSource(fd) method
632
633 String16 url16(url);
634 int fd = android::openContentProviderFile(url16);
635 if (fd < 0)
636 {
Steve Block29357bc2012-01-06 19:20:56 +0000637 ALOGE("Couldn't open fd for %s", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800638 return UNKNOWN_ERROR;
639 }
640 setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
641 close(fd);
642 return mStatus;
643 } else {
John Grossman44a7e422012-06-21 17:29:24 -0700644 player_type playerType = MediaPlayerFactory::getPlayerType(this, url);
John Grossmanc795b642012-02-22 15:38:35 -0800645 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
646 if (p == NULL) {
647 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800648 }
649
John Grossmanc795b642012-02-22 15:38:35 -0800650 setDataSource_post(p, p->setDataSource(url, headers));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800651 return mStatus;
652 }
653}
654
655status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
656{
Steve Block3856b092011-10-20 11:56:00 +0100657 ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800658 struct stat sb;
659 int ret = fstat(fd, &sb);
660 if (ret != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000661 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800662 return UNKNOWN_ERROR;
663 }
664
Steve Block3856b092011-10-20 11:56:00 +0100665 ALOGV("st_dev = %llu", sb.st_dev);
666 ALOGV("st_mode = %u", sb.st_mode);
667 ALOGV("st_uid = %lu", sb.st_uid);
668 ALOGV("st_gid = %lu", sb.st_gid);
669 ALOGV("st_size = %llu", sb.st_size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800670
671 if (offset >= sb.st_size) {
Steve Block29357bc2012-01-06 19:20:56 +0000672 ALOGE("offset error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800673 ::close(fd);
674 return UNKNOWN_ERROR;
675 }
676 if (offset + length > sb.st_size) {
677 length = sb.st_size - offset;
Steve Block3856b092011-10-20 11:56:00 +0100678 ALOGV("calculated length = %lld", length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800679 }
680
John Grossman44a7e422012-06-21 17:29:24 -0700681 player_type playerType = MediaPlayerFactory::getPlayerType(this,
682 fd,
683 offset,
684 length);
John Grossmanc795b642012-02-22 15:38:35 -0800685 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
686 if (p == NULL) {
687 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800688 }
689
690 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800691 setDataSource_post(p, p->setDataSource(fd, offset, length));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800692 return mStatus;
693}
694
Andreas Hubere2b10282010-11-23 11:41:34 -0800695status_t MediaPlayerService::Client::setDataSource(
696 const sp<IStreamSource> &source) {
697 // create the right type of player
John Grossman44a7e422012-06-21 17:29:24 -0700698 player_type playerType = MediaPlayerFactory::getPlayerType(this, source);
John Grossmanc795b642012-02-22 15:38:35 -0800699 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
Andreas Hubere2b10282010-11-23 11:41:34 -0800700 if (p == NULL) {
701 return NO_INIT;
702 }
703
Andreas Hubere2b10282010-11-23 11:41:34 -0800704 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800705 setDataSource_post(p, p->setDataSource(source));
Andreas Hubere2b10282010-11-23 11:41:34 -0800706 return mStatus;
707}
708
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700709void MediaPlayerService::Client::disconnectNativeWindow() {
710 if (mConnectedWindow != NULL) {
711 status_t err = native_window_api_disconnect(mConnectedWindow.get(),
712 NATIVE_WINDOW_API_MEDIA);
713
714 if (err != OK) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000715 ALOGW("native_window_api_disconnect returned an error: %s (%d)",
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700716 strerror(-err), err);
717 }
718 }
719 mConnectedWindow.clear();
720}
721
Glenn Kasten11731182011-02-08 17:26:17 -0800722status_t MediaPlayerService::Client::setVideoSurfaceTexture(
723 const sp<ISurfaceTexture>& surfaceTexture)
724{
Steve Block3856b092011-10-20 11:56:00 +0100725 ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, surfaceTexture.get());
Glenn Kasten11731182011-02-08 17:26:17 -0800726 sp<MediaPlayerBase> p = getPlayer();
727 if (p == 0) return UNKNOWN_ERROR;
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700728
729 sp<IBinder> binder(surfaceTexture == NULL ? NULL :
730 surfaceTexture->asBinder());
731 if (mConnectedWindowBinder == binder) {
732 return OK;
733 }
734
735 sp<ANativeWindow> anw;
736 if (surfaceTexture != NULL) {
737 anw = new SurfaceTextureClient(surfaceTexture);
738 status_t err = native_window_api_connect(anw.get(),
739 NATIVE_WINDOW_API_MEDIA);
740
741 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000742 ALOGE("setVideoSurfaceTexture failed: %d", err);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700743 // Note that we must do the reset before disconnecting from the ANW.
744 // Otherwise queue/dequeue calls could be made on the disconnected
745 // ANW, which may result in errors.
746 reset();
747
748 disconnectNativeWindow();
749
750 return err;
751 }
752 }
753
754 // Note that we must set the player's new SurfaceTexture before
755 // disconnecting the old one. Otherwise queue/dequeue calls could be made
756 // on the disconnected ANW, which may result in errors.
757 status_t err = p->setVideoSurfaceTexture(surfaceTexture);
758
759 disconnectNativeWindow();
760
761 mConnectedWindow = anw;
762
763 if (err == OK) {
764 mConnectedWindowBinder = binder;
765 } else {
766 disconnectNativeWindow();
767 }
768
769 return err;
Glenn Kasten11731182011-02-08 17:26:17 -0800770}
771
Nicolas Catania1d187f12009-05-12 23:25:55 -0700772status_t MediaPlayerService::Client::invoke(const Parcel& request,
773 Parcel *reply)
774{
775 sp<MediaPlayerBase> p = getPlayer();
776 if (p == NULL) return UNKNOWN_ERROR;
777 return p->invoke(request, reply);
778}
779
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700780// This call doesn't need to access the native player.
781status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
782{
783 status_t status;
nikoa64c8c72009-07-20 15:07:26 -0700784 media::Metadata::Filter allow, drop;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700785
Nicolas Catania48290382009-07-10 13:53:06 -0700786 if (unmarshallFilter(filter, &allow, &status) &&
787 unmarshallFilter(filter, &drop, &status)) {
788 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700789
790 mMetadataAllow = allow;
791 mMetadataDrop = drop;
792 }
793 return status;
794}
795
Nicolas Catania48290382009-07-10 13:53:06 -0700796status_t MediaPlayerService::Client::getMetadata(
797 bool update_only, bool apply_filter, Parcel *reply)
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700798{
nikoa64c8c72009-07-20 15:07:26 -0700799 sp<MediaPlayerBase> player = getPlayer();
800 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania48290382009-07-10 13:53:06 -0700801
nikod608a812009-07-16 16:39:53 -0700802 status_t status;
803 // Placeholder for the return code, updated by the caller.
804 reply->writeInt32(-1);
805
nikoa64c8c72009-07-20 15:07:26 -0700806 media::Metadata::Filter ids;
Nicolas Catania48290382009-07-10 13:53:06 -0700807
808 // We don't block notifications while we fetch the data. We clear
809 // mMetadataUpdated first so we don't lose notifications happening
810 // during the rest of this call.
811 {
812 Mutex::Autolock lock(mLock);
813 if (update_only) {
nikod608a812009-07-16 16:39:53 -0700814 ids = mMetadataUpdated;
Nicolas Catania48290382009-07-10 13:53:06 -0700815 }
816 mMetadataUpdated.clear();
817 }
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700818
nikoa64c8c72009-07-20 15:07:26 -0700819 media::Metadata metadata(reply);
Nicolas Catania48290382009-07-10 13:53:06 -0700820
nikoa64c8c72009-07-20 15:07:26 -0700821 metadata.appendHeader();
822 status = player->getMetadata(ids, reply);
nikod608a812009-07-16 16:39:53 -0700823
824 if (status != OK) {
nikoa64c8c72009-07-20 15:07:26 -0700825 metadata.resetParcel();
Steve Block29357bc2012-01-06 19:20:56 +0000826 ALOGE("getMetadata failed %d", status);
nikod608a812009-07-16 16:39:53 -0700827 return status;
828 }
829
830 // FIXME: Implement filtering on the result. Not critical since
831 // filtering takes place on the update notifications already. This
832 // would be when all the metadata are fetch and a filter is set.
833
nikod608a812009-07-16 16:39:53 -0700834 // Everything is fine, update the metadata length.
nikoa64c8c72009-07-20 15:07:26 -0700835 metadata.updateLength();
nikod608a812009-07-16 16:39:53 -0700836 return OK;
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700837}
838
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800839status_t MediaPlayerService::Client::prepareAsync()
840{
Steve Block3856b092011-10-20 11:56:00 +0100841 ALOGV("[%d] prepareAsync", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800842 sp<MediaPlayerBase> p = getPlayer();
843 if (p == 0) return UNKNOWN_ERROR;
844 status_t ret = p->prepareAsync();
845#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000846 ALOGD("start Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800847 if (ret == NO_ERROR) mAntagonizer->start();
848#endif
849 return ret;
850}
851
852status_t MediaPlayerService::Client::start()
853{
Steve Block3856b092011-10-20 11:56:00 +0100854 ALOGV("[%d] start", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800855 sp<MediaPlayerBase> p = getPlayer();
856 if (p == 0) return UNKNOWN_ERROR;
857 p->setLooping(mLoop);
858 return p->start();
859}
860
861status_t MediaPlayerService::Client::stop()
862{
Steve Block3856b092011-10-20 11:56:00 +0100863 ALOGV("[%d] stop", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800864 sp<MediaPlayerBase> p = getPlayer();
865 if (p == 0) return UNKNOWN_ERROR;
866 return p->stop();
867}
868
869status_t MediaPlayerService::Client::pause()
870{
Steve Block3856b092011-10-20 11:56:00 +0100871 ALOGV("[%d] pause", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800872 sp<MediaPlayerBase> p = getPlayer();
873 if (p == 0) return UNKNOWN_ERROR;
874 return p->pause();
875}
876
877status_t MediaPlayerService::Client::isPlaying(bool* state)
878{
879 *state = false;
880 sp<MediaPlayerBase> p = getPlayer();
881 if (p == 0) return UNKNOWN_ERROR;
882 *state = p->isPlaying();
Steve Block3856b092011-10-20 11:56:00 +0100883 ALOGV("[%d] isPlaying: %d", mConnId, *state);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800884 return NO_ERROR;
885}
886
887status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
888{
Steve Block3856b092011-10-20 11:56:00 +0100889 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800890 sp<MediaPlayerBase> p = getPlayer();
891 if (p == 0) return UNKNOWN_ERROR;
892 status_t ret = p->getCurrentPosition(msec);
893 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100894 ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800895 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000896 ALOGE("getCurrentPosition returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800897 }
898 return ret;
899}
900
901status_t MediaPlayerService::Client::getDuration(int *msec)
902{
Steve Block3856b092011-10-20 11:56:00 +0100903 ALOGV("getDuration");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800904 sp<MediaPlayerBase> p = getPlayer();
905 if (p == 0) return UNKNOWN_ERROR;
906 status_t ret = p->getDuration(msec);
907 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100908 ALOGV("[%d] getDuration = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800909 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000910 ALOGE("getDuration returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800911 }
912 return ret;
913}
914
Marco Nelissen6b74d672012-02-28 16:07:44 -0800915status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) {
916 ALOGV("setNextPlayer");
917 Mutex::Autolock l(mLock);
918 sp<Client> c = static_cast<Client*>(player.get());
919 mNextClient = c;
920 if (mAudioOutput != NULL && c != NULL) {
921 mAudioOutput->setNextOutput(c->mAudioOutput);
922 } else {
923 ALOGE("no current audio output");
924 }
925 return OK;
926}
927
928
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800929status_t MediaPlayerService::Client::seekTo(int msec)
930{
Steve Block3856b092011-10-20 11:56:00 +0100931 ALOGV("[%d] seekTo(%d)", mConnId, msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800932 sp<MediaPlayerBase> p = getPlayer();
933 if (p == 0) return UNKNOWN_ERROR;
934 return p->seekTo(msec);
935}
936
937status_t MediaPlayerService::Client::reset()
938{
Steve Block3856b092011-10-20 11:56:00 +0100939 ALOGV("[%d] reset", mConnId);
John Grossmanc795b642012-02-22 15:38:35 -0800940 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800941 sp<MediaPlayerBase> p = getPlayer();
942 if (p == 0) return UNKNOWN_ERROR;
943 return p->reset();
944}
945
Glenn Kastenfff6d712012-01-12 16:38:12 -0800946status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800947{
Steve Block3856b092011-10-20 11:56:00 +0100948 ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800949 // TODO: for hardware output, call player instead
950 Mutex::Autolock l(mLock);
951 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
952 return NO_ERROR;
953}
954
955status_t MediaPlayerService::Client::setLooping(int loop)
956{
Steve Block3856b092011-10-20 11:56:00 +0100957 ALOGV("[%d] setLooping(%d)", mConnId, loop);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800958 mLoop = loop;
959 sp<MediaPlayerBase> p = getPlayer();
960 if (p != 0) return p->setLooping(loop);
961 return NO_ERROR;
962}
963
964status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
965{
Steve Block3856b092011-10-20 11:56:00 +0100966 ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
John Grossman761defc2012-02-09 15:09:05 -0800967
968 // for hardware output, call player instead
969 sp<MediaPlayerBase> p = getPlayer();
970 {
971 Mutex::Autolock l(mLock);
972 if (p != 0 && p->hardwareOutput()) {
973 MediaPlayerHWInterface* hwp =
974 reinterpret_cast<MediaPlayerHWInterface*>(p.get());
975 return hwp->setVolume(leftVolume, rightVolume);
976 } else {
977 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
978 return NO_ERROR;
979 }
980 }
981
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800982 return NO_ERROR;
983}
984
Eric Laurent2beeb502010-07-16 07:43:46 -0700985status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
986{
Steve Block3856b092011-10-20 11:56:00 +0100987 ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
Eric Laurent2beeb502010-07-16 07:43:46 -0700988 Mutex::Autolock l(mLock);
989 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
990 return NO_ERROR;
991}
992
993status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
994{
Steve Block3856b092011-10-20 11:56:00 +0100995 ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700996 Mutex::Autolock l(mLock);
997 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
998 return NO_ERROR;
999}
Nicolas Catania48290382009-07-10 13:53:06 -07001000
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001001status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
Steve Block3856b092011-10-20 11:56:00 +01001002 ALOGV("[%d] setParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001003 sp<MediaPlayerBase> p = getPlayer();
1004 if (p == 0) return UNKNOWN_ERROR;
1005 return p->setParameter(key, request);
1006}
1007
1008status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
Steve Block3856b092011-10-20 11:56:00 +01001009 ALOGV("[%d] getParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001010 sp<MediaPlayerBase> p = getPlayer();
1011 if (p == 0) return UNKNOWN_ERROR;
1012 return p->getParameter(key, reply);
1013}
1014
John Grossmanc795b642012-02-22 15:38:35 -08001015status_t MediaPlayerService::Client::setRetransmitEndpoint(
1016 const struct sockaddr_in* endpoint) {
1017
1018 if (NULL != endpoint) {
1019 uint32_t a = ntohl(endpoint->sin_addr.s_addr);
1020 uint16_t p = ntohs(endpoint->sin_port);
1021 ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId,
1022 (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p);
1023 } else {
1024 ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId);
1025 }
1026
1027 sp<MediaPlayerBase> p = getPlayer();
1028
1029 // Right now, the only valid time to set a retransmit endpoint is before
1030 // player selection has been made (since the presence or absence of a
1031 // retransmit endpoint is going to determine which player is selected during
1032 // setDataSource).
1033 if (p != 0) return INVALID_OPERATION;
1034
1035 if (NULL != endpoint) {
1036 mRetransmitEndpoint = *endpoint;
1037 mRetransmitEndpointValid = true;
1038 } else {
1039 mRetransmitEndpointValid = false;
1040 }
1041
1042 return NO_ERROR;
1043}
1044
John Grossman44a7e422012-06-21 17:29:24 -07001045status_t MediaPlayerService::Client::getRetransmitEndpoint(
1046 struct sockaddr_in* endpoint)
1047{
1048 if (NULL == endpoint)
1049 return BAD_VALUE;
1050
1051 sp<MediaPlayerBase> p = getPlayer();
1052
1053 if (p != NULL)
1054 return p->getRetransmitEndpoint(endpoint);
1055
1056 if (!mRetransmitEndpointValid)
1057 return NO_INIT;
1058
1059 *endpoint = mRetransmitEndpoint;
1060
1061 return NO_ERROR;
1062}
1063
Gloria Wangb483c472011-04-11 17:23:27 -07001064void MediaPlayerService::Client::notify(
1065 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001066{
1067 Client* client = static_cast<Client*>(cookie);
James Dongb8a98252012-08-26 16:13:03 -07001068 if (client == NULL) {
1069 return;
1070 }
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001071
James Dongb8a98252012-08-26 16:13:03 -07001072 sp<IMediaPlayerClient> c;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001073 {
1074 Mutex::Autolock l(client->mLock);
James Dongb8a98252012-08-26 16:13:03 -07001075 c = client->mClient;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001076 if (msg == MEDIA_PLAYBACK_COMPLETE && client->mNextClient != NULL) {
John Grossmancb0b7552012-08-23 17:47:31 -07001077 if (client->mAudioOutput != NULL)
1078 client->mAudioOutput->switchToNextOutput();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001079 client->mNextClient->start();
1080 client->mNextClient->mClient->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj);
1081 }
1082 }
1083
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001084 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001085 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001086 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-07-10 13:53:06 -07001087
1088 if(client->shouldDropMetadata(metadata_type)) {
1089 return;
1090 }
1091
1092 // Update the list of metadata that have changed. getMetadata
1093 // also access mMetadataUpdated and clears it.
1094 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001095 }
James Dongb8a98252012-08-26 16:13:03 -07001096
1097 if (c != NULL) {
1098 ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
1099 c->notify(msg, ext1, ext2, obj);
1100 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001101}
1102
Nicolas Catania48290382009-07-10 13:53:06 -07001103
nikoa64c8c72009-07-20 15:07:26 -07001104bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001105{
Nicolas Catania48290382009-07-10 13:53:06 -07001106 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001107
Nicolas Catania48290382009-07-10 13:53:06 -07001108 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001109 return true;
1110 }
1111
Nicolas Catania48290382009-07-10 13:53:06 -07001112 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001113 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001114 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001115 return true;
1116 }
1117}
1118
Nicolas Catania48290382009-07-10 13:53:06 -07001119
nikoa64c8c72009-07-20 15:07:26 -07001120void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-07-10 13:53:06 -07001121 Mutex::Autolock lock(mLock);
1122 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1123 mMetadataUpdated.add(metadata_type);
1124 }
1125}
1126
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001127#if CALLBACK_ANTAGONIZER
1128const int Antagonizer::interval = 10000; // 10 msecs
1129
1130Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1131 mExit(false), mActive(false), mClient(client), mCb(cb)
1132{
1133 createThread(callbackThread, this);
1134}
1135
1136void Antagonizer::kill()
1137{
1138 Mutex::Autolock _l(mLock);
1139 mActive = false;
1140 mExit = true;
1141 mCondition.wait(mLock);
1142}
1143
1144int Antagonizer::callbackThread(void* user)
1145{
Steve Blockb8a80522011-12-20 16:23:08 +00001146 ALOGD("Antagonizer started");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001147 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1148 while (!p->mExit) {
1149 if (p->mActive) {
Steve Block3856b092011-10-20 11:56:00 +01001150 ALOGV("send event");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001151 p->mCb(p->mClient, 0, 0, 0);
1152 }
1153 usleep(interval);
1154 }
1155 Mutex::Autolock _l(p->mLock);
1156 p->mCondition.signal();
Steve Blockb8a80522011-12-20 16:23:08 +00001157 ALOGD("Antagonizer stopped");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001158 return 0;
1159}
1160#endif
1161
1162static size_t kDefaultHeapSize = 1024 * 1024; // 1MB
1163
Glenn Kastene1c39622012-01-04 09:36:37 -08001164sp<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 -08001165{
Steve Block3856b092011-10-20 11:56:00 +01001166 ALOGV("decode(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001167 sp<MemoryBase> mem;
1168 sp<MediaPlayerBase> player;
1169
1170 // Protect our precious, precious DRMd ringtones by only allowing
1171 // decoding of http, but not filesystem paths or content Uris.
1172 // If the application wants to decode those, it should open a
1173 // filedescriptor for them and use that.
1174 if (url != NULL && strncmp(url, "http://", 7) != 0) {
Steve Blockb8a80522011-12-20 16:23:08 +00001175 ALOGD("Can't decode %s by path, use filedescriptor instead", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001176 return mem;
1177 }
1178
John Grossman44a7e422012-06-21 17:29:24 -07001179 player_type playerType =
1180 MediaPlayerFactory::getPlayerType(NULL /* client */, url);
Steve Block3856b092011-10-20 11:56:00 +01001181 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001182
1183 // create the right type of player
1184 sp<AudioCache> cache = new AudioCache(url);
John Grossman44a7e422012-06-21 17:29:24 -07001185 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001186 if (player == NULL) goto Exit;
1187 if (player->hardwareOutput()) goto Exit;
1188
1189 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1190
1191 // set data source
1192 if (player->setDataSource(url) != NO_ERROR) goto Exit;
1193
Steve Block3856b092011-10-20 11:56:00 +01001194 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001195 player->prepareAsync();
1196
Steve Block3856b092011-10-20 11:56:00 +01001197 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001198 if (cache->wait() != NO_ERROR) goto Exit;
1199
Steve Block3856b092011-10-20 11:56:00 +01001200 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001201 player->start();
1202
Steve Block3856b092011-10-20 11:56:00 +01001203 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001204 cache->wait();
1205 // in case of error, return what was successfully decoded.
1206 if (cache->size() == 0) {
1207 goto Exit;
1208 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001209
1210 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1211 *pSampleRate = cache->sampleRate();
1212 *pNumChannels = cache->channelCount();
Glenn Kastene1c39622012-01-04 09:36:37 -08001213 *pFormat = cache->format();
Steve Block3856b092011-10-20 11:56:00 +01001214 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 -08001215
1216Exit:
1217 if (player != 0) player->reset();
1218 return mem;
1219}
1220
Glenn Kastene1c39622012-01-04 09:36:37 -08001221sp<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 -08001222{
Steve Block3856b092011-10-20 11:56:00 +01001223 ALOGV("decode(%d, %lld, %lld)", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001224 sp<MemoryBase> mem;
1225 sp<MediaPlayerBase> player;
1226
John Grossman44a7e422012-06-21 17:29:24 -07001227 player_type playerType = MediaPlayerFactory::getPlayerType(NULL /* client */,
1228 fd,
1229 offset,
1230 length);
Steve Block3856b092011-10-20 11:56:00 +01001231 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001232
1233 // create the right type of player
1234 sp<AudioCache> cache = new AudioCache("decode_fd");
John Grossman44a7e422012-06-21 17:29:24 -07001235 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001236 if (player == NULL) goto Exit;
1237 if (player->hardwareOutput()) goto Exit;
1238
1239 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1240
1241 // set data source
1242 if (player->setDataSource(fd, offset, length) != NO_ERROR) goto Exit;
1243
Steve Block3856b092011-10-20 11:56:00 +01001244 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001245 player->prepareAsync();
1246
Steve Block3856b092011-10-20 11:56:00 +01001247 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001248 if (cache->wait() != NO_ERROR) goto Exit;
1249
Steve Block3856b092011-10-20 11:56:00 +01001250 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001251 player->start();
1252
Steve Block3856b092011-10-20 11:56:00 +01001253 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001254 cache->wait();
1255 // in case of error, return what was successfully decoded.
1256 if (cache->size() == 0) {
1257 goto Exit;
1258 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001259
1260 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1261 *pSampleRate = cache->sampleRate();
1262 *pNumChannels = cache->channelCount();
1263 *pFormat = cache->format();
Steve Block3856b092011-10-20 11:56:00 +01001264 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 -08001265
1266Exit:
1267 if (player != 0) player->reset();
1268 ::close(fd);
1269 return mem;
1270}
1271
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001272
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001273#undef LOG_TAG
1274#define LOG_TAG "AudioSink"
Eric Laurenta514bdb2010-06-21 09:27:30 -07001275MediaPlayerService::AudioOutput::AudioOutput(int sessionId)
Andreas Huber20111aa2009-07-14 16:56:47 -07001276 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001277 mCallbackCookie(NULL),
Marco Nelissen6b74d672012-02-28 16:07:44 -08001278 mCallbackData(NULL),
Marco Nelissen4110c102012-03-29 09:31:28 -07001279 mBytesWritten(0),
Eric Laurent1948eb32012-04-13 16:50:19 -07001280 mSessionId(sessionId),
1281 mFlags(AUDIO_OUTPUT_FLAG_NONE) {
Steve Block3856b092011-10-20 11:56:00 +01001282 ALOGV("AudioOutput(%d)", sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001283 mTrack = 0;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001284 mRecycledTrack = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -07001285 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001286 mLeftVolume = 1.0;
1287 mRightVolume = 1.0;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001288 mPlaybackRatePermille = 1000;
1289 mSampleRateHz = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001290 mMsecsPerFrame = 0;
Eric Laurent2beeb502010-07-16 07:43:46 -07001291 mAuxEffectId = 0;
1292 mSendLevel = 0.0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001293 setMinBufferCount();
1294}
1295
1296MediaPlayerService::AudioOutput::~AudioOutput()
1297{
1298 close();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001299 delete mRecycledTrack;
1300 delete mCallbackData;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001301}
1302
1303void MediaPlayerService::AudioOutput::setMinBufferCount()
1304{
1305 char value[PROPERTY_VALUE_MAX];
1306 if (property_get("ro.kernel.qemu", value, 0)) {
1307 mIsOnEmulator = true;
1308 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1309 }
1310}
1311
1312bool MediaPlayerService::AudioOutput::isOnEmulator()
1313{
1314 setMinBufferCount();
1315 return mIsOnEmulator;
1316}
1317
1318int MediaPlayerService::AudioOutput::getMinBufferCount()
1319{
1320 setMinBufferCount();
1321 return mMinBufferCount;
1322}
1323
1324ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1325{
1326 if (mTrack == 0) return NO_INIT;
1327 return mTrack->frameCount() * frameSize();
1328}
1329
1330ssize_t MediaPlayerService::AudioOutput::frameCount() const
1331{
1332 if (mTrack == 0) return NO_INIT;
1333 return mTrack->frameCount();
1334}
1335
1336ssize_t MediaPlayerService::AudioOutput::channelCount() const
1337{
1338 if (mTrack == 0) return NO_INIT;
1339 return mTrack->channelCount();
1340}
1341
1342ssize_t MediaPlayerService::AudioOutput::frameSize() const
1343{
1344 if (mTrack == 0) return NO_INIT;
1345 return mTrack->frameSize();
1346}
1347
1348uint32_t MediaPlayerService::AudioOutput::latency () const
1349{
Eric Laurentdb354e52012-03-05 17:27:11 -08001350 if (mTrack == 0) return 0;
1351 return mTrack->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001352}
1353
1354float MediaPlayerService::AudioOutput::msecsPerFrame() const
1355{
1356 return mMsecsPerFrame;
1357}
1358
Marco Nelissen4110c102012-03-29 09:31:28 -07001359status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001360{
1361 if (mTrack == 0) return NO_INIT;
1362 return mTrack->getPosition(position);
1363}
1364
Marco Nelissen4110c102012-03-29 09:31:28 -07001365status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
1366{
1367 if (mTrack == 0) return NO_INIT;
1368 *frameswritten = mBytesWritten / frameSize();
1369 return OK;
1370}
1371
Andreas Huber20111aa2009-07-14 16:56:47 -07001372status_t MediaPlayerService::AudioOutput::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001373 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1374 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001375 AudioCallback cb, void *cookie,
1376 audio_output_flags_t flags)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001377{
Andreas Huber20111aa2009-07-14 16:56:47 -07001378 mCallback = cb;
1379 mCallbackCookie = cookie;
1380
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001381 // Check argument "bufferCount" against the mininum buffer count
1382 if (bufferCount < mMinBufferCount) {
Steve Blockb8a80522011-12-20 16:23:08 +00001383 ALOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001384 bufferCount = mMinBufferCount;
1385
1386 }
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001387 ALOGV("open(%u, %d, 0x%x, %d, %d, %d)", sampleRate, channelCount, channelMask,
1388 format, bufferCount, mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001389 int afSampleRate;
1390 int afFrameCount;
Eric Laurent1948eb32012-04-13 16:50:19 -07001391 uint32_t frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001392
1393 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1394 return NO_INIT;
1395 }
1396 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1397 return NO_INIT;
1398 }
1399
1400 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
Andreas Huber20111aa2009-07-14 16:56:47 -07001401
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001402 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
Glenn Kastenab334fd2012-03-14 12:56:06 -07001403 channelMask = audio_channel_out_mask_from_count(channelCount);
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001404 if (0 == channelMask) {
1405 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
1406 return NO_INIT;
1407 }
1408 }
Eric Laurent1948eb32012-04-13 16:50:19 -07001409
Andreas Huber20111aa2009-07-14 16:56:47 -07001410 AudioTrack *t;
Marco Nelissen67295b52012-06-11 14:52:53 -07001411 CallbackData *newcbd = NULL;
Andreas Huber20111aa2009-07-14 16:56:47 -07001412 if (mCallback != NULL) {
Marco Nelissen67295b52012-06-11 14:52:53 -07001413 newcbd = new CallbackData(this);
Andreas Huber20111aa2009-07-14 16:56:47 -07001414 t = new AudioTrack(
Eric Laurentc2f1f072009-07-17 12:17:14 -07001415 mStreamType,
1416 sampleRate,
1417 format,
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001418 channelMask,
Eric Laurentc2f1f072009-07-17 12:17:14 -07001419 frameCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001420 flags,
Eric Laurentc2f1f072009-07-17 12:17:14 -07001421 CallbackWrapper,
Marco Nelissen67295b52012-06-11 14:52:53 -07001422 newcbd,
Glenn Kasten17a736c2012-02-14 08:52:15 -08001423 0, // notification frames
Eric Laurenta514bdb2010-06-21 09:27:30 -07001424 mSessionId);
Andreas Huber20111aa2009-07-14 16:56:47 -07001425 } else {
1426 t = new AudioTrack(
Eric Laurentc2f1f072009-07-17 12:17:14 -07001427 mStreamType,
1428 sampleRate,
1429 format,
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001430 channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -07001431 frameCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001432 flags,
Eric Laurenta514bdb2010-06-21 09:27:30 -07001433 NULL,
1434 NULL,
1435 0,
1436 mSessionId);
Andreas Huber20111aa2009-07-14 16:56:47 -07001437 }
1438
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001439 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
Steve Block29357bc2012-01-06 19:20:56 +00001440 ALOGE("Unable to create audio track");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001441 delete t;
Marco Nelissen67295b52012-06-11 14:52:53 -07001442 delete newcbd;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001443 return NO_INIT;
1444 }
1445
Marco Nelissen67295b52012-06-11 14:52:53 -07001446
1447 if (mRecycledTrack) {
1448 // check if the existing track can be reused as-is, or if a new track needs to be created.
1449
1450 bool reuse = true;
1451 if ((mCallbackData == NULL && mCallback != NULL) ||
1452 (mCallbackData != NULL && mCallback == NULL)) {
1453 // recycled track uses callbacks but the caller wants to use writes, or vice versa
1454 ALOGV("can't chain callback and write");
1455 reuse = false;
1456 } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
1457 (mRecycledTrack->channelCount() != channelCount) ||
1458 (mRecycledTrack->frameCount() != t->frameCount())) {
1459 ALOGV("samplerate, channelcount or framecount differ: %d/%d Hz, %d/%d ch, %d/%d frames",
1460 mRecycledTrack->getSampleRate(), sampleRate,
1461 mRecycledTrack->channelCount(), channelCount,
1462 mRecycledTrack->frameCount(), t->frameCount());
1463 reuse = false;
1464 } else if (flags != mFlags) {
1465 ALOGV("output flags differ %08x/%08x", flags, mFlags);
1466 reuse = false;
1467 }
1468 if (reuse) {
1469 ALOGV("chaining to next output");
1470 close();
1471 mTrack = mRecycledTrack;
1472 mRecycledTrack = NULL;
1473 if (mCallbackData != NULL) {
1474 mCallbackData->setOutput(this);
1475 }
1476 delete t;
1477 delete newcbd;
1478 return OK;
1479 }
1480
1481 // if we're not going to reuse the track, unblock and flush it
1482 if (mCallbackData != NULL) {
1483 mCallbackData->setOutput(NULL);
1484 mCallbackData->endTrackSwitch();
1485 }
1486 mRecycledTrack->flush();
1487 delete mRecycledTrack;
1488 mRecycledTrack = NULL;
1489 delete mCallbackData;
1490 mCallbackData = NULL;
1491 close();
1492 }
1493
1494 mCallbackData = newcbd;
Steve Block3856b092011-10-20 11:56:00 +01001495 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001496 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001497
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001498 mSampleRateHz = sampleRate;
Eric Laurent1948eb32012-04-13 16:50:19 -07001499 mFlags = flags;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001500 mMsecsPerFrame = mPlaybackRatePermille / (float) sampleRate;
Marco Nelissen99448602012-04-02 12:16:49 -07001501 uint32_t pos;
1502 if (t->getPosition(&pos) == OK) {
1503 mBytesWritten = uint64_t(pos) * t->frameSize();
1504 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001505 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001506
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001507 status_t res = t->setSampleRate(mPlaybackRatePermille * mSampleRateHz / 1000);
1508 if (res != NO_ERROR) {
1509 return res;
1510 }
Eric Laurent2beeb502010-07-16 07:43:46 -07001511 t->setAuxEffectSendLevel(mSendLevel);
1512 return t->attachAuxEffect(mAuxEffectId);;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001513}
1514
1515void MediaPlayerService::AudioOutput::start()
1516{
Steve Block3856b092011-10-20 11:56:00 +01001517 ALOGV("start");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001518 if (mCallbackData != NULL) {
1519 mCallbackData->endTrackSwitch();
1520 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001521 if (mTrack) {
1522 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001523 mTrack->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001524 mTrack->start();
1525 }
1526}
1527
Marco Nelissen6b74d672012-02-28 16:07:44 -08001528void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
1529 mNextOutput = nextOutput;
1530}
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001531
1532
Marco Nelissen6b74d672012-02-28 16:07:44 -08001533void MediaPlayerService::AudioOutput::switchToNextOutput() {
1534 ALOGV("switchToNextOutput");
1535 if (mNextOutput != NULL) {
1536 if (mCallbackData != NULL) {
1537 mCallbackData->beginTrackSwitch();
1538 }
1539 delete mNextOutput->mCallbackData;
1540 mNextOutput->mCallbackData = mCallbackData;
1541 mCallbackData = NULL;
1542 mNextOutput->mRecycledTrack = mTrack;
1543 mTrack = NULL;
1544 mNextOutput->mSampleRateHz = mSampleRateHz;
1545 mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
Marco Nelissen4110c102012-03-29 09:31:28 -07001546 mNextOutput->mBytesWritten = mBytesWritten;
Marco Nelissend791e092012-06-11 17:00:59 -07001547 mNextOutput->mFlags = mFlags;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001548 }
1549}
1550
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001551ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size)
1552{
Andreas Huber20111aa2009-07-14 16:56:47 -07001553 LOG_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
1554
Steve Block3856b092011-10-20 11:56:00 +01001555 //ALOGV("write(%p, %u)", buffer, size);
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001556 if (mTrack) {
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001557 ssize_t ret = mTrack->write(buffer, size);
Marco Nelissen4110c102012-03-29 09:31:28 -07001558 mBytesWritten += ret;
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001559 return ret;
1560 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001561 return NO_INIT;
1562}
1563
1564void MediaPlayerService::AudioOutput::stop()
1565{
Steve Block3856b092011-10-20 11:56:00 +01001566 ALOGV("stop");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001567 if (mTrack) mTrack->stop();
1568}
1569
1570void MediaPlayerService::AudioOutput::flush()
1571{
Steve Block3856b092011-10-20 11:56:00 +01001572 ALOGV("flush");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001573 if (mTrack) mTrack->flush();
1574}
1575
1576void MediaPlayerService::AudioOutput::pause()
1577{
Steve Block3856b092011-10-20 11:56:00 +01001578 ALOGV("pause");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001579 if (mTrack) mTrack->pause();
1580}
1581
1582void MediaPlayerService::AudioOutput::close()
1583{
Steve Block3856b092011-10-20 11:56:00 +01001584 ALOGV("close");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001585 delete mTrack;
1586 mTrack = 0;
1587}
1588
1589void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1590{
Steve Block3856b092011-10-20 11:56:00 +01001591 ALOGV("setVolume(%f, %f)", left, right);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001592 mLeftVolume = left;
1593 mRightVolume = right;
1594 if (mTrack) {
1595 mTrack->setVolume(left, right);
1596 }
1597}
1598
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001599status_t MediaPlayerService::AudioOutput::setPlaybackRatePermille(int32_t ratePermille)
1600{
1601 ALOGV("setPlaybackRatePermille(%d)", ratePermille);
1602 status_t res = NO_ERROR;
1603 if (mTrack) {
1604 res = mTrack->setSampleRate(ratePermille * mSampleRateHz / 1000);
1605 } else {
1606 res = NO_INIT;
1607 }
1608 mPlaybackRatePermille = ratePermille;
1609 if (mSampleRateHz != 0) {
1610 mMsecsPerFrame = mPlaybackRatePermille / (float) mSampleRateHz;
1611 }
1612 return res;
1613}
1614
Eric Laurent2beeb502010-07-16 07:43:46 -07001615status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1616{
Steve Block3856b092011-10-20 11:56:00 +01001617 ALOGV("setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001618 mSendLevel = level;
1619 if (mTrack) {
1620 return mTrack->setAuxEffectSendLevel(level);
1621 }
1622 return NO_ERROR;
1623}
1624
1625status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1626{
Steve Block3856b092011-10-20 11:56:00 +01001627 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001628 mAuxEffectId = effectId;
1629 if (mTrack) {
1630 return mTrack->attachAuxEffect(effectId);
1631 }
1632 return NO_ERROR;
1633}
1634
Andreas Huber20111aa2009-07-14 16:56:47 -07001635// static
1636void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastend217a8c2011-06-01 15:20:35 -07001637 int event, void *cookie, void *info) {
Steve Block3856b092011-10-20 11:56:00 +01001638 //ALOGV("callbackwrapper");
Andreas Huber20111aa2009-07-14 16:56:47 -07001639 if (event != AudioTrack::EVENT_MORE_DATA) {
1640 return;
1641 }
1642
Marco Nelissen6b74d672012-02-28 16:07:44 -08001643 CallbackData *data = (CallbackData*)cookie;
1644 data->lock();
1645 AudioOutput *me = data->getOutput();
Andreas Huber20111aa2009-07-14 16:56:47 -07001646 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001647 if (me == NULL) {
1648 // no output set, likely because the track was scheduled to be reused
1649 // by another player, but the format turned out to be incompatible.
1650 data->unlock();
1651 buffer->size = 0;
1652 return;
1653 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001654
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001655 size_t actualSize = (*me->mCallback)(
Andreas Huber20111aa2009-07-14 16:56:47 -07001656 me, buffer->raw, buffer->size, me->mCallbackCookie);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001657
Marco Nelissen6b74d672012-02-28 16:07:44 -08001658 if (actualSize == 0 && buffer->size > 0 && me->mNextOutput == NULL) {
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001659 // We've reached EOS but the audio track is not stopped yet,
1660 // keep playing silence.
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08001661
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001662 memset(buffer->raw, 0, buffer->size);
1663 actualSize = buffer->size;
1664 }
1665
1666 buffer->size = actualSize;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001667 data->unlock();
Andreas Huber20111aa2009-07-14 16:56:47 -07001668}
1669
Marco Nelissen4110c102012-03-29 09:31:28 -07001670int MediaPlayerService::AudioOutput::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001671{
1672 return mSessionId;
1673}
1674
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001675#undef LOG_TAG
1676#define LOG_TAG "AudioCache"
1677MediaPlayerService::AudioCache::AudioCache(const char* name) :
1678 mChannelCount(0), mFrameCount(1024), mSampleRate(0), mSize(0),
1679 mError(NO_ERROR), mCommandComplete(false)
1680{
1681 // create ashmem heap
1682 mHeap = new MemoryHeapBase(kDefaultHeapSize, 0, name);
1683}
1684
1685uint32_t MediaPlayerService::AudioCache::latency () const
1686{
1687 return 0;
1688}
1689
1690float MediaPlayerService::AudioCache::msecsPerFrame() const
1691{
1692 return mMsecsPerFrame;
1693}
1694
Marco Nelissen4110c102012-03-29 09:31:28 -07001695status_t MediaPlayerService::AudioCache::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001696{
1697 if (position == 0) return BAD_VALUE;
1698 *position = mSize;
1699 return NO_ERROR;
1700}
1701
Marco Nelissen4110c102012-03-29 09:31:28 -07001702status_t MediaPlayerService::AudioCache::getFramesWritten(uint32_t *written) const
1703{
1704 if (written == 0) return BAD_VALUE;
1705 *written = mSize;
1706 return NO_ERROR;
1707}
1708
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001709////////////////////////////////////////////////////////////////////////////////
1710
1711struct CallbackThread : public Thread {
1712 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1713 MediaPlayerBase::AudioSink::AudioCallback cb,
1714 void *cookie);
1715
1716protected:
1717 virtual ~CallbackThread();
1718
1719 virtual bool threadLoop();
1720
1721private:
1722 wp<MediaPlayerBase::AudioSink> mSink;
1723 MediaPlayerBase::AudioSink::AudioCallback mCallback;
1724 void *mCookie;
1725 void *mBuffer;
1726 size_t mBufferSize;
1727
1728 CallbackThread(const CallbackThread &);
1729 CallbackThread &operator=(const CallbackThread &);
1730};
1731
1732CallbackThread::CallbackThread(
1733 const wp<MediaPlayerBase::AudioSink> &sink,
1734 MediaPlayerBase::AudioSink::AudioCallback cb,
1735 void *cookie)
1736 : mSink(sink),
1737 mCallback(cb),
1738 mCookie(cookie),
1739 mBuffer(NULL),
1740 mBufferSize(0) {
1741}
1742
1743CallbackThread::~CallbackThread() {
1744 if (mBuffer) {
1745 free(mBuffer);
1746 mBuffer = NULL;
1747 }
1748}
1749
1750bool CallbackThread::threadLoop() {
1751 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
1752 if (sink == NULL) {
1753 return false;
1754 }
1755
1756 if (mBuffer == NULL) {
1757 mBufferSize = sink->bufferSize();
1758 mBuffer = malloc(mBufferSize);
1759 }
1760
1761 size_t actualSize =
1762 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie);
1763
1764 if (actualSize > 0) {
1765 sink->write(mBuffer, actualSize);
1766 }
1767
1768 return true;
1769}
1770
1771////////////////////////////////////////////////////////////////////////////////
1772
Andreas Huber20111aa2009-07-14 16:56:47 -07001773status_t MediaPlayerService::AudioCache::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001774 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1775 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001776 AudioCallback cb, void *cookie, audio_output_flags_t flags)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001777{
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001778 ALOGV("open(%u, %d, 0x%x, %d, %d)", sampleRate, channelCount, channelMask, format, bufferCount);
Dave Sparks8eb80112009-12-09 20:20:26 -08001779 if (mHeap->getHeapID() < 0) {
1780 return NO_INIT;
1781 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001782
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001783 mSampleRate = sampleRate;
1784 mChannelCount = (uint16_t)channelCount;
Glenn Kastene1c39622012-01-04 09:36:37 -08001785 mFormat = format;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001786 mMsecsPerFrame = 1.e3 / (float) sampleRate;
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001787
1788 if (cb != NULL) {
1789 mCallbackThread = new CallbackThread(this, cb, cookie);
1790 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001791 return NO_ERROR;
1792}
1793
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001794void MediaPlayerService::AudioCache::start() {
1795 if (mCallbackThread != NULL) {
1796 mCallbackThread->run("AudioCache callback");
1797 }
1798}
1799
1800void MediaPlayerService::AudioCache::stop() {
1801 if (mCallbackThread != NULL) {
1802 mCallbackThread->requestExitAndWait();
1803 }
1804}
1805
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001806ssize_t MediaPlayerService::AudioCache::write(const void* buffer, size_t size)
1807{
Steve Block3856b092011-10-20 11:56:00 +01001808 ALOGV("write(%p, %u)", buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001809 if ((buffer == 0) || (size == 0)) return size;
1810
1811 uint8_t* p = static_cast<uint8_t*>(mHeap->getBase());
1812 if (p == NULL) return NO_INIT;
1813 p += mSize;
Steve Block3856b092011-10-20 11:56:00 +01001814 ALOGV("memcpy(%p, %p, %u)", p, buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001815 if (mSize + size > mHeap->getSize()) {
Steve Block29357bc2012-01-06 19:20:56 +00001816 ALOGE("Heap size overflow! req size: %d, max size: %d", (mSize + size), mHeap->getSize());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001817 size = mHeap->getSize() - mSize;
1818 }
1819 memcpy(p, buffer, size);
1820 mSize += size;
1821 return size;
1822}
1823
1824// call with lock held
1825status_t MediaPlayerService::AudioCache::wait()
1826{
1827 Mutex::Autolock lock(mLock);
Dave Sparks4bbc0ba2010-03-01 19:29:58 -08001828 while (!mCommandComplete) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001829 mSignal.wait(mLock);
1830 }
1831 mCommandComplete = false;
1832
1833 if (mError == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01001834 ALOGV("wait - success");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001835 } else {
Steve Block3856b092011-10-20 11:56:00 +01001836 ALOGV("wait - error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001837 }
1838 return mError;
1839}
1840
Gloria Wangb483c472011-04-11 17:23:27 -07001841void MediaPlayerService::AudioCache::notify(
1842 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001843{
Steve Block3856b092011-10-20 11:56:00 +01001844 ALOGV("notify(%p, %d, %d, %d)", cookie, msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001845 AudioCache* p = static_cast<AudioCache*>(cookie);
1846
1847 // ignore buffering messages
Dave Sparks8eb80112009-12-09 20:20:26 -08001848 switch (msg)
1849 {
1850 case MEDIA_ERROR:
Steve Block29357bc2012-01-06 19:20:56 +00001851 ALOGE("Error %d, %d occurred", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001852 p->mError = ext1;
Dave Sparks8eb80112009-12-09 20:20:26 -08001853 break;
1854 case MEDIA_PREPARED:
Steve Block3856b092011-10-20 11:56:00 +01001855 ALOGV("prepared");
Dave Sparks8eb80112009-12-09 20:20:26 -08001856 break;
1857 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +01001858 ALOGV("playback complete");
Dave Sparks8eb80112009-12-09 20:20:26 -08001859 break;
1860 default:
Steve Block3856b092011-10-20 11:56:00 +01001861 ALOGV("ignored");
Dave Sparks8eb80112009-12-09 20:20:26 -08001862 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001863 }
1864
1865 // wake up thread
Dave Sparksfe4c6f02010-03-02 12:56:37 -08001866 Mutex::Autolock lock(p->mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001867 p->mCommandComplete = true;
1868 p->mSignal.signal();
1869}
1870
Marco Nelissen4110c102012-03-29 09:31:28 -07001871int MediaPlayerService::AudioCache::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001872{
1873 return 0;
1874}
1875
Gloria Wang7cf180c2011-02-19 18:37:57 -08001876void MediaPlayerService::addBatteryData(uint32_t params)
1877{
1878 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08001879
1880 int32_t time = systemTime() / 1000000L;
1881
1882 // change audio output devices. This notification comes from AudioFlinger
1883 if ((params & kBatteryDataSpeakerOn)
1884 || (params & kBatteryDataOtherAudioDeviceOn)) {
1885
1886 int deviceOn[NUM_AUDIO_DEVICES];
1887 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1888 deviceOn[i] = 0;
1889 }
1890
1891 if ((params & kBatteryDataSpeakerOn)
1892 && (params & kBatteryDataOtherAudioDeviceOn)) {
1893 deviceOn[SPEAKER_AND_OTHER] = 1;
1894 } else if (params & kBatteryDataSpeakerOn) {
1895 deviceOn[SPEAKER] = 1;
1896 } else {
1897 deviceOn[OTHER_AUDIO_DEVICE] = 1;
1898 }
1899
1900 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1901 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
1902
1903 if (mBatteryAudio.refCount > 0) { // if playing audio
1904 if (!deviceOn[i]) {
1905 mBatteryAudio.lastTime[i] += time;
1906 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1907 mBatteryAudio.lastTime[i] = 0;
1908 } else {
1909 mBatteryAudio.lastTime[i] = 0 - time;
1910 }
1911 }
1912
1913 mBatteryAudio.deviceOn[i] = deviceOn[i];
1914 }
1915 }
1916 return;
1917 }
1918
1919 // an sudio stream is started
1920 if (params & kBatteryDataAudioFlingerStart) {
1921 // record the start time only if currently no other audio
1922 // is being played
1923 if (mBatteryAudio.refCount == 0) {
1924 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1925 if (mBatteryAudio.deviceOn[i]) {
1926 mBatteryAudio.lastTime[i] -= time;
1927 }
1928 }
1929 }
1930
1931 mBatteryAudio.refCount ++;
1932 return;
1933
1934 } else if (params & kBatteryDataAudioFlingerStop) {
1935 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001936 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001937 return;
1938 }
1939
1940 // record the stop time only if currently this is the only
1941 // audio being played
1942 if (mBatteryAudio.refCount == 1) {
1943 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1944 if (mBatteryAudio.deviceOn[i]) {
1945 mBatteryAudio.lastTime[i] += time;
1946 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1947 mBatteryAudio.lastTime[i] = 0;
1948 }
1949 }
1950 }
1951
1952 mBatteryAudio.refCount --;
1953 return;
1954 }
1955
Gloria Wang7cf180c2011-02-19 18:37:57 -08001956 int uid = IPCThreadState::self()->getCallingUid();
1957 if (uid == AID_MEDIA) {
1958 return;
1959 }
1960 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08001961
1962 if (index < 0) { // create a new entry for this UID
1963 BatteryUsageInfo info;
1964 info.audioTotalTime = 0;
1965 info.videoTotalTime = 0;
1966 info.audioLastTime = 0;
1967 info.videoLastTime = 0;
1968 info.refCount = 0;
1969
Gloria Wang9ee159b2011-02-24 14:51:45 -08001970 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00001971 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001972 return;
1973 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08001974 }
1975
1976 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
1977
1978 if (params & kBatteryDataCodecStarted) {
1979 if (params & kBatteryDataTrackAudio) {
1980 info.audioLastTime -= time;
1981 info.refCount ++;
1982 }
1983 if (params & kBatteryDataTrackVideo) {
1984 info.videoLastTime -= time;
1985 info.refCount ++;
1986 }
1987 } else {
1988 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001989 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08001990 return;
1991 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001992 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08001993 mBatteryData.removeItem(uid);
1994 return;
1995 }
1996
1997 if (params & kBatteryDataTrackAudio) {
1998 info.audioLastTime += time;
1999 info.refCount --;
2000 }
2001 if (params & kBatteryDataTrackVideo) {
2002 info.videoLastTime += time;
2003 info.refCount --;
2004 }
2005
2006 // no stream is being played by this UID
2007 if (info.refCount == 0) {
2008 info.audioTotalTime += info.audioLastTime;
2009 info.audioLastTime = 0;
2010 info.videoTotalTime += info.videoLastTime;
2011 info.videoLastTime = 0;
2012 }
2013 }
2014}
2015
2016status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
2017 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002018
2019 // audio output devices usage
2020 int32_t time = systemTime() / 1000000L; //in ms
2021 int32_t totalTime;
2022
2023 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2024 totalTime = mBatteryAudio.totalTime[i];
2025
2026 if (mBatteryAudio.deviceOn[i]
2027 && (mBatteryAudio.lastTime[i] != 0)) {
2028 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
2029 totalTime += tmpTime;
2030 }
2031
2032 reply->writeInt32(totalTime);
2033 // reset the total time
2034 mBatteryAudio.totalTime[i] = 0;
2035 }
2036
2037 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08002038 BatteryUsageInfo info;
2039 int size = mBatteryData.size();
2040
2041 reply->writeInt32(size);
2042 int i = 0;
2043
2044 while (i < size) {
2045 info = mBatteryData.valueAt(i);
2046
2047 reply->writeInt32(mBatteryData.keyAt(i)); //UID
2048 reply->writeInt32(info.audioTotalTime);
2049 reply->writeInt32(info.videoTotalTime);
2050
2051 info.audioTotalTime = 0;
2052 info.videoTotalTime = 0;
2053
2054 // remove the UID entry where no stream is being played
2055 if (info.refCount <= 0) {
2056 mBatteryData.removeItemsAt(i);
2057 size --;
2058 i --;
2059 }
2060 i++;
2061 }
2062 return NO_ERROR;
2063}
nikoa64c8c72009-07-20 15:07:26 -07002064} // namespace android