blob: 9567effe3e5ead75b68f0b5d5a9d30f7e77a767c [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
Chong Zhang0b30fd42014-07-23 14:46:05 -070037#include <binder/IBatteryStats.h>
Mathias Agopian75624082009-05-19 19:08:10 -070038#include <binder/IPCThreadState.h>
39#include <binder/IServiceManager.h>
40#include <binder/MemoryHeapBase.h>
41#include <binder/MemoryBase.h>
Mathias Agopianb1e7cd12013-02-14 17:11:27 -080042#include <gui/Surface.h>
Nicolas Catania1d187f12009-05-12 23:25:55 -070043#include <utils/Errors.h> // for status_t
44#include <utils/String8.h>
Marco Nelissen10dbb8e2009-09-20 10:42:13 -070045#include <utils/SystemClock.h>
Lajos Molnar06ad1522014-08-28 07:27:44 -070046#include <utils/Timers.h>
Nicolas Catania1d187f12009-05-12 23:25:55 -070047#include <utils/Vector.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080048
Andreas Huber1b86fe02014-01-29 11:13:26 -080049#include <media/IMediaHTTPService.h>
Jeff Brown2013a542012-09-04 21:38:42 -070050#include <media/IRemoteDisplay.h>
51#include <media/IRemoteDisplayClient.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080052#include <media/MediaPlayerInterface.h>
53#include <media/mediarecorder.h>
54#include <media/MediaMetadataRetrieverInterface.h>
nikoa64c8c72009-07-20 15:07:26 -070055#include <media/Metadata.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080056#include <media/AudioTrack.h>
James Dong8635b7b2011-03-14 17:01:38 -070057#include <media/MemoryLeakTrackUtil.h>
Lajos Molnar1381d4b2014-08-07 15:18:35 -070058#include <media/stagefright/MediaCodecList.h>
Eric Laurent9cb839a2011-09-27 09:48:56 -070059#include <media/stagefright/MediaErrors.h>
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +010060#include <media/stagefright/AudioPlayer.h>
61#include <media/stagefright/foundation/ADebug.h>
Marco Nelissenf09611f2015-02-13 14:12:42 -080062#include <media/stagefright/foundation/ALooperRoster.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080063
Dima Zavin64760242011-05-11 14:15:23 -070064#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070065
Gloria Wang7cf180c2011-02-19 18:37:57 -080066#include <private/android_filesystem_config.h>
67
James Dong559bf282012-03-28 10:29:14 -070068#include "ActivityManager.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080069#include "MediaRecorderClient.h"
70#include "MediaPlayerService.h"
71#include "MetadataRetrieverClient.h"
John Grossman44a7e422012-06-21 17:29:24 -070072#include "MediaPlayerFactory.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080073
Nicolas Catania14d27472009-07-13 14:37:49 -070074#include "TestPlayerStub.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070075#include "StagefrightPlayer.h"
Andreas Huberf9334412010-12-15 15:17:42 -080076#include "nuplayer/NuPlayerDriver.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070077
Andreas Huber20111aa2009-07-14 16:56:47 -070078#include <OMX.h>
Nicolas Catania14d27472009-07-13 14:37:49 -070079
Andreas Hubered3e3e02012-03-26 11:13:27 -070080#include "Crypto.h"
Jeff Tinkercc82dc62013-02-08 10:18:35 -080081#include "Drm.h"
Andreas Huber59451f82012-09-18 10:36:32 -070082#include "HDCP.h"
Andreas Huberb7319a72013-05-29 14:20:52 -070083#include "HTTPBase.h"
Andreas Huber35213f12012-08-29 11:41:50 -070084#include "RemoteDisplay.h"
Andreas Hubered3e3e02012-03-26 11:13:27 -070085
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070086namespace {
nikoa64c8c72009-07-20 15:07:26 -070087using android::media::Metadata;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070088using android::status_t;
89using android::OK;
90using android::BAD_VALUE;
91using android::NOT_ENOUGH_DATA;
92using android::Parcel;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070093
94// Max number of entries in the filter.
95const int kMaxFilterSize = 64; // I pulled that out of thin air.
96
nikoa64c8c72009-07-20 15:07:26 -070097// FIXME: Move all the metadata related function in the Metadata.cpp
nikod608a812009-07-16 16:39:53 -070098
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070099
100// Unmarshall a filter from a Parcel.
101// Filter format in a parcel:
102//
103// 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
104// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105// | number of entries (n) |
106// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
107// | metadata type 1 |
108// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
109// | metadata type 2 |
110// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
111// ....
112// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
113// | metadata type n |
114// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
115//
116// @param p Parcel that should start with a filter.
117// @param[out] filter On exit contains the list of metadata type to be
118// filtered.
119// @param[out] status On exit contains the status code to be returned.
120// @return true if the parcel starts with a valid filter.
121bool unmarshallFilter(const Parcel& p,
nikoa64c8c72009-07-20 15:07:26 -0700122 Metadata::Filter *filter,
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700123 status_t *status)
124{
Nicolas Catania48290382009-07-10 13:53:06 -0700125 int32_t val;
126 if (p.readInt32(&val) != OK)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700127 {
Steve Block29357bc2012-01-06 19:20:56 +0000128 ALOGE("Failed to read filter's length");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700129 *status = NOT_ENOUGH_DATA;
130 return false;
131 }
132
Nicolas Catania48290382009-07-10 13:53:06 -0700133 if( val > kMaxFilterSize || val < 0)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700134 {
Steve Block29357bc2012-01-06 19:20:56 +0000135 ALOGE("Invalid filter len %d", val);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700136 *status = BAD_VALUE;
137 return false;
138 }
139
Nicolas Catania48290382009-07-10 13:53:06 -0700140 const size_t num = val;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700141
142 filter->clear();
Nicolas Catania48290382009-07-10 13:53:06 -0700143 filter->setCapacity(num);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700144
nikoa64c8c72009-07-20 15:07:26 -0700145 size_t size = num * sizeof(Metadata::Type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700146
Nicolas Catania48290382009-07-10 13:53:06 -0700147
148 if (p.dataAvail() < size)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700149 {
Steve Block29357bc2012-01-06 19:20:56 +0000150 ALOGE("Filter too short expected %d but got %d", size, p.dataAvail());
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700151 *status = NOT_ENOUGH_DATA;
152 return false;
153 }
154
nikoa64c8c72009-07-20 15:07:26 -0700155 const Metadata::Type *data =
156 static_cast<const Metadata::Type*>(p.readInplace(size));
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700157
Nicolas Catania48290382009-07-10 13:53:06 -0700158 if (NULL == data)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700159 {
Steve Block29357bc2012-01-06 19:20:56 +0000160 ALOGE("Filter had no data");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700161 *status = BAD_VALUE;
162 return false;
163 }
164
165 // TODO: The stl impl of vector would be more efficient here
166 // because it degenerates into a memcpy on pod types. Try to
167 // replace later or use stl::set.
Nicolas Catania48290382009-07-10 13:53:06 -0700168 for (size_t i = 0; i < num; ++i)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700169 {
Nicolas Catania48290382009-07-10 13:53:06 -0700170 filter->add(*data);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700171 ++data;
172 }
173 *status = OK;
174 return true;
175}
176
Nicolas Catania48290382009-07-10 13:53:06 -0700177// @param filter Of metadata type.
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700178// @param val To be searched.
179// @return true if a match was found.
nikoa64c8c72009-07-20 15:07:26 -0700180bool findMetadata(const Metadata::Filter& filter, const int32_t val)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700181{
182 // Deal with empty and ANY right away
183 if (filter.isEmpty()) return false;
nikoa64c8c72009-07-20 15:07:26 -0700184 if (filter[0] == Metadata::kAny) return true;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700185
Nicolas Catania48290382009-07-10 13:53:06 -0700186 return filter.indexOf(val) >= 0;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700187}
188
189} // anonymous namespace
190
191
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700192namespace {
193using android::Parcel;
194using android::String16;
195
196// marshalling tag indicating flattened utf16 tags
197// keep in sync with frameworks/base/media/java/android/media/AudioAttributes.java
198const int32_t kAudioAttributesMarshallTagFlattenTags = 1;
199
200// Audio attributes format in a parcel:
201//
202// 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
203// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
204// | usage |
205// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
206// | content_type |
207// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Hyejin Kim4f418f92014-09-05 15:50:03 +0900208// | source |
209// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700210// | flags |
211// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
212// | kAudioAttributesMarshallTagFlattenTags | // ignore tags if not found
213// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
214// | flattened tags in UTF16 |
215// | ... |
216// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
217//
218// @param p Parcel that contains audio attributes.
219// @param[out] attributes On exit points to an initialized audio_attributes_t structure
220// @param[out] status On exit contains the status code to be returned.
221void unmarshallAudioAttributes(const Parcel& parcel, audio_attributes_t *attributes)
222{
223 attributes->usage = (audio_usage_t) parcel.readInt32();
224 attributes->content_type = (audio_content_type_t) parcel.readInt32();
Hyejin Kim4f418f92014-09-05 15:50:03 +0900225 attributes->source = (audio_source_t) parcel.readInt32();
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700226 attributes->flags = (audio_flags_mask_t) parcel.readInt32();
227 const bool hasFlattenedTag = (parcel.readInt32() == kAudioAttributesMarshallTagFlattenTags);
228 if (hasFlattenedTag) {
229 // the tags are UTF16, convert to UTF8
230 String16 tags = parcel.readString16();
231 ssize_t realTagSize = utf16_to_utf8_length(tags.string(), tags.size());
232 if (realTagSize <= 0) {
233 strcpy(attributes->tags, "");
234 } else {
235 // copy the flattened string into the attributes as the destination for the conversion:
236 // copying array size -1, array for tags was calloc'd, no need to NULL-terminate it
237 size_t tagSize = realTagSize > AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 ?
238 AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 : realTagSize;
239 utf16_to_utf8(tags.string(), tagSize, attributes->tags);
240 }
241 } else {
242 ALOGE("unmarshallAudioAttributes() received unflattened tags, ignoring tag values");
243 strcpy(attributes->tags, "");
244 }
245}
246} // anonymous namespace
247
248
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800249namespace android {
250
Marco Nelissenf09611f2015-02-13 14:12:42 -0800251extern ALooperRoster gLooperRoster;
252
253
Dave Burked681bbb2011-08-30 14:39:17 +0100254static bool checkPermission(const char* permissionString) {
255#ifndef HAVE_ANDROID_OS
256 return true;
257#endif
258 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
259 bool ok = checkCallingPermission(String16(permissionString));
Steve Block29357bc2012-01-06 19:20:56 +0000260 if (!ok) ALOGE("Request requires %s", permissionString);
Dave Burked681bbb2011-08-30 14:39:17 +0100261 return ok;
262}
263
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800264// 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 -0800265/* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4;
266/* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false;
267
268void MediaPlayerService::instantiate() {
269 defaultServiceManager()->addService(
270 String16("media.player"), new MediaPlayerService());
271}
272
273MediaPlayerService::MediaPlayerService()
274{
Steve Block3856b092011-10-20 11:56:00 +0100275 ALOGV("MediaPlayerService created");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800276 mNextConnId = 1;
Gloria Wang9ee159b2011-02-24 14:51:45 -0800277
278 mBatteryAudio.refCount = 0;
279 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
280 mBatteryAudio.deviceOn[i] = 0;
281 mBatteryAudio.lastTime[i] = 0;
282 mBatteryAudio.totalTime[i] = 0;
283 }
284 // speaker is on by default
285 mBatteryAudio.deviceOn[SPEAKER] = 1;
John Grossman44a7e422012-06-21 17:29:24 -0700286
Chong Zhang0b30fd42014-07-23 14:46:05 -0700287 // reset battery stats
288 // if the mediaserver has crashed, battery stats could be left
289 // in bad state, reset the state upon service start.
290 const sp<IServiceManager> sm(defaultServiceManager());
291 if (sm != NULL) {
292 const String16 name("batterystats");
Warren Rehmaned849b82015-03-05 15:26:12 +0900293 // use checkService() to avoid blocking if service is not up yet
Chong Zhang0b30fd42014-07-23 14:46:05 -0700294 sp<IBatteryStats> batteryStats =
Warren Rehmaned849b82015-03-05 15:26:12 +0900295 interface_cast<IBatteryStats>(sm->checkService(name));
Chong Zhang0b30fd42014-07-23 14:46:05 -0700296 if (batteryStats != NULL) {
297 batteryStats->noteResetVideo();
298 batteryStats->noteResetAudio();
299 }
300 }
301
John Grossman44a7e422012-06-21 17:29:24 -0700302 MediaPlayerFactory::registerBuiltinFactories();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800303}
304
305MediaPlayerService::~MediaPlayerService()
306{
Steve Block3856b092011-10-20 11:56:00 +0100307 ALOGV("MediaPlayerService destroyed");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800308}
309
Svet Ganovbe71aa22015-04-28 12:06:02 -0700310sp<IMediaRecorder> MediaPlayerService::createMediaRecorder(const String16 &opPackageName)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800311{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800312 pid_t pid = IPCThreadState::self()->getCallingPid();
Svet Ganovbe71aa22015-04-28 12:06:02 -0700313 sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid, opPackageName);
Gloria Wangdac6a312009-10-29 15:46:37 -0700314 wp<MediaRecorderClient> w = recorder;
315 Mutex::Autolock lock(mLock);
316 mMediaRecorderClients.add(w);
Steve Block3856b092011-10-20 11:56:00 +0100317 ALOGV("Create new media recorder client from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800318 return recorder;
319}
320
Gloria Wangdac6a312009-10-29 15:46:37 -0700321void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client)
322{
323 Mutex::Autolock lock(mLock);
324 mMediaRecorderClients.remove(client);
Steve Block3856b092011-10-20 11:56:00 +0100325 ALOGV("Delete media recorder client");
Gloria Wangdac6a312009-10-29 15:46:37 -0700326}
327
Glenn Kastenf37971f2012-02-03 11:06:53 -0800328sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800329{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800330 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800331 sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
Steve Block3856b092011-10-20 11:56:00 +0100332 ALOGV("Create new media retriever from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800333 return retriever;
334}
335
Glenn Kastenf37971f2012-02-03 11:06:53 -0800336sp<IMediaPlayer> MediaPlayerService::create(const sp<IMediaPlayerClient>& client,
Dave Burked681bbb2011-08-30 14:39:17 +0100337 int audioSessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800338{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800339 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800340 int32_t connId = android_atomic_inc(&mNextConnId);
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700341
342 sp<Client> c = new Client(
343 this, pid, connId, client, audioSessionId,
344 IPCThreadState::self()->getCallingUid());
345
Steve Block3856b092011-10-20 11:56:00 +0100346 ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid,
Dave Burked681bbb2011-08-30 14:39:17 +0100347 IPCThreadState::self()->getCallingUid());
348
349 wp<Client> w = c;
350 {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800351 Mutex::Autolock lock(mLock);
352 mClients.add(w);
353 }
Andreas Hubere2b10282010-11-23 11:41:34 -0800354 return c;
355}
356
Lajos Molnar1381d4b2014-08-07 15:18:35 -0700357sp<IMediaCodecList> MediaPlayerService::getCodecList() const {
358 return MediaCodecList::getLocalInstance();
359}
360
Andreas Huber318ad9c2009-10-15 13:46:54 -0700361sp<IOMX> MediaPlayerService::getOMX() {
362 Mutex::Autolock autoLock(mLock);
363
364 if (mOMX.get() == NULL) {
365 mOMX = new OMX;
366 }
367
368 return mOMX;
Andreas Huber20111aa2009-07-14 16:56:47 -0700369}
370
Andreas Hubered3e3e02012-03-26 11:13:27 -0700371sp<ICrypto> MediaPlayerService::makeCrypto() {
Andreas Huber1bd139a2012-04-03 14:19:20 -0700372 return new Crypto;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700373}
374
Jeff Tinkercc82dc62013-02-08 10:18:35 -0800375sp<IDrm> MediaPlayerService::makeDrm() {
376 return new Drm;
377}
378
Andreas Huber279dcd82013-01-30 10:41:25 -0800379sp<IHDCP> MediaPlayerService::makeHDCP(bool createEncryptionModule) {
380 return new HDCP(createEncryptionModule);
Andreas Huber59451f82012-09-18 10:36:32 -0700381}
382
Jeff Brown2013a542012-09-04 21:38:42 -0700383sp<IRemoteDisplay> MediaPlayerService::listenForRemoteDisplay(
Svet Ganovbe71aa22015-04-28 12:06:02 -0700384 const String16 &opPackageName,
Jeff Brown2013a542012-09-04 21:38:42 -0700385 const sp<IRemoteDisplayClient>& client, const String8& iface) {
Jeff Brownaba33d52012-09-07 17:38:58 -0700386 if (!checkPermission("android.permission.CONTROL_WIFI_DISPLAY")) {
387 return NULL;
388 }
389
Svet Ganovbe71aa22015-04-28 12:06:02 -0700390 return new RemoteDisplay(opPackageName, client, iface.string());
Jeff Brown2013a542012-09-04 21:38:42 -0700391}
392
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800393status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
394{
395 const size_t SIZE = 256;
396 char buffer[SIZE];
397 String8 result;
398
399 result.append(" AudioOutput\n");
400 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n",
401 mStreamType, mLeftVolume, mRightVolume);
402 result.append(buffer);
403 snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
Eric Laurentdb354e52012-03-05 17:27:11 -0800404 mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800405 result.append(buffer);
Eric Laurent2beeb502010-07-16 07:43:46 -0700406 snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
407 mAuxEffectId, mSendLevel);
408 result.append(buffer);
409
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800410 ::write(fd, result.string(), result.size());
411 if (mTrack != 0) {
412 mTrack->dump(fd, args);
413 }
414 return NO_ERROR;
415}
416
Lajos Molnar6d339f12015-04-17 16:15:53 -0700417status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800418{
419 const size_t SIZE = 256;
420 char buffer[SIZE];
421 String8 result;
422 result.append(" Client\n");
423 snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n",
424 mPid, mConnId, mStatus, mLoop?"true": "false");
425 result.append(buffer);
426 write(fd, result.string(), result.size());
Andreas Hubera0b1d4b2011-06-07 15:52:25 -0700427 if (mPlayer != NULL) {
428 mPlayer->dump(fd, args);
429 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800430 if (mAudioOutput != 0) {
431 mAudioOutput->dump(fd, args);
432 }
433 write(fd, "\n", 1);
434 return NO_ERROR;
435}
436
Marco Nelissenf09611f2015-02-13 14:12:42 -0800437/**
438 * The only arguments this understands right now are -c, -von and -voff,
439 * which are parsed by ALooperRoster::dump()
440 */
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800441status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
442{
443 const size_t SIZE = 256;
444 char buffer[SIZE];
445 String8 result;
Ravi Kumar Alamanda330c8e32014-12-22 10:05:29 +0530446 SortedVector< sp<Client> > clients; //to serialise the mutex unlock & client destruction.
447 SortedVector< sp<MediaRecorderClient> > mediaRecorderClients;
448
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800449 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
450 snprintf(buffer, SIZE, "Permission Denial: "
451 "can't dump MediaPlayerService from pid=%d, uid=%d\n",
452 IPCThreadState::self()->getCallingPid(),
453 IPCThreadState::self()->getCallingUid());
454 result.append(buffer);
455 } else {
456 Mutex::Autolock lock(mLock);
457 for (int i = 0, n = mClients.size(); i < n; ++i) {
458 sp<Client> c = mClients[i].promote();
459 if (c != 0) c->dump(fd, args);
Ravi Kumar Alamanda330c8e32014-12-22 10:05:29 +0530460 clients.add(c);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800461 }
James Dongb9141222010-07-08 11:16:11 -0700462 if (mMediaRecorderClients.size() == 0) {
463 result.append(" No media recorder client\n\n");
464 } else {
465 for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
466 sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
James Donge579e282011-10-18 22:29:20 -0700467 if (c != 0) {
468 snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid);
469 result.append(buffer);
470 write(fd, result.string(), result.size());
471 result = "\n";
472 c->dump(fd, args);
Ravi Kumar Alamanda330c8e32014-12-22 10:05:29 +0530473 mediaRecorderClients.add(c);
James Donge579e282011-10-18 22:29:20 -0700474 }
James Dongb9141222010-07-08 11:16:11 -0700475 }
Gloria Wangdac6a312009-10-29 15:46:37 -0700476 }
477
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800478 result.append(" Files opened and/or mapped:\n");
Marco Nelissenf09611f2015-02-13 14:12:42 -0800479 snprintf(buffer, SIZE, "/proc/%d/maps", getpid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800480 FILE *f = fopen(buffer, "r");
481 if (f) {
482 while (!feof(f)) {
483 fgets(buffer, SIZE, f);
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700484 if (strstr(buffer, " /storage/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800485 strstr(buffer, " /system/sounds/") ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700486 strstr(buffer, " /data/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800487 strstr(buffer, " /system/media/")) {
488 result.append(" ");
489 result.append(buffer);
490 }
491 }
492 fclose(f);
493 } else {
494 result.append("couldn't open ");
495 result.append(buffer);
496 result.append("\n");
497 }
498
Marco Nelissenf09611f2015-02-13 14:12:42 -0800499 snprintf(buffer, SIZE, "/proc/%d/fd", getpid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800500 DIR *d = opendir(buffer);
501 if (d) {
502 struct dirent *ent;
503 while((ent = readdir(d)) != NULL) {
504 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
Marco Nelissenf09611f2015-02-13 14:12:42 -0800505 snprintf(buffer, SIZE, "/proc/%d/fd/%s", getpid(), ent->d_name);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800506 struct stat s;
507 if (lstat(buffer, &s) == 0) {
508 if ((s.st_mode & S_IFMT) == S_IFLNK) {
509 char linkto[256];
510 int len = readlink(buffer, linkto, sizeof(linkto));
511 if(len > 0) {
512 if(len > 255) {
513 linkto[252] = '.';
514 linkto[253] = '.';
515 linkto[254] = '.';
516 linkto[255] = 0;
517 } else {
518 linkto[len] = 0;
519 }
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700520 if (strstr(linkto, "/storage/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800521 strstr(linkto, "/system/sounds/") == linkto ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700522 strstr(linkto, "/data/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800523 strstr(linkto, "/system/media/") == linkto) {
524 result.append(" ");
525 result.append(buffer);
526 result.append(" -> ");
527 result.append(linkto);
528 result.append("\n");
529 }
530 }
531 } else {
532 result.append(" unexpected type for ");
533 result.append(buffer);
534 result.append("\n");
535 }
536 }
537 }
538 }
539 closedir(d);
540 } else {
541 result.append("couldn't open ");
542 result.append(buffer);
543 result.append("\n");
544 }
545
Marco Nelissenf09611f2015-02-13 14:12:42 -0800546 gLooperRoster.dump(fd, args);
547
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800548 bool dumpMem = false;
549 for (size_t i = 0; i < args.size(); i++) {
550 if (args[i] == String16("-m")) {
551 dumpMem = true;
552 }
553 }
554 if (dumpMem) {
James Dong8635b7b2011-03-14 17:01:38 -0700555 dumpMemoryAddresses(fd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800556 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800557 }
558 write(fd, result.string(), result.size());
559 return NO_ERROR;
560}
561
562void MediaPlayerService::removeClient(wp<Client> client)
563{
564 Mutex::Autolock lock(mLock);
565 mClients.remove(client);
566}
567
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700568MediaPlayerService::Client::Client(
569 const sp<MediaPlayerService>& service, pid_t pid,
570 int32_t connId, const sp<IMediaPlayerClient>& client,
571 int audioSessionId, uid_t uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800572{
Steve Block3856b092011-10-20 11:56:00 +0100573 ALOGV("Client(%d) constructor", connId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800574 mPid = pid;
575 mConnId = connId;
576 mService = service;
577 mClient = client;
578 mLoop = false;
579 mStatus = NO_INIT;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700580 mAudioSessionId = audioSessionId;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700581 mUID = uid;
John Grossmanc795b642012-02-22 15:38:35 -0800582 mRetransmitEndpointValid = false;
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700583 mAudioAttributes = NULL;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700584
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800585#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000586 ALOGD("create Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800587 mAntagonizer = new Antagonizer(notify, this);
588#endif
589}
590
591MediaPlayerService::Client::~Client()
592{
Steve Block3856b092011-10-20 11:56:00 +0100593 ALOGV("Client(%d) destructor pid = %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800594 mAudioOutput.clear();
595 wp<Client> client(this);
596 disconnect();
597 mService->removeClient(client);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700598 if (mAudioAttributes != NULL) {
599 free(mAudioAttributes);
600 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800601}
602
603void MediaPlayerService::Client::disconnect()
604{
Steve Block3856b092011-10-20 11:56:00 +0100605 ALOGV("disconnect(%d) from pid %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800606 // grab local reference and clear main reference to prevent future
607 // access to object
608 sp<MediaPlayerBase> p;
609 {
610 Mutex::Autolock l(mLock);
611 p = mPlayer;
beanzdcfefde2012-11-05 09:51:43 +0800612 mClient.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800613 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700614
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800615 mPlayer.clear();
616
617 // clear the notification to prevent callbacks to dead client
618 // and reset the player. We assume the player will serialize
619 // access to itself if necessary.
620 if (p != 0) {
621 p->setNotifyCallback(0, 0);
622#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000623 ALOGD("kill Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800624 mAntagonizer->kill();
625#endif
626 p->reset();
627 }
628
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700629 disconnectNativeWindow();
630
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800631 IPCThreadState::self()->flushCommands();
632}
633
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800634sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
635{
636 // determine if we have the right player type
637 sp<MediaPlayerBase> p = mPlayer;
638 if ((p != NULL) && (p->playerType() != playerType)) {
Steve Block3856b092011-10-20 11:56:00 +0100639 ALOGV("delete player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800640 p.clear();
641 }
642 if (p == NULL) {
John Grossman44a7e422012-06-21 17:29:24 -0700643 p = MediaPlayerFactory::createPlayer(playerType, this, notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800644 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700645
Jason Simmonsdb29e522011-08-12 13:46:55 -0700646 if (p != NULL) {
647 p->setUID(mUID);
648 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700649
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800650 return p;
651}
652
John Grossmanc795b642012-02-22 15:38:35 -0800653sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre(
654 player_type playerType)
655{
656 ALOGV("player type = %d", playerType);
657
658 // create the right type of player
659 sp<MediaPlayerBase> p = createPlayer(playerType);
660 if (p == NULL) {
661 return p;
662 }
663
664 if (!p->hardwareOutput()) {
Marco Nelissend457c972014-02-11 08:47:07 -0800665 mAudioOutput = new AudioOutput(mAudioSessionId, IPCThreadState::self()->getCallingUid(),
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700666 mPid, mAudioAttributes);
John Grossmanc795b642012-02-22 15:38:35 -0800667 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
668 }
669
670 return p;
671}
672
673void MediaPlayerService::Client::setDataSource_post(
674 const sp<MediaPlayerBase>& p,
675 status_t status)
676{
677 ALOGV(" setDataSource");
678 mStatus = status;
679 if (mStatus != OK) {
680 ALOGE(" error: %d", mStatus);
681 return;
682 }
683
684 // Set the re-transmission endpoint if one was chosen.
685 if (mRetransmitEndpointValid) {
686 mStatus = p->setRetransmitEndpoint(&mRetransmitEndpoint);
687 if (mStatus != NO_ERROR) {
688 ALOGE("setRetransmitEndpoint error: %d", mStatus);
689 }
690 }
691
692 if (mStatus == OK) {
693 mPlayer = p;
694 }
695}
696
Andreas Huber2db84552010-01-28 11:19:57 -0800697status_t MediaPlayerService::Client::setDataSource(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800698 const sp<IMediaHTTPService> &httpService,
699 const char *url,
700 const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800701{
Steve Block3856b092011-10-20 11:56:00 +0100702 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800703 if (url == NULL)
704 return UNKNOWN_ERROR;
705
Dave Burked681bbb2011-08-30 14:39:17 +0100706 if ((strncmp(url, "http://", 7) == 0) ||
707 (strncmp(url, "https://", 8) == 0) ||
708 (strncmp(url, "rtsp://", 7) == 0)) {
709 if (!checkPermission("android.permission.INTERNET")) {
710 return PERMISSION_DENIED;
711 }
712 }
713
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800714 if (strncmp(url, "content://", 10) == 0) {
715 // get a filedescriptor for the content Uri and
716 // pass it to the setDataSource(fd) method
717
718 String16 url16(url);
719 int fd = android::openContentProviderFile(url16);
720 if (fd < 0)
721 {
Steve Block29357bc2012-01-06 19:20:56 +0000722 ALOGE("Couldn't open fd for %s", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800723 return UNKNOWN_ERROR;
724 }
725 setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
726 close(fd);
727 return mStatus;
728 } else {
John Grossman44a7e422012-06-21 17:29:24 -0700729 player_type playerType = MediaPlayerFactory::getPlayerType(this, url);
John Grossmanc795b642012-02-22 15:38:35 -0800730 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
731 if (p == NULL) {
732 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800733 }
734
Andreas Huber1b86fe02014-01-29 11:13:26 -0800735 setDataSource_post(p, p->setDataSource(httpService, url, headers));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800736 return mStatus;
737 }
738}
739
740status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
741{
Steve Block3856b092011-10-20 11:56:00 +0100742 ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800743 struct stat sb;
744 int ret = fstat(fd, &sb);
745 if (ret != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000746 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800747 return UNKNOWN_ERROR;
748 }
749
Ying Wang5fde15d2015-04-20 22:13:22 -0700750 ALOGV("st_dev = %llu", static_cast<uint64_t>(sb.st_dev));
Steve Block3856b092011-10-20 11:56:00 +0100751 ALOGV("st_mode = %u", sb.st_mode);
Mark Salyzyn77342f72014-06-18 16:31:32 -0700752 ALOGV("st_uid = %lu", static_cast<unsigned long>(sb.st_uid));
753 ALOGV("st_gid = %lu", static_cast<unsigned long>(sb.st_gid));
Steve Block3856b092011-10-20 11:56:00 +0100754 ALOGV("st_size = %llu", sb.st_size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800755
756 if (offset >= sb.st_size) {
Steve Block29357bc2012-01-06 19:20:56 +0000757 ALOGE("offset error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800758 ::close(fd);
759 return UNKNOWN_ERROR;
760 }
761 if (offset + length > sb.st_size) {
762 length = sb.st_size - offset;
Steve Block3856b092011-10-20 11:56:00 +0100763 ALOGV("calculated length = %lld", length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800764 }
765
John Grossman44a7e422012-06-21 17:29:24 -0700766 player_type playerType = MediaPlayerFactory::getPlayerType(this,
767 fd,
768 offset,
769 length);
John Grossmanc795b642012-02-22 15:38:35 -0800770 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
771 if (p == NULL) {
772 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800773 }
774
775 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800776 setDataSource_post(p, p->setDataSource(fd, offset, length));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800777 return mStatus;
778}
779
Andreas Hubere2b10282010-11-23 11:41:34 -0800780status_t MediaPlayerService::Client::setDataSource(
781 const sp<IStreamSource> &source) {
782 // create the right type of player
John Grossman44a7e422012-06-21 17:29:24 -0700783 player_type playerType = MediaPlayerFactory::getPlayerType(this, source);
John Grossmanc795b642012-02-22 15:38:35 -0800784 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
Andreas Hubere2b10282010-11-23 11:41:34 -0800785 if (p == NULL) {
786 return NO_INIT;
787 }
788
Andreas Hubere2b10282010-11-23 11:41:34 -0800789 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800790 setDataSource_post(p, p->setDataSource(source));
Andreas Hubere2b10282010-11-23 11:41:34 -0800791 return mStatus;
792}
793
Chris Watkins99f31602015-03-20 13:06:33 -0700794status_t MediaPlayerService::Client::setDataSource(
795 const sp<IDataSource> &source) {
796 sp<DataSource> dataSource = DataSource::CreateFromIDataSource(source);
797 player_type playerType = MediaPlayerFactory::getPlayerType(this, dataSource);
798 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
799 if (p == NULL) {
800 return NO_INIT;
801 }
802 // now set data source
803 setDataSource_post(p, p->setDataSource(dataSource));
804 return mStatus;
805}
806
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700807void MediaPlayerService::Client::disconnectNativeWindow() {
808 if (mConnectedWindow != NULL) {
809 status_t err = native_window_api_disconnect(mConnectedWindow.get(),
810 NATIVE_WINDOW_API_MEDIA);
811
812 if (err != OK) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000813 ALOGW("native_window_api_disconnect returned an error: %s (%d)",
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700814 strerror(-err), err);
815 }
816 }
817 mConnectedWindow.clear();
818}
819
Glenn Kasten11731182011-02-08 17:26:17 -0800820status_t MediaPlayerService::Client::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -0800821 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800822{
Andy McFadden484566c2012-12-18 09:46:54 -0800823 ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, bufferProducer.get());
Glenn Kasten11731182011-02-08 17:26:17 -0800824 sp<MediaPlayerBase> p = getPlayer();
825 if (p == 0) return UNKNOWN_ERROR;
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700826
Marco Nelissenf8880202014-11-14 07:58:25 -0800827 sp<IBinder> binder(IInterface::asBinder(bufferProducer));
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700828 if (mConnectedWindowBinder == binder) {
829 return OK;
830 }
831
832 sp<ANativeWindow> anw;
Andy McFadden484566c2012-12-18 09:46:54 -0800833 if (bufferProducer != NULL) {
Marco Nelissenee08f7e2013-09-16 13:30:01 -0700834 anw = new Surface(bufferProducer, true /* controlledByApp */);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700835 status_t err = native_window_api_connect(anw.get(),
836 NATIVE_WINDOW_API_MEDIA);
837
838 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000839 ALOGE("setVideoSurfaceTexture failed: %d", err);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700840 // Note that we must do the reset before disconnecting from the ANW.
841 // Otherwise queue/dequeue calls could be made on the disconnected
842 // ANW, which may result in errors.
843 reset();
844
845 disconnectNativeWindow();
846
847 return err;
848 }
849 }
850
Andy McFadden484566c2012-12-18 09:46:54 -0800851 // Note that we must set the player's new GraphicBufferProducer before
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700852 // disconnecting the old one. Otherwise queue/dequeue calls could be made
853 // on the disconnected ANW, which may result in errors.
Andy McFadden484566c2012-12-18 09:46:54 -0800854 status_t err = p->setVideoSurfaceTexture(bufferProducer);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700855
856 disconnectNativeWindow();
857
858 mConnectedWindow = anw;
859
860 if (err == OK) {
861 mConnectedWindowBinder = binder;
862 } else {
863 disconnectNativeWindow();
864 }
865
866 return err;
Glenn Kasten11731182011-02-08 17:26:17 -0800867}
868
Nicolas Catania1d187f12009-05-12 23:25:55 -0700869status_t MediaPlayerService::Client::invoke(const Parcel& request,
870 Parcel *reply)
871{
872 sp<MediaPlayerBase> p = getPlayer();
873 if (p == NULL) return UNKNOWN_ERROR;
874 return p->invoke(request, reply);
875}
876
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700877// This call doesn't need to access the native player.
878status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
879{
880 status_t status;
nikoa64c8c72009-07-20 15:07:26 -0700881 media::Metadata::Filter allow, drop;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700882
Nicolas Catania48290382009-07-10 13:53:06 -0700883 if (unmarshallFilter(filter, &allow, &status) &&
884 unmarshallFilter(filter, &drop, &status)) {
885 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700886
887 mMetadataAllow = allow;
888 mMetadataDrop = drop;
889 }
890 return status;
891}
892
Nicolas Catania48290382009-07-10 13:53:06 -0700893status_t MediaPlayerService::Client::getMetadata(
Mark Salyzyn77342f72014-06-18 16:31:32 -0700894 bool update_only, bool /*apply_filter*/, Parcel *reply)
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700895{
nikoa64c8c72009-07-20 15:07:26 -0700896 sp<MediaPlayerBase> player = getPlayer();
897 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania48290382009-07-10 13:53:06 -0700898
nikod608a812009-07-16 16:39:53 -0700899 status_t status;
900 // Placeholder for the return code, updated by the caller.
901 reply->writeInt32(-1);
902
nikoa64c8c72009-07-20 15:07:26 -0700903 media::Metadata::Filter ids;
Nicolas Catania48290382009-07-10 13:53:06 -0700904
905 // We don't block notifications while we fetch the data. We clear
906 // mMetadataUpdated first so we don't lose notifications happening
907 // during the rest of this call.
908 {
909 Mutex::Autolock lock(mLock);
910 if (update_only) {
nikod608a812009-07-16 16:39:53 -0700911 ids = mMetadataUpdated;
Nicolas Catania48290382009-07-10 13:53:06 -0700912 }
913 mMetadataUpdated.clear();
914 }
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700915
nikoa64c8c72009-07-20 15:07:26 -0700916 media::Metadata metadata(reply);
Nicolas Catania48290382009-07-10 13:53:06 -0700917
nikoa64c8c72009-07-20 15:07:26 -0700918 metadata.appendHeader();
919 status = player->getMetadata(ids, reply);
nikod608a812009-07-16 16:39:53 -0700920
921 if (status != OK) {
nikoa64c8c72009-07-20 15:07:26 -0700922 metadata.resetParcel();
Steve Block29357bc2012-01-06 19:20:56 +0000923 ALOGE("getMetadata failed %d", status);
nikod608a812009-07-16 16:39:53 -0700924 return status;
925 }
926
927 // FIXME: Implement filtering on the result. Not critical since
928 // filtering takes place on the update notifications already. This
929 // would be when all the metadata are fetch and a filter is set.
930
nikod608a812009-07-16 16:39:53 -0700931 // Everything is fine, update the metadata length.
nikoa64c8c72009-07-20 15:07:26 -0700932 metadata.updateLength();
nikod608a812009-07-16 16:39:53 -0700933 return OK;
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700934}
935
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800936status_t MediaPlayerService::Client::prepareAsync()
937{
Steve Block3856b092011-10-20 11:56:00 +0100938 ALOGV("[%d] prepareAsync", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800939 sp<MediaPlayerBase> p = getPlayer();
940 if (p == 0) return UNKNOWN_ERROR;
941 status_t ret = p->prepareAsync();
942#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000943 ALOGD("start Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800944 if (ret == NO_ERROR) mAntagonizer->start();
945#endif
946 return ret;
947}
948
949status_t MediaPlayerService::Client::start()
950{
Steve Block3856b092011-10-20 11:56:00 +0100951 ALOGV("[%d] start", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800952 sp<MediaPlayerBase> p = getPlayer();
953 if (p == 0) return UNKNOWN_ERROR;
954 p->setLooping(mLoop);
955 return p->start();
956}
957
958status_t MediaPlayerService::Client::stop()
959{
Steve Block3856b092011-10-20 11:56:00 +0100960 ALOGV("[%d] stop", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800961 sp<MediaPlayerBase> p = getPlayer();
962 if (p == 0) return UNKNOWN_ERROR;
963 return p->stop();
964}
965
966status_t MediaPlayerService::Client::pause()
967{
Steve Block3856b092011-10-20 11:56:00 +0100968 ALOGV("[%d] pause", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800969 sp<MediaPlayerBase> p = getPlayer();
970 if (p == 0) return UNKNOWN_ERROR;
971 return p->pause();
972}
973
974status_t MediaPlayerService::Client::isPlaying(bool* state)
975{
976 *state = false;
977 sp<MediaPlayerBase> p = getPlayer();
978 if (p == 0) return UNKNOWN_ERROR;
979 *state = p->isPlaying();
Steve Block3856b092011-10-20 11:56:00 +0100980 ALOGV("[%d] isPlaying: %d", mConnId, *state);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800981 return NO_ERROR;
982}
983
Wei Jia98160162015-02-04 17:01:11 -0800984status_t MediaPlayerService::Client::setPlaybackRate(float rate)
985{
986 ALOGV("[%d] setPlaybackRate(%f)", mConnId, rate);
987 sp<MediaPlayerBase> p = getPlayer();
988 if (p == 0) return UNKNOWN_ERROR;
989 return p->setPlaybackRate(rate);
990}
991
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800992status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
993{
Steve Block3856b092011-10-20 11:56:00 +0100994 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800995 sp<MediaPlayerBase> p = getPlayer();
996 if (p == 0) return UNKNOWN_ERROR;
997 status_t ret = p->getCurrentPosition(msec);
998 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100999 ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001000 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001001 ALOGE("getCurrentPosition returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001002 }
1003 return ret;
1004}
1005
1006status_t MediaPlayerService::Client::getDuration(int *msec)
1007{
Steve Block3856b092011-10-20 11:56:00 +01001008 ALOGV("getDuration");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001009 sp<MediaPlayerBase> p = getPlayer();
1010 if (p == 0) return UNKNOWN_ERROR;
1011 status_t ret = p->getDuration(msec);
1012 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01001013 ALOGV("[%d] getDuration = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001014 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001015 ALOGE("getDuration returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001016 }
1017 return ret;
1018}
1019
Marco Nelissen6b74d672012-02-28 16:07:44 -08001020status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) {
1021 ALOGV("setNextPlayer");
1022 Mutex::Autolock l(mLock);
1023 sp<Client> c = static_cast<Client*>(player.get());
1024 mNextClient = c;
John Grossman5f7e55e2012-08-24 14:47:25 -07001025
1026 if (c != NULL) {
1027 if (mAudioOutput != NULL) {
1028 mAudioOutput->setNextOutput(c->mAudioOutput);
1029 } else if ((mPlayer != NULL) && !mPlayer->hardwareOutput()) {
1030 ALOGE("no current audio output");
1031 }
1032
1033 if ((mPlayer != NULL) && (mNextClient->getPlayer() != NULL)) {
1034 mPlayer->setNextPlayer(mNextClient->getPlayer());
1035 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001036 }
John Grossman5f7e55e2012-08-24 14:47:25 -07001037
Marco Nelissen6b74d672012-02-28 16:07:44 -08001038 return OK;
1039}
1040
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001041status_t MediaPlayerService::Client::seekTo(int msec)
1042{
Steve Block3856b092011-10-20 11:56:00 +01001043 ALOGV("[%d] seekTo(%d)", mConnId, msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001044 sp<MediaPlayerBase> p = getPlayer();
1045 if (p == 0) return UNKNOWN_ERROR;
1046 return p->seekTo(msec);
1047}
1048
1049status_t MediaPlayerService::Client::reset()
1050{
Steve Block3856b092011-10-20 11:56:00 +01001051 ALOGV("[%d] reset", mConnId);
John Grossmanc795b642012-02-22 15:38:35 -08001052 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001053 sp<MediaPlayerBase> p = getPlayer();
1054 if (p == 0) return UNKNOWN_ERROR;
1055 return p->reset();
1056}
1057
Glenn Kastenfff6d712012-01-12 16:38:12 -08001058status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001059{
Steve Block3856b092011-10-20 11:56:00 +01001060 ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001061 // TODO: for hardware output, call player instead
1062 Mutex::Autolock l(mLock);
1063 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
1064 return NO_ERROR;
1065}
1066
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001067status_t MediaPlayerService::Client::setAudioAttributes_l(const Parcel &parcel)
1068{
1069 if (mAudioAttributes != NULL) { free(mAudioAttributes); }
1070 mAudioAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
1071 unmarshallAudioAttributes(parcel, mAudioAttributes);
1072
1073 ALOGV("setAudioAttributes_l() usage=%d content=%d flags=0x%x tags=%s",
1074 mAudioAttributes->usage, mAudioAttributes->content_type, mAudioAttributes->flags,
1075 mAudioAttributes->tags);
1076
1077 if (mAudioOutput != 0) {
1078 mAudioOutput->setAudioAttributes(mAudioAttributes);
1079 }
1080 return NO_ERROR;
1081}
1082
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001083status_t MediaPlayerService::Client::setLooping(int loop)
1084{
Steve Block3856b092011-10-20 11:56:00 +01001085 ALOGV("[%d] setLooping(%d)", mConnId, loop);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001086 mLoop = loop;
1087 sp<MediaPlayerBase> p = getPlayer();
1088 if (p != 0) return p->setLooping(loop);
1089 return NO_ERROR;
1090}
1091
1092status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
1093{
Steve Block3856b092011-10-20 11:56:00 +01001094 ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
John Grossman761defc2012-02-09 15:09:05 -08001095
1096 // for hardware output, call player instead
1097 sp<MediaPlayerBase> p = getPlayer();
1098 {
1099 Mutex::Autolock l(mLock);
1100 if (p != 0 && p->hardwareOutput()) {
1101 MediaPlayerHWInterface* hwp =
1102 reinterpret_cast<MediaPlayerHWInterface*>(p.get());
1103 return hwp->setVolume(leftVolume, rightVolume);
1104 } else {
1105 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
1106 return NO_ERROR;
1107 }
1108 }
1109
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001110 return NO_ERROR;
1111}
1112
Eric Laurent2beeb502010-07-16 07:43:46 -07001113status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
1114{
Steve Block3856b092011-10-20 11:56:00 +01001115 ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001116 Mutex::Autolock l(mLock);
1117 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
1118 return NO_ERROR;
1119}
1120
1121status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
1122{
Steve Block3856b092011-10-20 11:56:00 +01001123 ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001124 Mutex::Autolock l(mLock);
1125 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1126 return NO_ERROR;
1127}
Nicolas Catania48290382009-07-10 13:53:06 -07001128
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001129status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
Steve Block3856b092011-10-20 11:56:00 +01001130 ALOGV("[%d] setParameter(%d)", mConnId, key);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001131 switch (key) {
1132 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
1133 {
1134 Mutex::Autolock l(mLock);
1135 return setAudioAttributes_l(request);
1136 }
1137 default:
1138 sp<MediaPlayerBase> p = getPlayer();
1139 if (p == 0) { return UNKNOWN_ERROR; }
1140 return p->setParameter(key, request);
1141 }
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001142}
1143
1144status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
Steve Block3856b092011-10-20 11:56:00 +01001145 ALOGV("[%d] getParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001146 sp<MediaPlayerBase> p = getPlayer();
1147 if (p == 0) return UNKNOWN_ERROR;
1148 return p->getParameter(key, reply);
1149}
1150
John Grossmanc795b642012-02-22 15:38:35 -08001151status_t MediaPlayerService::Client::setRetransmitEndpoint(
1152 const struct sockaddr_in* endpoint) {
1153
1154 if (NULL != endpoint) {
1155 uint32_t a = ntohl(endpoint->sin_addr.s_addr);
1156 uint16_t p = ntohs(endpoint->sin_port);
1157 ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId,
1158 (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p);
1159 } else {
1160 ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId);
1161 }
1162
1163 sp<MediaPlayerBase> p = getPlayer();
1164
1165 // Right now, the only valid time to set a retransmit endpoint is before
1166 // player selection has been made (since the presence or absence of a
1167 // retransmit endpoint is going to determine which player is selected during
1168 // setDataSource).
1169 if (p != 0) return INVALID_OPERATION;
1170
1171 if (NULL != endpoint) {
1172 mRetransmitEndpoint = *endpoint;
1173 mRetransmitEndpointValid = true;
1174 } else {
1175 mRetransmitEndpointValid = false;
1176 }
1177
1178 return NO_ERROR;
1179}
1180
John Grossman44a7e422012-06-21 17:29:24 -07001181status_t MediaPlayerService::Client::getRetransmitEndpoint(
1182 struct sockaddr_in* endpoint)
1183{
1184 if (NULL == endpoint)
1185 return BAD_VALUE;
1186
1187 sp<MediaPlayerBase> p = getPlayer();
1188
1189 if (p != NULL)
1190 return p->getRetransmitEndpoint(endpoint);
1191
1192 if (!mRetransmitEndpointValid)
1193 return NO_INIT;
1194
1195 *endpoint = mRetransmitEndpoint;
1196
1197 return NO_ERROR;
1198}
1199
Gloria Wangb483c472011-04-11 17:23:27 -07001200void MediaPlayerService::Client::notify(
1201 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001202{
1203 Client* client = static_cast<Client*>(cookie);
James Dongb8a98252012-08-26 16:13:03 -07001204 if (client == NULL) {
1205 return;
1206 }
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001207
James Dongb8a98252012-08-26 16:13:03 -07001208 sp<IMediaPlayerClient> c;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001209 {
1210 Mutex::Autolock l(client->mLock);
James Dongb8a98252012-08-26 16:13:03 -07001211 c = client->mClient;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001212 if (msg == MEDIA_PLAYBACK_COMPLETE && client->mNextClient != NULL) {
John Grossmancb0b7552012-08-23 17:47:31 -07001213 if (client->mAudioOutput != NULL)
1214 client->mAudioOutput->switchToNextOutput();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001215 client->mNextClient->start();
1216 client->mNextClient->mClient->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj);
1217 }
1218 }
1219
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001220 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001221 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001222 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-07-10 13:53:06 -07001223
1224 if(client->shouldDropMetadata(metadata_type)) {
1225 return;
1226 }
1227
1228 // Update the list of metadata that have changed. getMetadata
1229 // also access mMetadataUpdated and clears it.
1230 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001231 }
James Dongb8a98252012-08-26 16:13:03 -07001232
1233 if (c != NULL) {
1234 ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
1235 c->notify(msg, ext1, ext2, obj);
1236 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001237}
1238
Nicolas Catania48290382009-07-10 13:53:06 -07001239
nikoa64c8c72009-07-20 15:07:26 -07001240bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001241{
Nicolas Catania48290382009-07-10 13:53:06 -07001242 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001243
Nicolas Catania48290382009-07-10 13:53:06 -07001244 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001245 return true;
1246 }
1247
Nicolas Catania48290382009-07-10 13:53:06 -07001248 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001249 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001250 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001251 return true;
1252 }
1253}
1254
Nicolas Catania48290382009-07-10 13:53:06 -07001255
nikoa64c8c72009-07-20 15:07:26 -07001256void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-07-10 13:53:06 -07001257 Mutex::Autolock lock(mLock);
1258 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1259 mMetadataUpdated.add(metadata_type);
1260 }
1261}
1262
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001263#if CALLBACK_ANTAGONIZER
1264const int Antagonizer::interval = 10000; // 10 msecs
1265
1266Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1267 mExit(false), mActive(false), mClient(client), mCb(cb)
1268{
1269 createThread(callbackThread, this);
1270}
1271
1272void Antagonizer::kill()
1273{
1274 Mutex::Autolock _l(mLock);
1275 mActive = false;
1276 mExit = true;
1277 mCondition.wait(mLock);
1278}
1279
1280int Antagonizer::callbackThread(void* user)
1281{
Steve Blockb8a80522011-12-20 16:23:08 +00001282 ALOGD("Antagonizer started");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001283 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1284 while (!p->mExit) {
1285 if (p->mActive) {
Steve Block3856b092011-10-20 11:56:00 +01001286 ALOGV("send event");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001287 p->mCb(p->mClient, 0, 0, 0);
1288 }
1289 usleep(interval);
1290 }
1291 Mutex::Autolock _l(p->mLock);
1292 p->mCondition.signal();
Steve Blockb8a80522011-12-20 16:23:08 +00001293 ALOGD("Antagonizer stopped");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001294 return 0;
1295}
1296#endif
1297
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001298#undef LOG_TAG
1299#define LOG_TAG "AudioSink"
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001300MediaPlayerService::AudioOutput::AudioOutput(int sessionId, int uid, int pid,
1301 const audio_attributes_t* attr)
Andreas Huber20111aa2009-07-14 16:56:47 -07001302 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001303 mCallbackCookie(NULL),
Marco Nelissen6b74d672012-02-28 16:07:44 -08001304 mCallbackData(NULL),
Marco Nelissen4110c102012-03-29 09:31:28 -07001305 mBytesWritten(0),
Eric Laurent1948eb32012-04-13 16:50:19 -07001306 mSessionId(sessionId),
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001307 mUid(uid),
Marco Nelissend457c972014-02-11 08:47:07 -08001308 mPid(pid),
Eric Laurent1948eb32012-04-13 16:50:19 -07001309 mFlags(AUDIO_OUTPUT_FLAG_NONE) {
Steve Block3856b092011-10-20 11:56:00 +01001310 ALOGV("AudioOutput(%d)", sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -07001311 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001312 mLeftVolume = 1.0;
1313 mRightVolume = 1.0;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001314 mPlaybackRatePermille = 1000;
1315 mSampleRateHz = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001316 mMsecsPerFrame = 0;
Eric Laurent2beeb502010-07-16 07:43:46 -07001317 mAuxEffectId = 0;
1318 mSendLevel = 0.0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001319 setMinBufferCount();
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001320 mAttributes = attr;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001321}
1322
1323MediaPlayerService::AudioOutput::~AudioOutput()
1324{
1325 close();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001326 delete mCallbackData;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001327}
1328
1329void MediaPlayerService::AudioOutput::setMinBufferCount()
1330{
1331 char value[PROPERTY_VALUE_MAX];
1332 if (property_get("ro.kernel.qemu", value, 0)) {
1333 mIsOnEmulator = true;
1334 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1335 }
1336}
1337
1338bool MediaPlayerService::AudioOutput::isOnEmulator()
1339{
1340 setMinBufferCount();
1341 return mIsOnEmulator;
1342}
1343
1344int MediaPlayerService::AudioOutput::getMinBufferCount()
1345{
1346 setMinBufferCount();
1347 return mMinBufferCount;
1348}
1349
1350ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1351{
1352 if (mTrack == 0) return NO_INIT;
1353 return mTrack->frameCount() * frameSize();
1354}
1355
1356ssize_t MediaPlayerService::AudioOutput::frameCount() const
1357{
1358 if (mTrack == 0) return NO_INIT;
1359 return mTrack->frameCount();
1360}
1361
1362ssize_t MediaPlayerService::AudioOutput::channelCount() const
1363{
1364 if (mTrack == 0) return NO_INIT;
1365 return mTrack->channelCount();
1366}
1367
1368ssize_t MediaPlayerService::AudioOutput::frameSize() const
1369{
1370 if (mTrack == 0) return NO_INIT;
1371 return mTrack->frameSize();
1372}
1373
1374uint32_t MediaPlayerService::AudioOutput::latency () const
1375{
Eric Laurentdb354e52012-03-05 17:27:11 -08001376 if (mTrack == 0) return 0;
1377 return mTrack->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001378}
1379
1380float MediaPlayerService::AudioOutput::msecsPerFrame() const
1381{
1382 return mMsecsPerFrame;
1383}
1384
Marco Nelissen4110c102012-03-29 09:31:28 -07001385status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001386{
1387 if (mTrack == 0) return NO_INIT;
1388 return mTrack->getPosition(position);
1389}
1390
Lajos Molnar06ad1522014-08-28 07:27:44 -07001391status_t MediaPlayerService::AudioOutput::getTimestamp(AudioTimestamp &ts) const
1392{
1393 if (mTrack == 0) return NO_INIT;
1394 return mTrack->getTimestamp(ts);
1395}
1396
Marco Nelissen4110c102012-03-29 09:31:28 -07001397status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
1398{
1399 if (mTrack == 0) return NO_INIT;
1400 *frameswritten = mBytesWritten / frameSize();
1401 return OK;
1402}
1403
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001404status_t MediaPlayerService::AudioOutput::setParameters(const String8& keyValuePairs)
1405{
1406 if (mTrack == 0) return NO_INIT;
1407 return mTrack->setParameters(keyValuePairs);
1408}
1409
1410String8 MediaPlayerService::AudioOutput::getParameters(const String8& keys)
1411{
1412 if (mTrack == 0) return String8::empty();
1413 return mTrack->getParameters(keys);
1414}
1415
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001416void MediaPlayerService::AudioOutput::setAudioAttributes(const audio_attributes_t * attributes) {
1417 mAttributes = attributes;
1418}
1419
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001420void MediaPlayerService::AudioOutput::deleteRecycledTrack()
1421{
1422 ALOGV("deleteRecycledTrack");
1423
1424 if (mRecycledTrack != 0) {
1425
1426 if (mCallbackData != NULL) {
1427 mCallbackData->setOutput(NULL);
1428 mCallbackData->endTrackSwitch();
1429 }
1430
1431 if ((mRecycledTrack->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
1432 mRecycledTrack->flush();
1433 }
1434 // An offloaded track isn't flushed because the STREAM_END is reported
1435 // slightly prematurely to allow time for the gapless track switch
1436 // but this means that if we decide not to recycle the track there
1437 // could be a small amount of residual data still playing. We leave
1438 // AudioFlinger to drain the track.
1439
1440 mRecycledTrack.clear();
1441 delete mCallbackData;
1442 mCallbackData = NULL;
1443 close();
1444 }
1445}
1446
Andreas Huber20111aa2009-07-14 16:56:47 -07001447status_t MediaPlayerService::AudioOutput::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001448 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1449 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001450 AudioCallback cb, void *cookie,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001451 audio_output_flags_t flags,
1452 const audio_offload_info_t *offloadInfo)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001453{
Andreas Huber20111aa2009-07-14 16:56:47 -07001454 mCallback = cb;
1455 mCallbackCookie = cookie;
1456
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001457 // Check argument "bufferCount" against the mininum buffer count
1458 if (bufferCount < mMinBufferCount) {
Steve Blockb8a80522011-12-20 16:23:08 +00001459 ALOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001460 bufferCount = mMinBufferCount;
1461
1462 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001463 ALOGV("open(%u, %d, 0x%x, 0x%x, %d, %d 0x%x)", sampleRate, channelCount, channelMask,
1464 format, bufferCount, mSessionId, flags);
Glenn Kastenbce50bf2014-02-27 15:29:51 -08001465 size_t frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001466
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001467 // offloading is only supported in callback mode for now.
1468 // offloadInfo must be present if offload flag is set
1469 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1470 ((cb == NULL) || (offloadInfo == NULL))) {
1471 return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001472 }
1473
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001474 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1475 frameCount = 0; // AudioTrack will get frame count from AudioFlinger
1476 } else {
1477 uint32_t afSampleRate;
1478 size_t afFrameCount;
1479
1480 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1481 return NO_INIT;
1482 }
1483 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1484 return NO_INIT;
1485 }
1486
1487 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
1488 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001489
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001490 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
Glenn Kastenab334fd2012-03-14 12:56:06 -07001491 channelMask = audio_channel_out_mask_from_count(channelCount);
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001492 if (0 == channelMask) {
1493 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
1494 return NO_INIT;
1495 }
1496 }
Eric Laurent1948eb32012-04-13 16:50:19 -07001497
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001498 // Check whether we can recycle the track
1499 bool reuse = false;
1500 bool bothOffloaded = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001501
Glenn Kasten2799d742013-05-30 14:33:29 -07001502 if (mRecycledTrack != 0) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001503 // check whether we are switching between two offloaded tracks
1504 bothOffloaded = (flags & mRecycledTrack->getFlags()
1505 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0;
Marco Nelissen67295b52012-06-11 14:52:53 -07001506
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001507 // check if the existing track can be reused as-is, or if a new track needs to be created.
1508 reuse = true;
1509
Marco Nelissen67295b52012-06-11 14:52:53 -07001510 if ((mCallbackData == NULL && mCallback != NULL) ||
1511 (mCallbackData != NULL && mCallback == NULL)) {
1512 // recycled track uses callbacks but the caller wants to use writes, or vice versa
1513 ALOGV("can't chain callback and write");
1514 reuse = false;
1515 } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001516 (mRecycledTrack->channelCount() != (uint32_t)channelCount) ) {
1517 ALOGV("samplerate, channelcount differ: %u/%u Hz, %u/%d ch",
Marco Nelissen67295b52012-06-11 14:52:53 -07001518 mRecycledTrack->getSampleRate(), sampleRate,
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001519 mRecycledTrack->channelCount(), channelCount);
Marco Nelissen67295b52012-06-11 14:52:53 -07001520 reuse = false;
1521 } else if (flags != mFlags) {
1522 ALOGV("output flags differ %08x/%08x", flags, mFlags);
1523 reuse = false;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001524 } else if (mRecycledTrack->format() != format) {
1525 reuse = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001526 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001527 } else {
1528 ALOGV("no track available to recycle");
1529 }
1530
1531 ALOGV_IF(bothOffloaded, "both tracks offloaded");
1532
1533 // If we can't recycle and both tracks are offloaded
1534 // we must close the previous output before opening a new one
1535 if (bothOffloaded && !reuse) {
1536 ALOGV("both offloaded and not recycling");
1537 deleteRecycledTrack();
1538 }
1539
1540 sp<AudioTrack> t;
1541 CallbackData *newcbd = NULL;
1542
1543 // We don't attempt to create a new track if we are recycling an
1544 // offloaded track. But, if we are recycling a non-offloaded or we
1545 // are switching where one is offloaded and one isn't then we create
1546 // the new track in advance so that we can read additional stream info
1547
1548 if (!(reuse && bothOffloaded)) {
1549 ALOGV("creating new AudioTrack");
1550
1551 if (mCallback != NULL) {
1552 newcbd = new CallbackData(this);
1553 t = new AudioTrack(
1554 mStreamType,
1555 sampleRate,
1556 format,
1557 channelMask,
1558 frameCount,
1559 flags,
1560 CallbackWrapper,
1561 newcbd,
1562 0, // notification frames
1563 mSessionId,
1564 AudioTrack::TRANSFER_CALLBACK,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001565 offloadInfo,
Marco Nelissend457c972014-02-11 08:47:07 -08001566 mUid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001567 mPid,
1568 mAttributes);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001569 } else {
1570 t = new AudioTrack(
1571 mStreamType,
1572 sampleRate,
1573 format,
1574 channelMask,
1575 frameCount,
1576 flags,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001577 NULL, // callback
1578 NULL, // user data
1579 0, // notification frames
1580 mSessionId,
1581 AudioTrack::TRANSFER_DEFAULT,
1582 NULL, // offload info
Marco Nelissend457c972014-02-11 08:47:07 -08001583 mUid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001584 mPid,
1585 mAttributes);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001586 }
1587
1588 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
1589 ALOGE("Unable to create audio track");
1590 delete newcbd;
1591 return NO_INIT;
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001592 } else {
1593 // successful AudioTrack initialization implies a legacy stream type was generated
1594 // from the audio attributes
1595 mStreamType = t->streamType();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001596 }
1597 }
1598
1599 if (reuse) {
1600 CHECK(mRecycledTrack != NULL);
1601
1602 if (!bothOffloaded) {
1603 if (mRecycledTrack->frameCount() != t->frameCount()) {
1604 ALOGV("framecount differs: %u/%u frames",
1605 mRecycledTrack->frameCount(), t->frameCount());
1606 reuse = false;
1607 }
1608 }
1609
Marco Nelissen67295b52012-06-11 14:52:53 -07001610 if (reuse) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001611 ALOGV("chaining to next output and recycling track");
Marco Nelissen67295b52012-06-11 14:52:53 -07001612 close();
1613 mTrack = mRecycledTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001614 mRecycledTrack.clear();
Marco Nelissen67295b52012-06-11 14:52:53 -07001615 if (mCallbackData != NULL) {
1616 mCallbackData->setOutput(this);
1617 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001618 delete newcbd;
1619 return OK;
1620 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001621 }
1622
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001623 // we're not going to reuse the track, unblock and flush it
1624 // this was done earlier if both tracks are offloaded
1625 if (!bothOffloaded) {
1626 deleteRecycledTrack();
1627 }
1628
1629 CHECK((t != NULL) && ((mCallback == NULL) || (newcbd != NULL)));
1630
Marco Nelissen67295b52012-06-11 14:52:53 -07001631 mCallbackData = newcbd;
Steve Block3856b092011-10-20 11:56:00 +01001632 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001633 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001634
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001635 mSampleRateHz = sampleRate;
Eric Laurent1948eb32012-04-13 16:50:19 -07001636 mFlags = flags;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001637 mMsecsPerFrame = mPlaybackRatePermille / (float) sampleRate;
Marco Nelissen99448602012-04-02 12:16:49 -07001638 uint32_t pos;
1639 if (t->getPosition(&pos) == OK) {
1640 mBytesWritten = uint64_t(pos) * t->frameSize();
1641 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001642 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001643
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001644 status_t res = NO_ERROR;
1645 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
1646 res = t->setSampleRate(mPlaybackRatePermille * mSampleRateHz / 1000);
1647 if (res == NO_ERROR) {
1648 t->setAuxEffectSendLevel(mSendLevel);
1649 res = t->attachAuxEffect(mAuxEffectId);
1650 }
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001651 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001652 ALOGV("open() DONE status %d", res);
1653 return res;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001654}
1655
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001656status_t MediaPlayerService::AudioOutput::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001657{
Steve Block3856b092011-10-20 11:56:00 +01001658 ALOGV("start");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001659 if (mCallbackData != NULL) {
1660 mCallbackData->endTrackSwitch();
1661 }
Glenn Kasten2799d742013-05-30 14:33:29 -07001662 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001663 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001664 mTrack->setAuxEffectSendLevel(mSendLevel);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001665 return mTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001666 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001667 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001668}
1669
Marco Nelissen6b74d672012-02-28 16:07:44 -08001670void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
1671 mNextOutput = nextOutput;
1672}
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001673
1674
Marco Nelissen6b74d672012-02-28 16:07:44 -08001675void MediaPlayerService::AudioOutput::switchToNextOutput() {
1676 ALOGV("switchToNextOutput");
1677 if (mNextOutput != NULL) {
1678 if (mCallbackData != NULL) {
1679 mCallbackData->beginTrackSwitch();
1680 }
1681 delete mNextOutput->mCallbackData;
1682 mNextOutput->mCallbackData = mCallbackData;
1683 mCallbackData = NULL;
1684 mNextOutput->mRecycledTrack = mTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001685 mTrack.clear();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001686 mNextOutput->mSampleRateHz = mSampleRateHz;
1687 mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
Marco Nelissen4110c102012-03-29 09:31:28 -07001688 mNextOutput->mBytesWritten = mBytesWritten;
Marco Nelissend791e092012-06-11 17:00:59 -07001689 mNextOutput->mFlags = mFlags;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001690 }
1691}
1692
Wei Jia7d3f4df2015-03-03 15:28:00 -08001693ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size, bool blocking)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001694{
Glenn Kastenadad3d72014-02-21 14:51:43 -08001695 LOG_ALWAYS_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
Andreas Huber20111aa2009-07-14 16:56:47 -07001696
Steve Block3856b092011-10-20 11:56:00 +01001697 //ALOGV("write(%p, %u)", buffer, size);
Glenn Kasten2799d742013-05-30 14:33:29 -07001698 if (mTrack != 0) {
Wei Jia7d3f4df2015-03-03 15:28:00 -08001699 ssize_t ret = mTrack->write(buffer, size, blocking);
Andy Hunga31335a2014-08-20 17:37:59 -07001700 if (ret >= 0) {
1701 mBytesWritten += ret;
1702 }
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001703 return ret;
1704 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001705 return NO_INIT;
1706}
1707
1708void MediaPlayerService::AudioOutput::stop()
1709{
Steve Block3856b092011-10-20 11:56:00 +01001710 ALOGV("stop");
Glenn Kasten2799d742013-05-30 14:33:29 -07001711 if (mTrack != 0) mTrack->stop();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001712}
1713
1714void MediaPlayerService::AudioOutput::flush()
1715{
Steve Block3856b092011-10-20 11:56:00 +01001716 ALOGV("flush");
Glenn Kasten2799d742013-05-30 14:33:29 -07001717 if (mTrack != 0) mTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001718}
1719
1720void MediaPlayerService::AudioOutput::pause()
1721{
Steve Block3856b092011-10-20 11:56:00 +01001722 ALOGV("pause");
Glenn Kasten2799d742013-05-30 14:33:29 -07001723 if (mTrack != 0) mTrack->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001724}
1725
1726void MediaPlayerService::AudioOutput::close()
1727{
Steve Block3856b092011-10-20 11:56:00 +01001728 ALOGV("close");
Glenn Kasten2799d742013-05-30 14:33:29 -07001729 mTrack.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001730}
1731
1732void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1733{
Steve Block3856b092011-10-20 11:56:00 +01001734 ALOGV("setVolume(%f, %f)", left, right);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001735 mLeftVolume = left;
1736 mRightVolume = right;
Glenn Kasten2799d742013-05-30 14:33:29 -07001737 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001738 mTrack->setVolume(left, right);
1739 }
1740}
1741
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001742status_t MediaPlayerService::AudioOutput::setPlaybackRatePermille(int32_t ratePermille)
1743{
1744 ALOGV("setPlaybackRatePermille(%d)", ratePermille);
1745 status_t res = NO_ERROR;
Glenn Kasten2799d742013-05-30 14:33:29 -07001746 if (mTrack != 0) {
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001747 res = mTrack->setSampleRate(ratePermille * mSampleRateHz / 1000);
1748 } else {
1749 res = NO_INIT;
1750 }
1751 mPlaybackRatePermille = ratePermille;
1752 if (mSampleRateHz != 0) {
1753 mMsecsPerFrame = mPlaybackRatePermille / (float) mSampleRateHz;
1754 }
1755 return res;
1756}
1757
Eric Laurent2beeb502010-07-16 07:43:46 -07001758status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1759{
Steve Block3856b092011-10-20 11:56:00 +01001760 ALOGV("setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001761 mSendLevel = level;
Glenn Kasten2799d742013-05-30 14:33:29 -07001762 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001763 return mTrack->setAuxEffectSendLevel(level);
1764 }
1765 return NO_ERROR;
1766}
1767
1768status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1769{
Steve Block3856b092011-10-20 11:56:00 +01001770 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001771 mAuxEffectId = effectId;
Glenn Kasten2799d742013-05-30 14:33:29 -07001772 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001773 return mTrack->attachAuxEffect(effectId);
1774 }
1775 return NO_ERROR;
1776}
1777
Andreas Huber20111aa2009-07-14 16:56:47 -07001778// static
1779void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastend217a8c2011-06-01 15:20:35 -07001780 int event, void *cookie, void *info) {
Steve Block3856b092011-10-20 11:56:00 +01001781 //ALOGV("callbackwrapper");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001782 CallbackData *data = (CallbackData*)cookie;
1783 data->lock();
1784 AudioOutput *me = data->getOutput();
Andreas Huber20111aa2009-07-14 16:56:47 -07001785 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001786 if (me == NULL) {
1787 // no output set, likely because the track was scheduled to be reused
1788 // by another player, but the format turned out to be incompatible.
1789 data->unlock();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001790 if (buffer != NULL) {
1791 buffer->size = 0;
1792 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001793 return;
1794 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001795
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001796 switch(event) {
1797 case AudioTrack::EVENT_MORE_DATA: {
1798 size_t actualSize = (*me->mCallback)(
1799 me, buffer->raw, buffer->size, me->mCallbackCookie,
1800 CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001801
aarti jadhav-gaikwadf2575572014-08-13 15:04:39 +05301802 if ((me->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0 &&
1803 actualSize == 0 && buffer->size > 0 && me->mNextOutput == NULL) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001804 // We've reached EOS but the audio track is not stopped yet,
1805 // keep playing silence.
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08001806
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001807 memset(buffer->raw, 0, buffer->size);
1808 actualSize = buffer->size;
1809 }
1810
1811 buffer->size = actualSize;
1812 } break;
1813
1814
1815 case AudioTrack::EVENT_STREAM_END:
1816 ALOGV("callbackwrapper: deliver EVENT_STREAM_END");
1817 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
1818 me->mCallbackCookie, CB_EVENT_STREAM_END);
1819 break;
1820
1821 case AudioTrack::EVENT_NEW_IAUDIOTRACK :
1822 ALOGV("callbackwrapper: deliver EVENT_TEAR_DOWN");
1823 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
1824 me->mCallbackCookie, CB_EVENT_TEAR_DOWN);
1825 break;
1826
1827 default:
1828 ALOGE("received unknown event type: %d inside CallbackWrapper !", event);
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001829 }
1830
Marco Nelissen6b74d672012-02-28 16:07:44 -08001831 data->unlock();
Andreas Huber20111aa2009-07-14 16:56:47 -07001832}
1833
Marco Nelissen4110c102012-03-29 09:31:28 -07001834int MediaPlayerService::AudioOutput::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001835{
1836 return mSessionId;
1837}
1838
Eric Laurent6f59db12013-07-26 17:16:50 -07001839uint32_t MediaPlayerService::AudioOutput::getSampleRate() const
1840{
1841 if (mTrack == 0) return 0;
1842 return mTrack->getSampleRate();
1843}
1844
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001845////////////////////////////////////////////////////////////////////////////////
1846
1847struct CallbackThread : public Thread {
1848 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1849 MediaPlayerBase::AudioSink::AudioCallback cb,
1850 void *cookie);
1851
1852protected:
1853 virtual ~CallbackThread();
1854
1855 virtual bool threadLoop();
1856
1857private:
1858 wp<MediaPlayerBase::AudioSink> mSink;
1859 MediaPlayerBase::AudioSink::AudioCallback mCallback;
1860 void *mCookie;
1861 void *mBuffer;
1862 size_t mBufferSize;
1863
1864 CallbackThread(const CallbackThread &);
1865 CallbackThread &operator=(const CallbackThread &);
1866};
1867
1868CallbackThread::CallbackThread(
1869 const wp<MediaPlayerBase::AudioSink> &sink,
1870 MediaPlayerBase::AudioSink::AudioCallback cb,
1871 void *cookie)
1872 : mSink(sink),
1873 mCallback(cb),
1874 mCookie(cookie),
1875 mBuffer(NULL),
1876 mBufferSize(0) {
1877}
1878
1879CallbackThread::~CallbackThread() {
1880 if (mBuffer) {
1881 free(mBuffer);
1882 mBuffer = NULL;
1883 }
1884}
1885
1886bool CallbackThread::threadLoop() {
1887 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
1888 if (sink == NULL) {
1889 return false;
1890 }
1891
1892 if (mBuffer == NULL) {
1893 mBufferSize = sink->bufferSize();
1894 mBuffer = malloc(mBufferSize);
1895 }
1896
1897 size_t actualSize =
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001898 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie,
1899 MediaPlayerBase::AudioSink::CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001900
1901 if (actualSize > 0) {
1902 sink->write(mBuffer, actualSize);
Andy Hunga31335a2014-08-20 17:37:59 -07001903 // Could return false on sink->write() error or short count.
1904 // Not necessarily appropriate but would work for AudioCache behavior.
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001905 }
1906
1907 return true;
1908}
1909
1910////////////////////////////////////////////////////////////////////////////////
1911
Gloria Wang7cf180c2011-02-19 18:37:57 -08001912void MediaPlayerService::addBatteryData(uint32_t params)
1913{
1914 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08001915
1916 int32_t time = systemTime() / 1000000L;
1917
1918 // change audio output devices. This notification comes from AudioFlinger
1919 if ((params & kBatteryDataSpeakerOn)
1920 || (params & kBatteryDataOtherAudioDeviceOn)) {
1921
1922 int deviceOn[NUM_AUDIO_DEVICES];
1923 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1924 deviceOn[i] = 0;
1925 }
1926
1927 if ((params & kBatteryDataSpeakerOn)
1928 && (params & kBatteryDataOtherAudioDeviceOn)) {
1929 deviceOn[SPEAKER_AND_OTHER] = 1;
1930 } else if (params & kBatteryDataSpeakerOn) {
1931 deviceOn[SPEAKER] = 1;
1932 } else {
1933 deviceOn[OTHER_AUDIO_DEVICE] = 1;
1934 }
1935
1936 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1937 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
1938
1939 if (mBatteryAudio.refCount > 0) { // if playing audio
1940 if (!deviceOn[i]) {
1941 mBatteryAudio.lastTime[i] += time;
1942 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1943 mBatteryAudio.lastTime[i] = 0;
1944 } else {
1945 mBatteryAudio.lastTime[i] = 0 - time;
1946 }
1947 }
1948
1949 mBatteryAudio.deviceOn[i] = deviceOn[i];
1950 }
1951 }
1952 return;
1953 }
1954
Marco Nelissenb7848f12014-12-04 08:57:56 -08001955 // an audio stream is started
Gloria Wang9ee159b2011-02-24 14:51:45 -08001956 if (params & kBatteryDataAudioFlingerStart) {
1957 // record the start time only if currently no other audio
1958 // is being played
1959 if (mBatteryAudio.refCount == 0) {
1960 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1961 if (mBatteryAudio.deviceOn[i]) {
1962 mBatteryAudio.lastTime[i] -= time;
1963 }
1964 }
1965 }
1966
1967 mBatteryAudio.refCount ++;
1968 return;
1969
1970 } else if (params & kBatteryDataAudioFlingerStop) {
1971 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001972 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001973 return;
1974 }
1975
1976 // record the stop time only if currently this is the only
1977 // audio being played
1978 if (mBatteryAudio.refCount == 1) {
1979 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1980 if (mBatteryAudio.deviceOn[i]) {
1981 mBatteryAudio.lastTime[i] += time;
1982 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1983 mBatteryAudio.lastTime[i] = 0;
1984 }
1985 }
1986 }
1987
1988 mBatteryAudio.refCount --;
1989 return;
1990 }
1991
Gloria Wang7cf180c2011-02-19 18:37:57 -08001992 int uid = IPCThreadState::self()->getCallingUid();
1993 if (uid == AID_MEDIA) {
1994 return;
1995 }
1996 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08001997
1998 if (index < 0) { // create a new entry for this UID
1999 BatteryUsageInfo info;
2000 info.audioTotalTime = 0;
2001 info.videoTotalTime = 0;
2002 info.audioLastTime = 0;
2003 info.videoLastTime = 0;
2004 info.refCount = 0;
2005
Gloria Wang9ee159b2011-02-24 14:51:45 -08002006 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00002007 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08002008 return;
2009 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08002010 }
2011
2012 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
2013
2014 if (params & kBatteryDataCodecStarted) {
2015 if (params & kBatteryDataTrackAudio) {
2016 info.audioLastTime -= time;
2017 info.refCount ++;
2018 }
2019 if (params & kBatteryDataTrackVideo) {
2020 info.videoLastTime -= time;
2021 info.refCount ++;
2022 }
2023 } else {
2024 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002025 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002026 return;
2027 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00002028 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002029 mBatteryData.removeItem(uid);
2030 return;
2031 }
2032
2033 if (params & kBatteryDataTrackAudio) {
2034 info.audioLastTime += time;
2035 info.refCount --;
2036 }
2037 if (params & kBatteryDataTrackVideo) {
2038 info.videoLastTime += time;
2039 info.refCount --;
2040 }
2041
2042 // no stream is being played by this UID
2043 if (info.refCount == 0) {
2044 info.audioTotalTime += info.audioLastTime;
2045 info.audioLastTime = 0;
2046 info.videoTotalTime += info.videoLastTime;
2047 info.videoLastTime = 0;
2048 }
2049 }
2050}
2051
2052status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
2053 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002054
2055 // audio output devices usage
2056 int32_t time = systemTime() / 1000000L; //in ms
2057 int32_t totalTime;
2058
2059 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2060 totalTime = mBatteryAudio.totalTime[i];
2061
2062 if (mBatteryAudio.deviceOn[i]
2063 && (mBatteryAudio.lastTime[i] != 0)) {
2064 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
2065 totalTime += tmpTime;
2066 }
2067
2068 reply->writeInt32(totalTime);
2069 // reset the total time
2070 mBatteryAudio.totalTime[i] = 0;
2071 }
2072
2073 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08002074 BatteryUsageInfo info;
2075 int size = mBatteryData.size();
2076
2077 reply->writeInt32(size);
2078 int i = 0;
2079
2080 while (i < size) {
2081 info = mBatteryData.valueAt(i);
2082
2083 reply->writeInt32(mBatteryData.keyAt(i)); //UID
2084 reply->writeInt32(info.audioTotalTime);
2085 reply->writeInt32(info.videoTotalTime);
2086
2087 info.audioTotalTime = 0;
2088 info.videoTotalTime = 0;
2089
2090 // remove the UID entry where no stream is being played
2091 if (info.refCount <= 0) {
2092 mBatteryData.removeItemsAt(i);
2093 size --;
2094 i --;
2095 }
2096 i++;
2097 }
2098 return NO_ERROR;
2099}
nikoa64c8c72009-07-20 15:07:26 -07002100} // namespace android