blob: 783f2d0382f897d56a0a24b2c8be0f34b40c7f4e [file] [log] [blame]
Andreas Hubere46b7be2009-07-14 16:56:47 -07001/*
2 * Copyright (C) 2009 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
Andreas Huberbd7b43b2009-10-13 10:22:55 -070017#include "include/HTTPStream.h"
18
Andreas Hubere46b7be2009-07-14 16:56:47 -070019#include <stdlib.h>
20
Andreas Hubere46b7be2009-07-14 16:56:47 -070021#include <media/stagefright/MediaBuffer.h>
22#include <media/stagefright/MediaBufferGroup.h>
Andreas Huberb5ceb9e2009-08-26 14:48:20 -070023#include <media/stagefright/MediaDebug.h>
Andreas Hubere6c40962009-09-10 14:13:30 -070024#include <media/stagefright/MediaDefs.h>
Andreas Hubere46b7be2009-07-14 16:56:47 -070025#include <media/stagefright/MetaData.h>
26#include <media/stagefright/ShoutcastSource.h>
Andreas Hubere46b7be2009-07-14 16:56:47 -070027
28namespace android {
29
30ShoutcastSource::ShoutcastSource(HTTPStream *http)
31 : mHttp(http),
32 mMetaDataOffset(0),
33 mBytesUntilMetaData(0),
34 mGroup(NULL),
35 mStarted(false) {
Andreas Huber61c79b62010-11-19 09:36:13 -080036 AString metaint;
Andreas Hubere46b7be2009-07-14 16:56:47 -070037 if (mHttp->find_header_value("icy-metaint", &metaint)) {
38 char *end;
39 const char *start = metaint.c_str();
40 mMetaDataOffset = strtol(start, &end, 10);
Andreas Huberb5ceb9e2009-08-26 14:48:20 -070041 CHECK(end > start && *end == '\0');
42 CHECK(mMetaDataOffset > 0);
Andreas Hubere46b7be2009-07-14 16:56:47 -070043
44 mBytesUntilMetaData = mMetaDataOffset;
45 }
46}
47
48ShoutcastSource::~ShoutcastSource() {
49 if (mStarted) {
50 stop();
51 }
52
53 delete mHttp;
54 mHttp = NULL;
55}
56
57status_t ShoutcastSource::start(MetaData *) {
Andreas Huberb5ceb9e2009-08-26 14:48:20 -070058 CHECK(!mStarted);
Andreas Hubere46b7be2009-07-14 16:56:47 -070059
60 mGroup = new MediaBufferGroup;
61 mGroup->add_buffer(new MediaBuffer(4096)); // XXX
62
63 mStarted = true;
64
65 return OK;
66}
67
68status_t ShoutcastSource::stop() {
Andreas Huberb5ceb9e2009-08-26 14:48:20 -070069 CHECK(mStarted);
Andreas Hubere46b7be2009-07-14 16:56:47 -070070
71 delete mGroup;
72 mGroup = NULL;
73
74 mStarted = false;
75
76 return OK;
77}
78
79sp<MetaData> ShoutcastSource::getFormat() {
80 sp<MetaData> meta = new MetaData;
Andreas Hubere6c40962009-09-10 14:13:30 -070081 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_MPEG);
Andreas Hubere46b7be2009-07-14 16:56:47 -070082 meta->setInt32(kKeySampleRate, 44100);
83 meta->setInt32(kKeyChannelCount, 2); // XXX
84
85 return meta;
86}
87
88status_t ShoutcastSource::read(
89 MediaBuffer **out, const ReadOptions *options) {
Andreas Huberb5ceb9e2009-08-26 14:48:20 -070090 CHECK(mStarted);
Andreas Hubere46b7be2009-07-14 16:56:47 -070091
92 *out = NULL;
93
94 int64_t seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -070095 ReadOptions::SeekMode mode;
96 if (options && options->getSeekTo(&seekTimeUs, &mode)) {
Andreas Hubere46b7be2009-07-14 16:56:47 -070097 return ERROR_UNSUPPORTED;
98 }
99
100 MediaBuffer *buffer;
101 status_t err = mGroup->acquire_buffer(&buffer);
102 if (err != OK) {
103 return err;
104 }
105
106 *out = buffer;
107
108 size_t num_bytes = buffer->size();
109 if (mMetaDataOffset > 0 && num_bytes > mBytesUntilMetaData) {
110 num_bytes = mBytesUntilMetaData;
111 }
112
113 ssize_t n = mHttp->receive(buffer->data(), num_bytes);
114
115 if (n <= 0) {
116 return (status_t)n;
117 }
118
119 buffer->set_range(0, n);
120
121 mBytesUntilMetaData -= (size_t)n;
122
123 if (mBytesUntilMetaData == 0) {
124 unsigned char num_16_byte_blocks = 0;
125 n = mHttp->receive((char *)&num_16_byte_blocks, 1);
Andreas Huberb5ceb9e2009-08-26 14:48:20 -0700126 CHECK_EQ(n, 1);
Andreas Hubere46b7be2009-07-14 16:56:47 -0700127
128 char meta[255 * 16];
129 size_t meta_size = num_16_byte_blocks * 16;
130 size_t meta_length = 0;
131 while (meta_length < meta_size) {
132 n = mHttp->receive(&meta[meta_length], meta_size - meta_length);
133 if (n <= 0) {
134 return (status_t)n;
135 }
136
137 meta_length += (size_t) n;
138 }
139
140 while (meta_length > 0 && meta[meta_length - 1] == '\0') {
141 --meta_length;
142 }
143
144 if (meta_length > 0) {
145 // Technically we should probably attach this meta data to the
146 // next buffer. XXX
147 buffer->meta_data()->setData('shou', 'shou', meta, meta_length);
148 }
149
150 mBytesUntilMetaData = mMetaDataOffset;
151 }
152
153 return OK;
154}
155
156} // namespace android
157