blob: ec6ace162db5646252d72c7a5b5e6d842bac0ce8 [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>
Mathias Agopian1a2952a2013-02-14 17:11:27 -080041#include <gui/Surface.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"
Jeff Tinker441a78d2013-02-08 10:18:35 -080075#include "Drm.h"
Andreas Huberefbb7812012-09-18 10:36:32 -070076#include "HDCP.h"
Andreas Huberd7bee3a2012-08-29 11:41:50 -070077#include "RemoteDisplay.h"
Andreas Hubered3e3e02012-03-26 11:13:27 -070078
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070079namespace {
nikoa64c8c72009-07-20 15:07:26 -070080using android::media::Metadata;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070081using android::status_t;
82using android::OK;
83using android::BAD_VALUE;
84using android::NOT_ENOUGH_DATA;
85using android::Parcel;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070086
87// Max number of entries in the filter.
88const int kMaxFilterSize = 64; // I pulled that out of thin air.
89
nikoa64c8c72009-07-20 15:07:26 -070090// FIXME: Move all the metadata related function in the Metadata.cpp
nikod608a812009-07-16 16:39:53 -070091
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070092
93// Unmarshall a filter from a Parcel.
94// Filter format in a parcel:
95//
96// 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
97// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
98// | number of entries (n) |
99// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
100// | metadata type 1 |
101// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
102// | metadata type 2 |
103// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
104// ....
105// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
106// | metadata type n |
107// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
108//
109// @param p Parcel that should start with a filter.
110// @param[out] filter On exit contains the list of metadata type to be
111// filtered.
112// @param[out] status On exit contains the status code to be returned.
113// @return true if the parcel starts with a valid filter.
114bool unmarshallFilter(const Parcel& p,
nikoa64c8c72009-07-20 15:07:26 -0700115 Metadata::Filter *filter,
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700116 status_t *status)
117{
Nicolas Catania48290382009-07-10 13:53:06 -0700118 int32_t val;
119 if (p.readInt32(&val) != OK)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700120 {
Steve Block29357bc2012-01-06 19:20:56 +0000121 ALOGE("Failed to read filter's length");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700122 *status = NOT_ENOUGH_DATA;
123 return false;
124 }
125
Nicolas Catania48290382009-07-10 13:53:06 -0700126 if( val > kMaxFilterSize || val < 0)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700127 {
Steve Block29357bc2012-01-06 19:20:56 +0000128 ALOGE("Invalid filter len %d", val);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700129 *status = BAD_VALUE;
130 return false;
131 }
132
Nicolas Catania48290382009-07-10 13:53:06 -0700133 const size_t num = val;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700134
135 filter->clear();
Nicolas Catania48290382009-07-10 13:53:06 -0700136 filter->setCapacity(num);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700137
nikoa64c8c72009-07-20 15:07:26 -0700138 size_t size = num * sizeof(Metadata::Type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700139
Nicolas Catania48290382009-07-10 13:53:06 -0700140
141 if (p.dataAvail() < size)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700142 {
Steve Block29357bc2012-01-06 19:20:56 +0000143 ALOGE("Filter too short expected %d but got %d", size, p.dataAvail());
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700144 *status = NOT_ENOUGH_DATA;
145 return false;
146 }
147
nikoa64c8c72009-07-20 15:07:26 -0700148 const Metadata::Type *data =
149 static_cast<const Metadata::Type*>(p.readInplace(size));
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700150
Nicolas Catania48290382009-07-10 13:53:06 -0700151 if (NULL == data)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700152 {
Steve Block29357bc2012-01-06 19:20:56 +0000153 ALOGE("Filter had no data");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700154 *status = BAD_VALUE;
155 return false;
156 }
157
158 // TODO: The stl impl of vector would be more efficient here
159 // because it degenerates into a memcpy on pod types. Try to
160 // replace later or use stl::set.
Nicolas Catania48290382009-07-10 13:53:06 -0700161 for (size_t i = 0; i < num; ++i)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700162 {
Nicolas Catania48290382009-07-10 13:53:06 -0700163 filter->add(*data);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700164 ++data;
165 }
166 *status = OK;
167 return true;
168}
169
Nicolas Catania48290382009-07-10 13:53:06 -0700170// @param filter Of metadata type.
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700171// @param val To be searched.
172// @return true if a match was found.
nikoa64c8c72009-07-20 15:07:26 -0700173bool findMetadata(const Metadata::Filter& filter, const int32_t val)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700174{
175 // Deal with empty and ANY right away
176 if (filter.isEmpty()) return false;
nikoa64c8c72009-07-20 15:07:26 -0700177 if (filter[0] == Metadata::kAny) return true;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700178
Nicolas Catania48290382009-07-10 13:53:06 -0700179 return filter.indexOf(val) >= 0;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700180}
181
182} // anonymous namespace
183
184
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800185namespace android {
186
Dave Burked681bbb2011-08-30 14:39:17 +0100187static bool checkPermission(const char* permissionString) {
188#ifndef HAVE_ANDROID_OS
189 return true;
190#endif
191 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
192 bool ok = checkCallingPermission(String16(permissionString));
Steve Block29357bc2012-01-06 19:20:56 +0000193 if (!ok) ALOGE("Request requires %s", permissionString);
Dave Burked681bbb2011-08-30 14:39:17 +0100194 return ok;
195}
196
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800197// 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 -0800198/* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4;
199/* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false;
200
201void MediaPlayerService::instantiate() {
202 defaultServiceManager()->addService(
203 String16("media.player"), new MediaPlayerService());
204}
205
206MediaPlayerService::MediaPlayerService()
207{
Steve Block3856b092011-10-20 11:56:00 +0100208 ALOGV("MediaPlayerService created");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800209 mNextConnId = 1;
Gloria Wang9ee159b2011-02-24 14:51:45 -0800210
211 mBatteryAudio.refCount = 0;
212 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
213 mBatteryAudio.deviceOn[i] = 0;
214 mBatteryAudio.lastTime[i] = 0;
215 mBatteryAudio.totalTime[i] = 0;
216 }
217 // speaker is on by default
218 mBatteryAudio.deviceOn[SPEAKER] = 1;
John Grossman44a7e422012-06-21 17:29:24 -0700219
220 MediaPlayerFactory::registerBuiltinFactories();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800221}
222
223MediaPlayerService::~MediaPlayerService()
224{
Steve Block3856b092011-10-20 11:56:00 +0100225 ALOGV("MediaPlayerService destroyed");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800226}
227
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800228sp<IMediaRecorder> MediaPlayerService::createMediaRecorder()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800229{
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800230 pid_t pid = IPCThreadState::self()->getCallingPid();
Gloria Wangdac6a312009-10-29 15:46:37 -0700231 sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid);
232 wp<MediaRecorderClient> w = recorder;
233 Mutex::Autolock lock(mLock);
234 mMediaRecorderClients.add(w);
Steve Block3856b092011-10-20 11:56:00 +0100235 ALOGV("Create new media recorder client from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800236 return recorder;
237}
238
Gloria Wangdac6a312009-10-29 15:46:37 -0700239void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client)
240{
241 Mutex::Autolock lock(mLock);
242 mMediaRecorderClients.remove(client);
Steve Block3856b092011-10-20 11:56:00 +0100243 ALOGV("Delete media recorder client");
Gloria Wangdac6a312009-10-29 15:46:37 -0700244}
245
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800246sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800247{
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800248 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800249 sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
Steve Block3856b092011-10-20 11:56:00 +0100250 ALOGV("Create new media retriever from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800251 return retriever;
252}
253
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800254sp<IMediaPlayer> MediaPlayerService::create(const sp<IMediaPlayerClient>& client,
Dave Burked681bbb2011-08-30 14:39:17 +0100255 int audioSessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800256{
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800257 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800258 int32_t connId = android_atomic_inc(&mNextConnId);
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700259
260 sp<Client> c = new Client(
261 this, pid, connId, client, audioSessionId,
262 IPCThreadState::self()->getCallingUid());
263
Steve Block3856b092011-10-20 11:56:00 +0100264 ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid,
Dave Burked681bbb2011-08-30 14:39:17 +0100265 IPCThreadState::self()->getCallingUid());
266
267 wp<Client> w = c;
268 {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800269 Mutex::Autolock lock(mLock);
270 mClients.add(w);
271 }
Andreas Hubere2b10282010-11-23 11:41:34 -0800272 return c;
273}
274
Andreas Huber318ad9c2009-10-15 13:46:54 -0700275sp<IOMX> MediaPlayerService::getOMX() {
276 Mutex::Autolock autoLock(mLock);
277
278 if (mOMX.get() == NULL) {
279 mOMX = new OMX;
280 }
281
282 return mOMX;
Andreas Huber20111aa2009-07-14 16:56:47 -0700283}
284
Andreas Hubered3e3e02012-03-26 11:13:27 -0700285sp<ICrypto> MediaPlayerService::makeCrypto() {
Andreas Huber1bd139a2012-04-03 14:19:20 -0700286 return new Crypto;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700287}
288
Jeff Tinker441a78d2013-02-08 10:18:35 -0800289sp<IDrm> MediaPlayerService::makeDrm() {
290 return new Drm;
291}
292
Andreas Hubera6a88d92013-01-30 10:41:25 -0800293sp<IHDCP> MediaPlayerService::makeHDCP(bool createEncryptionModule) {
294 return new HDCP(createEncryptionModule);
Andreas Huberefbb7812012-09-18 10:36:32 -0700295}
296
Jeff Browne1045962012-09-04 21:38:42 -0700297sp<IRemoteDisplay> MediaPlayerService::listenForRemoteDisplay(
298 const sp<IRemoteDisplayClient>& client, const String8& iface) {
Jeff Brownb0192502012-09-07 17:38:58 -0700299 if (!checkPermission("android.permission.CONTROL_WIFI_DISPLAY")) {
300 return NULL;
301 }
302
Jeff Brown455d02e2012-09-05 17:48:03 -0700303 return new RemoteDisplay(client, iface.string());
Jeff Browne1045962012-09-04 21:38:42 -0700304}
305
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800306status_t MediaPlayerService::AudioCache::dump(int fd, const Vector<String16>& args) const
307{
308 const size_t SIZE = 256;
309 char buffer[SIZE];
310 String8 result;
311
312 result.append(" AudioCache\n");
313 if (mHeap != 0) {
314 snprintf(buffer, 255, " heap base(%p), size(%d), flags(%d), device(%s)\n",
315 mHeap->getBase(), mHeap->getSize(), mHeap->getFlags(), mHeap->getDevice());
316 result.append(buffer);
317 }
318 snprintf(buffer, 255, " msec per frame(%f), channel count(%d), format(%d), frame count(%ld)\n",
319 mMsecsPerFrame, mChannelCount, mFormat, mFrameCount);
320 result.append(buffer);
321 snprintf(buffer, 255, " sample rate(%d), size(%d), error(%d), command complete(%s)\n",
322 mSampleRate, mSize, mError, mCommandComplete?"true":"false");
323 result.append(buffer);
324 ::write(fd, result.string(), result.size());
325 return NO_ERROR;
326}
327
328status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
329{
330 const size_t SIZE = 256;
331 char buffer[SIZE];
332 String8 result;
333
334 result.append(" AudioOutput\n");
335 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n",
336 mStreamType, mLeftVolume, mRightVolume);
337 result.append(buffer);
338 snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
Eric Laurentdb354e52012-03-05 17:27:11 -0800339 mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800340 result.append(buffer);
Eric Laurent2beeb502010-07-16 07:43:46 -0700341 snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
342 mAuxEffectId, mSendLevel);
343 result.append(buffer);
344
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800345 ::write(fd, result.string(), result.size());
346 if (mTrack != 0) {
347 mTrack->dump(fd, args);
348 }
349 return NO_ERROR;
350}
351
352status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args) const
353{
354 const size_t SIZE = 256;
355 char buffer[SIZE];
356 String8 result;
357 result.append(" Client\n");
358 snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n",
359 mPid, mConnId, mStatus, mLoop?"true": "false");
360 result.append(buffer);
361 write(fd, result.string(), result.size());
Andreas Hubera0b1d4b2011-06-07 15:52:25 -0700362 if (mPlayer != NULL) {
363 mPlayer->dump(fd, args);
364 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800365 if (mAudioOutput != 0) {
366 mAudioOutput->dump(fd, args);
367 }
368 write(fd, "\n", 1);
369 return NO_ERROR;
370}
371
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800372status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
373{
374 const size_t SIZE = 256;
375 char buffer[SIZE];
376 String8 result;
377 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
378 snprintf(buffer, SIZE, "Permission Denial: "
379 "can't dump MediaPlayerService from pid=%d, uid=%d\n",
380 IPCThreadState::self()->getCallingPid(),
381 IPCThreadState::self()->getCallingUid());
382 result.append(buffer);
383 } else {
384 Mutex::Autolock lock(mLock);
385 for (int i = 0, n = mClients.size(); i < n; ++i) {
386 sp<Client> c = mClients[i].promote();
387 if (c != 0) c->dump(fd, args);
388 }
James Dongb9141222010-07-08 11:16:11 -0700389 if (mMediaRecorderClients.size() == 0) {
390 result.append(" No media recorder client\n\n");
391 } else {
392 for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
393 sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
James Donge579e282011-10-18 22:29:20 -0700394 if (c != 0) {
395 snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid);
396 result.append(buffer);
397 write(fd, result.string(), result.size());
398 result = "\n";
399 c->dump(fd, args);
400 }
James Dongb9141222010-07-08 11:16:11 -0700401 }
Gloria Wangdac6a312009-10-29 15:46:37 -0700402 }
403
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800404 result.append(" Files opened and/or mapped:\n");
Glenn Kasten0512ab52011-05-04 17:58:57 -0700405 snprintf(buffer, SIZE, "/proc/%d/maps", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800406 FILE *f = fopen(buffer, "r");
407 if (f) {
408 while (!feof(f)) {
409 fgets(buffer, SIZE, f);
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700410 if (strstr(buffer, " /storage/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800411 strstr(buffer, " /system/sounds/") ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700412 strstr(buffer, " /data/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800413 strstr(buffer, " /system/media/")) {
414 result.append(" ");
415 result.append(buffer);
416 }
417 }
418 fclose(f);
419 } else {
420 result.append("couldn't open ");
421 result.append(buffer);
422 result.append("\n");
423 }
424
Glenn Kasten0512ab52011-05-04 17:58:57 -0700425 snprintf(buffer, SIZE, "/proc/%d/fd", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800426 DIR *d = opendir(buffer);
427 if (d) {
428 struct dirent *ent;
429 while((ent = readdir(d)) != NULL) {
430 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
Glenn Kasten0512ab52011-05-04 17:58:57 -0700431 snprintf(buffer, SIZE, "/proc/%d/fd/%s", gettid(), ent->d_name);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800432 struct stat s;
433 if (lstat(buffer, &s) == 0) {
434 if ((s.st_mode & S_IFMT) == S_IFLNK) {
435 char linkto[256];
436 int len = readlink(buffer, linkto, sizeof(linkto));
437 if(len > 0) {
438 if(len > 255) {
439 linkto[252] = '.';
440 linkto[253] = '.';
441 linkto[254] = '.';
442 linkto[255] = 0;
443 } else {
444 linkto[len] = 0;
445 }
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700446 if (strstr(linkto, "/storage/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800447 strstr(linkto, "/system/sounds/") == linkto ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700448 strstr(linkto, "/data/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800449 strstr(linkto, "/system/media/") == linkto) {
450 result.append(" ");
451 result.append(buffer);
452 result.append(" -> ");
453 result.append(linkto);
454 result.append("\n");
455 }
456 }
457 } else {
458 result.append(" unexpected type for ");
459 result.append(buffer);
460 result.append("\n");
461 }
462 }
463 }
464 }
465 closedir(d);
466 } else {
467 result.append("couldn't open ");
468 result.append(buffer);
469 result.append("\n");
470 }
471
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800472 bool dumpMem = false;
473 for (size_t i = 0; i < args.size(); i++) {
474 if (args[i] == String16("-m")) {
475 dumpMem = true;
476 }
477 }
478 if (dumpMem) {
James Dong8635b7b2011-03-14 17:01:38 -0700479 dumpMemoryAddresses(fd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800480 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800481 }
482 write(fd, result.string(), result.size());
483 return NO_ERROR;
484}
485
486void MediaPlayerService::removeClient(wp<Client> client)
487{
488 Mutex::Autolock lock(mLock);
489 mClients.remove(client);
490}
491
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700492MediaPlayerService::Client::Client(
493 const sp<MediaPlayerService>& service, pid_t pid,
494 int32_t connId, const sp<IMediaPlayerClient>& client,
495 int audioSessionId, uid_t uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800496{
Steve Block3856b092011-10-20 11:56:00 +0100497 ALOGV("Client(%d) constructor", connId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800498 mPid = pid;
499 mConnId = connId;
500 mService = service;
501 mClient = client;
502 mLoop = false;
503 mStatus = NO_INIT;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700504 mAudioSessionId = audioSessionId;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700505 mUID = uid;
John Grossmanc795b642012-02-22 15:38:35 -0800506 mRetransmitEndpointValid = false;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700507
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800508#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000509 ALOGD("create Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800510 mAntagonizer = new Antagonizer(notify, this);
511#endif
512}
513
514MediaPlayerService::Client::~Client()
515{
Steve Block3856b092011-10-20 11:56:00 +0100516 ALOGV("Client(%d) destructor pid = %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800517 mAudioOutput.clear();
518 wp<Client> client(this);
519 disconnect();
520 mService->removeClient(client);
521}
522
523void MediaPlayerService::Client::disconnect()
524{
Steve Block3856b092011-10-20 11:56:00 +0100525 ALOGV("disconnect(%d) from pid %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800526 // grab local reference and clear main reference to prevent future
527 // access to object
528 sp<MediaPlayerBase> p;
529 {
530 Mutex::Autolock l(mLock);
531 p = mPlayer;
532 }
Dave Sparks795fa582009-03-24 17:57:12 -0700533 mClient.clear();
Andreas Huber20111aa2009-07-14 16:56:47 -0700534
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800535 mPlayer.clear();
536
537 // clear the notification to prevent callbacks to dead client
538 // and reset the player. We assume the player will serialize
539 // access to itself if necessary.
540 if (p != 0) {
541 p->setNotifyCallback(0, 0);
542#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000543 ALOGD("kill Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800544 mAntagonizer->kill();
545#endif
546 p->reset();
547 }
548
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700549 disconnectNativeWindow();
550
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800551 IPCThreadState::self()->flushCommands();
552}
553
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800554sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
555{
556 // determine if we have the right player type
557 sp<MediaPlayerBase> p = mPlayer;
558 if ((p != NULL) && (p->playerType() != playerType)) {
Steve Block3856b092011-10-20 11:56:00 +0100559 ALOGV("delete player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800560 p.clear();
561 }
562 if (p == NULL) {
John Grossman44a7e422012-06-21 17:29:24 -0700563 p = MediaPlayerFactory::createPlayer(playerType, this, notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800564 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700565
Jason Simmonsdb29e522011-08-12 13:46:55 -0700566 if (p != NULL) {
567 p->setUID(mUID);
568 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700569
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800570 return p;
571}
572
John Grossmanc795b642012-02-22 15:38:35 -0800573sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre(
574 player_type playerType)
575{
576 ALOGV("player type = %d", playerType);
577
578 // create the right type of player
579 sp<MediaPlayerBase> p = createPlayer(playerType);
580 if (p == NULL) {
581 return p;
582 }
583
584 if (!p->hardwareOutput()) {
585 mAudioOutput = new AudioOutput(mAudioSessionId);
586 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
587 }
588
589 return p;
590}
591
592void MediaPlayerService::Client::setDataSource_post(
593 const sp<MediaPlayerBase>& p,
594 status_t status)
595{
596 ALOGV(" setDataSource");
597 mStatus = status;
598 if (mStatus != OK) {
599 ALOGE(" error: %d", mStatus);
600 return;
601 }
602
603 // Set the re-transmission endpoint if one was chosen.
604 if (mRetransmitEndpointValid) {
605 mStatus = p->setRetransmitEndpoint(&mRetransmitEndpoint);
606 if (mStatus != NO_ERROR) {
607 ALOGE("setRetransmitEndpoint error: %d", mStatus);
608 }
609 }
610
611 if (mStatus == OK) {
612 mPlayer = p;
613 }
614}
615
Andreas Huber2db84552010-01-28 11:19:57 -0800616status_t MediaPlayerService::Client::setDataSource(
617 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800618{
Steve Block3856b092011-10-20 11:56:00 +0100619 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800620 if (url == NULL)
621 return UNKNOWN_ERROR;
622
Dave Burked681bbb2011-08-30 14:39:17 +0100623 if ((strncmp(url, "http://", 7) == 0) ||
624 (strncmp(url, "https://", 8) == 0) ||
625 (strncmp(url, "rtsp://", 7) == 0)) {
626 if (!checkPermission("android.permission.INTERNET")) {
627 return PERMISSION_DENIED;
628 }
629 }
630
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800631 if (strncmp(url, "content://", 10) == 0) {
632 // get a filedescriptor for the content Uri and
633 // pass it to the setDataSource(fd) method
634
635 String16 url16(url);
636 int fd = android::openContentProviderFile(url16);
637 if (fd < 0)
638 {
Steve Block29357bc2012-01-06 19:20:56 +0000639 ALOGE("Couldn't open fd for %s", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800640 return UNKNOWN_ERROR;
641 }
642 setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
643 close(fd);
644 return mStatus;
645 } else {
John Grossman44a7e422012-06-21 17:29:24 -0700646 player_type playerType = MediaPlayerFactory::getPlayerType(this, url);
John Grossmanc795b642012-02-22 15:38:35 -0800647 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
648 if (p == NULL) {
649 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800650 }
651
John Grossmanc795b642012-02-22 15:38:35 -0800652 setDataSource_post(p, p->setDataSource(url, headers));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800653 return mStatus;
654 }
655}
656
657status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
658{
Steve Block3856b092011-10-20 11:56:00 +0100659 ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800660 struct stat sb;
661 int ret = fstat(fd, &sb);
662 if (ret != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000663 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800664 return UNKNOWN_ERROR;
665 }
666
Steve Block3856b092011-10-20 11:56:00 +0100667 ALOGV("st_dev = %llu", sb.st_dev);
668 ALOGV("st_mode = %u", sb.st_mode);
669 ALOGV("st_uid = %lu", sb.st_uid);
670 ALOGV("st_gid = %lu", sb.st_gid);
671 ALOGV("st_size = %llu", sb.st_size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800672
673 if (offset >= sb.st_size) {
Steve Block29357bc2012-01-06 19:20:56 +0000674 ALOGE("offset error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800675 ::close(fd);
676 return UNKNOWN_ERROR;
677 }
678 if (offset + length > sb.st_size) {
679 length = sb.st_size - offset;
Steve Block3856b092011-10-20 11:56:00 +0100680 ALOGV("calculated length = %lld", length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800681 }
682
John Grossman44a7e422012-06-21 17:29:24 -0700683 player_type playerType = MediaPlayerFactory::getPlayerType(this,
684 fd,
685 offset,
686 length);
John Grossmanc795b642012-02-22 15:38:35 -0800687 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
688 if (p == NULL) {
689 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800690 }
691
692 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800693 setDataSource_post(p, p->setDataSource(fd, offset, length));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800694 return mStatus;
695}
696
Andreas Hubere2b10282010-11-23 11:41:34 -0800697status_t MediaPlayerService::Client::setDataSource(
698 const sp<IStreamSource> &source) {
699 // create the right type of player
John Grossman44a7e422012-06-21 17:29:24 -0700700 player_type playerType = MediaPlayerFactory::getPlayerType(this, source);
John Grossmanc795b642012-02-22 15:38:35 -0800701 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
Andreas Hubere2b10282010-11-23 11:41:34 -0800702 if (p == NULL) {
703 return NO_INIT;
704 }
705
Andreas Hubere2b10282010-11-23 11:41:34 -0800706 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800707 setDataSource_post(p, p->setDataSource(source));
Andreas Hubere2b10282010-11-23 11:41:34 -0800708 return mStatus;
709}
710
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700711void MediaPlayerService::Client::disconnectNativeWindow() {
712 if (mConnectedWindow != NULL) {
713 status_t err = native_window_api_disconnect(mConnectedWindow.get(),
714 NATIVE_WINDOW_API_MEDIA);
715
716 if (err != OK) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000717 ALOGW("native_window_api_disconnect returned an error: %s (%d)",
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700718 strerror(-err), err);
719 }
720 }
721 mConnectedWindow.clear();
722}
723
Glenn Kasten11731182011-02-08 17:26:17 -0800724status_t MediaPlayerService::Client::setVideoSurfaceTexture(
Andy McFadden8ba01022012-12-18 09:46:54 -0800725 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800726{
Andy McFadden8ba01022012-12-18 09:46:54 -0800727 ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, bufferProducer.get());
Glenn Kasten11731182011-02-08 17:26:17 -0800728 sp<MediaPlayerBase> p = getPlayer();
729 if (p == 0) return UNKNOWN_ERROR;
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700730
Andy McFadden8ba01022012-12-18 09:46:54 -0800731 sp<IBinder> binder(bufferProducer == NULL ? NULL :
732 bufferProducer->asBinder());
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700733 if (mConnectedWindowBinder == binder) {
734 return OK;
735 }
736
737 sp<ANativeWindow> anw;
Andy McFadden8ba01022012-12-18 09:46:54 -0800738 if (bufferProducer != NULL) {
Mathias Agopian1a2952a2013-02-14 17:11:27 -0800739 anw = new Surface(bufferProducer);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700740 status_t err = native_window_api_connect(anw.get(),
741 NATIVE_WINDOW_API_MEDIA);
742
743 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000744 ALOGE("setVideoSurfaceTexture failed: %d", err);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700745 // Note that we must do the reset before disconnecting from the ANW.
746 // Otherwise queue/dequeue calls could be made on the disconnected
747 // ANW, which may result in errors.
748 reset();
749
750 disconnectNativeWindow();
751
752 return err;
753 }
754 }
755
Andy McFadden8ba01022012-12-18 09:46:54 -0800756 // Note that we must set the player's new GraphicBufferProducer before
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700757 // disconnecting the old one. Otherwise queue/dequeue calls could be made
758 // on the disconnected ANW, which may result in errors.
Andy McFadden8ba01022012-12-18 09:46:54 -0800759 status_t err = p->setVideoSurfaceTexture(bufferProducer);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700760
761 disconnectNativeWindow();
762
763 mConnectedWindow = anw;
764
765 if (err == OK) {
766 mConnectedWindowBinder = binder;
767 } else {
768 disconnectNativeWindow();
769 }
770
771 return err;
Glenn Kasten11731182011-02-08 17:26:17 -0800772}
773
Nicolas Catania1d187f12009-05-12 23:25:55 -0700774status_t MediaPlayerService::Client::invoke(const Parcel& request,
775 Parcel *reply)
776{
777 sp<MediaPlayerBase> p = getPlayer();
778 if (p == NULL) return UNKNOWN_ERROR;
779 return p->invoke(request, reply);
780}
781
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700782// This call doesn't need to access the native player.
783status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
784{
785 status_t status;
nikoa64c8c72009-07-20 15:07:26 -0700786 media::Metadata::Filter allow, drop;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700787
Nicolas Catania48290382009-07-10 13:53:06 -0700788 if (unmarshallFilter(filter, &allow, &status) &&
789 unmarshallFilter(filter, &drop, &status)) {
790 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700791
792 mMetadataAllow = allow;
793 mMetadataDrop = drop;
794 }
795 return status;
796}
797
Nicolas Catania48290382009-07-10 13:53:06 -0700798status_t MediaPlayerService::Client::getMetadata(
799 bool update_only, bool apply_filter, Parcel *reply)
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700800{
nikoa64c8c72009-07-20 15:07:26 -0700801 sp<MediaPlayerBase> player = getPlayer();
802 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania48290382009-07-10 13:53:06 -0700803
nikod608a812009-07-16 16:39:53 -0700804 status_t status;
805 // Placeholder for the return code, updated by the caller.
806 reply->writeInt32(-1);
807
nikoa64c8c72009-07-20 15:07:26 -0700808 media::Metadata::Filter ids;
Nicolas Catania48290382009-07-10 13:53:06 -0700809
810 // We don't block notifications while we fetch the data. We clear
811 // mMetadataUpdated first so we don't lose notifications happening
812 // during the rest of this call.
813 {
814 Mutex::Autolock lock(mLock);
815 if (update_only) {
nikod608a812009-07-16 16:39:53 -0700816 ids = mMetadataUpdated;
Nicolas Catania48290382009-07-10 13:53:06 -0700817 }
818 mMetadataUpdated.clear();
819 }
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700820
nikoa64c8c72009-07-20 15:07:26 -0700821 media::Metadata metadata(reply);
Nicolas Catania48290382009-07-10 13:53:06 -0700822
nikoa64c8c72009-07-20 15:07:26 -0700823 metadata.appendHeader();
824 status = player->getMetadata(ids, reply);
nikod608a812009-07-16 16:39:53 -0700825
826 if (status != OK) {
nikoa64c8c72009-07-20 15:07:26 -0700827 metadata.resetParcel();
Steve Block29357bc2012-01-06 19:20:56 +0000828 ALOGE("getMetadata failed %d", status);
nikod608a812009-07-16 16:39:53 -0700829 return status;
830 }
831
832 // FIXME: Implement filtering on the result. Not critical since
833 // filtering takes place on the update notifications already. This
834 // would be when all the metadata are fetch and a filter is set.
835
nikod608a812009-07-16 16:39:53 -0700836 // Everything is fine, update the metadata length.
nikoa64c8c72009-07-20 15:07:26 -0700837 metadata.updateLength();
nikod608a812009-07-16 16:39:53 -0700838 return OK;
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700839}
840
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800841status_t MediaPlayerService::Client::prepareAsync()
842{
Steve Block3856b092011-10-20 11:56:00 +0100843 ALOGV("[%d] prepareAsync", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800844 sp<MediaPlayerBase> p = getPlayer();
845 if (p == 0) return UNKNOWN_ERROR;
846 status_t ret = p->prepareAsync();
847#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000848 ALOGD("start Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800849 if (ret == NO_ERROR) mAntagonizer->start();
850#endif
851 return ret;
852}
853
854status_t MediaPlayerService::Client::start()
855{
Steve Block3856b092011-10-20 11:56:00 +0100856 ALOGV("[%d] start", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800857 sp<MediaPlayerBase> p = getPlayer();
858 if (p == 0) return UNKNOWN_ERROR;
859 p->setLooping(mLoop);
860 return p->start();
861}
862
863status_t MediaPlayerService::Client::stop()
864{
Steve Block3856b092011-10-20 11:56:00 +0100865 ALOGV("[%d] stop", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800866 sp<MediaPlayerBase> p = getPlayer();
867 if (p == 0) return UNKNOWN_ERROR;
868 return p->stop();
869}
870
871status_t MediaPlayerService::Client::pause()
872{
Steve Block3856b092011-10-20 11:56:00 +0100873 ALOGV("[%d] pause", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800874 sp<MediaPlayerBase> p = getPlayer();
875 if (p == 0) return UNKNOWN_ERROR;
876 return p->pause();
877}
878
879status_t MediaPlayerService::Client::isPlaying(bool* state)
880{
881 *state = false;
882 sp<MediaPlayerBase> p = getPlayer();
883 if (p == 0) return UNKNOWN_ERROR;
884 *state = p->isPlaying();
Steve Block3856b092011-10-20 11:56:00 +0100885 ALOGV("[%d] isPlaying: %d", mConnId, *state);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800886 return NO_ERROR;
887}
888
889status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
890{
Steve Block3856b092011-10-20 11:56:00 +0100891 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800892 sp<MediaPlayerBase> p = getPlayer();
893 if (p == 0) return UNKNOWN_ERROR;
894 status_t ret = p->getCurrentPosition(msec);
895 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100896 ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800897 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000898 ALOGE("getCurrentPosition returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800899 }
900 return ret;
901}
902
903status_t MediaPlayerService::Client::getDuration(int *msec)
904{
Steve Block3856b092011-10-20 11:56:00 +0100905 ALOGV("getDuration");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800906 sp<MediaPlayerBase> p = getPlayer();
907 if (p == 0) return UNKNOWN_ERROR;
908 status_t ret = p->getDuration(msec);
909 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100910 ALOGV("[%d] getDuration = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800911 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000912 ALOGE("getDuration returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800913 }
914 return ret;
915}
916
Marco Nelissen6b74d672012-02-28 16:07:44 -0800917status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) {
918 ALOGV("setNextPlayer");
919 Mutex::Autolock l(mLock);
920 sp<Client> c = static_cast<Client*>(player.get());
921 mNextClient = c;
John Grossman2e5f22e2012-08-24 14:47:25 -0700922
923 if (c != NULL) {
924 if (mAudioOutput != NULL) {
925 mAudioOutput->setNextOutput(c->mAudioOutput);
926 } else if ((mPlayer != NULL) && !mPlayer->hardwareOutput()) {
927 ALOGE("no current audio output");
928 }
929
930 if ((mPlayer != NULL) && (mNextClient->getPlayer() != NULL)) {
931 mPlayer->setNextPlayer(mNextClient->getPlayer());
932 }
Marco Nelissen6b74d672012-02-28 16:07:44 -0800933 }
John Grossman2e5f22e2012-08-24 14:47:25 -0700934
Marco Nelissen6b74d672012-02-28 16:07:44 -0800935 return OK;
936}
937
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800938status_t MediaPlayerService::Client::seekTo(int msec)
939{
Steve Block3856b092011-10-20 11:56:00 +0100940 ALOGV("[%d] seekTo(%d)", mConnId, msec);
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->seekTo(msec);
944}
945
946status_t MediaPlayerService::Client::reset()
947{
Steve Block3856b092011-10-20 11:56:00 +0100948 ALOGV("[%d] reset", mConnId);
John Grossmanc795b642012-02-22 15:38:35 -0800949 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800950 sp<MediaPlayerBase> p = getPlayer();
951 if (p == 0) return UNKNOWN_ERROR;
952 return p->reset();
953}
954
Glenn Kastenfff6d712012-01-12 16:38:12 -0800955status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800956{
Steve Block3856b092011-10-20 11:56:00 +0100957 ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800958 // TODO: for hardware output, call player instead
959 Mutex::Autolock l(mLock);
960 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
961 return NO_ERROR;
962}
963
964status_t MediaPlayerService::Client::setLooping(int loop)
965{
Steve Block3856b092011-10-20 11:56:00 +0100966 ALOGV("[%d] setLooping(%d)", mConnId, loop);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800967 mLoop = loop;
968 sp<MediaPlayerBase> p = getPlayer();
969 if (p != 0) return p->setLooping(loop);
970 return NO_ERROR;
971}
972
973status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
974{
Steve Block3856b092011-10-20 11:56:00 +0100975 ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
John Grossman761defc2012-02-09 15:09:05 -0800976
977 // for hardware output, call player instead
978 sp<MediaPlayerBase> p = getPlayer();
979 {
980 Mutex::Autolock l(mLock);
981 if (p != 0 && p->hardwareOutput()) {
982 MediaPlayerHWInterface* hwp =
983 reinterpret_cast<MediaPlayerHWInterface*>(p.get());
984 return hwp->setVolume(leftVolume, rightVolume);
985 } else {
986 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
987 return NO_ERROR;
988 }
989 }
990
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800991 return NO_ERROR;
992}
993
Eric Laurent2beeb502010-07-16 07:43:46 -0700994status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
995{
Steve Block3856b092011-10-20 11:56:00 +0100996 ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
Eric Laurent2beeb502010-07-16 07:43:46 -0700997 Mutex::Autolock l(mLock);
998 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
999 return NO_ERROR;
1000}
1001
1002status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
1003{
Steve Block3856b092011-10-20 11:56:00 +01001004 ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001005 Mutex::Autolock l(mLock);
1006 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1007 return NO_ERROR;
1008}
Nicolas Catania48290382009-07-10 13:53:06 -07001009
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001010status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
Steve Block3856b092011-10-20 11:56:00 +01001011 ALOGV("[%d] setParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001012 sp<MediaPlayerBase> p = getPlayer();
1013 if (p == 0) return UNKNOWN_ERROR;
1014 return p->setParameter(key, request);
1015}
1016
1017status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
Steve Block3856b092011-10-20 11:56:00 +01001018 ALOGV("[%d] getParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001019 sp<MediaPlayerBase> p = getPlayer();
1020 if (p == 0) return UNKNOWN_ERROR;
1021 return p->getParameter(key, reply);
1022}
1023
John Grossmanc795b642012-02-22 15:38:35 -08001024status_t MediaPlayerService::Client::setRetransmitEndpoint(
1025 const struct sockaddr_in* endpoint) {
1026
1027 if (NULL != endpoint) {
1028 uint32_t a = ntohl(endpoint->sin_addr.s_addr);
1029 uint16_t p = ntohs(endpoint->sin_port);
1030 ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId,
1031 (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p);
1032 } else {
1033 ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId);
1034 }
1035
1036 sp<MediaPlayerBase> p = getPlayer();
1037
1038 // Right now, the only valid time to set a retransmit endpoint is before
1039 // player selection has been made (since the presence or absence of a
1040 // retransmit endpoint is going to determine which player is selected during
1041 // setDataSource).
1042 if (p != 0) return INVALID_OPERATION;
1043
1044 if (NULL != endpoint) {
1045 mRetransmitEndpoint = *endpoint;
1046 mRetransmitEndpointValid = true;
1047 } else {
1048 mRetransmitEndpointValid = false;
1049 }
1050
1051 return NO_ERROR;
1052}
1053
John Grossman44a7e422012-06-21 17:29:24 -07001054status_t MediaPlayerService::Client::getRetransmitEndpoint(
1055 struct sockaddr_in* endpoint)
1056{
1057 if (NULL == endpoint)
1058 return BAD_VALUE;
1059
1060 sp<MediaPlayerBase> p = getPlayer();
1061
1062 if (p != NULL)
1063 return p->getRetransmitEndpoint(endpoint);
1064
1065 if (!mRetransmitEndpointValid)
1066 return NO_INIT;
1067
1068 *endpoint = mRetransmitEndpoint;
1069
1070 return NO_ERROR;
1071}
1072
Gloria Wangb483c472011-04-11 17:23:27 -07001073void MediaPlayerService::Client::notify(
1074 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001075{
1076 Client* client = static_cast<Client*>(cookie);
James Dongb8a98252012-08-26 16:13:03 -07001077 if (client == NULL) {
1078 return;
1079 }
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001080
James Dongb8a98252012-08-26 16:13:03 -07001081 sp<IMediaPlayerClient> c;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001082 {
1083 Mutex::Autolock l(client->mLock);
James Dongb8a98252012-08-26 16:13:03 -07001084 c = client->mClient;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001085 if (msg == MEDIA_PLAYBACK_COMPLETE && client->mNextClient != NULL) {
John Grossmancb0b7552012-08-23 17:47:31 -07001086 if (client->mAudioOutput != NULL)
1087 client->mAudioOutput->switchToNextOutput();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001088 client->mNextClient->start();
1089 client->mNextClient->mClient->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj);
1090 }
1091 }
1092
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001093 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001094 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001095 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-07-10 13:53:06 -07001096
1097 if(client->shouldDropMetadata(metadata_type)) {
1098 return;
1099 }
1100
1101 // Update the list of metadata that have changed. getMetadata
1102 // also access mMetadataUpdated and clears it.
1103 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001104 }
James Dongb8a98252012-08-26 16:13:03 -07001105
1106 if (c != NULL) {
1107 ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
1108 c->notify(msg, ext1, ext2, obj);
1109 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001110}
1111
Nicolas Catania48290382009-07-10 13:53:06 -07001112
nikoa64c8c72009-07-20 15:07:26 -07001113bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001114{
Nicolas Catania48290382009-07-10 13:53:06 -07001115 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001116
Nicolas Catania48290382009-07-10 13:53:06 -07001117 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001118 return true;
1119 }
1120
Nicolas Catania48290382009-07-10 13:53:06 -07001121 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001122 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001123 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001124 return true;
1125 }
1126}
1127
Nicolas Catania48290382009-07-10 13:53:06 -07001128
nikoa64c8c72009-07-20 15:07:26 -07001129void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-07-10 13:53:06 -07001130 Mutex::Autolock lock(mLock);
1131 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1132 mMetadataUpdated.add(metadata_type);
1133 }
1134}
1135
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001136#if CALLBACK_ANTAGONIZER
1137const int Antagonizer::interval = 10000; // 10 msecs
1138
1139Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1140 mExit(false), mActive(false), mClient(client), mCb(cb)
1141{
1142 createThread(callbackThread, this);
1143}
1144
1145void Antagonizer::kill()
1146{
1147 Mutex::Autolock _l(mLock);
1148 mActive = false;
1149 mExit = true;
1150 mCondition.wait(mLock);
1151}
1152
1153int Antagonizer::callbackThread(void* user)
1154{
Steve Blockb8a80522011-12-20 16:23:08 +00001155 ALOGD("Antagonizer started");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001156 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1157 while (!p->mExit) {
1158 if (p->mActive) {
Steve Block3856b092011-10-20 11:56:00 +01001159 ALOGV("send event");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001160 p->mCb(p->mClient, 0, 0, 0);
1161 }
1162 usleep(interval);
1163 }
1164 Mutex::Autolock _l(p->mLock);
1165 p->mCondition.signal();
Steve Blockb8a80522011-12-20 16:23:08 +00001166 ALOGD("Antagonizer stopped");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001167 return 0;
1168}
1169#endif
1170
1171static size_t kDefaultHeapSize = 1024 * 1024; // 1MB
1172
Glenn Kastene1c39622012-01-04 09:36:37 -08001173sp<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 -08001174{
Steve Block3856b092011-10-20 11:56:00 +01001175 ALOGV("decode(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001176 sp<MemoryBase> mem;
1177 sp<MediaPlayerBase> player;
1178
1179 // Protect our precious, precious DRMd ringtones by only allowing
1180 // decoding of http, but not filesystem paths or content Uris.
1181 // If the application wants to decode those, it should open a
1182 // filedescriptor for them and use that.
1183 if (url != NULL && strncmp(url, "http://", 7) != 0) {
Steve Blockb8a80522011-12-20 16:23:08 +00001184 ALOGD("Can't decode %s by path, use filedescriptor instead", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001185 return mem;
1186 }
1187
John Grossman44a7e422012-06-21 17:29:24 -07001188 player_type playerType =
1189 MediaPlayerFactory::getPlayerType(NULL /* client */, url);
Steve Block3856b092011-10-20 11:56:00 +01001190 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001191
1192 // create the right type of player
1193 sp<AudioCache> cache = new AudioCache(url);
John Grossman44a7e422012-06-21 17:29:24 -07001194 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001195 if (player == NULL) goto Exit;
1196 if (player->hardwareOutput()) goto Exit;
1197
1198 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1199
1200 // set data source
1201 if (player->setDataSource(url) != NO_ERROR) goto Exit;
1202
Steve Block3856b092011-10-20 11:56:00 +01001203 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001204 player->prepareAsync();
1205
Steve Block3856b092011-10-20 11:56:00 +01001206 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001207 if (cache->wait() != NO_ERROR) goto Exit;
1208
Steve Block3856b092011-10-20 11:56:00 +01001209 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001210 player->start();
1211
Steve Block3856b092011-10-20 11:56:00 +01001212 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001213 cache->wait();
1214 // in case of error, return what was successfully decoded.
1215 if (cache->size() == 0) {
1216 goto Exit;
1217 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001218
1219 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1220 *pSampleRate = cache->sampleRate();
1221 *pNumChannels = cache->channelCount();
Glenn Kastene1c39622012-01-04 09:36:37 -08001222 *pFormat = cache->format();
Steve Block3856b092011-10-20 11:56:00 +01001223 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 -08001224
1225Exit:
1226 if (player != 0) player->reset();
1227 return mem;
1228}
1229
Glenn Kastene1c39622012-01-04 09:36:37 -08001230sp<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 -08001231{
Steve Block3856b092011-10-20 11:56:00 +01001232 ALOGV("decode(%d, %lld, %lld)", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001233 sp<MemoryBase> mem;
1234 sp<MediaPlayerBase> player;
1235
John Grossman44a7e422012-06-21 17:29:24 -07001236 player_type playerType = MediaPlayerFactory::getPlayerType(NULL /* client */,
1237 fd,
1238 offset,
1239 length);
Steve Block3856b092011-10-20 11:56:00 +01001240 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001241
1242 // create the right type of player
1243 sp<AudioCache> cache = new AudioCache("decode_fd");
John Grossman44a7e422012-06-21 17:29:24 -07001244 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001245 if (player == NULL) goto Exit;
1246 if (player->hardwareOutput()) goto Exit;
1247
1248 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1249
1250 // set data source
1251 if (player->setDataSource(fd, offset, length) != NO_ERROR) goto Exit;
1252
Steve Block3856b092011-10-20 11:56:00 +01001253 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001254 player->prepareAsync();
1255
Steve Block3856b092011-10-20 11:56:00 +01001256 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001257 if (cache->wait() != NO_ERROR) goto Exit;
1258
Steve Block3856b092011-10-20 11:56:00 +01001259 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001260 player->start();
1261
Steve Block3856b092011-10-20 11:56:00 +01001262 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001263 cache->wait();
1264 // in case of error, return what was successfully decoded.
1265 if (cache->size() == 0) {
1266 goto Exit;
1267 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001268
1269 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1270 *pSampleRate = cache->sampleRate();
1271 *pNumChannels = cache->channelCount();
1272 *pFormat = cache->format();
Steve Block3856b092011-10-20 11:56:00 +01001273 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 -08001274
1275Exit:
1276 if (player != 0) player->reset();
1277 ::close(fd);
1278 return mem;
1279}
1280
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001281
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001282#undef LOG_TAG
1283#define LOG_TAG "AudioSink"
Eric Laurenta514bdb2010-06-21 09:27:30 -07001284MediaPlayerService::AudioOutput::AudioOutput(int sessionId)
Andreas Huber20111aa2009-07-14 16:56:47 -07001285 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001286 mCallbackCookie(NULL),
Marco Nelissen6b74d672012-02-28 16:07:44 -08001287 mCallbackData(NULL),
Marco Nelissen4110c102012-03-29 09:31:28 -07001288 mBytesWritten(0),
Eric Laurent1948eb32012-04-13 16:50:19 -07001289 mSessionId(sessionId),
1290 mFlags(AUDIO_OUTPUT_FLAG_NONE) {
Steve Block3856b092011-10-20 11:56:00 +01001291 ALOGV("AudioOutput(%d)", sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001292 mTrack = 0;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001293 mRecycledTrack = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -07001294 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001295 mLeftVolume = 1.0;
1296 mRightVolume = 1.0;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001297 mPlaybackRatePermille = 1000;
1298 mSampleRateHz = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001299 mMsecsPerFrame = 0;
Eric Laurent2beeb502010-07-16 07:43:46 -07001300 mAuxEffectId = 0;
1301 mSendLevel = 0.0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001302 setMinBufferCount();
1303}
1304
1305MediaPlayerService::AudioOutput::~AudioOutput()
1306{
1307 close();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001308 delete mRecycledTrack;
1309 delete mCallbackData;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001310}
1311
1312void MediaPlayerService::AudioOutput::setMinBufferCount()
1313{
1314 char value[PROPERTY_VALUE_MAX];
1315 if (property_get("ro.kernel.qemu", value, 0)) {
1316 mIsOnEmulator = true;
1317 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1318 }
1319}
1320
1321bool MediaPlayerService::AudioOutput::isOnEmulator()
1322{
1323 setMinBufferCount();
1324 return mIsOnEmulator;
1325}
1326
1327int MediaPlayerService::AudioOutput::getMinBufferCount()
1328{
1329 setMinBufferCount();
1330 return mMinBufferCount;
1331}
1332
1333ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1334{
1335 if (mTrack == 0) return NO_INIT;
1336 return mTrack->frameCount() * frameSize();
1337}
1338
1339ssize_t MediaPlayerService::AudioOutput::frameCount() const
1340{
1341 if (mTrack == 0) return NO_INIT;
1342 return mTrack->frameCount();
1343}
1344
1345ssize_t MediaPlayerService::AudioOutput::channelCount() const
1346{
1347 if (mTrack == 0) return NO_INIT;
1348 return mTrack->channelCount();
1349}
1350
1351ssize_t MediaPlayerService::AudioOutput::frameSize() const
1352{
1353 if (mTrack == 0) return NO_INIT;
1354 return mTrack->frameSize();
1355}
1356
1357uint32_t MediaPlayerService::AudioOutput::latency () const
1358{
Eric Laurentdb354e52012-03-05 17:27:11 -08001359 if (mTrack == 0) return 0;
1360 return mTrack->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001361}
1362
1363float MediaPlayerService::AudioOutput::msecsPerFrame() const
1364{
1365 return mMsecsPerFrame;
1366}
1367
Marco Nelissen4110c102012-03-29 09:31:28 -07001368status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001369{
1370 if (mTrack == 0) return NO_INIT;
1371 return mTrack->getPosition(position);
1372}
1373
Marco Nelissen4110c102012-03-29 09:31:28 -07001374status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
1375{
1376 if (mTrack == 0) return NO_INIT;
1377 *frameswritten = mBytesWritten / frameSize();
1378 return OK;
1379}
1380
Andreas Huber20111aa2009-07-14 16:56:47 -07001381status_t MediaPlayerService::AudioOutput::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001382 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1383 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001384 AudioCallback cb, void *cookie,
1385 audio_output_flags_t flags)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001386{
Andreas Huber20111aa2009-07-14 16:56:47 -07001387 mCallback = cb;
1388 mCallbackCookie = cookie;
1389
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001390 // Check argument "bufferCount" against the mininum buffer count
1391 if (bufferCount < mMinBufferCount) {
Steve Blockb8a80522011-12-20 16:23:08 +00001392 ALOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001393 bufferCount = mMinBufferCount;
1394
1395 }
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001396 ALOGV("open(%u, %d, 0x%x, %d, %d, %d)", sampleRate, channelCount, channelMask,
1397 format, bufferCount, mSessionId);
Glenn Kasten3b16c762012-11-14 08:44:39 -08001398 uint32_t afSampleRate;
Glenn Kastene33054e2012-11-14 12:54:39 -08001399 size_t afFrameCount;
Eric Laurent1948eb32012-04-13 16:50:19 -07001400 uint32_t frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001401
1402 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1403 return NO_INIT;
1404 }
1405 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1406 return NO_INIT;
1407 }
1408
1409 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
Andreas Huber20111aa2009-07-14 16:56:47 -07001410
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001411 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
Glenn Kastenab334fd2012-03-14 12:56:06 -07001412 channelMask = audio_channel_out_mask_from_count(channelCount);
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001413 if (0 == channelMask) {
1414 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
1415 return NO_INIT;
1416 }
1417 }
Eric Laurent1948eb32012-04-13 16:50:19 -07001418
Andreas Huber20111aa2009-07-14 16:56:47 -07001419 AudioTrack *t;
Marco Nelissen67295b52012-06-11 14:52:53 -07001420 CallbackData *newcbd = NULL;
Andreas Huber20111aa2009-07-14 16:56:47 -07001421 if (mCallback != NULL) {
Marco Nelissen67295b52012-06-11 14:52:53 -07001422 newcbd = new CallbackData(this);
Andreas Huber20111aa2009-07-14 16:56:47 -07001423 t = new AudioTrack(
Eric Laurentc2f1f072009-07-17 12:17:14 -07001424 mStreamType,
1425 sampleRate,
1426 format,
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001427 channelMask,
Eric Laurentc2f1f072009-07-17 12:17:14 -07001428 frameCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001429 flags,
Eric Laurentc2f1f072009-07-17 12:17:14 -07001430 CallbackWrapper,
Marco Nelissen67295b52012-06-11 14:52:53 -07001431 newcbd,
Glenn Kasten17a736c2012-02-14 08:52:15 -08001432 0, // notification frames
Eric Laurenta514bdb2010-06-21 09:27:30 -07001433 mSessionId);
Andreas Huber20111aa2009-07-14 16:56:47 -07001434 } else {
1435 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 Laurenta514bdb2010-06-21 09:27:30 -07001440 frameCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001441 flags,
Eric Laurenta514bdb2010-06-21 09:27:30 -07001442 NULL,
1443 NULL,
1444 0,
1445 mSessionId);
Andreas Huber20111aa2009-07-14 16:56:47 -07001446 }
1447
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001448 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
Steve Block29357bc2012-01-06 19:20:56 +00001449 ALOGE("Unable to create audio track");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001450 delete t;
Marco Nelissen67295b52012-06-11 14:52:53 -07001451 delete newcbd;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001452 return NO_INIT;
1453 }
1454
Marco Nelissen67295b52012-06-11 14:52:53 -07001455
1456 if (mRecycledTrack) {
1457 // check if the existing track can be reused as-is, or if a new track needs to be created.
1458
1459 bool reuse = true;
1460 if ((mCallbackData == NULL && mCallback != NULL) ||
1461 (mCallbackData != NULL && mCallback == NULL)) {
1462 // recycled track uses callbacks but the caller wants to use writes, or vice versa
1463 ALOGV("can't chain callback and write");
1464 reuse = false;
1465 } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
1466 (mRecycledTrack->channelCount() != channelCount) ||
1467 (mRecycledTrack->frameCount() != t->frameCount())) {
1468 ALOGV("samplerate, channelcount or framecount differ: %d/%d Hz, %d/%d ch, %d/%d frames",
1469 mRecycledTrack->getSampleRate(), sampleRate,
1470 mRecycledTrack->channelCount(), channelCount,
1471 mRecycledTrack->frameCount(), t->frameCount());
1472 reuse = false;
1473 } else if (flags != mFlags) {
1474 ALOGV("output flags differ %08x/%08x", flags, mFlags);
1475 reuse = false;
1476 }
1477 if (reuse) {
1478 ALOGV("chaining to next output");
1479 close();
1480 mTrack = mRecycledTrack;
1481 mRecycledTrack = NULL;
1482 if (mCallbackData != NULL) {
1483 mCallbackData->setOutput(this);
1484 }
1485 delete t;
1486 delete newcbd;
1487 return OK;
1488 }
1489
1490 // if we're not going to reuse the track, unblock and flush it
1491 if (mCallbackData != NULL) {
1492 mCallbackData->setOutput(NULL);
1493 mCallbackData->endTrackSwitch();
1494 }
1495 mRecycledTrack->flush();
1496 delete mRecycledTrack;
1497 mRecycledTrack = NULL;
1498 delete mCallbackData;
1499 mCallbackData = NULL;
1500 close();
1501 }
1502
1503 mCallbackData = newcbd;
Steve Block3856b092011-10-20 11:56:00 +01001504 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001505 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001506
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001507 mSampleRateHz = sampleRate;
Eric Laurent1948eb32012-04-13 16:50:19 -07001508 mFlags = flags;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001509 mMsecsPerFrame = mPlaybackRatePermille / (float) sampleRate;
Marco Nelissen99448602012-04-02 12:16:49 -07001510 uint32_t pos;
1511 if (t->getPosition(&pos) == OK) {
1512 mBytesWritten = uint64_t(pos) * t->frameSize();
1513 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001514 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001515
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001516 status_t res = t->setSampleRate(mPlaybackRatePermille * mSampleRateHz / 1000);
1517 if (res != NO_ERROR) {
1518 return res;
1519 }
Eric Laurent2beeb502010-07-16 07:43:46 -07001520 t->setAuxEffectSendLevel(mSendLevel);
1521 return t->attachAuxEffect(mAuxEffectId);;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001522}
1523
1524void MediaPlayerService::AudioOutput::start()
1525{
Steve Block3856b092011-10-20 11:56:00 +01001526 ALOGV("start");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001527 if (mCallbackData != NULL) {
1528 mCallbackData->endTrackSwitch();
1529 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001530 if (mTrack) {
1531 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001532 mTrack->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001533 mTrack->start();
1534 }
1535}
1536
Marco Nelissen6b74d672012-02-28 16:07:44 -08001537void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
1538 mNextOutput = nextOutput;
1539}
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001540
1541
Marco Nelissen6b74d672012-02-28 16:07:44 -08001542void MediaPlayerService::AudioOutput::switchToNextOutput() {
1543 ALOGV("switchToNextOutput");
1544 if (mNextOutput != NULL) {
1545 if (mCallbackData != NULL) {
1546 mCallbackData->beginTrackSwitch();
1547 }
1548 delete mNextOutput->mCallbackData;
1549 mNextOutput->mCallbackData = mCallbackData;
1550 mCallbackData = NULL;
1551 mNextOutput->mRecycledTrack = mTrack;
1552 mTrack = NULL;
1553 mNextOutput->mSampleRateHz = mSampleRateHz;
1554 mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
Marco Nelissen4110c102012-03-29 09:31:28 -07001555 mNextOutput->mBytesWritten = mBytesWritten;
Marco Nelissend791e092012-06-11 17:00:59 -07001556 mNextOutput->mFlags = mFlags;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001557 }
1558}
1559
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001560ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size)
1561{
Andreas Huber20111aa2009-07-14 16:56:47 -07001562 LOG_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
1563
Steve Block3856b092011-10-20 11:56:00 +01001564 //ALOGV("write(%p, %u)", buffer, size);
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001565 if (mTrack) {
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001566 ssize_t ret = mTrack->write(buffer, size);
Marco Nelissen4110c102012-03-29 09:31:28 -07001567 mBytesWritten += ret;
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001568 return ret;
1569 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001570 return NO_INIT;
1571}
1572
1573void MediaPlayerService::AudioOutput::stop()
1574{
Steve Block3856b092011-10-20 11:56:00 +01001575 ALOGV("stop");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001576 if (mTrack) mTrack->stop();
1577}
1578
1579void MediaPlayerService::AudioOutput::flush()
1580{
Steve Block3856b092011-10-20 11:56:00 +01001581 ALOGV("flush");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001582 if (mTrack) mTrack->flush();
1583}
1584
1585void MediaPlayerService::AudioOutput::pause()
1586{
Steve Block3856b092011-10-20 11:56:00 +01001587 ALOGV("pause");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001588 if (mTrack) mTrack->pause();
1589}
1590
1591void MediaPlayerService::AudioOutput::close()
1592{
Steve Block3856b092011-10-20 11:56:00 +01001593 ALOGV("close");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001594 delete mTrack;
1595 mTrack = 0;
1596}
1597
1598void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1599{
Steve Block3856b092011-10-20 11:56:00 +01001600 ALOGV("setVolume(%f, %f)", left, right);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001601 mLeftVolume = left;
1602 mRightVolume = right;
1603 if (mTrack) {
1604 mTrack->setVolume(left, right);
1605 }
1606}
1607
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001608status_t MediaPlayerService::AudioOutput::setPlaybackRatePermille(int32_t ratePermille)
1609{
1610 ALOGV("setPlaybackRatePermille(%d)", ratePermille);
1611 status_t res = NO_ERROR;
1612 if (mTrack) {
1613 res = mTrack->setSampleRate(ratePermille * mSampleRateHz / 1000);
1614 } else {
1615 res = NO_INIT;
1616 }
1617 mPlaybackRatePermille = ratePermille;
1618 if (mSampleRateHz != 0) {
1619 mMsecsPerFrame = mPlaybackRatePermille / (float) mSampleRateHz;
1620 }
1621 return res;
1622}
1623
Eric Laurent2beeb502010-07-16 07:43:46 -07001624status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1625{
Steve Block3856b092011-10-20 11:56:00 +01001626 ALOGV("setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001627 mSendLevel = level;
1628 if (mTrack) {
1629 return mTrack->setAuxEffectSendLevel(level);
1630 }
1631 return NO_ERROR;
1632}
1633
1634status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1635{
Steve Block3856b092011-10-20 11:56:00 +01001636 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001637 mAuxEffectId = effectId;
1638 if (mTrack) {
1639 return mTrack->attachAuxEffect(effectId);
1640 }
1641 return NO_ERROR;
1642}
1643
Andreas Huber20111aa2009-07-14 16:56:47 -07001644// static
1645void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastend217a8c2011-06-01 15:20:35 -07001646 int event, void *cookie, void *info) {
Steve Block3856b092011-10-20 11:56:00 +01001647 //ALOGV("callbackwrapper");
Andreas Huber20111aa2009-07-14 16:56:47 -07001648 if (event != AudioTrack::EVENT_MORE_DATA) {
1649 return;
1650 }
1651
Marco Nelissen6b74d672012-02-28 16:07:44 -08001652 CallbackData *data = (CallbackData*)cookie;
1653 data->lock();
1654 AudioOutput *me = data->getOutput();
Andreas Huber20111aa2009-07-14 16:56:47 -07001655 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001656 if (me == NULL) {
1657 // no output set, likely because the track was scheduled to be reused
1658 // by another player, but the format turned out to be incompatible.
1659 data->unlock();
1660 buffer->size = 0;
1661 return;
1662 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001663
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001664 size_t actualSize = (*me->mCallback)(
Andreas Huber20111aa2009-07-14 16:56:47 -07001665 me, buffer->raw, buffer->size, me->mCallbackCookie);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001666
Marco Nelissen6b74d672012-02-28 16:07:44 -08001667 if (actualSize == 0 && buffer->size > 0 && me->mNextOutput == NULL) {
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001668 // We've reached EOS but the audio track is not stopped yet,
1669 // keep playing silence.
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08001670
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001671 memset(buffer->raw, 0, buffer->size);
1672 actualSize = buffer->size;
1673 }
1674
1675 buffer->size = actualSize;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001676 data->unlock();
Andreas Huber20111aa2009-07-14 16:56:47 -07001677}
1678
Marco Nelissen4110c102012-03-29 09:31:28 -07001679int MediaPlayerService::AudioOutput::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001680{
1681 return mSessionId;
1682}
1683
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001684#undef LOG_TAG
1685#define LOG_TAG "AudioCache"
1686MediaPlayerService::AudioCache::AudioCache(const char* name) :
1687 mChannelCount(0), mFrameCount(1024), mSampleRate(0), mSize(0),
1688 mError(NO_ERROR), mCommandComplete(false)
1689{
1690 // create ashmem heap
1691 mHeap = new MemoryHeapBase(kDefaultHeapSize, 0, name);
1692}
1693
1694uint32_t MediaPlayerService::AudioCache::latency () const
1695{
1696 return 0;
1697}
1698
1699float MediaPlayerService::AudioCache::msecsPerFrame() const
1700{
1701 return mMsecsPerFrame;
1702}
1703
Marco Nelissen4110c102012-03-29 09:31:28 -07001704status_t MediaPlayerService::AudioCache::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001705{
1706 if (position == 0) return BAD_VALUE;
1707 *position = mSize;
1708 return NO_ERROR;
1709}
1710
Marco Nelissen4110c102012-03-29 09:31:28 -07001711status_t MediaPlayerService::AudioCache::getFramesWritten(uint32_t *written) const
1712{
1713 if (written == 0) return BAD_VALUE;
1714 *written = mSize;
1715 return NO_ERROR;
1716}
1717
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001718////////////////////////////////////////////////////////////////////////////////
1719
1720struct CallbackThread : public Thread {
1721 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1722 MediaPlayerBase::AudioSink::AudioCallback cb,
1723 void *cookie);
1724
1725protected:
1726 virtual ~CallbackThread();
1727
1728 virtual bool threadLoop();
1729
1730private:
1731 wp<MediaPlayerBase::AudioSink> mSink;
1732 MediaPlayerBase::AudioSink::AudioCallback mCallback;
1733 void *mCookie;
1734 void *mBuffer;
1735 size_t mBufferSize;
1736
1737 CallbackThread(const CallbackThread &);
1738 CallbackThread &operator=(const CallbackThread &);
1739};
1740
1741CallbackThread::CallbackThread(
1742 const wp<MediaPlayerBase::AudioSink> &sink,
1743 MediaPlayerBase::AudioSink::AudioCallback cb,
1744 void *cookie)
1745 : mSink(sink),
1746 mCallback(cb),
1747 mCookie(cookie),
1748 mBuffer(NULL),
1749 mBufferSize(0) {
1750}
1751
1752CallbackThread::~CallbackThread() {
1753 if (mBuffer) {
1754 free(mBuffer);
1755 mBuffer = NULL;
1756 }
1757}
1758
1759bool CallbackThread::threadLoop() {
1760 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
1761 if (sink == NULL) {
1762 return false;
1763 }
1764
1765 if (mBuffer == NULL) {
1766 mBufferSize = sink->bufferSize();
1767 mBuffer = malloc(mBufferSize);
1768 }
1769
1770 size_t actualSize =
1771 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie);
1772
1773 if (actualSize > 0) {
1774 sink->write(mBuffer, actualSize);
1775 }
1776
1777 return true;
1778}
1779
1780////////////////////////////////////////////////////////////////////////////////
1781
Andreas Huber20111aa2009-07-14 16:56:47 -07001782status_t MediaPlayerService::AudioCache::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001783 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1784 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001785 AudioCallback cb, void *cookie, audio_output_flags_t flags)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001786{
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001787 ALOGV("open(%u, %d, 0x%x, %d, %d)", sampleRate, channelCount, channelMask, format, bufferCount);
Dave Sparks8eb80112009-12-09 20:20:26 -08001788 if (mHeap->getHeapID() < 0) {
1789 return NO_INIT;
1790 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001791
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001792 mSampleRate = sampleRate;
1793 mChannelCount = (uint16_t)channelCount;
Glenn Kastene1c39622012-01-04 09:36:37 -08001794 mFormat = format;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001795 mMsecsPerFrame = 1.e3 / (float) sampleRate;
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001796
1797 if (cb != NULL) {
1798 mCallbackThread = new CallbackThread(this, cb, cookie);
1799 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001800 return NO_ERROR;
1801}
1802
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001803void MediaPlayerService::AudioCache::start() {
1804 if (mCallbackThread != NULL) {
1805 mCallbackThread->run("AudioCache callback");
1806 }
1807}
1808
1809void MediaPlayerService::AudioCache::stop() {
1810 if (mCallbackThread != NULL) {
1811 mCallbackThread->requestExitAndWait();
1812 }
1813}
1814
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001815ssize_t MediaPlayerService::AudioCache::write(const void* buffer, size_t size)
1816{
Steve Block3856b092011-10-20 11:56:00 +01001817 ALOGV("write(%p, %u)", buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001818 if ((buffer == 0) || (size == 0)) return size;
1819
1820 uint8_t* p = static_cast<uint8_t*>(mHeap->getBase());
1821 if (p == NULL) return NO_INIT;
1822 p += mSize;
Steve Block3856b092011-10-20 11:56:00 +01001823 ALOGV("memcpy(%p, %p, %u)", p, buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001824 if (mSize + size > mHeap->getSize()) {
Steve Block29357bc2012-01-06 19:20:56 +00001825 ALOGE("Heap size overflow! req size: %d, max size: %d", (mSize + size), mHeap->getSize());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001826 size = mHeap->getSize() - mSize;
1827 }
1828 memcpy(p, buffer, size);
1829 mSize += size;
1830 return size;
1831}
1832
1833// call with lock held
1834status_t MediaPlayerService::AudioCache::wait()
1835{
1836 Mutex::Autolock lock(mLock);
Dave Sparks4bbc0ba2010-03-01 19:29:58 -08001837 while (!mCommandComplete) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001838 mSignal.wait(mLock);
1839 }
1840 mCommandComplete = false;
1841
1842 if (mError == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01001843 ALOGV("wait - success");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001844 } else {
Steve Block3856b092011-10-20 11:56:00 +01001845 ALOGV("wait - error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001846 }
1847 return mError;
1848}
1849
Gloria Wangb483c472011-04-11 17:23:27 -07001850void MediaPlayerService::AudioCache::notify(
1851 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001852{
Steve Block3856b092011-10-20 11:56:00 +01001853 ALOGV("notify(%p, %d, %d, %d)", cookie, msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001854 AudioCache* p = static_cast<AudioCache*>(cookie);
1855
1856 // ignore buffering messages
Dave Sparks8eb80112009-12-09 20:20:26 -08001857 switch (msg)
1858 {
1859 case MEDIA_ERROR:
Steve Block29357bc2012-01-06 19:20:56 +00001860 ALOGE("Error %d, %d occurred", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001861 p->mError = ext1;
Dave Sparks8eb80112009-12-09 20:20:26 -08001862 break;
1863 case MEDIA_PREPARED:
Steve Block3856b092011-10-20 11:56:00 +01001864 ALOGV("prepared");
Dave Sparks8eb80112009-12-09 20:20:26 -08001865 break;
1866 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +01001867 ALOGV("playback complete");
Dave Sparks8eb80112009-12-09 20:20:26 -08001868 break;
1869 default:
Steve Block3856b092011-10-20 11:56:00 +01001870 ALOGV("ignored");
Dave Sparks8eb80112009-12-09 20:20:26 -08001871 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001872 }
1873
1874 // wake up thread
Dave Sparksfe4c6f02010-03-02 12:56:37 -08001875 Mutex::Autolock lock(p->mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001876 p->mCommandComplete = true;
1877 p->mSignal.signal();
1878}
1879
Marco Nelissen4110c102012-03-29 09:31:28 -07001880int MediaPlayerService::AudioCache::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001881{
1882 return 0;
1883}
1884
Gloria Wang7cf180c2011-02-19 18:37:57 -08001885void MediaPlayerService::addBatteryData(uint32_t params)
1886{
1887 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08001888
1889 int32_t time = systemTime() / 1000000L;
1890
1891 // change audio output devices. This notification comes from AudioFlinger
1892 if ((params & kBatteryDataSpeakerOn)
1893 || (params & kBatteryDataOtherAudioDeviceOn)) {
1894
1895 int deviceOn[NUM_AUDIO_DEVICES];
1896 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1897 deviceOn[i] = 0;
1898 }
1899
1900 if ((params & kBatteryDataSpeakerOn)
1901 && (params & kBatteryDataOtherAudioDeviceOn)) {
1902 deviceOn[SPEAKER_AND_OTHER] = 1;
1903 } else if (params & kBatteryDataSpeakerOn) {
1904 deviceOn[SPEAKER] = 1;
1905 } else {
1906 deviceOn[OTHER_AUDIO_DEVICE] = 1;
1907 }
1908
1909 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1910 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
1911
1912 if (mBatteryAudio.refCount > 0) { // if playing audio
1913 if (!deviceOn[i]) {
1914 mBatteryAudio.lastTime[i] += time;
1915 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1916 mBatteryAudio.lastTime[i] = 0;
1917 } else {
1918 mBatteryAudio.lastTime[i] = 0 - time;
1919 }
1920 }
1921
1922 mBatteryAudio.deviceOn[i] = deviceOn[i];
1923 }
1924 }
1925 return;
1926 }
1927
1928 // an sudio stream is started
1929 if (params & kBatteryDataAudioFlingerStart) {
1930 // record the start time only if currently no other audio
1931 // is being played
1932 if (mBatteryAudio.refCount == 0) {
1933 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1934 if (mBatteryAudio.deviceOn[i]) {
1935 mBatteryAudio.lastTime[i] -= time;
1936 }
1937 }
1938 }
1939
1940 mBatteryAudio.refCount ++;
1941 return;
1942
1943 } else if (params & kBatteryDataAudioFlingerStop) {
1944 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001945 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001946 return;
1947 }
1948
1949 // record the stop time only if currently this is the only
1950 // audio being played
1951 if (mBatteryAudio.refCount == 1) {
1952 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1953 if (mBatteryAudio.deviceOn[i]) {
1954 mBatteryAudio.lastTime[i] += time;
1955 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1956 mBatteryAudio.lastTime[i] = 0;
1957 }
1958 }
1959 }
1960
1961 mBatteryAudio.refCount --;
1962 return;
1963 }
1964
Gloria Wang7cf180c2011-02-19 18:37:57 -08001965 int uid = IPCThreadState::self()->getCallingUid();
1966 if (uid == AID_MEDIA) {
1967 return;
1968 }
1969 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08001970
1971 if (index < 0) { // create a new entry for this UID
1972 BatteryUsageInfo info;
1973 info.audioTotalTime = 0;
1974 info.videoTotalTime = 0;
1975 info.audioLastTime = 0;
1976 info.videoLastTime = 0;
1977 info.refCount = 0;
1978
Gloria Wang9ee159b2011-02-24 14:51:45 -08001979 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00001980 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001981 return;
1982 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08001983 }
1984
1985 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
1986
1987 if (params & kBatteryDataCodecStarted) {
1988 if (params & kBatteryDataTrackAudio) {
1989 info.audioLastTime -= time;
1990 info.refCount ++;
1991 }
1992 if (params & kBatteryDataTrackVideo) {
1993 info.videoLastTime -= time;
1994 info.refCount ++;
1995 }
1996 } else {
1997 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001998 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08001999 return;
2000 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00002001 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002002 mBatteryData.removeItem(uid);
2003 return;
2004 }
2005
2006 if (params & kBatteryDataTrackAudio) {
2007 info.audioLastTime += time;
2008 info.refCount --;
2009 }
2010 if (params & kBatteryDataTrackVideo) {
2011 info.videoLastTime += time;
2012 info.refCount --;
2013 }
2014
2015 // no stream is being played by this UID
2016 if (info.refCount == 0) {
2017 info.audioTotalTime += info.audioLastTime;
2018 info.audioLastTime = 0;
2019 info.videoTotalTime += info.videoLastTime;
2020 info.videoLastTime = 0;
2021 }
2022 }
2023}
2024
2025status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
2026 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002027
2028 // audio output devices usage
2029 int32_t time = systemTime() / 1000000L; //in ms
2030 int32_t totalTime;
2031
2032 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2033 totalTime = mBatteryAudio.totalTime[i];
2034
2035 if (mBatteryAudio.deviceOn[i]
2036 && (mBatteryAudio.lastTime[i] != 0)) {
2037 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
2038 totalTime += tmpTime;
2039 }
2040
2041 reply->writeInt32(totalTime);
2042 // reset the total time
2043 mBatteryAudio.totalTime[i] = 0;
2044 }
2045
2046 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08002047 BatteryUsageInfo info;
2048 int size = mBatteryData.size();
2049
2050 reply->writeInt32(size);
2051 int i = 0;
2052
2053 while (i < size) {
2054 info = mBatteryData.valueAt(i);
2055
2056 reply->writeInt32(mBatteryData.keyAt(i)); //UID
2057 reply->writeInt32(info.audioTotalTime);
2058 reply->writeInt32(info.videoTotalTime);
2059
2060 info.audioTotalTime = 0;
2061 info.videoTotalTime = 0;
2062
2063 // remove the UID entry where no stream is being played
2064 if (info.refCount <= 0) {
2065 mBatteryData.removeItemsAt(i);
2066 size --;
2067 i --;
2068 }
2069 i++;
2070 }
2071 return NO_ERROR;
2072}
nikoa64c8c72009-07-20 15:07:26 -07002073} // namespace android