Skip to content

Commit 4355b00

Browse files
theandi666Android (Google) Code Review
authored andcommitted
Merge "Use NuPlayer for media playback everywhere"
2 parents 3558486 + 8686938 commit 4355b00

File tree

11 files changed

+420
-12
lines changed

11 files changed

+420
-12
lines changed

cmds/stagefright/sf2.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,9 @@ struct Controller : public AHandler {
176176
}
177177

178178
onDrainThisBuffer(msg);
179-
} else if (what == ACodec::kWhatEOS) {
180-
printf("$\n");
179+
} else if (what == ACodec::kWhatEOS
180+
|| what == ACodec::kWhatError) {
181+
printf((what == ACodec::kWhatEOS) ? "$\n" : "E\n");
181182

182183
int64_t delayUs = ALooper::GetNowUs() - mStartTimeUs;
183184

@@ -412,7 +413,8 @@ struct Controller : public AHandler {
412413
sp<AMessage> reply;
413414
CHECK(msg->findMessage("reply", &reply));
414415

415-
if (mSeekState == SEEK_FLUSHING) {
416+
if (mSource == NULL || mSeekState == SEEK_FLUSHING) {
417+
reply->setInt32("err", ERROR_END_OF_STREAM);
416418
reply->post();
417419
return;
418420
}

media/libmediaplayerservice/MediaPlayerService.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,12 @@ void MediaPlayerService::Client::disconnect()
541541
}
542542

543543
static player_type getDefaultPlayerType() {
544+
char value[PROPERTY_VALUE_MAX];
545+
if (property_get("media.stagefright.use-nuplayer", value, NULL)
546+
&& (!strcmp("1", value) || !strcasecmp("true", value))) {
547+
return NU_PLAYER;
548+
}
549+
544550
return STAGEFRIGHT_PLAYER;
545551
}
546552

media/libmediaplayerservice/MetadataRetrieverClient.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ static sp<MediaMetadataRetrieverBase> createRetriever(player_type playerType)
8787
sp<MediaMetadataRetrieverBase> p;
8888
switch (playerType) {
8989
case STAGEFRIGHT_PLAYER:
90+
case NU_PLAYER:
9091
{
9192
p = new StagefrightMetadataRetriever;
9293
break;

media/libmediaplayerservice/nuplayer/Android.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ LOCAL_PATH:= $(call my-dir)
22
include $(CLEAR_VARS)
33

44
LOCAL_SRC_FILES:= \
5+
GenericSource.cpp \
56
HTTPLiveSource.cpp \
67
NuPlayer.cpp \
78
NuPlayerDecoder.cpp \
Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
1+
/*
2+
* Copyright (C) 2012 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "GenericSource.h"
18+
19+
#include "AnotherPacketSource.h"
20+
21+
#include <media/stagefright/foundation/ABuffer.h>
22+
#include <media/stagefright/foundation/ADebug.h>
23+
#include <media/stagefright/foundation/AMessage.h>
24+
#include <media/stagefright/DataSource.h>
25+
#include <media/stagefright/FileSource.h>
26+
#include <media/stagefright/MediaBuffer.h>
27+
#include <media/stagefright/MediaDefs.h>
28+
#include <media/stagefright/MediaExtractor.h>
29+
#include <media/stagefright/MediaSource.h>
30+
#include <media/stagefright/MetaData.h>
31+
32+
namespace android {
33+
34+
NuPlayer::GenericSource::GenericSource(
35+
const char *url,
36+
const KeyedVector<String8, String8> *headers,
37+
bool uidValid,
38+
uid_t uid)
39+
: mDurationUs(0ll),
40+
mAudioIsVorbis(false) {
41+
DataSource::RegisterDefaultSniffers();
42+
43+
sp<DataSource> dataSource =
44+
DataSource::CreateFromURI(url, headers);
45+
CHECK(dataSource != NULL);
46+
47+
initFromDataSource(dataSource);
48+
}
49+
50+
NuPlayer::GenericSource::GenericSource(
51+
int fd, int64_t offset, int64_t length)
52+
: mDurationUs(0ll),
53+
mAudioIsVorbis(false) {
54+
DataSource::RegisterDefaultSniffers();
55+
56+
sp<DataSource> dataSource = new FileSource(dup(fd), offset, length);
57+
58+
initFromDataSource(dataSource);
59+
}
60+
61+
void NuPlayer::GenericSource::initFromDataSource(
62+
const sp<DataSource> &dataSource) {
63+
sp<MediaExtractor> extractor = MediaExtractor::Create(dataSource);
64+
65+
CHECK(extractor != NULL);
66+
67+
for (size_t i = 0; i < extractor->countTracks(); ++i) {
68+
sp<MetaData> meta = extractor->getTrackMetaData(i);
69+
70+
const char *mime;
71+
CHECK(meta->findCString(kKeyMIMEType, &mime));
72+
73+
sp<MediaSource> track;
74+
75+
if (!strncasecmp(mime, "audio/", 6)) {
76+
if (mAudioTrack.mSource == NULL) {
77+
mAudioTrack.mSource = track = extractor->getTrack(i);
78+
79+
if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_VORBIS)) {
80+
mAudioIsVorbis = true;
81+
} else {
82+
mAudioIsVorbis = false;
83+
}
84+
}
85+
} else if (!strncasecmp(mime, "video/", 6)) {
86+
if (mVideoTrack.mSource == NULL) {
87+
mVideoTrack.mSource = track = extractor->getTrack(i);
88+
}
89+
}
90+
91+
if (track != NULL) {
92+
int64_t durationUs;
93+
if (meta->findInt64(kKeyDuration, &durationUs)) {
94+
if (durationUs > mDurationUs) {
95+
mDurationUs = durationUs;
96+
}
97+
}
98+
}
99+
}
100+
}
101+
102+
NuPlayer::GenericSource::~GenericSource() {
103+
}
104+
105+
void NuPlayer::GenericSource::start() {
106+
ALOGI("start");
107+
108+
if (mAudioTrack.mSource != NULL) {
109+
CHECK_EQ(mAudioTrack.mSource->start(), (status_t)OK);
110+
111+
mAudioTrack.mPackets =
112+
new AnotherPacketSource(mAudioTrack.mSource->getFormat());
113+
114+
readBuffer(true /* audio */);
115+
}
116+
117+
if (mVideoTrack.mSource != NULL) {
118+
CHECK_EQ(mVideoTrack.mSource->start(), (status_t)OK);
119+
120+
mVideoTrack.mPackets =
121+
new AnotherPacketSource(mVideoTrack.mSource->getFormat());
122+
123+
readBuffer(false /* audio */);
124+
}
125+
}
126+
127+
status_t NuPlayer::GenericSource::feedMoreTSData() {
128+
return OK;
129+
}
130+
131+
sp<MetaData> NuPlayer::GenericSource::getFormat(bool audio) {
132+
sp<MediaSource> source = audio ? mAudioTrack.mSource : mVideoTrack.mSource;
133+
134+
if (source == NULL) {
135+
return NULL;
136+
}
137+
138+
return source->getFormat();
139+
}
140+
141+
status_t NuPlayer::GenericSource::dequeueAccessUnit(
142+
bool audio, sp<ABuffer> *accessUnit) {
143+
Track *track = audio ? &mAudioTrack : &mVideoTrack;
144+
145+
if (track->mSource == NULL) {
146+
return -EWOULDBLOCK;
147+
}
148+
149+
status_t finalResult;
150+
if (!track->mPackets->hasBufferAvailable(&finalResult)) {
151+
return finalResult == OK ? -EWOULDBLOCK : finalResult;
152+
}
153+
154+
status_t result = track->mPackets->dequeueAccessUnit(accessUnit);
155+
156+
readBuffer(audio, -1ll);
157+
158+
return result;
159+
}
160+
161+
status_t NuPlayer::GenericSource::getDuration(int64_t *durationUs) {
162+
*durationUs = mDurationUs;
163+
return OK;
164+
}
165+
166+
status_t NuPlayer::GenericSource::seekTo(int64_t seekTimeUs) {
167+
if (mVideoTrack.mSource != NULL) {
168+
int64_t actualTimeUs;
169+
readBuffer(false /* audio */, seekTimeUs, &actualTimeUs);
170+
171+
seekTimeUs = actualTimeUs;
172+
}
173+
174+
if (mAudioTrack.mSource != NULL) {
175+
readBuffer(true /* audio */, seekTimeUs);
176+
}
177+
178+
return OK;
179+
}
180+
181+
void NuPlayer::GenericSource::readBuffer(
182+
bool audio, int64_t seekTimeUs, int64_t *actualTimeUs) {
183+
Track *track = audio ? &mAudioTrack : &mVideoTrack;
184+
CHECK(track->mSource != NULL);
185+
186+
if (actualTimeUs) {
187+
*actualTimeUs = seekTimeUs;
188+
}
189+
190+
MediaSource::ReadOptions options;
191+
192+
bool seeking = false;
193+
194+
if (seekTimeUs >= 0) {
195+
options.setSeekTo(seekTimeUs);
196+
seeking = true;
197+
}
198+
199+
for (;;) {
200+
MediaBuffer *mbuf;
201+
status_t err = track->mSource->read(&mbuf, &options);
202+
203+
options.clearSeekTo();
204+
205+
if (err == OK) {
206+
size_t outLength = mbuf->range_length();
207+
208+
if (audio && mAudioIsVorbis) {
209+
outLength += sizeof(int32_t);
210+
}
211+
212+
sp<ABuffer> buffer = new ABuffer(outLength);
213+
214+
memcpy(buffer->data(),
215+
(const uint8_t *)mbuf->data() + mbuf->range_offset(),
216+
mbuf->range_length());
217+
218+
if (audio && mAudioIsVorbis) {
219+
int32_t numPageSamples;
220+
if (!mbuf->meta_data()->findInt32(
221+
kKeyValidSamples, &numPageSamples)) {
222+
numPageSamples = -1;
223+
}
224+
225+
memcpy(buffer->data() + mbuf->range_length(),
226+
&numPageSamples,
227+
sizeof(numPageSamples));
228+
}
229+
230+
int64_t timeUs;
231+
CHECK(mbuf->meta_data()->findInt64(kKeyTime, &timeUs));
232+
233+
buffer->meta()->setInt64("timeUs", timeUs);
234+
235+
if (actualTimeUs) {
236+
*actualTimeUs = timeUs;
237+
}
238+
239+
mbuf->release();
240+
mbuf = NULL;
241+
242+
if (seeking) {
243+
track->mPackets->queueDiscontinuity(
244+
ATSParser::DISCONTINUITY_SEEK, NULL);
245+
}
246+
247+
track->mPackets->queueAccessUnit(buffer);
248+
break;
249+
} else if (err == INFO_FORMAT_CHANGED) {
250+
#if 0
251+
track->mPackets->queueDiscontinuity(
252+
ATSParser::DISCONTINUITY_FORMATCHANGE, NULL);
253+
#endif
254+
} else {
255+
track->mPackets->signalEOS(err);
256+
break;
257+
}
258+
}
259+
}
260+
261+
bool NuPlayer::GenericSource::isSeekable() {
262+
return true;
263+
}
264+
265+
} // namespace android
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright (C) 2012 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef GENERIC_SOURCE_H_
18+
19+
#define GENERIC_SOURCE_H_
20+
21+
#include "NuPlayer.h"
22+
#include "NuPlayerSource.h"
23+
24+
#include "ATSParser.h"
25+
26+
namespace android {
27+
28+
struct AnotherPacketSource;
29+
struct ARTSPController;
30+
struct DataSource;
31+
struct MediaSource;
32+
33+
struct NuPlayer::GenericSource : public NuPlayer::Source {
34+
GenericSource(
35+
const char *url,
36+
const KeyedVector<String8, String8> *headers,
37+
bool uidValid = false,
38+
uid_t uid = 0);
39+
40+
GenericSource(int fd, int64_t offset, int64_t length);
41+
42+
virtual void start();
43+
44+
virtual status_t feedMoreTSData();
45+
46+
virtual sp<MetaData> getFormat(bool audio);
47+
virtual status_t dequeueAccessUnit(bool audio, sp<ABuffer> *accessUnit);
48+
49+
virtual status_t getDuration(int64_t *durationUs);
50+
virtual status_t seekTo(int64_t seekTimeUs);
51+
virtual bool isSeekable();
52+
53+
protected:
54+
virtual ~GenericSource();
55+
56+
private:
57+
struct Track {
58+
sp<MediaSource> mSource;
59+
sp<AnotherPacketSource> mPackets;
60+
};
61+
62+
Track mAudioTrack;
63+
Track mVideoTrack;
64+
65+
int64_t mDurationUs;
66+
bool mAudioIsVorbis;
67+
68+
void initFromDataSource(const sp<DataSource> &dataSource);
69+
70+
void readBuffer(
71+
bool audio,
72+
int64_t seekTimeUs = -1ll, int64_t *actualTimeUs = NULL);
73+
74+
DISALLOW_EVIL_CONSTRUCTORS(GenericSource);
75+
};
76+
77+
} // namespace android
78+
79+
#endif // GENERIC_SOURCE_H_

0 commit comments

Comments
 (0)