blob: c656e6999e0631c782a72a24c1f76f9801b21039 [file] [log] [blame]
Andreas Huber5bc087c2010-12-23 10:27:40 -08001/*
2 * Copyright (C) 2010 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//#define LOG_NDEBUG 0
18#define LOG_TAG "HTTPLiveSource"
19#include <utils/Log.h>
20
21#include "HTTPLiveSource.h"
22
23#include "ATSParser.h"
24#include "AnotherPacketSource.h"
25#include "LiveDataSource.h"
26#include "LiveSession.h"
27
28#include <media/stagefright/foundation/ABuffer.h>
29#include <media/stagefright/foundation/ADebug.h>
30#include <media/stagefright/foundation/AMessage.h>
31#include <media/stagefright/MediaErrors.h>
32#include <media/stagefright/MetaData.h>
33
34namespace android {
35
36NuPlayer::HTTPLiveSource::HTTPLiveSource(const char *url)
37 : mURL(url),
38 mEOS(false),
39 mOffset(0) {
40}
41
42NuPlayer::HTTPLiveSource::~HTTPLiveSource() {
43 mLiveSession->disconnect();
44 mLiveLooper->stop();
45}
46
47void NuPlayer::HTTPLiveSource::start() {
48 mLiveLooper = new ALooper;
49 mLiveLooper->setName("http live");
50 mLiveLooper->start();
51
52 mLiveSession = new LiveSession;
53 mLiveLooper->registerHandler(mLiveSession);
54
55 mLiveSession->connect(mURL.c_str());
56
57 mTSParser = new ATSParser;
58}
59
60sp<MetaData> NuPlayer::HTTPLiveSource::getFormat(bool audio) {
61 ATSParser::SourceType type =
62 audio ? ATSParser::MPEG2ADTS_AUDIO : ATSParser::AVC_VIDEO;
63
64 sp<AnotherPacketSource> source =
65 static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get());
66
67 if (source == NULL) {
68 return NULL;
69 }
70
71 return source->getFormat();
72}
73
74bool NuPlayer::HTTPLiveSource::feedMoreTSData() {
75 if (mEOS) {
76 return false;
77 }
78
79 sp<LiveDataSource> source =
80 static_cast<LiveDataSource *>(mLiveSession->getDataSource().get());
81
82 for (int32_t i = 0; i < 10; ++i) {
83 char buffer[188];
84 ssize_t n = source->readAtNonBlocking(mOffset, buffer, sizeof(buffer));
85
86 if (n == -EWOULDBLOCK) {
87 break;
88 } else if (n < 0) {
89 LOGI("input data EOS reached.");
90 mTSParser->signalEOS(ERROR_END_OF_STREAM);
91 mEOS = true;
92 break;
93 } else {
94 if (buffer[0] == 0x00) {
95 // XXX legacy
96 mTSParser->signalDiscontinuity(
97 buffer[1] == 0x00
98 ? ATSParser::DISCONTINUITY_SEEK
99 : ATSParser::DISCONTINUITY_FORMATCHANGE);
100 } else {
101 mTSParser->feedTSPacket(buffer, sizeof(buffer));
102 }
103
104 mOffset += n;
105 }
106 }
107
108 return true;
109}
110
111status_t NuPlayer::HTTPLiveSource::dequeueAccessUnit(
112 bool audio, sp<ABuffer> *accessUnit) {
113 ATSParser::SourceType type =
114 audio ? ATSParser::MPEG2ADTS_AUDIO : ATSParser::AVC_VIDEO;
115
116 sp<AnotherPacketSource> source =
117 static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get());
118
119 if (source == NULL) {
120 return -EWOULDBLOCK;
121 }
122
123 status_t finalResult;
124 if (!source->hasBufferAvailable(&finalResult)) {
125 return finalResult == OK ? -EWOULDBLOCK : finalResult;
126 }
127
128 return source->dequeueAccessUnit(accessUnit);
129}
130
131} // namespace android
132