blob: 1be4edf2d3857ae66a9ca07e837a95bae16ea166 [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
Jeff Browne1045962012-09-04 21:38:42 -070047#include <media/IRemoteDisplay.h>
48#include <media/IRemoteDisplayClient.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080049#include <media/MediaPlayerInterface.h>
50#include <media/mediarecorder.h>
51#include <media/MediaMetadataRetrieverInterface.h>
nikoa64c8c72009-07-20 15:07:26 -070052#include <media/Metadata.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080053#include <media/AudioTrack.h>
James Dong8635b7b2011-03-14 17:01:38 -070054#include <media/MemoryLeakTrackUtil.h>
Eric Laurent9cb839a2011-09-27 09:48:56 -070055#include <media/stagefright/MediaErrors.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080056
Dima Zavin64760242011-05-11 14:15:23 -070057#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070058
Gloria Wang7cf180c2011-02-19 18:37:57 -080059#include <private/android_filesystem_config.h>
60
James Dong559bf282012-03-28 10:29:14 -070061#include "ActivityManager.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080062#include "MediaRecorderClient.h"
63#include "MediaPlayerService.h"
64#include "MetadataRetrieverClient.h"
John Grossman44a7e422012-06-21 17:29:24 -070065#include "MediaPlayerFactory.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080066
67#include "MidiFile.h"
Nicolas Catania14d27472009-07-13 14:37:49 -070068#include "TestPlayerStub.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070069#include "StagefrightPlayer.h"
Andreas Huberf9334412010-12-15 15:17:42 -080070#include "nuplayer/NuPlayerDriver.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070071
Andreas Huber20111aa2009-07-14 16:56:47 -070072#include <OMX.h>
Nicolas Catania14d27472009-07-13 14:37:49 -070073
Andreas Hubered3e3e02012-03-26 11:13:27 -070074#include "Crypto.h"
Andreas Huberd7bee3a2012-08-29 11:41:50 -070075#include "RemoteDisplay.h"
Andreas Hubered3e3e02012-03-26 11:13:27 -070076
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070077namespace {
nikoa64c8c72009-07-20 15:07:26 -070078using android::media::Metadata;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070079using android::status_t;
80using android::OK;
81using android::BAD_VALUE;
82using android::NOT_ENOUGH_DATA;
83using android::Parcel;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070084
85// Max number of entries in the filter.
86const int kMaxFilterSize = 64; // I pulled that out of thin air.
87
nikoa64c8c72009-07-20 15:07:26 -070088// FIXME: Move all the metadata related function in the Metadata.cpp
nikod608a812009-07-16 16:39:53 -070089
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070090
91// Unmarshall a filter from a Parcel.
92// Filter format in a parcel:
93//
94// 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
95// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
96// | number of entries (n) |
97// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
98// | metadata type 1 |
99// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
100// | metadata type 2 |
101// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
102// ....
103// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
104// | metadata type n |
105// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
106//
107// @param p Parcel that should start with a filter.
108// @param[out] filter On exit contains the list of metadata type to be
109// filtered.
110// @param[out] status On exit contains the status code to be returned.
111// @return true if the parcel starts with a valid filter.
112bool unmarshallFilter(const Parcel& p,
nikoa64c8c72009-07-20 15:07:26 -0700113 Metadata::Filter *filter,
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700114 status_t *status)
115{
Nicolas Catania48290382009-07-10 13:53:06 -0700116 int32_t val;
117 if (p.readInt32(&val) != OK)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700118 {
Steve Block29357bc2012-01-06 19:20:56 +0000119 ALOGE("Failed to read filter's length");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700120 *status = NOT_ENOUGH_DATA;
121 return false;
122 }
123
Nicolas Catania48290382009-07-10 13:53:06 -0700124 if( val > kMaxFilterSize || val < 0)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700125 {
Steve Block29357bc2012-01-06 19:20:56 +0000126 ALOGE("Invalid filter len %d", val);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700127 *status = BAD_VALUE;
128 return false;
129 }
130
Nicolas Catania48290382009-07-10 13:53:06 -0700131 const size_t num = val;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700132
133 filter->clear();
Nicolas Catania48290382009-07-10 13:53:06 -0700134 filter->setCapacity(num);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700135
nikoa64c8c72009-07-20 15:07:26 -0700136 size_t size = num * sizeof(Metadata::Type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700137
Nicolas Catania48290382009-07-10 13:53:06 -0700138
139 if (p.dataAvail() < size)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700140 {
Steve Block29357bc2012-01-06 19:20:56 +0000141 ALOGE("Filter too short expected %d but got %d", size, p.dataAvail());
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700142 *status = NOT_ENOUGH_DATA;
143 return false;
144 }
145
nikoa64c8c72009-07-20 15:07:26 -0700146 const Metadata::Type *data =
147 static_cast<const Metadata::Type*>(p.readInplace(size));
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700148
Nicolas Catania48290382009-07-10 13:53:06 -0700149 if (NULL == data)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700150 {
Steve Block29357bc2012-01-06 19:20:56 +0000151 ALOGE("Filter had no data");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700152 *status = BAD_VALUE;
153 return false;
154 }
155
156 // TODO: The stl impl of vector would be more efficient here
157 // because it degenerates into a memcpy on pod types. Try to
158 // replace later or use stl::set.
Nicolas Catania48290382009-07-10 13:53:06 -0700159 for (size_t i = 0; i < num; ++i)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700160 {
Nicolas Catania48290382009-07-10 13:53:06 -0700161 filter->add(*data);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700162 ++data;
163 }
164 *status = OK;
165 return true;
166}
167
Nicolas Catania48290382009-07-10 13:53:06 -0700168// @param filter Of metadata type.
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700169// @param val To be searched.
170// @return true if a match was found.
nikoa64c8c72009-07-20 15:07:26 -0700171bool findMetadata(const Metadata::Filter& filter, const int32_t val)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700172{
173 // Deal with empty and ANY right away
174 if (filter.isEmpty()) return false;
nikoa64c8c72009-07-20 15:07:26 -0700175 if (filter[0] == Metadata::kAny) return true;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700176
Nicolas Catania48290382009-07-10 13:53:06 -0700177 return filter.indexOf(val) >= 0;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700178}
179
180} // anonymous namespace
181
182
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800183namespace android {
184
Dave Burked681bbb2011-08-30 14:39:17 +0100185static bool checkPermission(const char* permissionString) {
186#ifndef HAVE_ANDROID_OS
187 return true;
188#endif
189 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
190 bool ok = checkCallingPermission(String16(permissionString));
Steve Block29357bc2012-01-06 19:20:56 +0000191 if (!ok) ALOGE("Request requires %s", permissionString);
Dave Burked681bbb2011-08-30 14:39:17 +0100192 return ok;
193}
194
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800195// 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 -0800196/* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4;
197/* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false;
198
199void MediaPlayerService::instantiate() {
200 defaultServiceManager()->addService(
201 String16("media.player"), new MediaPlayerService());
202}
203
204MediaPlayerService::MediaPlayerService()
205{
Steve Block3856b092011-10-20 11:56:00 +0100206 ALOGV("MediaPlayerService created");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800207 mNextConnId = 1;
Gloria Wang9ee159b2011-02-24 14:51:45 -0800208
209 mBatteryAudio.refCount = 0;
210 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
211 mBatteryAudio.deviceOn[i] = 0;
212 mBatteryAudio.lastTime[i] = 0;
213 mBatteryAudio.totalTime[i] = 0;
214 }
215 // speaker is on by default
216 mBatteryAudio.deviceOn[SPEAKER] = 1;
John Grossman44a7e422012-06-21 17:29:24 -0700217
218 MediaPlayerFactory::registerBuiltinFactories();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800219}
220
221MediaPlayerService::~MediaPlayerService()
222{
Steve Block3856b092011-10-20 11:56:00 +0100223 ALOGV("MediaPlayerService destroyed");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800224}
225
226sp<IMediaRecorder> MediaPlayerService::createMediaRecorder(pid_t pid)
227{
Gloria Wangdac6a312009-10-29 15:46:37 -0700228 sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid);
229 wp<MediaRecorderClient> w = recorder;
230 Mutex::Autolock lock(mLock);
231 mMediaRecorderClients.add(w);
Steve Block3856b092011-10-20 11:56:00 +0100232 ALOGV("Create new media recorder client from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800233 return recorder;
234}
235
Gloria Wangdac6a312009-10-29 15:46:37 -0700236void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client)
237{
238 Mutex::Autolock lock(mLock);
239 mMediaRecorderClients.remove(client);
Steve Block3856b092011-10-20 11:56:00 +0100240 ALOGV("Delete media recorder client");
Gloria Wangdac6a312009-10-29 15:46:37 -0700241}
242
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800243sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever(pid_t pid)
244{
245 sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
Steve Block3856b092011-10-20 11:56:00 +0100246 ALOGV("Create new media retriever from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800247 return retriever;
248}
249
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800250sp<IMediaPlayer> MediaPlayerService::create(pid_t pid, const sp<IMediaPlayerClient>& client,
Dave Burked681bbb2011-08-30 14:39:17 +0100251 int audioSessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800252{
253 int32_t connId = android_atomic_inc(&mNextConnId);
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700254
255 sp<Client> c = new Client(
256 this, pid, connId, client, audioSessionId,
257 IPCThreadState::self()->getCallingUid());
258
Steve Block3856b092011-10-20 11:56:00 +0100259 ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid,
Dave Burked681bbb2011-08-30 14:39:17 +0100260 IPCThreadState::self()->getCallingUid());
261
262 wp<Client> w = c;
263 {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800264 Mutex::Autolock lock(mLock);
265 mClients.add(w);
266 }
Andreas Hubere2b10282010-11-23 11:41:34 -0800267 return c;
268}
269
Andreas Huber318ad9c2009-10-15 13:46:54 -0700270sp<IOMX> MediaPlayerService::getOMX() {
271 Mutex::Autolock autoLock(mLock);
272
273 if (mOMX.get() == NULL) {
274 mOMX = new OMX;
275 }
276
277 return mOMX;
Andreas Huber20111aa2009-07-14 16:56:47 -0700278}
279
Andreas Hubered3e3e02012-03-26 11:13:27 -0700280sp<ICrypto> MediaPlayerService::makeCrypto() {
Andreas Huber1bd139a2012-04-03 14:19:20 -0700281 return new Crypto;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700282}
283
Jeff Browne1045962012-09-04 21:38:42 -0700284sp<IRemoteDisplay> MediaPlayerService::listenForRemoteDisplay(
285 const sp<IRemoteDisplayClient>& client, const String8& iface) {
Jeff Brownb0192502012-09-07 17:38:58 -0700286 if (!checkPermission("android.permission.CONTROL_WIFI_DISPLAY")) {
287 return NULL;
288 }
289
Jeff Brown455d02e2012-09-05 17:48:03 -0700290 return new RemoteDisplay(client, iface.string());
Jeff Browne1045962012-09-04 21:38:42 -0700291}
292
Jeff Brownb0192502012-09-07 17:38:58 -0700293// TODO: delete this method
Andreas Huberbcf09f82012-08-30 14:51:40 -0700294status_t MediaPlayerService::enableRemoteDisplay(const char *iface) {
Jeff Brownb0192502012-09-07 17:38:58 -0700295 if (!checkPermission("android.permission.CONTROL_WIFI_DISPLAY")) {
296 return PERMISSION_DENIED;
297 }
298
Andreas Huberd7bee3a2012-08-29 11:41:50 -0700299 Mutex::Autolock autoLock(mLock);
300
Andreas Huberbcf09f82012-08-30 14:51:40 -0700301 if (iface != NULL) {
302 if (mRemoteDisplay != NULL) {
303 return INVALID_OPERATION;
304 }
305
Andreas Huber0b73d472012-09-05 10:26:52 -0700306 mRemoteDisplay = new RemoteDisplay(NULL /* client */, iface);
Andreas Huberd7bee3a2012-08-29 11:41:50 -0700307 return OK;
Andreas Huberbcf09f82012-08-30 14:51:40 -0700308 }
309
310 if (mRemoteDisplay != NULL) {
Jeff Brown455d02e2012-09-05 17:48:03 -0700311 mRemoteDisplay->dispose();
Andreas Huberd7bee3a2012-08-29 11:41:50 -0700312 mRemoteDisplay.clear();
313 }
314
315 return OK;
316}
317
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800318status_t MediaPlayerService::AudioCache::dump(int fd, const Vector<String16>& args) const
319{
320 const size_t SIZE = 256;
321 char buffer[SIZE];
322 String8 result;
323
324 result.append(" AudioCache\n");
325 if (mHeap != 0) {
326 snprintf(buffer, 255, " heap base(%p), size(%d), flags(%d), device(%s)\n",
327 mHeap->getBase(), mHeap->getSize(), mHeap->getFlags(), mHeap->getDevice());
328 result.append(buffer);
329 }
330 snprintf(buffer, 255, " msec per frame(%f), channel count(%d), format(%d), frame count(%ld)\n",
331 mMsecsPerFrame, mChannelCount, mFormat, mFrameCount);
332 result.append(buffer);
333 snprintf(buffer, 255, " sample rate(%d), size(%d), error(%d), command complete(%s)\n",
334 mSampleRate, mSize, mError, mCommandComplete?"true":"false");
335 result.append(buffer);
336 ::write(fd, result.string(), result.size());
337 return NO_ERROR;
338}
339
340status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
341{
342 const size_t SIZE = 256;
343 char buffer[SIZE];
344 String8 result;
345
346 result.append(" AudioOutput\n");
347 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n",
348 mStreamType, mLeftVolume, mRightVolume);
349 result.append(buffer);
350 snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
Eric Laurentdb354e52012-03-05 17:27:11 -0800351 mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800352 result.append(buffer);
Eric Laurent2beeb502010-07-16 07:43:46 -0700353 snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
354 mAuxEffectId, mSendLevel);
355 result.append(buffer);
356
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800357 ::write(fd, result.string(), result.size());
358 if (mTrack != 0) {
359 mTrack->dump(fd, args);
360 }
361 return NO_ERROR;
362}
363
364status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args) const
365{
366 const size_t SIZE = 256;
367 char buffer[SIZE];
368 String8 result;
369 result.append(" Client\n");
370 snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n",
371 mPid, mConnId, mStatus, mLoop?"true": "false");
372 result.append(buffer);
373 write(fd, result.string(), result.size());
Andreas Hubera0b1d4b2011-06-07 15:52:25 -0700374 if (mPlayer != NULL) {
375 mPlayer->dump(fd, args);
376 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800377 if (mAudioOutput != 0) {
378 mAudioOutput->dump(fd, args);
379 }
380 write(fd, "\n", 1);
381 return NO_ERROR;
382}
383
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800384status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
385{
386 const size_t SIZE = 256;
387 char buffer[SIZE];
388 String8 result;
389 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
390 snprintf(buffer, SIZE, "Permission Denial: "
391 "can't dump MediaPlayerService from pid=%d, uid=%d\n",
392 IPCThreadState::self()->getCallingPid(),
393 IPCThreadState::self()->getCallingUid());
394 result.append(buffer);
395 } else {
396 Mutex::Autolock lock(mLock);
397 for (int i = 0, n = mClients.size(); i < n; ++i) {
398 sp<Client> c = mClients[i].promote();
399 if (c != 0) c->dump(fd, args);
400 }
James Dongb9141222010-07-08 11:16:11 -0700401 if (mMediaRecorderClients.size() == 0) {
402 result.append(" No media recorder client\n\n");
403 } else {
404 for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
405 sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
James Donge579e282011-10-18 22:29:20 -0700406 if (c != 0) {
407 snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid);
408 result.append(buffer);
409 write(fd, result.string(), result.size());
410 result = "\n";
411 c->dump(fd, args);
412 }
James Dongb9141222010-07-08 11:16:11 -0700413 }
Gloria Wangdac6a312009-10-29 15:46:37 -0700414 }
415
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800416 result.append(" Files opened and/or mapped:\n");
Glenn Kasten0512ab52011-05-04 17:58:57 -0700417 snprintf(buffer, SIZE, "/proc/%d/maps", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800418 FILE *f = fopen(buffer, "r");
419 if (f) {
420 while (!feof(f)) {
421 fgets(buffer, SIZE, f);
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700422 if (strstr(buffer, " /storage/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800423 strstr(buffer, " /system/sounds/") ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700424 strstr(buffer, " /data/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800425 strstr(buffer, " /system/media/")) {
426 result.append(" ");
427 result.append(buffer);
428 }
429 }
430 fclose(f);
431 } else {
432 result.append("couldn't open ");
433 result.append(buffer);
434 result.append("\n");
435 }
436
Glenn Kasten0512ab52011-05-04 17:58:57 -0700437 snprintf(buffer, SIZE, "/proc/%d/fd", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800438 DIR *d = opendir(buffer);
439 if (d) {
440 struct dirent *ent;
441 while((ent = readdir(d)) != NULL) {
442 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
Glenn Kasten0512ab52011-05-04 17:58:57 -0700443 snprintf(buffer, SIZE, "/proc/%d/fd/%s", gettid(), ent->d_name);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800444 struct stat s;
445 if (lstat(buffer, &s) == 0) {
446 if ((s.st_mode & S_IFMT) == S_IFLNK) {
447 char linkto[256];
448 int len = readlink(buffer, linkto, sizeof(linkto));
449 if(len > 0) {
450 if(len > 255) {
451 linkto[252] = '.';
452 linkto[253] = '.';
453 linkto[254] = '.';
454 linkto[255] = 0;
455 } else {
456 linkto[len] = 0;
457 }
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700458 if (strstr(linkto, "/storage/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800459 strstr(linkto, "/system/sounds/") == linkto ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700460 strstr(linkto, "/data/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800461 strstr(linkto, "/system/media/") == linkto) {
462 result.append(" ");
463 result.append(buffer);
464 result.append(" -> ");
465 result.append(linkto);
466 result.append("\n");
467 }
468 }
469 } else {
470 result.append(" unexpected type for ");
471 result.append(buffer);
472 result.append("\n");
473 }
474 }
475 }
476 }
477 closedir(d);
478 } else {
479 result.append("couldn't open ");
480 result.append(buffer);
481 result.append("\n");
482 }
483
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800484 bool dumpMem = false;
485 for (size_t i = 0; i < args.size(); i++) {
486 if (args[i] == String16("-m")) {
487 dumpMem = true;
488 }
489 }
490 if (dumpMem) {
James Dong8635b7b2011-03-14 17:01:38 -0700491 dumpMemoryAddresses(fd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800492 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800493 }
494 write(fd, result.string(), result.size());
495 return NO_ERROR;
496}
497
498void MediaPlayerService::removeClient(wp<Client> client)
499{
500 Mutex::Autolock lock(mLock);
501 mClients.remove(client);
502}
503
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700504MediaPlayerService::Client::Client(
505 const sp<MediaPlayerService>& service, pid_t pid,
506 int32_t connId, const sp<IMediaPlayerClient>& client,
507 int audioSessionId, uid_t uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800508{
Steve Block3856b092011-10-20 11:56:00 +0100509 ALOGV("Client(%d) constructor", connId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800510 mPid = pid;
511 mConnId = connId;
512 mService = service;
513 mClient = client;
514 mLoop = false;
515 mStatus = NO_INIT;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700516 mAudioSessionId = audioSessionId;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700517 mUID = uid;
John Grossmanc795b642012-02-22 15:38:35 -0800518 mRetransmitEndpointValid = false;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700519
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800520#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000521 ALOGD("create Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800522 mAntagonizer = new Antagonizer(notify, this);
523#endif
524}
525
526MediaPlayerService::Client::~Client()
527{
Steve Block3856b092011-10-20 11:56:00 +0100528 ALOGV("Client(%d) destructor pid = %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800529 mAudioOutput.clear();
530 wp<Client> client(this);
531 disconnect();
532 mService->removeClient(client);
533}
534
535void MediaPlayerService::Client::disconnect()
536{
Steve Block3856b092011-10-20 11:56:00 +0100537 ALOGV("disconnect(%d) from pid %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800538 // grab local reference and clear main reference to prevent future
539 // access to object
540 sp<MediaPlayerBase> p;
541 {
542 Mutex::Autolock l(mLock);
543 p = mPlayer;
544 }
Dave Sparks795fa582009-03-24 17:57:12 -0700545 mClient.clear();
Andreas Huber20111aa2009-07-14 16:56:47 -0700546
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800547 mPlayer.clear();
548
549 // clear the notification to prevent callbacks to dead client
550 // and reset the player. We assume the player will serialize
551 // access to itself if necessary.
552 if (p != 0) {
553 p->setNotifyCallback(0, 0);
554#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000555 ALOGD("kill Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800556 mAntagonizer->kill();
557#endif
558 p->reset();
559 }
560
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700561 disconnectNativeWindow();
562
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800563 IPCThreadState::self()->flushCommands();
564}
565
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800566sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
567{
568 // determine if we have the right player type
569 sp<MediaPlayerBase> p = mPlayer;
570 if ((p != NULL) && (p->playerType() != playerType)) {
Steve Block3856b092011-10-20 11:56:00 +0100571 ALOGV("delete player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800572 p.clear();
573 }
574 if (p == NULL) {
John Grossman44a7e422012-06-21 17:29:24 -0700575 p = MediaPlayerFactory::createPlayer(playerType, this, notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800576 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700577
Jason Simmonsdb29e522011-08-12 13:46:55 -0700578 if (p != NULL) {
579 p->setUID(mUID);
580 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700581
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800582 return p;
583}
584
John Grossmanc795b642012-02-22 15:38:35 -0800585sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre(
586 player_type playerType)
587{
588 ALOGV("player type = %d", playerType);
589
590 // create the right type of player
591 sp<MediaPlayerBase> p = createPlayer(playerType);
592 if (p == NULL) {
593 return p;
594 }
595
596 if (!p->hardwareOutput()) {
597 mAudioOutput = new AudioOutput(mAudioSessionId);
598 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
599 }
600
601 return p;
602}
603
604void MediaPlayerService::Client::setDataSource_post(
605 const sp<MediaPlayerBase>& p,
606 status_t status)
607{
608 ALOGV(" setDataSource");
609 mStatus = status;
610 if (mStatus != OK) {
611 ALOGE(" error: %d", mStatus);
612 return;
613 }
614
615 // Set the re-transmission endpoint if one was chosen.
616 if (mRetransmitEndpointValid) {
617 mStatus = p->setRetransmitEndpoint(&mRetransmitEndpoint);
618 if (mStatus != NO_ERROR) {
619 ALOGE("setRetransmitEndpoint error: %d", mStatus);
620 }
621 }
622
623 if (mStatus == OK) {
624 mPlayer = p;
625 }
626}
627
Andreas Huber2db84552010-01-28 11:19:57 -0800628status_t MediaPlayerService::Client::setDataSource(
629 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800630{
Steve Block3856b092011-10-20 11:56:00 +0100631 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800632 if (url == NULL)
633 return UNKNOWN_ERROR;
634
Dave Burked681bbb2011-08-30 14:39:17 +0100635 if ((strncmp(url, "http://", 7) == 0) ||
636 (strncmp(url, "https://", 8) == 0) ||
637 (strncmp(url, "rtsp://", 7) == 0)) {
638 if (!checkPermission("android.permission.INTERNET")) {
639 return PERMISSION_DENIED;
640 }
641 }
642
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800643 if (strncmp(url, "content://", 10) == 0) {
644 // get a filedescriptor for the content Uri and
645 // pass it to the setDataSource(fd) method
646
647 String16 url16(url);
648 int fd = android::openContentProviderFile(url16);
649 if (fd < 0)
650 {
Steve Block29357bc2012-01-06 19:20:56 +0000651 ALOGE("Couldn't open fd for %s", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800652 return UNKNOWN_ERROR;
653 }
654 setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
655 close(fd);
656 return mStatus;
657 } else {
John Grossman44a7e422012-06-21 17:29:24 -0700658 player_type playerType = MediaPlayerFactory::getPlayerType(this, url);
John Grossmanc795b642012-02-22 15:38:35 -0800659 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
660 if (p == NULL) {
661 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800662 }
663
John Grossmanc795b642012-02-22 15:38:35 -0800664 setDataSource_post(p, p->setDataSource(url, headers));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800665 return mStatus;
666 }
667}
668
669status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
670{
Steve Block3856b092011-10-20 11:56:00 +0100671 ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800672 struct stat sb;
673 int ret = fstat(fd, &sb);
674 if (ret != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000675 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800676 return UNKNOWN_ERROR;
677 }
678
Steve Block3856b092011-10-20 11:56:00 +0100679 ALOGV("st_dev = %llu", sb.st_dev);
680 ALOGV("st_mode = %u", sb.st_mode);
681 ALOGV("st_uid = %lu", sb.st_uid);
682 ALOGV("st_gid = %lu", sb.st_gid);
683 ALOGV("st_size = %llu", sb.st_size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800684
685 if (offset >= sb.st_size) {
Steve Block29357bc2012-01-06 19:20:56 +0000686 ALOGE("offset error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800687 ::close(fd);
688 return UNKNOWN_ERROR;
689 }
690 if (offset + length > sb.st_size) {
691 length = sb.st_size - offset;
Steve Block3856b092011-10-20 11:56:00 +0100692 ALOGV("calculated length = %lld", length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800693 }
694
John Grossman44a7e422012-06-21 17:29:24 -0700695 player_type playerType = MediaPlayerFactory::getPlayerType(this,
696 fd,
697 offset,
698 length);
John Grossmanc795b642012-02-22 15:38:35 -0800699 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
700 if (p == NULL) {
701 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800702 }
703
704 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800705 setDataSource_post(p, p->setDataSource(fd, offset, length));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800706 return mStatus;
707}
708
Andreas Hubere2b10282010-11-23 11:41:34 -0800709status_t MediaPlayerService::Client::setDataSource(
710 const sp<IStreamSource> &source) {
711 // create the right type of player
John Grossman44a7e422012-06-21 17:29:24 -0700712 player_type playerType = MediaPlayerFactory::getPlayerType(this, source);
John Grossmanc795b642012-02-22 15:38:35 -0800713 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
Andreas Hubere2b10282010-11-23 11:41:34 -0800714 if (p == NULL) {
715 return NO_INIT;
716 }
717
Andreas Hubere2b10282010-11-23 11:41:34 -0800718 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800719 setDataSource_post(p, p->setDataSource(source));
Andreas Hubere2b10282010-11-23 11:41:34 -0800720 return mStatus;
721}
722
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700723void MediaPlayerService::Client::disconnectNativeWindow() {
724 if (mConnectedWindow != NULL) {
725 status_t err = native_window_api_disconnect(mConnectedWindow.get(),
726 NATIVE_WINDOW_API_MEDIA);
727
728 if (err != OK) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000729 ALOGW("native_window_api_disconnect returned an error: %s (%d)",
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700730 strerror(-err), err);
731 }
732 }
733 mConnectedWindow.clear();
734}
735
Glenn Kasten11731182011-02-08 17:26:17 -0800736status_t MediaPlayerService::Client::setVideoSurfaceTexture(
737 const sp<ISurfaceTexture>& surfaceTexture)
738{
Steve Block3856b092011-10-20 11:56:00 +0100739 ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, surfaceTexture.get());
Glenn Kasten11731182011-02-08 17:26:17 -0800740 sp<MediaPlayerBase> p = getPlayer();
741 if (p == 0) return UNKNOWN_ERROR;
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700742
743 sp<IBinder> binder(surfaceTexture == NULL ? NULL :
744 surfaceTexture->asBinder());
745 if (mConnectedWindowBinder == binder) {
746 return OK;
747 }
748
749 sp<ANativeWindow> anw;
750 if (surfaceTexture != NULL) {
751 anw = new SurfaceTextureClient(surfaceTexture);
752 status_t err = native_window_api_connect(anw.get(),
753 NATIVE_WINDOW_API_MEDIA);
754
755 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000756 ALOGE("setVideoSurfaceTexture failed: %d", err);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700757 // Note that we must do the reset before disconnecting from the ANW.
758 // Otherwise queue/dequeue calls could be made on the disconnected
759 // ANW, which may result in errors.
760 reset();
761
762 disconnectNativeWindow();
763
764 return err;
765 }
766 }
767
768 // Note that we must set the player's new SurfaceTexture before
769 // disconnecting the old one. Otherwise queue/dequeue calls could be made
770 // on the disconnected ANW, which may result in errors.
771 status_t err = p->setVideoSurfaceTexture(surfaceTexture);
772
773 disconnectNativeWindow();
774
775 mConnectedWindow = anw;
776
777 if (err == OK) {
778 mConnectedWindowBinder = binder;
779 } else {
780 disconnectNativeWindow();
781 }
782
783 return err;
Glenn Kasten11731182011-02-08 17:26:17 -0800784}
785
Nicolas Catania1d187f12009-05-12 23:25:55 -0700786status_t MediaPlayerService::Client::invoke(const Parcel& request,
787 Parcel *reply)
788{
789 sp<MediaPlayerBase> p = getPlayer();
790 if (p == NULL) return UNKNOWN_ERROR;
791 return p->invoke(request, reply);
792}
793
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700794// This call doesn't need to access the native player.
795status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
796{
797 status_t status;
nikoa64c8c72009-07-20 15:07:26 -0700798 media::Metadata::Filter allow, drop;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700799
Nicolas Catania48290382009-07-10 13:53:06 -0700800 if (unmarshallFilter(filter, &allow, &status) &&
801 unmarshallFilter(filter, &drop, &status)) {
802 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700803
804 mMetadataAllow = allow;
805 mMetadataDrop = drop;
806 }
807 return status;
808}
809
Nicolas Catania48290382009-07-10 13:53:06 -0700810status_t MediaPlayerService::Client::getMetadata(
811 bool update_only, bool apply_filter, Parcel *reply)
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700812{
nikoa64c8c72009-07-20 15:07:26 -0700813 sp<MediaPlayerBase> player = getPlayer();
814 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania48290382009-07-10 13:53:06 -0700815
nikod608a812009-07-16 16:39:53 -0700816 status_t status;
817 // Placeholder for the return code, updated by the caller.
818 reply->writeInt32(-1);
819
nikoa64c8c72009-07-20 15:07:26 -0700820 media::Metadata::Filter ids;
Nicolas Catania48290382009-07-10 13:53:06 -0700821
822 // We don't block notifications while we fetch the data. We clear
823 // mMetadataUpdated first so we don't lose notifications happening
824 // during the rest of this call.
825 {
826 Mutex::Autolock lock(mLock);
827 if (update_only) {
nikod608a812009-07-16 16:39:53 -0700828 ids = mMetadataUpdated;
Nicolas Catania48290382009-07-10 13:53:06 -0700829 }
830 mMetadataUpdated.clear();
831 }
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700832
nikoa64c8c72009-07-20 15:07:26 -0700833 media::Metadata metadata(reply);
Nicolas Catania48290382009-07-10 13:53:06 -0700834
nikoa64c8c72009-07-20 15:07:26 -0700835 metadata.appendHeader();
836 status = player->getMetadata(ids, reply);
nikod608a812009-07-16 16:39:53 -0700837
838 if (status != OK) {
nikoa64c8c72009-07-20 15:07:26 -0700839 metadata.resetParcel();
Steve Block29357bc2012-01-06 19:20:56 +0000840 ALOGE("getMetadata failed %d", status);
nikod608a812009-07-16 16:39:53 -0700841 return status;
842 }
843
844 // FIXME: Implement filtering on the result. Not critical since
845 // filtering takes place on the update notifications already. This
846 // would be when all the metadata are fetch and a filter is set.
847
nikod608a812009-07-16 16:39:53 -0700848 // Everything is fine, update the metadata length.
nikoa64c8c72009-07-20 15:07:26 -0700849 metadata.updateLength();
nikod608a812009-07-16 16:39:53 -0700850 return OK;
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700851}
852
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800853status_t MediaPlayerService::Client::prepareAsync()
854{
Steve Block3856b092011-10-20 11:56:00 +0100855 ALOGV("[%d] prepareAsync", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800856 sp<MediaPlayerBase> p = getPlayer();
857 if (p == 0) return UNKNOWN_ERROR;
858 status_t ret = p->prepareAsync();
859#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000860 ALOGD("start Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800861 if (ret == NO_ERROR) mAntagonizer->start();
862#endif
863 return ret;
864}
865
866status_t MediaPlayerService::Client::start()
867{
Steve Block3856b092011-10-20 11:56:00 +0100868 ALOGV("[%d] start", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800869 sp<MediaPlayerBase> p = getPlayer();
870 if (p == 0) return UNKNOWN_ERROR;
871 p->setLooping(mLoop);
872 return p->start();
873}
874
875status_t MediaPlayerService::Client::stop()
876{
Steve Block3856b092011-10-20 11:56:00 +0100877 ALOGV("[%d] stop", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800878 sp<MediaPlayerBase> p = getPlayer();
879 if (p == 0) return UNKNOWN_ERROR;
880 return p->stop();
881}
882
883status_t MediaPlayerService::Client::pause()
884{
Steve Block3856b092011-10-20 11:56:00 +0100885 ALOGV("[%d] pause", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800886 sp<MediaPlayerBase> p = getPlayer();
887 if (p == 0) return UNKNOWN_ERROR;
888 return p->pause();
889}
890
891status_t MediaPlayerService::Client::isPlaying(bool* state)
892{
893 *state = false;
894 sp<MediaPlayerBase> p = getPlayer();
895 if (p == 0) return UNKNOWN_ERROR;
896 *state = p->isPlaying();
Steve Block3856b092011-10-20 11:56:00 +0100897 ALOGV("[%d] isPlaying: %d", mConnId, *state);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800898 return NO_ERROR;
899}
900
901status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
902{
Steve Block3856b092011-10-20 11:56:00 +0100903 ALOGV("getCurrentPosition");
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->getCurrentPosition(msec);
907 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100908 ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800909 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000910 ALOGE("getCurrentPosition returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800911 }
912 return ret;
913}
914
915status_t MediaPlayerService::Client::getDuration(int *msec)
916{
Steve Block3856b092011-10-20 11:56:00 +0100917 ALOGV("getDuration");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800918 sp<MediaPlayerBase> p = getPlayer();
919 if (p == 0) return UNKNOWN_ERROR;
920 status_t ret = p->getDuration(msec);
921 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100922 ALOGV("[%d] getDuration = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800923 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000924 ALOGE("getDuration returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800925 }
926 return ret;
927}
928
Marco Nelissen6b74d672012-02-28 16:07:44 -0800929status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) {
930 ALOGV("setNextPlayer");
931 Mutex::Autolock l(mLock);
932 sp<Client> c = static_cast<Client*>(player.get());
933 mNextClient = c;
John Grossman2e5f22e2012-08-24 14:47:25 -0700934
935 if (c != NULL) {
936 if (mAudioOutput != NULL) {
937 mAudioOutput->setNextOutput(c->mAudioOutput);
938 } else if ((mPlayer != NULL) && !mPlayer->hardwareOutput()) {
939 ALOGE("no current audio output");
940 }
941
942 if ((mPlayer != NULL) && (mNextClient->getPlayer() != NULL)) {
943 mPlayer->setNextPlayer(mNextClient->getPlayer());
944 }
Marco Nelissen6b74d672012-02-28 16:07:44 -0800945 }
John Grossman2e5f22e2012-08-24 14:47:25 -0700946
Marco Nelissen6b74d672012-02-28 16:07:44 -0800947 return OK;
948}
949
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800950status_t MediaPlayerService::Client::seekTo(int msec)
951{
Steve Block3856b092011-10-20 11:56:00 +0100952 ALOGV("[%d] seekTo(%d)", mConnId, msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800953 sp<MediaPlayerBase> p = getPlayer();
954 if (p == 0) return UNKNOWN_ERROR;
955 return p->seekTo(msec);
956}
957
958status_t MediaPlayerService::Client::reset()
959{
Steve Block3856b092011-10-20 11:56:00 +0100960 ALOGV("[%d] reset", mConnId);
John Grossmanc795b642012-02-22 15:38:35 -0800961 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800962 sp<MediaPlayerBase> p = getPlayer();
963 if (p == 0) return UNKNOWN_ERROR;
964 return p->reset();
965}
966
Glenn Kastenfff6d712012-01-12 16:38:12 -0800967status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800968{
Steve Block3856b092011-10-20 11:56:00 +0100969 ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800970 // TODO: for hardware output, call player instead
971 Mutex::Autolock l(mLock);
972 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
973 return NO_ERROR;
974}
975
976status_t MediaPlayerService::Client::setLooping(int loop)
977{
Steve Block3856b092011-10-20 11:56:00 +0100978 ALOGV("[%d] setLooping(%d)", mConnId, loop);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800979 mLoop = loop;
980 sp<MediaPlayerBase> p = getPlayer();
981 if (p != 0) return p->setLooping(loop);
982 return NO_ERROR;
983}
984
985status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
986{
Steve Block3856b092011-10-20 11:56:00 +0100987 ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
John Grossman761defc2012-02-09 15:09:05 -0800988
989 // for hardware output, call player instead
990 sp<MediaPlayerBase> p = getPlayer();
991 {
992 Mutex::Autolock l(mLock);
993 if (p != 0 && p->hardwareOutput()) {
994 MediaPlayerHWInterface* hwp =
995 reinterpret_cast<MediaPlayerHWInterface*>(p.get());
996 return hwp->setVolume(leftVolume, rightVolume);
997 } else {
998 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
999 return NO_ERROR;
1000 }
1001 }
1002
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001003 return NO_ERROR;
1004}
1005
Eric Laurent2beeb502010-07-16 07:43:46 -07001006status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
1007{
Steve Block3856b092011-10-20 11:56:00 +01001008 ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001009 Mutex::Autolock l(mLock);
1010 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
1011 return NO_ERROR;
1012}
1013
1014status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
1015{
Steve Block3856b092011-10-20 11:56:00 +01001016 ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001017 Mutex::Autolock l(mLock);
1018 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1019 return NO_ERROR;
1020}
Nicolas Catania48290382009-07-10 13:53:06 -07001021
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001022status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
Steve Block3856b092011-10-20 11:56:00 +01001023 ALOGV("[%d] setParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001024 sp<MediaPlayerBase> p = getPlayer();
1025 if (p == 0) return UNKNOWN_ERROR;
1026 return p->setParameter(key, request);
1027}
1028
1029status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
Steve Block3856b092011-10-20 11:56:00 +01001030 ALOGV("[%d] getParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001031 sp<MediaPlayerBase> p = getPlayer();
1032 if (p == 0) return UNKNOWN_ERROR;
1033 return p->getParameter(key, reply);
1034}
1035
John Grossmanc795b642012-02-22 15:38:35 -08001036status_t MediaPlayerService::Client::setRetransmitEndpoint(
1037 const struct sockaddr_in* endpoint) {
1038
1039 if (NULL != endpoint) {
1040 uint32_t a = ntohl(endpoint->sin_addr.s_addr);
1041 uint16_t p = ntohs(endpoint->sin_port);
1042 ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId,
1043 (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p);
1044 } else {
1045 ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId);
1046 }
1047
1048 sp<MediaPlayerBase> p = getPlayer();
1049
1050 // Right now, the only valid time to set a retransmit endpoint is before
1051 // player selection has been made (since the presence or absence of a
1052 // retransmit endpoint is going to determine which player is selected during
1053 // setDataSource).
1054 if (p != 0) return INVALID_OPERATION;
1055
1056 if (NULL != endpoint) {
1057 mRetransmitEndpoint = *endpoint;
1058 mRetransmitEndpointValid = true;
1059 } else {
1060 mRetransmitEndpointValid = false;
1061 }
1062
1063 return NO_ERROR;
1064}
1065
John Grossman44a7e422012-06-21 17:29:24 -07001066status_t MediaPlayerService::Client::getRetransmitEndpoint(
1067 struct sockaddr_in* endpoint)
1068{
1069 if (NULL == endpoint)
1070 return BAD_VALUE;
1071
1072 sp<MediaPlayerBase> p = getPlayer();
1073
1074 if (p != NULL)
1075 return p->getRetransmitEndpoint(endpoint);
1076
1077 if (!mRetransmitEndpointValid)
1078 return NO_INIT;
1079
1080 *endpoint = mRetransmitEndpoint;
1081
1082 return NO_ERROR;
1083}
1084
Gloria Wangb483c472011-04-11 17:23:27 -07001085void MediaPlayerService::Client::notify(
1086 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001087{
1088 Client* client = static_cast<Client*>(cookie);
James Dongb8a98252012-08-26 16:13:03 -07001089 if (client == NULL) {
1090 return;
1091 }
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001092
James Dongb8a98252012-08-26 16:13:03 -07001093 sp<IMediaPlayerClient> c;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001094 {
1095 Mutex::Autolock l(client->mLock);
James Dongb8a98252012-08-26 16:13:03 -07001096 c = client->mClient;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001097 if (msg == MEDIA_PLAYBACK_COMPLETE && client->mNextClient != NULL) {
John Grossmancb0b7552012-08-23 17:47:31 -07001098 if (client->mAudioOutput != NULL)
1099 client->mAudioOutput->switchToNextOutput();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001100 client->mNextClient->start();
1101 client->mNextClient->mClient->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj);
1102 }
1103 }
1104
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001105 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001106 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001107 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-07-10 13:53:06 -07001108
1109 if(client->shouldDropMetadata(metadata_type)) {
1110 return;
1111 }
1112
1113 // Update the list of metadata that have changed. getMetadata
1114 // also access mMetadataUpdated and clears it.
1115 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001116 }
James Dongb8a98252012-08-26 16:13:03 -07001117
1118 if (c != NULL) {
1119 ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
1120 c->notify(msg, ext1, ext2, obj);
1121 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001122}
1123
Nicolas Catania48290382009-07-10 13:53:06 -07001124
nikoa64c8c72009-07-20 15:07:26 -07001125bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001126{
Nicolas Catania48290382009-07-10 13:53:06 -07001127 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001128
Nicolas Catania48290382009-07-10 13:53:06 -07001129 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001130 return true;
1131 }
1132
Nicolas Catania48290382009-07-10 13:53:06 -07001133 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001134 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001135 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001136 return true;
1137 }
1138}
1139
Nicolas Catania48290382009-07-10 13:53:06 -07001140
nikoa64c8c72009-07-20 15:07:26 -07001141void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-07-10 13:53:06 -07001142 Mutex::Autolock lock(mLock);
1143 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1144 mMetadataUpdated.add(metadata_type);
1145 }
1146}
1147
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001148#if CALLBACK_ANTAGONIZER
1149const int Antagonizer::interval = 10000; // 10 msecs
1150
1151Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1152 mExit(false), mActive(false), mClient(client), mCb(cb)
1153{
1154 createThread(callbackThread, this);
1155}
1156
1157void Antagonizer::kill()
1158{
1159 Mutex::Autolock _l(mLock);
1160 mActive = false;
1161 mExit = true;
1162 mCondition.wait(mLock);
1163}
1164
1165int Antagonizer::callbackThread(void* user)
1166{
Steve Blockb8a80522011-12-20 16:23:08 +00001167 ALOGD("Antagonizer started");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001168 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1169 while (!p->mExit) {
1170 if (p->mActive) {
Steve Block3856b092011-10-20 11:56:00 +01001171 ALOGV("send event");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001172 p->mCb(p->mClient, 0, 0, 0);
1173 }
1174 usleep(interval);
1175 }
1176 Mutex::Autolock _l(p->mLock);
1177 p->mCondition.signal();
Steve Blockb8a80522011-12-20 16:23:08 +00001178 ALOGD("Antagonizer stopped");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001179 return 0;
1180}
1181#endif
1182
1183static size_t kDefaultHeapSize = 1024 * 1024; // 1MB
1184
Glenn Kastene1c39622012-01-04 09:36:37 -08001185sp<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 -08001186{
Steve Block3856b092011-10-20 11:56:00 +01001187 ALOGV("decode(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001188 sp<MemoryBase> mem;
1189 sp<MediaPlayerBase> player;
1190
1191 // Protect our precious, precious DRMd ringtones by only allowing
1192 // decoding of http, but not filesystem paths or content Uris.
1193 // If the application wants to decode those, it should open a
1194 // filedescriptor for them and use that.
1195 if (url != NULL && strncmp(url, "http://", 7) != 0) {
Steve Blockb8a80522011-12-20 16:23:08 +00001196 ALOGD("Can't decode %s by path, use filedescriptor instead", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001197 return mem;
1198 }
1199
John Grossman44a7e422012-06-21 17:29:24 -07001200 player_type playerType =
1201 MediaPlayerFactory::getPlayerType(NULL /* client */, url);
Steve Block3856b092011-10-20 11:56:00 +01001202 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001203
1204 // create the right type of player
1205 sp<AudioCache> cache = new AudioCache(url);
John Grossman44a7e422012-06-21 17:29:24 -07001206 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001207 if (player == NULL) goto Exit;
1208 if (player->hardwareOutput()) goto Exit;
1209
1210 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1211
1212 // set data source
1213 if (player->setDataSource(url) != NO_ERROR) goto Exit;
1214
Steve Block3856b092011-10-20 11:56:00 +01001215 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001216 player->prepareAsync();
1217
Steve Block3856b092011-10-20 11:56:00 +01001218 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001219 if (cache->wait() != NO_ERROR) goto Exit;
1220
Steve Block3856b092011-10-20 11:56:00 +01001221 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001222 player->start();
1223
Steve Block3856b092011-10-20 11:56:00 +01001224 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001225 cache->wait();
1226 // in case of error, return what was successfully decoded.
1227 if (cache->size() == 0) {
1228 goto Exit;
1229 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001230
1231 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1232 *pSampleRate = cache->sampleRate();
1233 *pNumChannels = cache->channelCount();
Glenn Kastene1c39622012-01-04 09:36:37 -08001234 *pFormat = cache->format();
Steve Block3856b092011-10-20 11:56:00 +01001235 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 -08001236
1237Exit:
1238 if (player != 0) player->reset();
1239 return mem;
1240}
1241
Glenn Kastene1c39622012-01-04 09:36:37 -08001242sp<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 -08001243{
Steve Block3856b092011-10-20 11:56:00 +01001244 ALOGV("decode(%d, %lld, %lld)", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001245 sp<MemoryBase> mem;
1246 sp<MediaPlayerBase> player;
1247
John Grossman44a7e422012-06-21 17:29:24 -07001248 player_type playerType = MediaPlayerFactory::getPlayerType(NULL /* client */,
1249 fd,
1250 offset,
1251 length);
Steve Block3856b092011-10-20 11:56:00 +01001252 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001253
1254 // create the right type of player
1255 sp<AudioCache> cache = new AudioCache("decode_fd");
John Grossman44a7e422012-06-21 17:29:24 -07001256 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001257 if (player == NULL) goto Exit;
1258 if (player->hardwareOutput()) goto Exit;
1259
1260 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1261
1262 // set data source
1263 if (player->setDataSource(fd, offset, length) != NO_ERROR) goto Exit;
1264
Steve Block3856b092011-10-20 11:56:00 +01001265 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001266 player->prepareAsync();
1267
Steve Block3856b092011-10-20 11:56:00 +01001268 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001269 if (cache->wait() != NO_ERROR) goto Exit;
1270
Steve Block3856b092011-10-20 11:56:00 +01001271 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001272 player->start();
1273
Steve Block3856b092011-10-20 11:56:00 +01001274 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001275 cache->wait();
1276 // in case of error, return what was successfully decoded.
1277 if (cache->size() == 0) {
1278 goto Exit;
1279 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001280
1281 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1282 *pSampleRate = cache->sampleRate();
1283 *pNumChannels = cache->channelCount();
1284 *pFormat = cache->format();
Steve Block3856b092011-10-20 11:56:00 +01001285 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 -08001286
1287Exit:
1288 if (player != 0) player->reset();
1289 ::close(fd);
1290 return mem;
1291}
1292
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001293
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001294#undef LOG_TAG
1295#define LOG_TAG "AudioSink"
Eric Laurenta514bdb2010-06-21 09:27:30 -07001296MediaPlayerService::AudioOutput::AudioOutput(int sessionId)
Andreas Huber20111aa2009-07-14 16:56:47 -07001297 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001298 mCallbackCookie(NULL),
Marco Nelissen6b74d672012-02-28 16:07:44 -08001299 mCallbackData(NULL),
Marco Nelissen4110c102012-03-29 09:31:28 -07001300 mBytesWritten(0),
Eric Laurent1948eb32012-04-13 16:50:19 -07001301 mSessionId(sessionId),
1302 mFlags(AUDIO_OUTPUT_FLAG_NONE) {
Steve Block3856b092011-10-20 11:56:00 +01001303 ALOGV("AudioOutput(%d)", sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001304 mTrack = 0;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001305 mRecycledTrack = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -07001306 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001307 mLeftVolume = 1.0;
1308 mRightVolume = 1.0;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001309 mPlaybackRatePermille = 1000;
1310 mSampleRateHz = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001311 mMsecsPerFrame = 0;
Eric Laurent2beeb502010-07-16 07:43:46 -07001312 mAuxEffectId = 0;
1313 mSendLevel = 0.0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001314 setMinBufferCount();
1315}
1316
1317MediaPlayerService::AudioOutput::~AudioOutput()
1318{
1319 close();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001320 delete mRecycledTrack;
1321 delete mCallbackData;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001322}
1323
1324void MediaPlayerService::AudioOutput::setMinBufferCount()
1325{
1326 char value[PROPERTY_VALUE_MAX];
1327 if (property_get("ro.kernel.qemu", value, 0)) {
1328 mIsOnEmulator = true;
1329 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1330 }
1331}
1332
1333bool MediaPlayerService::AudioOutput::isOnEmulator()
1334{
1335 setMinBufferCount();
1336 return mIsOnEmulator;
1337}
1338
1339int MediaPlayerService::AudioOutput::getMinBufferCount()
1340{
1341 setMinBufferCount();
1342 return mMinBufferCount;
1343}
1344
1345ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1346{
1347 if (mTrack == 0) return NO_INIT;
1348 return mTrack->frameCount() * frameSize();
1349}
1350
1351ssize_t MediaPlayerService::AudioOutput::frameCount() const
1352{
1353 if (mTrack == 0) return NO_INIT;
1354 return mTrack->frameCount();
1355}
1356
1357ssize_t MediaPlayerService::AudioOutput::channelCount() const
1358{
1359 if (mTrack == 0) return NO_INIT;
1360 return mTrack->channelCount();
1361}
1362
1363ssize_t MediaPlayerService::AudioOutput::frameSize() const
1364{
1365 if (mTrack == 0) return NO_INIT;
1366 return mTrack->frameSize();
1367}
1368
1369uint32_t MediaPlayerService::AudioOutput::latency () const
1370{
Eric Laurentdb354e52012-03-05 17:27:11 -08001371 if (mTrack == 0) return 0;
1372 return mTrack->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001373}
1374
1375float MediaPlayerService::AudioOutput::msecsPerFrame() const
1376{
1377 return mMsecsPerFrame;
1378}
1379
Marco Nelissen4110c102012-03-29 09:31:28 -07001380status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001381{
1382 if (mTrack == 0) return NO_INIT;
1383 return mTrack->getPosition(position);
1384}
1385
Marco Nelissen4110c102012-03-29 09:31:28 -07001386status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
1387{
1388 if (mTrack == 0) return NO_INIT;
1389 *frameswritten = mBytesWritten / frameSize();
1390 return OK;
1391}
1392
Andreas Huber20111aa2009-07-14 16:56:47 -07001393status_t MediaPlayerService::AudioOutput::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001394 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1395 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001396 AudioCallback cb, void *cookie,
1397 audio_output_flags_t flags)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001398{
Andreas Huber20111aa2009-07-14 16:56:47 -07001399 mCallback = cb;
1400 mCallbackCookie = cookie;
1401
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001402 // Check argument "bufferCount" against the mininum buffer count
1403 if (bufferCount < mMinBufferCount) {
Steve Blockb8a80522011-12-20 16:23:08 +00001404 ALOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001405 bufferCount = mMinBufferCount;
1406
1407 }
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001408 ALOGV("open(%u, %d, 0x%x, %d, %d, %d)", sampleRate, channelCount, channelMask,
1409 format, bufferCount, mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001410 int afSampleRate;
1411 int afFrameCount;
Eric Laurent1948eb32012-04-13 16:50:19 -07001412 uint32_t frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001413
1414 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1415 return NO_INIT;
1416 }
1417 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1418 return NO_INIT;
1419 }
1420
1421 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
Andreas Huber20111aa2009-07-14 16:56:47 -07001422
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001423 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
Glenn Kastenab334fd2012-03-14 12:56:06 -07001424 channelMask = audio_channel_out_mask_from_count(channelCount);
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001425 if (0 == channelMask) {
1426 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
1427 return NO_INIT;
1428 }
1429 }
Eric Laurent1948eb32012-04-13 16:50:19 -07001430
Andreas Huber20111aa2009-07-14 16:56:47 -07001431 AudioTrack *t;
Marco Nelissen67295b52012-06-11 14:52:53 -07001432 CallbackData *newcbd = NULL;
Andreas Huber20111aa2009-07-14 16:56:47 -07001433 if (mCallback != NULL) {
Marco Nelissen67295b52012-06-11 14:52:53 -07001434 newcbd = new CallbackData(this);
Andreas Huber20111aa2009-07-14 16:56:47 -07001435 t = new AudioTrack(
Eric Laurentc2f1f072009-07-17 12:17:14 -07001436 mStreamType,
1437 sampleRate,
1438 format,
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001439 channelMask,
Eric Laurentc2f1f072009-07-17 12:17:14 -07001440 frameCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001441 flags,
Eric Laurentc2f1f072009-07-17 12:17:14 -07001442 CallbackWrapper,
Marco Nelissen67295b52012-06-11 14:52:53 -07001443 newcbd,
Glenn Kasten17a736c2012-02-14 08:52:15 -08001444 0, // notification frames
Eric Laurenta514bdb2010-06-21 09:27:30 -07001445 mSessionId);
Andreas Huber20111aa2009-07-14 16:56:47 -07001446 } else {
1447 t = new AudioTrack(
Eric Laurentc2f1f072009-07-17 12:17:14 -07001448 mStreamType,
1449 sampleRate,
1450 format,
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001451 channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -07001452 frameCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001453 flags,
Eric Laurenta514bdb2010-06-21 09:27:30 -07001454 NULL,
1455 NULL,
1456 0,
1457 mSessionId);
Andreas Huber20111aa2009-07-14 16:56:47 -07001458 }
1459
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001460 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
Steve Block29357bc2012-01-06 19:20:56 +00001461 ALOGE("Unable to create audio track");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001462 delete t;
Marco Nelissen67295b52012-06-11 14:52:53 -07001463 delete newcbd;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001464 return NO_INIT;
1465 }
1466
Marco Nelissen67295b52012-06-11 14:52:53 -07001467
1468 if (mRecycledTrack) {
1469 // check if the existing track can be reused as-is, or if a new track needs to be created.
1470
1471 bool reuse = true;
1472 if ((mCallbackData == NULL && mCallback != NULL) ||
1473 (mCallbackData != NULL && mCallback == NULL)) {
1474 // recycled track uses callbacks but the caller wants to use writes, or vice versa
1475 ALOGV("can't chain callback and write");
1476 reuse = false;
1477 } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
1478 (mRecycledTrack->channelCount() != channelCount) ||
1479 (mRecycledTrack->frameCount() != t->frameCount())) {
1480 ALOGV("samplerate, channelcount or framecount differ: %d/%d Hz, %d/%d ch, %d/%d frames",
1481 mRecycledTrack->getSampleRate(), sampleRate,
1482 mRecycledTrack->channelCount(), channelCount,
1483 mRecycledTrack->frameCount(), t->frameCount());
1484 reuse = false;
1485 } else if (flags != mFlags) {
1486 ALOGV("output flags differ %08x/%08x", flags, mFlags);
1487 reuse = false;
1488 }
1489 if (reuse) {
1490 ALOGV("chaining to next output");
1491 close();
1492 mTrack = mRecycledTrack;
1493 mRecycledTrack = NULL;
1494 if (mCallbackData != NULL) {
1495 mCallbackData->setOutput(this);
1496 }
1497 delete t;
1498 delete newcbd;
1499 return OK;
1500 }
1501
1502 // if we're not going to reuse the track, unblock and flush it
1503 if (mCallbackData != NULL) {
1504 mCallbackData->setOutput(NULL);
1505 mCallbackData->endTrackSwitch();
1506 }
1507 mRecycledTrack->flush();
1508 delete mRecycledTrack;
1509 mRecycledTrack = NULL;
1510 delete mCallbackData;
1511 mCallbackData = NULL;
1512 close();
1513 }
1514
1515 mCallbackData = newcbd;
Steve Block3856b092011-10-20 11:56:00 +01001516 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001517 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001518
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001519 mSampleRateHz = sampleRate;
Eric Laurent1948eb32012-04-13 16:50:19 -07001520 mFlags = flags;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001521 mMsecsPerFrame = mPlaybackRatePermille / (float) sampleRate;
Marco Nelissen99448602012-04-02 12:16:49 -07001522 uint32_t pos;
1523 if (t->getPosition(&pos) == OK) {
1524 mBytesWritten = uint64_t(pos) * t->frameSize();
1525 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001526 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001527
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001528 status_t res = t->setSampleRate(mPlaybackRatePermille * mSampleRateHz / 1000);
1529 if (res != NO_ERROR) {
1530 return res;
1531 }
Eric Laurent2beeb502010-07-16 07:43:46 -07001532 t->setAuxEffectSendLevel(mSendLevel);
1533 return t->attachAuxEffect(mAuxEffectId);;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001534}
1535
1536void MediaPlayerService::AudioOutput::start()
1537{
Steve Block3856b092011-10-20 11:56:00 +01001538 ALOGV("start");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001539 if (mCallbackData != NULL) {
1540 mCallbackData->endTrackSwitch();
1541 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001542 if (mTrack) {
1543 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001544 mTrack->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001545 mTrack->start();
1546 }
1547}
1548
Marco Nelissen6b74d672012-02-28 16:07:44 -08001549void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
1550 mNextOutput = nextOutput;
1551}
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001552
1553
Marco Nelissen6b74d672012-02-28 16:07:44 -08001554void MediaPlayerService::AudioOutput::switchToNextOutput() {
1555 ALOGV("switchToNextOutput");
1556 if (mNextOutput != NULL) {
1557 if (mCallbackData != NULL) {
1558 mCallbackData->beginTrackSwitch();
1559 }
1560 delete mNextOutput->mCallbackData;
1561 mNextOutput->mCallbackData = mCallbackData;
1562 mCallbackData = NULL;
1563 mNextOutput->mRecycledTrack = mTrack;
1564 mTrack = NULL;
1565 mNextOutput->mSampleRateHz = mSampleRateHz;
1566 mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
Marco Nelissen4110c102012-03-29 09:31:28 -07001567 mNextOutput->mBytesWritten = mBytesWritten;
Marco Nelissend791e092012-06-11 17:00:59 -07001568 mNextOutput->mFlags = mFlags;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001569 }
1570}
1571
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001572ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size)
1573{
Andreas Huber20111aa2009-07-14 16:56:47 -07001574 LOG_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
1575
Steve Block3856b092011-10-20 11:56:00 +01001576 //ALOGV("write(%p, %u)", buffer, size);
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001577 if (mTrack) {
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001578 ssize_t ret = mTrack->write(buffer, size);
Marco Nelissen4110c102012-03-29 09:31:28 -07001579 mBytesWritten += ret;
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001580 return ret;
1581 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001582 return NO_INIT;
1583}
1584
1585void MediaPlayerService::AudioOutput::stop()
1586{
Steve Block3856b092011-10-20 11:56:00 +01001587 ALOGV("stop");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001588 if (mTrack) mTrack->stop();
1589}
1590
1591void MediaPlayerService::AudioOutput::flush()
1592{
Steve Block3856b092011-10-20 11:56:00 +01001593 ALOGV("flush");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001594 if (mTrack) mTrack->flush();
1595}
1596
1597void MediaPlayerService::AudioOutput::pause()
1598{
Steve Block3856b092011-10-20 11:56:00 +01001599 ALOGV("pause");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001600 if (mTrack) mTrack->pause();
1601}
1602
1603void MediaPlayerService::AudioOutput::close()
1604{
Steve Block3856b092011-10-20 11:56:00 +01001605 ALOGV("close");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001606 delete mTrack;
1607 mTrack = 0;
1608}
1609
1610void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1611{
Steve Block3856b092011-10-20 11:56:00 +01001612 ALOGV("setVolume(%f, %f)", left, right);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001613 mLeftVolume = left;
1614 mRightVolume = right;
1615 if (mTrack) {
1616 mTrack->setVolume(left, right);
1617 }
1618}
1619
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001620status_t MediaPlayerService::AudioOutput::setPlaybackRatePermille(int32_t ratePermille)
1621{
1622 ALOGV("setPlaybackRatePermille(%d)", ratePermille);
1623 status_t res = NO_ERROR;
1624 if (mTrack) {
1625 res = mTrack->setSampleRate(ratePermille * mSampleRateHz / 1000);
1626 } else {
1627 res = NO_INIT;
1628 }
1629 mPlaybackRatePermille = ratePermille;
1630 if (mSampleRateHz != 0) {
1631 mMsecsPerFrame = mPlaybackRatePermille / (float) mSampleRateHz;
1632 }
1633 return res;
1634}
1635
Eric Laurent2beeb502010-07-16 07:43:46 -07001636status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1637{
Steve Block3856b092011-10-20 11:56:00 +01001638 ALOGV("setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001639 mSendLevel = level;
1640 if (mTrack) {
1641 return mTrack->setAuxEffectSendLevel(level);
1642 }
1643 return NO_ERROR;
1644}
1645
1646status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1647{
Steve Block3856b092011-10-20 11:56:00 +01001648 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001649 mAuxEffectId = effectId;
1650 if (mTrack) {
1651 return mTrack->attachAuxEffect(effectId);
1652 }
1653 return NO_ERROR;
1654}
1655
Andreas Huber20111aa2009-07-14 16:56:47 -07001656// static
1657void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastend217a8c2011-06-01 15:20:35 -07001658 int event, void *cookie, void *info) {
Steve Block3856b092011-10-20 11:56:00 +01001659 //ALOGV("callbackwrapper");
Andreas Huber20111aa2009-07-14 16:56:47 -07001660 if (event != AudioTrack::EVENT_MORE_DATA) {
1661 return;
1662 }
1663
Marco Nelissen6b74d672012-02-28 16:07:44 -08001664 CallbackData *data = (CallbackData*)cookie;
1665 data->lock();
1666 AudioOutput *me = data->getOutput();
Andreas Huber20111aa2009-07-14 16:56:47 -07001667 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001668 if (me == NULL) {
1669 // no output set, likely because the track was scheduled to be reused
1670 // by another player, but the format turned out to be incompatible.
1671 data->unlock();
1672 buffer->size = 0;
1673 return;
1674 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001675
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001676 size_t actualSize = (*me->mCallback)(
Andreas Huber20111aa2009-07-14 16:56:47 -07001677 me, buffer->raw, buffer->size, me->mCallbackCookie);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001678
Marco Nelissen6b74d672012-02-28 16:07:44 -08001679 if (actualSize == 0 && buffer->size > 0 && me->mNextOutput == NULL) {
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001680 // We've reached EOS but the audio track is not stopped yet,
1681 // keep playing silence.
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08001682
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001683 memset(buffer->raw, 0, buffer->size);
1684 actualSize = buffer->size;
1685 }
1686
1687 buffer->size = actualSize;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001688 data->unlock();
Andreas Huber20111aa2009-07-14 16:56:47 -07001689}
1690
Marco Nelissen4110c102012-03-29 09:31:28 -07001691int MediaPlayerService::AudioOutput::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001692{
1693 return mSessionId;
1694}
1695
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001696#undef LOG_TAG
1697#define LOG_TAG "AudioCache"
1698MediaPlayerService::AudioCache::AudioCache(const char* name) :
1699 mChannelCount(0), mFrameCount(1024), mSampleRate(0), mSize(0),
1700 mError(NO_ERROR), mCommandComplete(false)
1701{
1702 // create ashmem heap
1703 mHeap = new MemoryHeapBase(kDefaultHeapSize, 0, name);
1704}
1705
1706uint32_t MediaPlayerService::AudioCache::latency () const
1707{
1708 return 0;
1709}
1710
1711float MediaPlayerService::AudioCache::msecsPerFrame() const
1712{
1713 return mMsecsPerFrame;
1714}
1715
Marco Nelissen4110c102012-03-29 09:31:28 -07001716status_t MediaPlayerService::AudioCache::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001717{
1718 if (position == 0) return BAD_VALUE;
1719 *position = mSize;
1720 return NO_ERROR;
1721}
1722
Marco Nelissen4110c102012-03-29 09:31:28 -07001723status_t MediaPlayerService::AudioCache::getFramesWritten(uint32_t *written) const
1724{
1725 if (written == 0) return BAD_VALUE;
1726 *written = mSize;
1727 return NO_ERROR;
1728}
1729
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001730////////////////////////////////////////////////////////////////////////////////
1731
1732struct CallbackThread : public Thread {
1733 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1734 MediaPlayerBase::AudioSink::AudioCallback cb,
1735 void *cookie);
1736
1737protected:
1738 virtual ~CallbackThread();
1739
1740 virtual bool threadLoop();
1741
1742private:
1743 wp<MediaPlayerBase::AudioSink> mSink;
1744 MediaPlayerBase::AudioSink::AudioCallback mCallback;
1745 void *mCookie;
1746 void *mBuffer;
1747 size_t mBufferSize;
1748
1749 CallbackThread(const CallbackThread &);
1750 CallbackThread &operator=(const CallbackThread &);
1751};
1752
1753CallbackThread::CallbackThread(
1754 const wp<MediaPlayerBase::AudioSink> &sink,
1755 MediaPlayerBase::AudioSink::AudioCallback cb,
1756 void *cookie)
1757 : mSink(sink),
1758 mCallback(cb),
1759 mCookie(cookie),
1760 mBuffer(NULL),
1761 mBufferSize(0) {
1762}
1763
1764CallbackThread::~CallbackThread() {
1765 if (mBuffer) {
1766 free(mBuffer);
1767 mBuffer = NULL;
1768 }
1769}
1770
1771bool CallbackThread::threadLoop() {
1772 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
1773 if (sink == NULL) {
1774 return false;
1775 }
1776
1777 if (mBuffer == NULL) {
1778 mBufferSize = sink->bufferSize();
1779 mBuffer = malloc(mBufferSize);
1780 }
1781
1782 size_t actualSize =
1783 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie);
1784
1785 if (actualSize > 0) {
1786 sink->write(mBuffer, actualSize);
1787 }
1788
1789 return true;
1790}
1791
1792////////////////////////////////////////////////////////////////////////////////
1793
Andreas Huber20111aa2009-07-14 16:56:47 -07001794status_t MediaPlayerService::AudioCache::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001795 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1796 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001797 AudioCallback cb, void *cookie, audio_output_flags_t flags)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001798{
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001799 ALOGV("open(%u, %d, 0x%x, %d, %d)", sampleRate, channelCount, channelMask, format, bufferCount);
Dave Sparks8eb80112009-12-09 20:20:26 -08001800 if (mHeap->getHeapID() < 0) {
1801 return NO_INIT;
1802 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001803
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001804 mSampleRate = sampleRate;
1805 mChannelCount = (uint16_t)channelCount;
Glenn Kastene1c39622012-01-04 09:36:37 -08001806 mFormat = format;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001807 mMsecsPerFrame = 1.e3 / (float) sampleRate;
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001808
1809 if (cb != NULL) {
1810 mCallbackThread = new CallbackThread(this, cb, cookie);
1811 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001812 return NO_ERROR;
1813}
1814
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001815void MediaPlayerService::AudioCache::start() {
1816 if (mCallbackThread != NULL) {
1817 mCallbackThread->run("AudioCache callback");
1818 }
1819}
1820
1821void MediaPlayerService::AudioCache::stop() {
1822 if (mCallbackThread != NULL) {
1823 mCallbackThread->requestExitAndWait();
1824 }
1825}
1826
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001827ssize_t MediaPlayerService::AudioCache::write(const void* buffer, size_t size)
1828{
Steve Block3856b092011-10-20 11:56:00 +01001829 ALOGV("write(%p, %u)", buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001830 if ((buffer == 0) || (size == 0)) return size;
1831
1832 uint8_t* p = static_cast<uint8_t*>(mHeap->getBase());
1833 if (p == NULL) return NO_INIT;
1834 p += mSize;
Steve Block3856b092011-10-20 11:56:00 +01001835 ALOGV("memcpy(%p, %p, %u)", p, buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001836 if (mSize + size > mHeap->getSize()) {
Steve Block29357bc2012-01-06 19:20:56 +00001837 ALOGE("Heap size overflow! req size: %d, max size: %d", (mSize + size), mHeap->getSize());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001838 size = mHeap->getSize() - mSize;
1839 }
1840 memcpy(p, buffer, size);
1841 mSize += size;
1842 return size;
1843}
1844
1845// call with lock held
1846status_t MediaPlayerService::AudioCache::wait()
1847{
1848 Mutex::Autolock lock(mLock);
Dave Sparks4bbc0ba2010-03-01 19:29:58 -08001849 while (!mCommandComplete) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001850 mSignal.wait(mLock);
1851 }
1852 mCommandComplete = false;
1853
1854 if (mError == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01001855 ALOGV("wait - success");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001856 } else {
Steve Block3856b092011-10-20 11:56:00 +01001857 ALOGV("wait - error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001858 }
1859 return mError;
1860}
1861
Gloria Wangb483c472011-04-11 17:23:27 -07001862void MediaPlayerService::AudioCache::notify(
1863 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001864{
Steve Block3856b092011-10-20 11:56:00 +01001865 ALOGV("notify(%p, %d, %d, %d)", cookie, msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001866 AudioCache* p = static_cast<AudioCache*>(cookie);
1867
1868 // ignore buffering messages
Dave Sparks8eb80112009-12-09 20:20:26 -08001869 switch (msg)
1870 {
1871 case MEDIA_ERROR:
Steve Block29357bc2012-01-06 19:20:56 +00001872 ALOGE("Error %d, %d occurred", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001873 p->mError = ext1;
Dave Sparks8eb80112009-12-09 20:20:26 -08001874 break;
1875 case MEDIA_PREPARED:
Steve Block3856b092011-10-20 11:56:00 +01001876 ALOGV("prepared");
Dave Sparks8eb80112009-12-09 20:20:26 -08001877 break;
1878 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +01001879 ALOGV("playback complete");
Dave Sparks8eb80112009-12-09 20:20:26 -08001880 break;
1881 default:
Steve Block3856b092011-10-20 11:56:00 +01001882 ALOGV("ignored");
Dave Sparks8eb80112009-12-09 20:20:26 -08001883 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001884 }
1885
1886 // wake up thread
Dave Sparksfe4c6f02010-03-02 12:56:37 -08001887 Mutex::Autolock lock(p->mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001888 p->mCommandComplete = true;
1889 p->mSignal.signal();
1890}
1891
Marco Nelissen4110c102012-03-29 09:31:28 -07001892int MediaPlayerService::AudioCache::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001893{
1894 return 0;
1895}
1896
Gloria Wang7cf180c2011-02-19 18:37:57 -08001897void MediaPlayerService::addBatteryData(uint32_t params)
1898{
1899 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08001900
1901 int32_t time = systemTime() / 1000000L;
1902
1903 // change audio output devices. This notification comes from AudioFlinger
1904 if ((params & kBatteryDataSpeakerOn)
1905 || (params & kBatteryDataOtherAudioDeviceOn)) {
1906
1907 int deviceOn[NUM_AUDIO_DEVICES];
1908 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1909 deviceOn[i] = 0;
1910 }
1911
1912 if ((params & kBatteryDataSpeakerOn)
1913 && (params & kBatteryDataOtherAudioDeviceOn)) {
1914 deviceOn[SPEAKER_AND_OTHER] = 1;
1915 } else if (params & kBatteryDataSpeakerOn) {
1916 deviceOn[SPEAKER] = 1;
1917 } else {
1918 deviceOn[OTHER_AUDIO_DEVICE] = 1;
1919 }
1920
1921 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1922 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
1923
1924 if (mBatteryAudio.refCount > 0) { // if playing audio
1925 if (!deviceOn[i]) {
1926 mBatteryAudio.lastTime[i] += time;
1927 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1928 mBatteryAudio.lastTime[i] = 0;
1929 } else {
1930 mBatteryAudio.lastTime[i] = 0 - time;
1931 }
1932 }
1933
1934 mBatteryAudio.deviceOn[i] = deviceOn[i];
1935 }
1936 }
1937 return;
1938 }
1939
1940 // an sudio stream is started
1941 if (params & kBatteryDataAudioFlingerStart) {
1942 // record the start time only if currently no other audio
1943 // is being played
1944 if (mBatteryAudio.refCount == 0) {
1945 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1946 if (mBatteryAudio.deviceOn[i]) {
1947 mBatteryAudio.lastTime[i] -= time;
1948 }
1949 }
1950 }
1951
1952 mBatteryAudio.refCount ++;
1953 return;
1954
1955 } else if (params & kBatteryDataAudioFlingerStop) {
1956 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001957 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001958 return;
1959 }
1960
1961 // record the stop time only if currently this is the only
1962 // audio being played
1963 if (mBatteryAudio.refCount == 1) {
1964 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1965 if (mBatteryAudio.deviceOn[i]) {
1966 mBatteryAudio.lastTime[i] += time;
1967 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1968 mBatteryAudio.lastTime[i] = 0;
1969 }
1970 }
1971 }
1972
1973 mBatteryAudio.refCount --;
1974 return;
1975 }
1976
Gloria Wang7cf180c2011-02-19 18:37:57 -08001977 int uid = IPCThreadState::self()->getCallingUid();
1978 if (uid == AID_MEDIA) {
1979 return;
1980 }
1981 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08001982
1983 if (index < 0) { // create a new entry for this UID
1984 BatteryUsageInfo info;
1985 info.audioTotalTime = 0;
1986 info.videoTotalTime = 0;
1987 info.audioLastTime = 0;
1988 info.videoLastTime = 0;
1989 info.refCount = 0;
1990
Gloria Wang9ee159b2011-02-24 14:51:45 -08001991 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00001992 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001993 return;
1994 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08001995 }
1996
1997 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
1998
1999 if (params & kBatteryDataCodecStarted) {
2000 if (params & kBatteryDataTrackAudio) {
2001 info.audioLastTime -= time;
2002 info.refCount ++;
2003 }
2004 if (params & kBatteryDataTrackVideo) {
2005 info.videoLastTime -= time;
2006 info.refCount ++;
2007 }
2008 } else {
2009 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002010 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002011 return;
2012 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00002013 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002014 mBatteryData.removeItem(uid);
2015 return;
2016 }
2017
2018 if (params & kBatteryDataTrackAudio) {
2019 info.audioLastTime += time;
2020 info.refCount --;
2021 }
2022 if (params & kBatteryDataTrackVideo) {
2023 info.videoLastTime += time;
2024 info.refCount --;
2025 }
2026
2027 // no stream is being played by this UID
2028 if (info.refCount == 0) {
2029 info.audioTotalTime += info.audioLastTime;
2030 info.audioLastTime = 0;
2031 info.videoTotalTime += info.videoLastTime;
2032 info.videoLastTime = 0;
2033 }
2034 }
2035}
2036
2037status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
2038 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002039
2040 // audio output devices usage
2041 int32_t time = systemTime() / 1000000L; //in ms
2042 int32_t totalTime;
2043
2044 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2045 totalTime = mBatteryAudio.totalTime[i];
2046
2047 if (mBatteryAudio.deviceOn[i]
2048 && (mBatteryAudio.lastTime[i] != 0)) {
2049 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
2050 totalTime += tmpTime;
2051 }
2052
2053 reply->writeInt32(totalTime);
2054 // reset the total time
2055 mBatteryAudio.totalTime[i] = 0;
2056 }
2057
2058 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08002059 BatteryUsageInfo info;
2060 int size = mBatteryData.size();
2061
2062 reply->writeInt32(size);
2063 int i = 0;
2064
2065 while (i < size) {
2066 info = mBatteryData.valueAt(i);
2067
2068 reply->writeInt32(mBatteryData.keyAt(i)); //UID
2069 reply->writeInt32(info.audioTotalTime);
2070 reply->writeInt32(info.videoTotalTime);
2071
2072 info.audioTotalTime = 0;
2073 info.videoTotalTime = 0;
2074
2075 // remove the UID entry where no stream is being played
2076 if (info.refCount <= 0) {
2077 mBatteryData.removeItemsAt(i);
2078 size --;
2079 i --;
2080 }
2081 i++;
2082 }
2083 return NO_ERROR;
2084}
nikoa64c8c72009-07-20 15:07:26 -07002085} // namespace android