blob: 23b76813346398cc31037719fefdca20b05252a6 [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 Huber63d524f2009-10-19 10:15:44 -070017#include "include/stagefright_string.h"
Andreas Huberbd7b43b2009-10-13 10:22:55 -070018#include "include/HTTPStream.h"
19
Andreas Hubere46b7be2009-07-14 16:56:47 -070020#include <stdlib.h>
21
Andreas Hubere46b7be2009-07-14 16:56:47 -070022#include <media/stagefright/MediaBuffer.h>
23#include <media/stagefright/MediaBufferGroup.h>
Andreas Huberb5ceb9e2009-08-26 14:48:20 -070024#include <media/stagefright/MediaDebug.h>
Andreas Hubere6c40962009-09-10 14:13:30 -070025#include <media/stagefright/MediaDefs.h>
Andreas Hubere46b7be2009-07-14 16:56:47 -070026#include <media/stagefright/MetaData.h>
27#include <media/stagefright/ShoutcastSource.h>
Andreas Hubere46b7be2009-07-14 16:56:47 -070028
29namespace android {
30
31ShoutcastSource::ShoutcastSource(HTTPStream *http)
32 : mHttp(http),
33 mMetaDataOffset(0),
34 mBytesUntilMetaData(0),
35 mGroup(NULL),
36 mStarted(false) {
37 string metaint;
38 if (mHttp->find_header_value("icy-metaint", &metaint)) {
39 char *end;
40 const char *start = metaint.c_str();
41 mMetaDataOffset = strtol(start, &end, 10);
Andreas Huberb5ceb9e2009-08-26 14:48:20 -070042 CHECK(end > start && *end == '\0');
43 CHECK(mMetaDataOffset > 0);
Andreas Hubere46b7be2009-07-14 16:56:47 -070044
45 mBytesUntilMetaData = mMetaDataOffset;
46 }
47}
48
49ShoutcastSource::~ShoutcastSource() {
50 if (mStarted) {
51 stop();
52 }
53
54 delete mHttp;
55 mHttp = NULL;
56}
57
58status_t ShoutcastSource::start(MetaData *) {
Andreas Huberb5ceb9e2009-08-26 14:48:20 -070059 CHECK(!mStarted);
Andreas Hubere46b7be2009-07-14 16:56:47 -070060
61 mGroup = new MediaBufferGroup;
62 mGroup->add_buffer(new MediaBuffer(4096)); // XXX
63
64 mStarted = true;
65
66 return OK;
67}
68
69status_t ShoutcastSource::stop() {
Andreas Huberb5ceb9e2009-08-26 14:48:20 -070070 CHECK(mStarted);
Andreas Hubere46b7be2009-07-14 16:56:47 -070071
72 delete mGroup;
73 mGroup = NULL;
74
75 mStarted = false;
76
77 return OK;
78}
79
80sp<MetaData> ShoutcastSource::getFormat() {
81 sp<MetaData> meta = new MetaData;
Andreas Hubere6c40962009-09-10 14:13:30 -070082 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_MPEG);
Andreas Hubere46b7be2009-07-14 16:56:47 -070083 meta->setInt32(kKeySampleRate, 44100);
84 meta->setInt32(kKeyChannelCount, 2); // XXX
85
86 return meta;
87}
88
89status_t ShoutcastSource::read(
90 MediaBuffer **out, const ReadOptions *options) {
Andreas Huberb5ceb9e2009-08-26 14:48:20 -070091 CHECK(mStarted);
Andreas Hubere46b7be2009-07-14 16:56:47 -070092
93 *out = NULL;
94
95 int64_t seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -070096 ReadOptions::SeekMode mode;
97 if (options && options->getSeekTo(&seekTimeUs, &mode)) {
Andreas Hubere46b7be2009-07-14 16:56:47 -070098 return ERROR_UNSUPPORTED;
99 }
100
101 MediaBuffer *buffer;
102 status_t err = mGroup->acquire_buffer(&buffer);
103 if (err != OK) {
104 return err;
105 }
106
107 *out = buffer;
108
109 size_t num_bytes = buffer->size();
110 if (mMetaDataOffset > 0 && num_bytes > mBytesUntilMetaData) {
111 num_bytes = mBytesUntilMetaData;
112 }
113
114 ssize_t n = mHttp->receive(buffer->data(), num_bytes);
115
116 if (n <= 0) {
117 return (status_t)n;
118 }
119
120 buffer->set_range(0, n);
121
122 mBytesUntilMetaData -= (size_t)n;
123
124 if (mBytesUntilMetaData == 0) {
125 unsigned char num_16_byte_blocks = 0;
126 n = mHttp->receive((char *)&num_16_byte_blocks, 1);
Andreas Huberb5ceb9e2009-08-26 14:48:20 -0700127 CHECK_EQ(n, 1);
Andreas Hubere46b7be2009-07-14 16:56:47 -0700128
129 char meta[255 * 16];
130 size_t meta_size = num_16_byte_blocks * 16;
131 size_t meta_length = 0;
132 while (meta_length < meta_size) {
133 n = mHttp->receive(&meta[meta_length], meta_size - meta_length);
134 if (n <= 0) {
135 return (status_t)n;
136 }
137
138 meta_length += (size_t) n;
139 }
140
141 while (meta_length > 0 && meta[meta_length - 1] == '\0') {
142 --meta_length;
143 }
144
145 if (meta_length > 0) {
146 // Technically we should probably attach this meta data to the
147 // next buffer. XXX
148 buffer->meta_data()->setData('shou', 'shou', meta, meta_length);
149 }
150
151 mBytesUntilMetaData = mMetaDataOffset;
152 }
153
154 return OK;
155}
156
157} // namespace android
158