blob: 3e2d9673081989aeaef2ae248bdf7984b75b05cb [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 Agopianb1e7cd12013-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>
Lajos Molnar06ad1522014-08-28 07:27:44 -070045#include <utils/Timers.h>
Nicolas Catania1d187f12009-05-12 23:25:55 -070046#include <utils/Vector.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080047
Andreas Huber1b86fe02014-01-29 11:13:26 -080048#include <media/IMediaHTTPService.h>
Jeff Brown2013a542012-09-04 21:38:42 -070049#include <media/IRemoteDisplay.h>
50#include <media/IRemoteDisplayClient.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080051#include <media/MediaPlayerInterface.h>
52#include <media/mediarecorder.h>
53#include <media/MediaMetadataRetrieverInterface.h>
nikoa64c8c72009-07-20 15:07:26 -070054#include <media/Metadata.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080055#include <media/AudioTrack.h>
James Dong8635b7b2011-03-14 17:01:38 -070056#include <media/MemoryLeakTrackUtil.h>
Lajos Molnar1381d4b2014-08-07 15:18:35 -070057#include <media/stagefright/MediaCodecList.h>
Eric Laurent9cb839a2011-09-27 09:48:56 -070058#include <media/stagefright/MediaErrors.h>
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +010059#include <media/stagefright/AudioPlayer.h>
60#include <media/stagefright/foundation/ADebug.h>
Marco Nelissenf09611f2015-02-13 14:12:42 -080061#include <media/stagefright/foundation/ALooperRoster.h>
Ruben Brunk99e69712015-05-26 17:25:07 -070062#include <mediautils/BatteryNotifier.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.
Ruben Brunk99e69712015-05-26 17:25:07 -0700290 BatteryNotifier& notifier(BatteryNotifier::getInstance());
291 notifier.noteResetVideo();
292 notifier.noteResetAudio();
Chong Zhang0b30fd42014-07-23 14:46:05 -0700293
John Grossman44a7e422012-06-21 17:29:24 -0700294 MediaPlayerFactory::registerBuiltinFactories();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800295}
296
297MediaPlayerService::~MediaPlayerService()
298{
Steve Block3856b092011-10-20 11:56:00 +0100299 ALOGV("MediaPlayerService destroyed");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800300}
301
Svet Ganovbe71aa22015-04-28 12:06:02 -0700302sp<IMediaRecorder> MediaPlayerService::createMediaRecorder(const String16 &opPackageName)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800303{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800304 pid_t pid = IPCThreadState::self()->getCallingPid();
Svet Ganovbe71aa22015-04-28 12:06:02 -0700305 sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid, opPackageName);
Gloria Wangdac6a312009-10-29 15:46:37 -0700306 wp<MediaRecorderClient> w = recorder;
307 Mutex::Autolock lock(mLock);
308 mMediaRecorderClients.add(w);
Steve Block3856b092011-10-20 11:56:00 +0100309 ALOGV("Create new media recorder client from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800310 return recorder;
311}
312
Gloria Wangdac6a312009-10-29 15:46:37 -0700313void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client)
314{
315 Mutex::Autolock lock(mLock);
316 mMediaRecorderClients.remove(client);
Steve Block3856b092011-10-20 11:56:00 +0100317 ALOGV("Delete media recorder client");
Gloria Wangdac6a312009-10-29 15:46:37 -0700318}
319
Glenn Kastenf37971f2012-02-03 11:06:53 -0800320sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800321{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800322 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800323 sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
Steve Block3856b092011-10-20 11:56:00 +0100324 ALOGV("Create new media retriever from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800325 return retriever;
326}
327
Glenn Kastenf37971f2012-02-03 11:06:53 -0800328sp<IMediaPlayer> MediaPlayerService::create(const sp<IMediaPlayerClient>& client,
Dave Burked681bbb2011-08-30 14:39:17 +0100329 int audioSessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800330{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800331 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800332 int32_t connId = android_atomic_inc(&mNextConnId);
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700333
334 sp<Client> c = new Client(
335 this, pid, connId, client, audioSessionId,
336 IPCThreadState::self()->getCallingUid());
337
Steve Block3856b092011-10-20 11:56:00 +0100338 ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid,
Dave Burked681bbb2011-08-30 14:39:17 +0100339 IPCThreadState::self()->getCallingUid());
340
341 wp<Client> w = c;
342 {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800343 Mutex::Autolock lock(mLock);
344 mClients.add(w);
345 }
Andreas Hubere2b10282010-11-23 11:41:34 -0800346 return c;
347}
348
Lajos Molnar1381d4b2014-08-07 15:18:35 -0700349sp<IMediaCodecList> MediaPlayerService::getCodecList() const {
350 return MediaCodecList::getLocalInstance();
351}
352
Andreas Huber318ad9c2009-10-15 13:46:54 -0700353sp<IOMX> MediaPlayerService::getOMX() {
354 Mutex::Autolock autoLock(mLock);
355
356 if (mOMX.get() == NULL) {
357 mOMX = new OMX;
358 }
359
360 return mOMX;
Andreas Huber20111aa2009-07-14 16:56:47 -0700361}
362
Andreas Hubered3e3e02012-03-26 11:13:27 -0700363sp<ICrypto> MediaPlayerService::makeCrypto() {
Andreas Huber1bd139a2012-04-03 14:19:20 -0700364 return new Crypto;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700365}
366
Jeff Tinkercc82dc62013-02-08 10:18:35 -0800367sp<IDrm> MediaPlayerService::makeDrm() {
368 return new Drm;
369}
370
Andreas Huber279dcd82013-01-30 10:41:25 -0800371sp<IHDCP> MediaPlayerService::makeHDCP(bool createEncryptionModule) {
372 return new HDCP(createEncryptionModule);
Andreas Huber59451f82012-09-18 10:36:32 -0700373}
374
Jeff Brown2013a542012-09-04 21:38:42 -0700375sp<IRemoteDisplay> MediaPlayerService::listenForRemoteDisplay(
Svet Ganovbe71aa22015-04-28 12:06:02 -0700376 const String16 &opPackageName,
Jeff Brown2013a542012-09-04 21:38:42 -0700377 const sp<IRemoteDisplayClient>& client, const String8& iface) {
Jeff Brownaba33d52012-09-07 17:38:58 -0700378 if (!checkPermission("android.permission.CONTROL_WIFI_DISPLAY")) {
379 return NULL;
380 }
381
Svet Ganovbe71aa22015-04-28 12:06:02 -0700382 return new RemoteDisplay(opPackageName, client, iface.string());
Jeff Brown2013a542012-09-04 21:38:42 -0700383}
384
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800385status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
386{
387 const size_t SIZE = 256;
388 char buffer[SIZE];
389 String8 result;
390
391 result.append(" AudioOutput\n");
392 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n",
393 mStreamType, mLeftVolume, mRightVolume);
394 result.append(buffer);
395 snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
Eric Laurentdb354e52012-03-05 17:27:11 -0800396 mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800397 result.append(buffer);
Eric Laurent2beeb502010-07-16 07:43:46 -0700398 snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
399 mAuxEffectId, mSendLevel);
400 result.append(buffer);
401
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800402 ::write(fd, result.string(), result.size());
403 if (mTrack != 0) {
404 mTrack->dump(fd, args);
405 }
406 return NO_ERROR;
407}
408
Lajos Molnar6d339f12015-04-17 16:15:53 -0700409status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800410{
411 const size_t SIZE = 256;
412 char buffer[SIZE];
413 String8 result;
414 result.append(" Client\n");
415 snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n",
416 mPid, mConnId, mStatus, mLoop?"true": "false");
417 result.append(buffer);
418 write(fd, result.string(), result.size());
Andreas Hubera0b1d4b2011-06-07 15:52:25 -0700419 if (mPlayer != NULL) {
420 mPlayer->dump(fd, args);
421 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800422 if (mAudioOutput != 0) {
423 mAudioOutput->dump(fd, args);
424 }
425 write(fd, "\n", 1);
426 return NO_ERROR;
427}
428
Marco Nelissenf09611f2015-02-13 14:12:42 -0800429/**
430 * The only arguments this understands right now are -c, -von and -voff,
431 * which are parsed by ALooperRoster::dump()
432 */
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800433status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
434{
435 const size_t SIZE = 256;
436 char buffer[SIZE];
437 String8 result;
Ravi Kumar Alamanda330c8e32014-12-22 10:05:29 +0530438 SortedVector< sp<Client> > clients; //to serialise the mutex unlock & client destruction.
439 SortedVector< sp<MediaRecorderClient> > mediaRecorderClients;
440
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800441 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
442 snprintf(buffer, SIZE, "Permission Denial: "
443 "can't dump MediaPlayerService from pid=%d, uid=%d\n",
444 IPCThreadState::self()->getCallingPid(),
445 IPCThreadState::self()->getCallingUid());
446 result.append(buffer);
447 } else {
448 Mutex::Autolock lock(mLock);
449 for (int i = 0, n = mClients.size(); i < n; ++i) {
450 sp<Client> c = mClients[i].promote();
451 if (c != 0) c->dump(fd, args);
Ravi Kumar Alamanda330c8e32014-12-22 10:05:29 +0530452 clients.add(c);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800453 }
James Dongb9141222010-07-08 11:16:11 -0700454 if (mMediaRecorderClients.size() == 0) {
455 result.append(" No media recorder client\n\n");
456 } else {
457 for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
458 sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
James Donge579e282011-10-18 22:29:20 -0700459 if (c != 0) {
460 snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid);
461 result.append(buffer);
462 write(fd, result.string(), result.size());
463 result = "\n";
464 c->dump(fd, args);
Ravi Kumar Alamanda330c8e32014-12-22 10:05:29 +0530465 mediaRecorderClients.add(c);
James Donge579e282011-10-18 22:29:20 -0700466 }
James Dongb9141222010-07-08 11:16:11 -0700467 }
Gloria Wangdac6a312009-10-29 15:46:37 -0700468 }
469
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800470 result.append(" Files opened and/or mapped:\n");
Marco Nelissenf09611f2015-02-13 14:12:42 -0800471 snprintf(buffer, SIZE, "/proc/%d/maps", getpid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800472 FILE *f = fopen(buffer, "r");
473 if (f) {
474 while (!feof(f)) {
475 fgets(buffer, SIZE, f);
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700476 if (strstr(buffer, " /storage/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800477 strstr(buffer, " /system/sounds/") ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700478 strstr(buffer, " /data/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800479 strstr(buffer, " /system/media/")) {
480 result.append(" ");
481 result.append(buffer);
482 }
483 }
484 fclose(f);
485 } else {
486 result.append("couldn't open ");
487 result.append(buffer);
488 result.append("\n");
489 }
490
Marco Nelissenf09611f2015-02-13 14:12:42 -0800491 snprintf(buffer, SIZE, "/proc/%d/fd", getpid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800492 DIR *d = opendir(buffer);
493 if (d) {
494 struct dirent *ent;
495 while((ent = readdir(d)) != NULL) {
496 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
Marco Nelissenf09611f2015-02-13 14:12:42 -0800497 snprintf(buffer, SIZE, "/proc/%d/fd/%s", getpid(), ent->d_name);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800498 struct stat s;
499 if (lstat(buffer, &s) == 0) {
500 if ((s.st_mode & S_IFMT) == S_IFLNK) {
501 char linkto[256];
502 int len = readlink(buffer, linkto, sizeof(linkto));
503 if(len > 0) {
504 if(len > 255) {
505 linkto[252] = '.';
506 linkto[253] = '.';
507 linkto[254] = '.';
508 linkto[255] = 0;
509 } else {
510 linkto[len] = 0;
511 }
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700512 if (strstr(linkto, "/storage/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800513 strstr(linkto, "/system/sounds/") == linkto ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700514 strstr(linkto, "/data/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800515 strstr(linkto, "/system/media/") == linkto) {
516 result.append(" ");
517 result.append(buffer);
518 result.append(" -> ");
519 result.append(linkto);
520 result.append("\n");
521 }
522 }
523 } else {
524 result.append(" unexpected type for ");
525 result.append(buffer);
526 result.append("\n");
527 }
528 }
529 }
530 }
531 closedir(d);
532 } else {
533 result.append("couldn't open ");
534 result.append(buffer);
535 result.append("\n");
536 }
537
Marco Nelissenf09611f2015-02-13 14:12:42 -0800538 gLooperRoster.dump(fd, args);
539
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800540 bool dumpMem = false;
541 for (size_t i = 0; i < args.size(); i++) {
542 if (args[i] == String16("-m")) {
543 dumpMem = true;
544 }
545 }
546 if (dumpMem) {
James Dong8635b7b2011-03-14 17:01:38 -0700547 dumpMemoryAddresses(fd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800548 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800549 }
550 write(fd, result.string(), result.size());
551 return NO_ERROR;
552}
553
554void MediaPlayerService::removeClient(wp<Client> client)
555{
556 Mutex::Autolock lock(mLock);
557 mClients.remove(client);
558}
559
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700560MediaPlayerService::Client::Client(
561 const sp<MediaPlayerService>& service, pid_t pid,
562 int32_t connId, const sp<IMediaPlayerClient>& client,
563 int audioSessionId, uid_t uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800564{
Steve Block3856b092011-10-20 11:56:00 +0100565 ALOGV("Client(%d) constructor", connId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800566 mPid = pid;
567 mConnId = connId;
568 mService = service;
569 mClient = client;
570 mLoop = false;
571 mStatus = NO_INIT;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700572 mAudioSessionId = audioSessionId;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700573 mUID = uid;
John Grossmanc795b642012-02-22 15:38:35 -0800574 mRetransmitEndpointValid = false;
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700575 mAudioAttributes = NULL;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700576
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800577#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000578 ALOGD("create Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800579 mAntagonizer = new Antagonizer(notify, this);
580#endif
581}
582
583MediaPlayerService::Client::~Client()
584{
Steve Block3856b092011-10-20 11:56:00 +0100585 ALOGV("Client(%d) destructor pid = %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800586 mAudioOutput.clear();
587 wp<Client> client(this);
588 disconnect();
589 mService->removeClient(client);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700590 if (mAudioAttributes != NULL) {
591 free(mAudioAttributes);
592 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800593}
594
595void MediaPlayerService::Client::disconnect()
596{
Steve Block3856b092011-10-20 11:56:00 +0100597 ALOGV("disconnect(%d) from pid %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800598 // grab local reference and clear main reference to prevent future
599 // access to object
600 sp<MediaPlayerBase> p;
601 {
602 Mutex::Autolock l(mLock);
603 p = mPlayer;
beanzdcfefde2012-11-05 09:51:43 +0800604 mClient.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800605 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700606
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800607 mPlayer.clear();
608
609 // clear the notification to prevent callbacks to dead client
610 // and reset the player. We assume the player will serialize
611 // access to itself if necessary.
612 if (p != 0) {
613 p->setNotifyCallback(0, 0);
614#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000615 ALOGD("kill Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800616 mAntagonizer->kill();
617#endif
618 p->reset();
619 }
620
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700621 disconnectNativeWindow();
622
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800623 IPCThreadState::self()->flushCommands();
624}
625
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800626sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
627{
628 // determine if we have the right player type
629 sp<MediaPlayerBase> p = mPlayer;
630 if ((p != NULL) && (p->playerType() != playerType)) {
Steve Block3856b092011-10-20 11:56:00 +0100631 ALOGV("delete player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800632 p.clear();
633 }
634 if (p == NULL) {
John Grossman44a7e422012-06-21 17:29:24 -0700635 p = MediaPlayerFactory::createPlayer(playerType, this, notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800636 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700637
Jason Simmonsdb29e522011-08-12 13:46:55 -0700638 if (p != NULL) {
639 p->setUID(mUID);
640 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700641
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800642 return p;
643}
644
John Grossmanc795b642012-02-22 15:38:35 -0800645sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre(
646 player_type playerType)
647{
648 ALOGV("player type = %d", playerType);
649
650 // create the right type of player
651 sp<MediaPlayerBase> p = createPlayer(playerType);
652 if (p == NULL) {
653 return p;
654 }
655
656 if (!p->hardwareOutput()) {
Marco Nelissend457c972014-02-11 08:47:07 -0800657 mAudioOutput = new AudioOutput(mAudioSessionId, IPCThreadState::self()->getCallingUid(),
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700658 mPid, mAudioAttributes);
John Grossmanc795b642012-02-22 15:38:35 -0800659 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
660 }
661
662 return p;
663}
664
665void MediaPlayerService::Client::setDataSource_post(
666 const sp<MediaPlayerBase>& p,
667 status_t status)
668{
669 ALOGV(" setDataSource");
670 mStatus = status;
671 if (mStatus != OK) {
672 ALOGE(" error: %d", mStatus);
673 return;
674 }
675
676 // Set the re-transmission endpoint if one was chosen.
677 if (mRetransmitEndpointValid) {
678 mStatus = p->setRetransmitEndpoint(&mRetransmitEndpoint);
679 if (mStatus != NO_ERROR) {
680 ALOGE("setRetransmitEndpoint error: %d", mStatus);
681 }
682 }
683
684 if (mStatus == OK) {
685 mPlayer = p;
686 }
687}
688
Andreas Huber2db84552010-01-28 11:19:57 -0800689status_t MediaPlayerService::Client::setDataSource(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800690 const sp<IMediaHTTPService> &httpService,
691 const char *url,
692 const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800693{
Steve Block3856b092011-10-20 11:56:00 +0100694 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800695 if (url == NULL)
696 return UNKNOWN_ERROR;
697
Dave Burked681bbb2011-08-30 14:39:17 +0100698 if ((strncmp(url, "http://", 7) == 0) ||
699 (strncmp(url, "https://", 8) == 0) ||
700 (strncmp(url, "rtsp://", 7) == 0)) {
701 if (!checkPermission("android.permission.INTERNET")) {
702 return PERMISSION_DENIED;
703 }
704 }
705
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800706 if (strncmp(url, "content://", 10) == 0) {
707 // get a filedescriptor for the content Uri and
708 // pass it to the setDataSource(fd) method
709
710 String16 url16(url);
711 int fd = android::openContentProviderFile(url16);
712 if (fd < 0)
713 {
Steve Block29357bc2012-01-06 19:20:56 +0000714 ALOGE("Couldn't open fd for %s", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800715 return UNKNOWN_ERROR;
716 }
717 setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
718 close(fd);
719 return mStatus;
720 } else {
John Grossman44a7e422012-06-21 17:29:24 -0700721 player_type playerType = MediaPlayerFactory::getPlayerType(this, url);
John Grossmanc795b642012-02-22 15:38:35 -0800722 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
723 if (p == NULL) {
724 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800725 }
726
Andreas Huber1b86fe02014-01-29 11:13:26 -0800727 setDataSource_post(p, p->setDataSource(httpService, url, headers));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800728 return mStatus;
729 }
730}
731
732status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
733{
Steve Block3856b092011-10-20 11:56:00 +0100734 ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800735 struct stat sb;
736 int ret = fstat(fd, &sb);
737 if (ret != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000738 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800739 return UNKNOWN_ERROR;
740 }
741
Ying Wang5fde15d2015-04-20 22:13:22 -0700742 ALOGV("st_dev = %llu", static_cast<uint64_t>(sb.st_dev));
Steve Block3856b092011-10-20 11:56:00 +0100743 ALOGV("st_mode = %u", sb.st_mode);
Mark Salyzyn77342f72014-06-18 16:31:32 -0700744 ALOGV("st_uid = %lu", static_cast<unsigned long>(sb.st_uid));
745 ALOGV("st_gid = %lu", static_cast<unsigned long>(sb.st_gid));
Steve Block3856b092011-10-20 11:56:00 +0100746 ALOGV("st_size = %llu", sb.st_size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800747
748 if (offset >= sb.st_size) {
Steve Block29357bc2012-01-06 19:20:56 +0000749 ALOGE("offset error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800750 ::close(fd);
751 return UNKNOWN_ERROR;
752 }
753 if (offset + length > sb.st_size) {
754 length = sb.st_size - offset;
Steve Block3856b092011-10-20 11:56:00 +0100755 ALOGV("calculated length = %lld", length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800756 }
757
John Grossman44a7e422012-06-21 17:29:24 -0700758 player_type playerType = MediaPlayerFactory::getPlayerType(this,
759 fd,
760 offset,
761 length);
John Grossmanc795b642012-02-22 15:38:35 -0800762 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
763 if (p == NULL) {
764 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800765 }
766
767 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800768 setDataSource_post(p, p->setDataSource(fd, offset, length));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800769 return mStatus;
770}
771
Andreas Hubere2b10282010-11-23 11:41:34 -0800772status_t MediaPlayerService::Client::setDataSource(
773 const sp<IStreamSource> &source) {
774 // create the right type of player
John Grossman44a7e422012-06-21 17:29:24 -0700775 player_type playerType = MediaPlayerFactory::getPlayerType(this, source);
John Grossmanc795b642012-02-22 15:38:35 -0800776 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
Andreas Hubere2b10282010-11-23 11:41:34 -0800777 if (p == NULL) {
778 return NO_INIT;
779 }
780
Andreas Hubere2b10282010-11-23 11:41:34 -0800781 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800782 setDataSource_post(p, p->setDataSource(source));
Andreas Hubere2b10282010-11-23 11:41:34 -0800783 return mStatus;
784}
785
Chris Watkins99f31602015-03-20 13:06:33 -0700786status_t MediaPlayerService::Client::setDataSource(
787 const sp<IDataSource> &source) {
788 sp<DataSource> dataSource = DataSource::CreateFromIDataSource(source);
789 player_type playerType = MediaPlayerFactory::getPlayerType(this, dataSource);
790 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
791 if (p == NULL) {
792 return NO_INIT;
793 }
794 // now set data source
795 setDataSource_post(p, p->setDataSource(dataSource));
796 return mStatus;
797}
798
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700799void MediaPlayerService::Client::disconnectNativeWindow() {
800 if (mConnectedWindow != NULL) {
801 status_t err = native_window_api_disconnect(mConnectedWindow.get(),
802 NATIVE_WINDOW_API_MEDIA);
803
804 if (err != OK) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000805 ALOGW("native_window_api_disconnect returned an error: %s (%d)",
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700806 strerror(-err), err);
807 }
808 }
809 mConnectedWindow.clear();
810}
811
Glenn Kasten11731182011-02-08 17:26:17 -0800812status_t MediaPlayerService::Client::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -0800813 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800814{
Andy McFadden484566c2012-12-18 09:46:54 -0800815 ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, bufferProducer.get());
Glenn Kasten11731182011-02-08 17:26:17 -0800816 sp<MediaPlayerBase> p = getPlayer();
817 if (p == 0) return UNKNOWN_ERROR;
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700818
Marco Nelissenf8880202014-11-14 07:58:25 -0800819 sp<IBinder> binder(IInterface::asBinder(bufferProducer));
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700820 if (mConnectedWindowBinder == binder) {
821 return OK;
822 }
823
824 sp<ANativeWindow> anw;
Andy McFadden484566c2012-12-18 09:46:54 -0800825 if (bufferProducer != NULL) {
Marco Nelissenee08f7e2013-09-16 13:30:01 -0700826 anw = new Surface(bufferProducer, true /* controlledByApp */);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700827 status_t err = native_window_api_connect(anw.get(),
828 NATIVE_WINDOW_API_MEDIA);
829
830 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000831 ALOGE("setVideoSurfaceTexture failed: %d", err);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700832 // Note that we must do the reset before disconnecting from the ANW.
833 // Otherwise queue/dequeue calls could be made on the disconnected
834 // ANW, which may result in errors.
835 reset();
836
837 disconnectNativeWindow();
838
839 return err;
840 }
841 }
842
Andy McFadden484566c2012-12-18 09:46:54 -0800843 // Note that we must set the player's new GraphicBufferProducer before
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700844 // disconnecting the old one. Otherwise queue/dequeue calls could be made
845 // on the disconnected ANW, which may result in errors.
Andy McFadden484566c2012-12-18 09:46:54 -0800846 status_t err = p->setVideoSurfaceTexture(bufferProducer);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700847
848 disconnectNativeWindow();
849
850 mConnectedWindow = anw;
851
852 if (err == OK) {
853 mConnectedWindowBinder = binder;
854 } else {
855 disconnectNativeWindow();
856 }
857
858 return err;
Glenn Kasten11731182011-02-08 17:26:17 -0800859}
860
Nicolas Catania1d187f12009-05-12 23:25:55 -0700861status_t MediaPlayerService::Client::invoke(const Parcel& request,
862 Parcel *reply)
863{
864 sp<MediaPlayerBase> p = getPlayer();
865 if (p == NULL) return UNKNOWN_ERROR;
866 return p->invoke(request, reply);
867}
868
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700869// This call doesn't need to access the native player.
870status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
871{
872 status_t status;
nikoa64c8c72009-07-20 15:07:26 -0700873 media::Metadata::Filter allow, drop;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700874
Nicolas Catania48290382009-07-10 13:53:06 -0700875 if (unmarshallFilter(filter, &allow, &status) &&
876 unmarshallFilter(filter, &drop, &status)) {
877 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700878
879 mMetadataAllow = allow;
880 mMetadataDrop = drop;
881 }
882 return status;
883}
884
Nicolas Catania48290382009-07-10 13:53:06 -0700885status_t MediaPlayerService::Client::getMetadata(
Mark Salyzyn77342f72014-06-18 16:31:32 -0700886 bool update_only, bool /*apply_filter*/, Parcel *reply)
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700887{
nikoa64c8c72009-07-20 15:07:26 -0700888 sp<MediaPlayerBase> player = getPlayer();
889 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania48290382009-07-10 13:53:06 -0700890
nikod608a812009-07-16 16:39:53 -0700891 status_t status;
892 // Placeholder for the return code, updated by the caller.
893 reply->writeInt32(-1);
894
nikoa64c8c72009-07-20 15:07:26 -0700895 media::Metadata::Filter ids;
Nicolas Catania48290382009-07-10 13:53:06 -0700896
897 // We don't block notifications while we fetch the data. We clear
898 // mMetadataUpdated first so we don't lose notifications happening
899 // during the rest of this call.
900 {
901 Mutex::Autolock lock(mLock);
902 if (update_only) {
nikod608a812009-07-16 16:39:53 -0700903 ids = mMetadataUpdated;
Nicolas Catania48290382009-07-10 13:53:06 -0700904 }
905 mMetadataUpdated.clear();
906 }
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700907
nikoa64c8c72009-07-20 15:07:26 -0700908 media::Metadata metadata(reply);
Nicolas Catania48290382009-07-10 13:53:06 -0700909
nikoa64c8c72009-07-20 15:07:26 -0700910 metadata.appendHeader();
911 status = player->getMetadata(ids, reply);
nikod608a812009-07-16 16:39:53 -0700912
913 if (status != OK) {
nikoa64c8c72009-07-20 15:07:26 -0700914 metadata.resetParcel();
Steve Block29357bc2012-01-06 19:20:56 +0000915 ALOGE("getMetadata failed %d", status);
nikod608a812009-07-16 16:39:53 -0700916 return status;
917 }
918
919 // FIXME: Implement filtering on the result. Not critical since
920 // filtering takes place on the update notifications already. This
921 // would be when all the metadata are fetch and a filter is set.
922
nikod608a812009-07-16 16:39:53 -0700923 // Everything is fine, update the metadata length.
nikoa64c8c72009-07-20 15:07:26 -0700924 metadata.updateLength();
nikod608a812009-07-16 16:39:53 -0700925 return OK;
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700926}
927
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800928status_t MediaPlayerService::Client::prepareAsync()
929{
Steve Block3856b092011-10-20 11:56:00 +0100930 ALOGV("[%d] prepareAsync", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800931 sp<MediaPlayerBase> p = getPlayer();
932 if (p == 0) return UNKNOWN_ERROR;
933 status_t ret = p->prepareAsync();
934#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000935 ALOGD("start Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800936 if (ret == NO_ERROR) mAntagonizer->start();
937#endif
938 return ret;
939}
940
941status_t MediaPlayerService::Client::start()
942{
Steve Block3856b092011-10-20 11:56:00 +0100943 ALOGV("[%d] start", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800944 sp<MediaPlayerBase> p = getPlayer();
945 if (p == 0) return UNKNOWN_ERROR;
946 p->setLooping(mLoop);
947 return p->start();
948}
949
950status_t MediaPlayerService::Client::stop()
951{
Steve Block3856b092011-10-20 11:56:00 +0100952 ALOGV("[%d] stop", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800953 sp<MediaPlayerBase> p = getPlayer();
954 if (p == 0) return UNKNOWN_ERROR;
955 return p->stop();
956}
957
958status_t MediaPlayerService::Client::pause()
959{
Steve Block3856b092011-10-20 11:56:00 +0100960 ALOGV("[%d] pause", 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->pause();
964}
965
966status_t MediaPlayerService::Client::isPlaying(bool* state)
967{
968 *state = false;
969 sp<MediaPlayerBase> p = getPlayer();
970 if (p == 0) return UNKNOWN_ERROR;
971 *state = p->isPlaying();
Steve Block3856b092011-10-20 11:56:00 +0100972 ALOGV("[%d] isPlaying: %d", mConnId, *state);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800973 return NO_ERROR;
974}
975
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700976status_t MediaPlayerService::Client::setPlaybackSettings(const AudioPlaybackRate& rate)
Wei Jia98160162015-02-04 17:01:11 -0800977{
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700978 ALOGV("[%d] setPlaybackSettings(%f, %f, %d, %d)",
979 mConnId, rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode);
Wei Jia98160162015-02-04 17:01:11 -0800980 sp<MediaPlayerBase> p = getPlayer();
981 if (p == 0) return UNKNOWN_ERROR;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700982 return p->setPlaybackSettings(rate);
983}
984
985status_t MediaPlayerService::Client::getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */)
986{
987 sp<MediaPlayerBase> p = getPlayer();
988 if (p == 0) return UNKNOWN_ERROR;
989 status_t ret = p->getPlaybackSettings(rate);
990 if (ret == NO_ERROR) {
991 ALOGV("[%d] getPlaybackSettings(%f, %f, %d, %d)",
992 mConnId, rate->mSpeed, rate->mPitch, rate->mFallbackMode, rate->mStretchMode);
993 } else {
994 ALOGV("[%d] getPlaybackSettings returned %d", mConnId, ret);
995 }
996 return ret;
997}
998
999status_t MediaPlayerService::Client::setSyncSettings(
1000 const AVSyncSettings& sync, float videoFpsHint)
1001{
1002 ALOGV("[%d] setSyncSettings(%u, %u, %f, %f)",
1003 mConnId, sync.mSource, sync.mAudioAdjustMode, sync.mTolerance, videoFpsHint);
1004 sp<MediaPlayerBase> p = getPlayer();
1005 if (p == 0) return UNKNOWN_ERROR;
1006 return p->setSyncSettings(sync, videoFpsHint);
1007}
1008
1009status_t MediaPlayerService::Client::getSyncSettings(
1010 AVSyncSettings* sync /* nonnull */, float* videoFps /* nonnull */)
1011{
1012 sp<MediaPlayerBase> p = getPlayer();
1013 if (p == 0) return UNKNOWN_ERROR;
1014 status_t ret = p->getSyncSettings(sync, videoFps);
1015 if (ret == NO_ERROR) {
1016 ALOGV("[%d] getSyncSettings(%u, %u, %f, %f)",
1017 mConnId, sync->mSource, sync->mAudioAdjustMode, sync->mTolerance, *videoFps);
1018 } else {
1019 ALOGV("[%d] getSyncSettings returned %d", mConnId, ret);
1020 }
1021 return ret;
Wei Jia98160162015-02-04 17:01:11 -08001022}
1023
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001024status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
1025{
Steve Block3856b092011-10-20 11:56:00 +01001026 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001027 sp<MediaPlayerBase> p = getPlayer();
1028 if (p == 0) return UNKNOWN_ERROR;
1029 status_t ret = p->getCurrentPosition(msec);
1030 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01001031 ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001032 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001033 ALOGE("getCurrentPosition returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001034 }
1035 return ret;
1036}
1037
1038status_t MediaPlayerService::Client::getDuration(int *msec)
1039{
Steve Block3856b092011-10-20 11:56:00 +01001040 ALOGV("getDuration");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001041 sp<MediaPlayerBase> p = getPlayer();
1042 if (p == 0) return UNKNOWN_ERROR;
1043 status_t ret = p->getDuration(msec);
1044 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01001045 ALOGV("[%d] getDuration = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001046 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001047 ALOGE("getDuration returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001048 }
1049 return ret;
1050}
1051
Marco Nelissen6b74d672012-02-28 16:07:44 -08001052status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) {
1053 ALOGV("setNextPlayer");
1054 Mutex::Autolock l(mLock);
1055 sp<Client> c = static_cast<Client*>(player.get());
1056 mNextClient = c;
John Grossman5f7e55e2012-08-24 14:47:25 -07001057
1058 if (c != NULL) {
1059 if (mAudioOutput != NULL) {
1060 mAudioOutput->setNextOutput(c->mAudioOutput);
1061 } else if ((mPlayer != NULL) && !mPlayer->hardwareOutput()) {
1062 ALOGE("no current audio output");
1063 }
1064
1065 if ((mPlayer != NULL) && (mNextClient->getPlayer() != NULL)) {
1066 mPlayer->setNextPlayer(mNextClient->getPlayer());
1067 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001068 }
John Grossman5f7e55e2012-08-24 14:47:25 -07001069
Marco Nelissen6b74d672012-02-28 16:07:44 -08001070 return OK;
1071}
1072
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001073status_t MediaPlayerService::Client::seekTo(int msec)
1074{
Steve Block3856b092011-10-20 11:56:00 +01001075 ALOGV("[%d] seekTo(%d)", mConnId, msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001076 sp<MediaPlayerBase> p = getPlayer();
1077 if (p == 0) return UNKNOWN_ERROR;
1078 return p->seekTo(msec);
1079}
1080
1081status_t MediaPlayerService::Client::reset()
1082{
Steve Block3856b092011-10-20 11:56:00 +01001083 ALOGV("[%d] reset", mConnId);
John Grossmanc795b642012-02-22 15:38:35 -08001084 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001085 sp<MediaPlayerBase> p = getPlayer();
1086 if (p == 0) return UNKNOWN_ERROR;
1087 return p->reset();
1088}
1089
Glenn Kastenfff6d712012-01-12 16:38:12 -08001090status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001091{
Steve Block3856b092011-10-20 11:56:00 +01001092 ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001093 // TODO: for hardware output, call player instead
1094 Mutex::Autolock l(mLock);
1095 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
1096 return NO_ERROR;
1097}
1098
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001099status_t MediaPlayerService::Client::setAudioAttributes_l(const Parcel &parcel)
1100{
1101 if (mAudioAttributes != NULL) { free(mAudioAttributes); }
1102 mAudioAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
1103 unmarshallAudioAttributes(parcel, mAudioAttributes);
1104
1105 ALOGV("setAudioAttributes_l() usage=%d content=%d flags=0x%x tags=%s",
1106 mAudioAttributes->usage, mAudioAttributes->content_type, mAudioAttributes->flags,
1107 mAudioAttributes->tags);
1108
1109 if (mAudioOutput != 0) {
1110 mAudioOutput->setAudioAttributes(mAudioAttributes);
1111 }
1112 return NO_ERROR;
1113}
1114
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001115status_t MediaPlayerService::Client::setLooping(int loop)
1116{
Steve Block3856b092011-10-20 11:56:00 +01001117 ALOGV("[%d] setLooping(%d)", mConnId, loop);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001118 mLoop = loop;
1119 sp<MediaPlayerBase> p = getPlayer();
1120 if (p != 0) return p->setLooping(loop);
1121 return NO_ERROR;
1122}
1123
1124status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
1125{
Steve Block3856b092011-10-20 11:56:00 +01001126 ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
John Grossman761defc2012-02-09 15:09:05 -08001127
1128 // for hardware output, call player instead
1129 sp<MediaPlayerBase> p = getPlayer();
1130 {
1131 Mutex::Autolock l(mLock);
1132 if (p != 0 && p->hardwareOutput()) {
1133 MediaPlayerHWInterface* hwp =
1134 reinterpret_cast<MediaPlayerHWInterface*>(p.get());
1135 return hwp->setVolume(leftVolume, rightVolume);
1136 } else {
1137 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
1138 return NO_ERROR;
1139 }
1140 }
1141
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001142 return NO_ERROR;
1143}
1144
Eric Laurent2beeb502010-07-16 07:43:46 -07001145status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
1146{
Steve Block3856b092011-10-20 11:56:00 +01001147 ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001148 Mutex::Autolock l(mLock);
1149 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
1150 return NO_ERROR;
1151}
1152
1153status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
1154{
Steve Block3856b092011-10-20 11:56:00 +01001155 ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001156 Mutex::Autolock l(mLock);
1157 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1158 return NO_ERROR;
1159}
Nicolas Catania48290382009-07-10 13:53:06 -07001160
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001161status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
Steve Block3856b092011-10-20 11:56:00 +01001162 ALOGV("[%d] setParameter(%d)", mConnId, key);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001163 switch (key) {
1164 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
1165 {
1166 Mutex::Autolock l(mLock);
1167 return setAudioAttributes_l(request);
1168 }
1169 default:
1170 sp<MediaPlayerBase> p = getPlayer();
1171 if (p == 0) { return UNKNOWN_ERROR; }
1172 return p->setParameter(key, request);
1173 }
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001174}
1175
1176status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
Steve Block3856b092011-10-20 11:56:00 +01001177 ALOGV("[%d] getParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001178 sp<MediaPlayerBase> p = getPlayer();
1179 if (p == 0) return UNKNOWN_ERROR;
1180 return p->getParameter(key, reply);
1181}
1182
John Grossmanc795b642012-02-22 15:38:35 -08001183status_t MediaPlayerService::Client::setRetransmitEndpoint(
1184 const struct sockaddr_in* endpoint) {
1185
1186 if (NULL != endpoint) {
1187 uint32_t a = ntohl(endpoint->sin_addr.s_addr);
1188 uint16_t p = ntohs(endpoint->sin_port);
1189 ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId,
1190 (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p);
1191 } else {
1192 ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId);
1193 }
1194
1195 sp<MediaPlayerBase> p = getPlayer();
1196
1197 // Right now, the only valid time to set a retransmit endpoint is before
1198 // player selection has been made (since the presence or absence of a
1199 // retransmit endpoint is going to determine which player is selected during
1200 // setDataSource).
1201 if (p != 0) return INVALID_OPERATION;
1202
1203 if (NULL != endpoint) {
1204 mRetransmitEndpoint = *endpoint;
1205 mRetransmitEndpointValid = true;
1206 } else {
1207 mRetransmitEndpointValid = false;
1208 }
1209
1210 return NO_ERROR;
1211}
1212
John Grossman44a7e422012-06-21 17:29:24 -07001213status_t MediaPlayerService::Client::getRetransmitEndpoint(
1214 struct sockaddr_in* endpoint)
1215{
1216 if (NULL == endpoint)
1217 return BAD_VALUE;
1218
1219 sp<MediaPlayerBase> p = getPlayer();
1220
1221 if (p != NULL)
1222 return p->getRetransmitEndpoint(endpoint);
1223
1224 if (!mRetransmitEndpointValid)
1225 return NO_INIT;
1226
1227 *endpoint = mRetransmitEndpoint;
1228
1229 return NO_ERROR;
1230}
1231
Gloria Wangb483c472011-04-11 17:23:27 -07001232void MediaPlayerService::Client::notify(
1233 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001234{
1235 Client* client = static_cast<Client*>(cookie);
James Dongb8a98252012-08-26 16:13:03 -07001236 if (client == NULL) {
1237 return;
1238 }
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001239
James Dongb8a98252012-08-26 16:13:03 -07001240 sp<IMediaPlayerClient> c;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001241 {
1242 Mutex::Autolock l(client->mLock);
James Dongb8a98252012-08-26 16:13:03 -07001243 c = client->mClient;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001244 if (msg == MEDIA_PLAYBACK_COMPLETE && client->mNextClient != NULL) {
John Grossmancb0b7552012-08-23 17:47:31 -07001245 if (client->mAudioOutput != NULL)
1246 client->mAudioOutput->switchToNextOutput();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001247 client->mNextClient->start();
1248 client->mNextClient->mClient->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj);
1249 }
1250 }
1251
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001252 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001253 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001254 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-07-10 13:53:06 -07001255
1256 if(client->shouldDropMetadata(metadata_type)) {
1257 return;
1258 }
1259
1260 // Update the list of metadata that have changed. getMetadata
1261 // also access mMetadataUpdated and clears it.
1262 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001263 }
James Dongb8a98252012-08-26 16:13:03 -07001264
1265 if (c != NULL) {
1266 ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
1267 c->notify(msg, ext1, ext2, obj);
1268 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001269}
1270
Nicolas Catania48290382009-07-10 13:53:06 -07001271
nikoa64c8c72009-07-20 15:07:26 -07001272bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001273{
Nicolas Catania48290382009-07-10 13:53:06 -07001274 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001275
Nicolas Catania48290382009-07-10 13:53:06 -07001276 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001277 return true;
1278 }
1279
Nicolas Catania48290382009-07-10 13:53:06 -07001280 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001281 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001282 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001283 return true;
1284 }
1285}
1286
Nicolas Catania48290382009-07-10 13:53:06 -07001287
nikoa64c8c72009-07-20 15:07:26 -07001288void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-07-10 13:53:06 -07001289 Mutex::Autolock lock(mLock);
1290 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1291 mMetadataUpdated.add(metadata_type);
1292 }
1293}
1294
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001295#if CALLBACK_ANTAGONIZER
1296const int Antagonizer::interval = 10000; // 10 msecs
1297
1298Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1299 mExit(false), mActive(false), mClient(client), mCb(cb)
1300{
1301 createThread(callbackThread, this);
1302}
1303
1304void Antagonizer::kill()
1305{
1306 Mutex::Autolock _l(mLock);
1307 mActive = false;
1308 mExit = true;
1309 mCondition.wait(mLock);
1310}
1311
1312int Antagonizer::callbackThread(void* user)
1313{
Steve Blockb8a80522011-12-20 16:23:08 +00001314 ALOGD("Antagonizer started");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001315 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1316 while (!p->mExit) {
1317 if (p->mActive) {
Steve Block3856b092011-10-20 11:56:00 +01001318 ALOGV("send event");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001319 p->mCb(p->mClient, 0, 0, 0);
1320 }
1321 usleep(interval);
1322 }
1323 Mutex::Autolock _l(p->mLock);
1324 p->mCondition.signal();
Steve Blockb8a80522011-12-20 16:23:08 +00001325 ALOGD("Antagonizer stopped");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001326 return 0;
1327}
1328#endif
1329
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001330#undef LOG_TAG
1331#define LOG_TAG "AudioSink"
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001332MediaPlayerService::AudioOutput::AudioOutput(int sessionId, int uid, int pid,
1333 const audio_attributes_t* attr)
Andreas Huber20111aa2009-07-14 16:56:47 -07001334 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001335 mCallbackCookie(NULL),
Marco Nelissen6b74d672012-02-28 16:07:44 -08001336 mCallbackData(NULL),
Marco Nelissen4110c102012-03-29 09:31:28 -07001337 mBytesWritten(0),
Eric Laurent1948eb32012-04-13 16:50:19 -07001338 mSessionId(sessionId),
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001339 mUid(uid),
Marco Nelissend457c972014-02-11 08:47:07 -08001340 mPid(pid),
Eric Laurent1948eb32012-04-13 16:50:19 -07001341 mFlags(AUDIO_OUTPUT_FLAG_NONE) {
Steve Block3856b092011-10-20 11:56:00 +01001342 ALOGV("AudioOutput(%d)", sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -07001343 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001344 mLeftVolume = 1.0;
1345 mRightVolume = 1.0;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001346 mPlaybackRate = AUDIO_PLAYBACK_RATE_DEFAULT;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001347 mSampleRateHz = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001348 mMsecsPerFrame = 0;
Eric Laurent2beeb502010-07-16 07:43:46 -07001349 mAuxEffectId = 0;
1350 mSendLevel = 0.0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001351 setMinBufferCount();
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001352 mAttributes = attr;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001353}
1354
1355MediaPlayerService::AudioOutput::~AudioOutput()
1356{
1357 close();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001358 delete mCallbackData;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001359}
1360
1361void MediaPlayerService::AudioOutput::setMinBufferCount()
1362{
1363 char value[PROPERTY_VALUE_MAX];
1364 if (property_get("ro.kernel.qemu", value, 0)) {
1365 mIsOnEmulator = true;
1366 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1367 }
1368}
1369
1370bool MediaPlayerService::AudioOutput::isOnEmulator()
1371{
1372 setMinBufferCount();
1373 return mIsOnEmulator;
1374}
1375
1376int MediaPlayerService::AudioOutput::getMinBufferCount()
1377{
1378 setMinBufferCount();
1379 return mMinBufferCount;
1380}
1381
1382ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1383{
1384 if (mTrack == 0) return NO_INIT;
1385 return mTrack->frameCount() * frameSize();
1386}
1387
1388ssize_t MediaPlayerService::AudioOutput::frameCount() const
1389{
1390 if (mTrack == 0) return NO_INIT;
1391 return mTrack->frameCount();
1392}
1393
1394ssize_t MediaPlayerService::AudioOutput::channelCount() const
1395{
1396 if (mTrack == 0) return NO_INIT;
1397 return mTrack->channelCount();
1398}
1399
1400ssize_t MediaPlayerService::AudioOutput::frameSize() const
1401{
1402 if (mTrack == 0) return NO_INIT;
1403 return mTrack->frameSize();
1404}
1405
1406uint32_t MediaPlayerService::AudioOutput::latency () const
1407{
Eric Laurentdb354e52012-03-05 17:27:11 -08001408 if (mTrack == 0) return 0;
1409 return mTrack->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001410}
1411
1412float MediaPlayerService::AudioOutput::msecsPerFrame() const
1413{
1414 return mMsecsPerFrame;
1415}
1416
Marco Nelissen4110c102012-03-29 09:31:28 -07001417status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001418{
1419 if (mTrack == 0) return NO_INIT;
1420 return mTrack->getPosition(position);
1421}
1422
Lajos Molnar06ad1522014-08-28 07:27:44 -07001423status_t MediaPlayerService::AudioOutput::getTimestamp(AudioTimestamp &ts) const
1424{
1425 if (mTrack == 0) return NO_INIT;
1426 return mTrack->getTimestamp(ts);
1427}
1428
Marco Nelissen4110c102012-03-29 09:31:28 -07001429status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
1430{
1431 if (mTrack == 0) return NO_INIT;
1432 *frameswritten = mBytesWritten / frameSize();
1433 return OK;
1434}
1435
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001436status_t MediaPlayerService::AudioOutput::setParameters(const String8& keyValuePairs)
1437{
1438 if (mTrack == 0) return NO_INIT;
1439 return mTrack->setParameters(keyValuePairs);
1440}
1441
1442String8 MediaPlayerService::AudioOutput::getParameters(const String8& keys)
1443{
1444 if (mTrack == 0) return String8::empty();
1445 return mTrack->getParameters(keys);
1446}
1447
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001448void MediaPlayerService::AudioOutput::setAudioAttributes(const audio_attributes_t * attributes) {
1449 mAttributes = attributes;
1450}
1451
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001452void MediaPlayerService::AudioOutput::deleteRecycledTrack()
1453{
1454 ALOGV("deleteRecycledTrack");
1455
1456 if (mRecycledTrack != 0) {
1457
1458 if (mCallbackData != NULL) {
1459 mCallbackData->setOutput(NULL);
1460 mCallbackData->endTrackSwitch();
1461 }
1462
1463 if ((mRecycledTrack->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
1464 mRecycledTrack->flush();
1465 }
1466 // An offloaded track isn't flushed because the STREAM_END is reported
1467 // slightly prematurely to allow time for the gapless track switch
1468 // but this means that if we decide not to recycle the track there
1469 // could be a small amount of residual data still playing. We leave
1470 // AudioFlinger to drain the track.
1471
1472 mRecycledTrack.clear();
1473 delete mCallbackData;
1474 mCallbackData = NULL;
1475 close();
1476 }
1477}
1478
Andreas Huber20111aa2009-07-14 16:56:47 -07001479status_t MediaPlayerService::AudioOutput::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001480 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1481 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001482 AudioCallback cb, void *cookie,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001483 audio_output_flags_t flags,
1484 const audio_offload_info_t *offloadInfo)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001485{
Andreas Huber20111aa2009-07-14 16:56:47 -07001486 mCallback = cb;
1487 mCallbackCookie = cookie;
1488
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001489 // Check argument "bufferCount" against the mininum buffer count
1490 if (bufferCount < mMinBufferCount) {
Steve Blockb8a80522011-12-20 16:23:08 +00001491 ALOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001492 bufferCount = mMinBufferCount;
1493
1494 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001495 ALOGV("open(%u, %d, 0x%x, 0x%x, %d, %d 0x%x)", sampleRate, channelCount, channelMask,
1496 format, bufferCount, mSessionId, flags);
Glenn Kastenbce50bf2014-02-27 15:29:51 -08001497 size_t frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001498
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001499 // offloading is only supported in callback mode for now.
1500 // offloadInfo must be present if offload flag is set
1501 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1502 ((cb == NULL) || (offloadInfo == NULL))) {
1503 return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001504 }
1505
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001506 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1507 frameCount = 0; // AudioTrack will get frame count from AudioFlinger
1508 } else {
1509 uint32_t afSampleRate;
1510 size_t afFrameCount;
1511
1512 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1513 return NO_INIT;
1514 }
1515 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1516 return NO_INIT;
1517 }
1518
1519 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
1520 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001521
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001522 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
Glenn Kastenab334fd2012-03-14 12:56:06 -07001523 channelMask = audio_channel_out_mask_from_count(channelCount);
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001524 if (0 == channelMask) {
1525 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
1526 return NO_INIT;
1527 }
1528 }
Eric Laurent1948eb32012-04-13 16:50:19 -07001529
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001530 // Check whether we can recycle the track
1531 bool reuse = false;
1532 bool bothOffloaded = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001533
Glenn Kasten2799d742013-05-30 14:33:29 -07001534 if (mRecycledTrack != 0) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001535 // check whether we are switching between two offloaded tracks
1536 bothOffloaded = (flags & mRecycledTrack->getFlags()
1537 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0;
Marco Nelissen67295b52012-06-11 14:52:53 -07001538
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001539 // check if the existing track can be reused as-is, or if a new track needs to be created.
1540 reuse = true;
1541
Marco Nelissen67295b52012-06-11 14:52:53 -07001542 if ((mCallbackData == NULL && mCallback != NULL) ||
1543 (mCallbackData != NULL && mCallback == NULL)) {
1544 // recycled track uses callbacks but the caller wants to use writes, or vice versa
1545 ALOGV("can't chain callback and write");
1546 reuse = false;
1547 } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001548 (mRecycledTrack->channelCount() != (uint32_t)channelCount) ) {
1549 ALOGV("samplerate, channelcount differ: %u/%u Hz, %u/%d ch",
Marco Nelissen67295b52012-06-11 14:52:53 -07001550 mRecycledTrack->getSampleRate(), sampleRate,
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001551 mRecycledTrack->channelCount(), channelCount);
Marco Nelissen67295b52012-06-11 14:52:53 -07001552 reuse = false;
1553 } else if (flags != mFlags) {
1554 ALOGV("output flags differ %08x/%08x", flags, mFlags);
1555 reuse = false;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001556 } else if (mRecycledTrack->format() != format) {
1557 reuse = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001558 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001559 } else {
1560 ALOGV("no track available to recycle");
1561 }
1562
1563 ALOGV_IF(bothOffloaded, "both tracks offloaded");
1564
1565 // If we can't recycle and both tracks are offloaded
1566 // we must close the previous output before opening a new one
1567 if (bothOffloaded && !reuse) {
1568 ALOGV("both offloaded and not recycling");
1569 deleteRecycledTrack();
1570 }
1571
1572 sp<AudioTrack> t;
1573 CallbackData *newcbd = NULL;
1574
1575 // We don't attempt to create a new track if we are recycling an
1576 // offloaded track. But, if we are recycling a non-offloaded or we
1577 // are switching where one is offloaded and one isn't then we create
1578 // the new track in advance so that we can read additional stream info
1579
1580 if (!(reuse && bothOffloaded)) {
1581 ALOGV("creating new AudioTrack");
1582
1583 if (mCallback != NULL) {
1584 newcbd = new CallbackData(this);
1585 t = new AudioTrack(
1586 mStreamType,
1587 sampleRate,
1588 format,
1589 channelMask,
1590 frameCount,
1591 flags,
1592 CallbackWrapper,
1593 newcbd,
1594 0, // notification frames
1595 mSessionId,
1596 AudioTrack::TRANSFER_CALLBACK,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001597 offloadInfo,
Marco Nelissend457c972014-02-11 08:47:07 -08001598 mUid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001599 mPid,
1600 mAttributes);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001601 } else {
1602 t = new AudioTrack(
1603 mStreamType,
1604 sampleRate,
1605 format,
1606 channelMask,
1607 frameCount,
1608 flags,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001609 NULL, // callback
1610 NULL, // user data
1611 0, // notification frames
1612 mSessionId,
1613 AudioTrack::TRANSFER_DEFAULT,
1614 NULL, // offload info
Marco Nelissend457c972014-02-11 08:47:07 -08001615 mUid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001616 mPid,
1617 mAttributes);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001618 }
1619
1620 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
1621 ALOGE("Unable to create audio track");
1622 delete newcbd;
1623 return NO_INIT;
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001624 } else {
1625 // successful AudioTrack initialization implies a legacy stream type was generated
1626 // from the audio attributes
1627 mStreamType = t->streamType();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001628 }
1629 }
1630
1631 if (reuse) {
1632 CHECK(mRecycledTrack != NULL);
1633
1634 if (!bothOffloaded) {
1635 if (mRecycledTrack->frameCount() != t->frameCount()) {
1636 ALOGV("framecount differs: %u/%u frames",
1637 mRecycledTrack->frameCount(), t->frameCount());
1638 reuse = false;
1639 }
1640 }
1641
Marco Nelissen67295b52012-06-11 14:52:53 -07001642 if (reuse) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001643 ALOGV("chaining to next output and recycling track");
Marco Nelissen67295b52012-06-11 14:52:53 -07001644 close();
1645 mTrack = mRecycledTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001646 mRecycledTrack.clear();
Marco Nelissen67295b52012-06-11 14:52:53 -07001647 if (mCallbackData != NULL) {
1648 mCallbackData->setOutput(this);
1649 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001650 delete newcbd;
1651 return OK;
1652 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001653 }
1654
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001655 // we're not going to reuse the track, unblock and flush it
1656 // this was done earlier if both tracks are offloaded
1657 if (!bothOffloaded) {
1658 deleteRecycledTrack();
1659 }
1660
1661 CHECK((t != NULL) && ((mCallback == NULL) || (newcbd != NULL)));
1662
Marco Nelissen67295b52012-06-11 14:52:53 -07001663 mCallbackData = newcbd;
Steve Block3856b092011-10-20 11:56:00 +01001664 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001665 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001666
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001667 mSampleRateHz = sampleRate;
Eric Laurent1948eb32012-04-13 16:50:19 -07001668 mFlags = flags;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001669 mMsecsPerFrame = 1E3f / (mPlaybackRate.mSpeed * sampleRate);
Marco Nelissen99448602012-04-02 12:16:49 -07001670 uint32_t pos;
1671 if (t->getPosition(&pos) == OK) {
1672 mBytesWritten = uint64_t(pos) * t->frameSize();
1673 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001674 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001675
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001676 status_t res = NO_ERROR;
1677 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001678 res = t->setPlaybackRate(mPlaybackRate);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001679 if (res == NO_ERROR) {
1680 t->setAuxEffectSendLevel(mSendLevel);
1681 res = t->attachAuxEffect(mAuxEffectId);
1682 }
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001683 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001684 ALOGV("open() DONE status %d", res);
1685 return res;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001686}
1687
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001688status_t MediaPlayerService::AudioOutput::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001689{
Steve Block3856b092011-10-20 11:56:00 +01001690 ALOGV("start");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001691 if (mCallbackData != NULL) {
1692 mCallbackData->endTrackSwitch();
1693 }
Glenn Kasten2799d742013-05-30 14:33:29 -07001694 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001695 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001696 mTrack->setAuxEffectSendLevel(mSendLevel);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001697 return mTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001698 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001699 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001700}
1701
Marco Nelissen6b74d672012-02-28 16:07:44 -08001702void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
1703 mNextOutput = nextOutput;
1704}
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001705
1706
Marco Nelissen6b74d672012-02-28 16:07:44 -08001707void MediaPlayerService::AudioOutput::switchToNextOutput() {
1708 ALOGV("switchToNextOutput");
1709 if (mNextOutput != NULL) {
1710 if (mCallbackData != NULL) {
1711 mCallbackData->beginTrackSwitch();
1712 }
1713 delete mNextOutput->mCallbackData;
1714 mNextOutput->mCallbackData = mCallbackData;
1715 mCallbackData = NULL;
1716 mNextOutput->mRecycledTrack = mTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001717 mTrack.clear();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001718 mNextOutput->mSampleRateHz = mSampleRateHz;
1719 mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
Marco Nelissen4110c102012-03-29 09:31:28 -07001720 mNextOutput->mBytesWritten = mBytesWritten;
Marco Nelissend791e092012-06-11 17:00:59 -07001721 mNextOutput->mFlags = mFlags;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001722 }
1723}
1724
Wei Jia7d3f4df2015-03-03 15:28:00 -08001725ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size, bool blocking)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001726{
Glenn Kastenadad3d72014-02-21 14:51:43 -08001727 LOG_ALWAYS_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
Andreas Huber20111aa2009-07-14 16:56:47 -07001728
Steve Block3856b092011-10-20 11:56:00 +01001729 //ALOGV("write(%p, %u)", buffer, size);
Glenn Kasten2799d742013-05-30 14:33:29 -07001730 if (mTrack != 0) {
Wei Jia7d3f4df2015-03-03 15:28:00 -08001731 ssize_t ret = mTrack->write(buffer, size, blocking);
Andy Hunga31335a2014-08-20 17:37:59 -07001732 if (ret >= 0) {
1733 mBytesWritten += ret;
1734 }
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001735 return ret;
1736 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001737 return NO_INIT;
1738}
1739
1740void MediaPlayerService::AudioOutput::stop()
1741{
Steve Block3856b092011-10-20 11:56:00 +01001742 ALOGV("stop");
Glenn Kasten2799d742013-05-30 14:33:29 -07001743 if (mTrack != 0) mTrack->stop();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001744}
1745
1746void MediaPlayerService::AudioOutput::flush()
1747{
Steve Block3856b092011-10-20 11:56:00 +01001748 ALOGV("flush");
Glenn Kasten2799d742013-05-30 14:33:29 -07001749 if (mTrack != 0) mTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001750}
1751
1752void MediaPlayerService::AudioOutput::pause()
1753{
Steve Block3856b092011-10-20 11:56:00 +01001754 ALOGV("pause");
Glenn Kasten2799d742013-05-30 14:33:29 -07001755 if (mTrack != 0) mTrack->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001756}
1757
1758void MediaPlayerService::AudioOutput::close()
1759{
Steve Block3856b092011-10-20 11:56:00 +01001760 ALOGV("close");
Glenn Kasten2799d742013-05-30 14:33:29 -07001761 mTrack.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001762}
1763
1764void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1765{
Steve Block3856b092011-10-20 11:56:00 +01001766 ALOGV("setVolume(%f, %f)", left, right);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001767 mLeftVolume = left;
1768 mRightVolume = right;
Glenn Kasten2799d742013-05-30 14:33:29 -07001769 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001770 mTrack->setVolume(left, right);
1771 }
1772}
1773
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001774status_t MediaPlayerService::AudioOutput::setPlaybackRate(const AudioPlaybackRate &rate)
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001775{
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001776 ALOGV("setPlaybackRate(%f %f %d %d)",
1777 rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode);
1778 if (mTrack == 0) {
1779 // remember rate so that we can set it when the track is opened
1780 mPlaybackRate = rate;
1781 return OK;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001782 }
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001783 status_t res = mTrack->setPlaybackRate(rate);
1784 if (res != NO_ERROR) {
1785 return res;
1786 }
1787 // rate.mSpeed is always greater than 0 if setPlaybackRate succeeded
1788 CHECK_GT(rate.mSpeed, 0.f);
1789 mPlaybackRate = rate;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001790 if (mSampleRateHz != 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001791 mMsecsPerFrame = 1E3f / (rate.mSpeed * mSampleRateHz);
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001792 }
1793 return res;
1794}
1795
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001796status_t MediaPlayerService::AudioOutput::getPlaybackRate(AudioPlaybackRate *rate)
1797{
1798 ALOGV("setPlaybackRate");
1799 if (mTrack == 0) {
1800 return NO_INIT;
1801 }
1802 *rate = mTrack->getPlaybackRate();
1803 return NO_ERROR;
1804}
1805
Eric Laurent2beeb502010-07-16 07:43:46 -07001806status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1807{
Steve Block3856b092011-10-20 11:56:00 +01001808 ALOGV("setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001809 mSendLevel = level;
Glenn Kasten2799d742013-05-30 14:33:29 -07001810 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001811 return mTrack->setAuxEffectSendLevel(level);
1812 }
1813 return NO_ERROR;
1814}
1815
1816status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1817{
Steve Block3856b092011-10-20 11:56:00 +01001818 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001819 mAuxEffectId = effectId;
Glenn Kasten2799d742013-05-30 14:33:29 -07001820 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001821 return mTrack->attachAuxEffect(effectId);
1822 }
1823 return NO_ERROR;
1824}
1825
Andreas Huber20111aa2009-07-14 16:56:47 -07001826// static
1827void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastend217a8c2011-06-01 15:20:35 -07001828 int event, void *cookie, void *info) {
Steve Block3856b092011-10-20 11:56:00 +01001829 //ALOGV("callbackwrapper");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001830 CallbackData *data = (CallbackData*)cookie;
1831 data->lock();
1832 AudioOutput *me = data->getOutput();
Andreas Huber20111aa2009-07-14 16:56:47 -07001833 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001834 if (me == NULL) {
1835 // no output set, likely because the track was scheduled to be reused
1836 // by another player, but the format turned out to be incompatible.
1837 data->unlock();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001838 if (buffer != NULL) {
1839 buffer->size = 0;
1840 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001841 return;
1842 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001843
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001844 switch(event) {
1845 case AudioTrack::EVENT_MORE_DATA: {
1846 size_t actualSize = (*me->mCallback)(
1847 me, buffer->raw, buffer->size, me->mCallbackCookie,
1848 CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001849
aarti jadhav-gaikwadf2575572014-08-13 15:04:39 +05301850 if ((me->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0 &&
1851 actualSize == 0 && buffer->size > 0 && me->mNextOutput == NULL) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001852 // We've reached EOS but the audio track is not stopped yet,
1853 // keep playing silence.
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08001854
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001855 memset(buffer->raw, 0, buffer->size);
1856 actualSize = buffer->size;
1857 }
1858
1859 buffer->size = actualSize;
1860 } break;
1861
1862
1863 case AudioTrack::EVENT_STREAM_END:
1864 ALOGV("callbackwrapper: deliver EVENT_STREAM_END");
1865 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
1866 me->mCallbackCookie, CB_EVENT_STREAM_END);
1867 break;
1868
1869 case AudioTrack::EVENT_NEW_IAUDIOTRACK :
1870 ALOGV("callbackwrapper: deliver EVENT_TEAR_DOWN");
1871 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
1872 me->mCallbackCookie, CB_EVENT_TEAR_DOWN);
1873 break;
1874
1875 default:
1876 ALOGE("received unknown event type: %d inside CallbackWrapper !", event);
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001877 }
1878
Marco Nelissen6b74d672012-02-28 16:07:44 -08001879 data->unlock();
Andreas Huber20111aa2009-07-14 16:56:47 -07001880}
1881
Marco Nelissen4110c102012-03-29 09:31:28 -07001882int MediaPlayerService::AudioOutput::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001883{
1884 return mSessionId;
1885}
1886
Eric Laurent6f59db12013-07-26 17:16:50 -07001887uint32_t MediaPlayerService::AudioOutput::getSampleRate() const
1888{
1889 if (mTrack == 0) return 0;
1890 return mTrack->getSampleRate();
1891}
1892
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001893////////////////////////////////////////////////////////////////////////////////
1894
1895struct CallbackThread : public Thread {
1896 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1897 MediaPlayerBase::AudioSink::AudioCallback cb,
1898 void *cookie);
1899
1900protected:
1901 virtual ~CallbackThread();
1902
1903 virtual bool threadLoop();
1904
1905private:
1906 wp<MediaPlayerBase::AudioSink> mSink;
1907 MediaPlayerBase::AudioSink::AudioCallback mCallback;
1908 void *mCookie;
1909 void *mBuffer;
1910 size_t mBufferSize;
1911
1912 CallbackThread(const CallbackThread &);
1913 CallbackThread &operator=(const CallbackThread &);
1914};
1915
1916CallbackThread::CallbackThread(
1917 const wp<MediaPlayerBase::AudioSink> &sink,
1918 MediaPlayerBase::AudioSink::AudioCallback cb,
1919 void *cookie)
1920 : mSink(sink),
1921 mCallback(cb),
1922 mCookie(cookie),
1923 mBuffer(NULL),
1924 mBufferSize(0) {
1925}
1926
1927CallbackThread::~CallbackThread() {
1928 if (mBuffer) {
1929 free(mBuffer);
1930 mBuffer = NULL;
1931 }
1932}
1933
1934bool CallbackThread::threadLoop() {
1935 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
1936 if (sink == NULL) {
1937 return false;
1938 }
1939
1940 if (mBuffer == NULL) {
1941 mBufferSize = sink->bufferSize();
1942 mBuffer = malloc(mBufferSize);
1943 }
1944
1945 size_t actualSize =
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001946 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie,
1947 MediaPlayerBase::AudioSink::CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001948
1949 if (actualSize > 0) {
1950 sink->write(mBuffer, actualSize);
Andy Hunga31335a2014-08-20 17:37:59 -07001951 // Could return false on sink->write() error or short count.
1952 // Not necessarily appropriate but would work for AudioCache behavior.
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001953 }
1954
1955 return true;
1956}
1957
1958////////////////////////////////////////////////////////////////////////////////
1959
Gloria Wang7cf180c2011-02-19 18:37:57 -08001960void MediaPlayerService::addBatteryData(uint32_t params)
1961{
1962 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08001963
1964 int32_t time = systemTime() / 1000000L;
1965
1966 // change audio output devices. This notification comes from AudioFlinger
1967 if ((params & kBatteryDataSpeakerOn)
1968 || (params & kBatteryDataOtherAudioDeviceOn)) {
1969
1970 int deviceOn[NUM_AUDIO_DEVICES];
1971 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1972 deviceOn[i] = 0;
1973 }
1974
1975 if ((params & kBatteryDataSpeakerOn)
1976 && (params & kBatteryDataOtherAudioDeviceOn)) {
1977 deviceOn[SPEAKER_AND_OTHER] = 1;
1978 } else if (params & kBatteryDataSpeakerOn) {
1979 deviceOn[SPEAKER] = 1;
1980 } else {
1981 deviceOn[OTHER_AUDIO_DEVICE] = 1;
1982 }
1983
1984 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1985 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
1986
1987 if (mBatteryAudio.refCount > 0) { // if playing audio
1988 if (!deviceOn[i]) {
1989 mBatteryAudio.lastTime[i] += time;
1990 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1991 mBatteryAudio.lastTime[i] = 0;
1992 } else {
1993 mBatteryAudio.lastTime[i] = 0 - time;
1994 }
1995 }
1996
1997 mBatteryAudio.deviceOn[i] = deviceOn[i];
1998 }
1999 }
2000 return;
2001 }
2002
Marco Nelissenb7848f12014-12-04 08:57:56 -08002003 // an audio stream is started
Gloria Wang9ee159b2011-02-24 14:51:45 -08002004 if (params & kBatteryDataAudioFlingerStart) {
2005 // record the start time only if currently no other audio
2006 // is being played
2007 if (mBatteryAudio.refCount == 0) {
2008 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2009 if (mBatteryAudio.deviceOn[i]) {
2010 mBatteryAudio.lastTime[i] -= time;
2011 }
2012 }
2013 }
2014
2015 mBatteryAudio.refCount ++;
2016 return;
2017
2018 } else if (params & kBatteryDataAudioFlingerStop) {
2019 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002020 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08002021 return;
2022 }
2023
2024 // record the stop time only if currently this is the only
2025 // audio being played
2026 if (mBatteryAudio.refCount == 1) {
2027 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2028 if (mBatteryAudio.deviceOn[i]) {
2029 mBatteryAudio.lastTime[i] += time;
2030 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
2031 mBatteryAudio.lastTime[i] = 0;
2032 }
2033 }
2034 }
2035
2036 mBatteryAudio.refCount --;
2037 return;
2038 }
2039
Gloria Wang7cf180c2011-02-19 18:37:57 -08002040 int uid = IPCThreadState::self()->getCallingUid();
2041 if (uid == AID_MEDIA) {
2042 return;
2043 }
2044 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08002045
2046 if (index < 0) { // create a new entry for this UID
2047 BatteryUsageInfo info;
2048 info.audioTotalTime = 0;
2049 info.videoTotalTime = 0;
2050 info.audioLastTime = 0;
2051 info.videoLastTime = 0;
2052 info.refCount = 0;
2053
Gloria Wang9ee159b2011-02-24 14:51:45 -08002054 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00002055 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08002056 return;
2057 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08002058 }
2059
2060 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
2061
2062 if (params & kBatteryDataCodecStarted) {
2063 if (params & kBatteryDataTrackAudio) {
2064 info.audioLastTime -= time;
2065 info.refCount ++;
2066 }
2067 if (params & kBatteryDataTrackVideo) {
2068 info.videoLastTime -= time;
2069 info.refCount ++;
2070 }
2071 } else {
2072 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002073 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002074 return;
2075 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00002076 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002077 mBatteryData.removeItem(uid);
2078 return;
2079 }
2080
2081 if (params & kBatteryDataTrackAudio) {
2082 info.audioLastTime += time;
2083 info.refCount --;
2084 }
2085 if (params & kBatteryDataTrackVideo) {
2086 info.videoLastTime += time;
2087 info.refCount --;
2088 }
2089
2090 // no stream is being played by this UID
2091 if (info.refCount == 0) {
2092 info.audioTotalTime += info.audioLastTime;
2093 info.audioLastTime = 0;
2094 info.videoTotalTime += info.videoLastTime;
2095 info.videoLastTime = 0;
2096 }
2097 }
2098}
2099
2100status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
2101 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002102
2103 // audio output devices usage
2104 int32_t time = systemTime() / 1000000L; //in ms
2105 int32_t totalTime;
2106
2107 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2108 totalTime = mBatteryAudio.totalTime[i];
2109
2110 if (mBatteryAudio.deviceOn[i]
2111 && (mBatteryAudio.lastTime[i] != 0)) {
2112 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
2113 totalTime += tmpTime;
2114 }
2115
2116 reply->writeInt32(totalTime);
2117 // reset the total time
2118 mBatteryAudio.totalTime[i] = 0;
2119 }
2120
2121 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08002122 BatteryUsageInfo info;
2123 int size = mBatteryData.size();
2124
2125 reply->writeInt32(size);
2126 int i = 0;
2127
2128 while (i < size) {
2129 info = mBatteryData.valueAt(i);
2130
2131 reply->writeInt32(mBatteryData.keyAt(i)); //UID
2132 reply->writeInt32(info.audioTotalTime);
2133 reply->writeInt32(info.videoTotalTime);
2134
2135 info.audioTotalTime = 0;
2136 info.videoTotalTime = 0;
2137
2138 // remove the UID entry where no stream is being played
2139 if (info.refCount <= 0) {
2140 mBatteryData.removeItemsAt(i);
2141 size --;
2142 i --;
2143 }
2144 i++;
2145 }
2146 return NO_ERROR;
2147}
nikoa64c8c72009-07-20 15:07:26 -07002148} // namespace android