1/*
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 "rtp_test"
19#include <utils/Log.h>
20
21#include <binder/ProcessState.h>
22
23#include <media/stagefright/foundation/base64.h>
24#include <media/stagefright/foundation/ADebug.h>
25#include <media/stagefright/foundation/ALooper.h>
26#include <media/stagefright/DataSource.h>
27#include <media/stagefright/MediaBuffer.h>
28#include <media/stagefright/MetaData.h>
29#include <media/stagefright/SimpleDecodingSource.h>
30
31#include "ARTPSession.h"
32#include "ASessionDescription.h"
33#include "UDPPusher.h"
34
35using namespace android;
36
37int main(int argc, char **argv) {
38    android::ProcessState::self()->startThreadPool();
39
40    const char *rtpFilename = NULL;
41    const char *rtcpFilename = NULL;
42
43    if (argc == 3) {
44        rtpFilename = argv[1];
45        rtcpFilename = argv[2];
46    } else if (argc != 1) {
47        fprintf(stderr, "usage: %s [ rtpFilename rtcpFilename ]\n", argv[0]);
48        return 1;
49    }
50
51#if 0
52    static const uint8_t kSPS[] = {
53        0x67, 0x42, 0x80, 0x0a, 0xe9, 0x02, 0x83, 0xe4, 0x20, 0x00, 0x00, 0x7d, 0x00, 0x00, 0x0e, 0xa6, 0x00, 0x80
54    };
55    static const uint8_t kPPS[] = {
56        0x68, 0xce, 0x3c, 0x80
57    };
58    AString out1, out2;
59    encodeBase64(kSPS, sizeof(kSPS), &out1);
60    encodeBase64(kPPS, sizeof(kPPS), &out2);
61    printf("params=%s,%s\n", out1.c_str(), out2.c_str());
62#endif
63
64    sp<ALooper> looper = new ALooper;
65
66    sp<UDPPusher> rtp_pusher;
67    sp<UDPPusher> rtcp_pusher;
68
69    if (rtpFilename != NULL) {
70        rtp_pusher = new UDPPusher(rtpFilename, 5434);
71        looper->registerHandler(rtp_pusher);
72
73        rtcp_pusher = new UDPPusher(rtcpFilename, 5435);
74        looper->registerHandler(rtcp_pusher);
75    }
76
77    sp<ARTPSession> session = new ARTPSession;
78    looper->registerHandler(session);
79
80#if 0
81    // My H264 SDP
82    static const char *raw =
83        "v=0\r\n"
84        "o=- 64 233572944 IN IP4 127.0.0.0\r\n"
85        "s=QuickTime\r\n"
86        "t=0 0\r\n"
87        "a=range:npt=0-315\r\n"
88        "a=isma-compliance:2,2.0,2\r\n"
89        "m=video 5434 RTP/AVP 97\r\n"
90        "c=IN IP4 127.0.0.1\r\n"
91        "b=AS:30\r\n"
92        "a=rtpmap:97 H264/90000\r\n"
93        "a=fmtp:97 packetization-mode=1;profile-level-id=42000C;"
94          "sprop-parameter-sets=Z0IADJZUCg+I,aM44gA==\r\n"
95        "a=mpeg4-esid:201\r\n"
96        "a=cliprect:0,0,240,320\r\n"
97        "a=framesize:97 320-240\r\n";
98#elif 0
99    // My H263 SDP
100    static const char *raw =
101        "v=0\r\n"
102        "o=- 64 233572944 IN IP4 127.0.0.0\r\n"
103        "s=QuickTime\r\n"
104        "t=0 0\r\n"
105        "a=range:npt=0-315\r\n"
106        "a=isma-compliance:2,2.0,2\r\n"
107        "m=video 5434 RTP/AVP 97\r\n"
108        "c=IN IP4 127.0.0.1\r\n"
109        "b=AS:30\r\n"
110        "a=rtpmap:97 H263-1998/90000\r\n"
111        "a=cliprect:0,0,240,320\r\n"
112        "a=framesize:97 320-240\r\n";
113#elif 0
114    // My AMR SDP
115    static const char *raw =
116        "v=0\r\n"
117        "o=- 64 233572944 IN IP4 127.0.0.0\r\n"
118        "s=QuickTime\r\n"
119        "t=0 0\r\n"
120        "a=range:npt=0-315\r\n"
121        "a=isma-compliance:2,2.0,2\r\n"
122        "m=audio 5434 RTP/AVP 97\r\n"
123        "c=IN IP4 127.0.0.1\r\n"
124        "b=AS:30\r\n"
125        "a=rtpmap:97 AMR/8000/1\r\n"
126        "a=fmtp:97 octet-align\r\n";
127#elif 1
128    // GTalk's H264 SDP
129    static const char *raw =
130        "v=0\r\n"
131        "o=- 64 233572944 IN IP4 127.0.0.0\r\n"
132        "s=QuickTime\r\n"
133        "t=0 0\r\n"
134        "a=range:npt=now-\r\n"
135        "m=video 5434 RTP/AVP 96\r\n"
136        "c=IN IP4 127.0.0.1\r\n"
137        "b=AS:320000\r\n"
138        "a=rtpmap:96 H264/90000\r\n"
139        "a=fmtp:96 packetization-mode=1;profile-level-id=42001E;"
140          "sprop-parameter-sets=Z0IAHpZUBaHogA==,aM44gA==\r\n"
141        "a=cliprect:0,0,480,270\r\n"
142        "a=framesize:96 720-480\r\n";
143#else
144    // sholes H264 SDP
145    static const char *raw =
146        "v=0\r\n"
147        "o=- 64 233572944 IN IP4 127.0.0.0\r\n"
148        "s=QuickTime\r\n"
149        "t=0 0\r\n"
150        "a=range:npt=now-\r\n"
151        "m=video 5434 RTP/AVP 96\r\n"
152        "c=IN IP4 127.0.0.1\r\n"
153        "b=AS:320000\r\n"
154        "a=rtpmap:96 H264/90000\r\n"
155        "a=fmtp:96 packetization-mode=1;profile-level-id=42001E;"
156          "sprop-parameter-sets=Z0KACukCg+QgAAB9AAAOpgCA,aM48gA==\r\n"
157        "a=cliprect:0,0,240,320\r\n"
158        "a=framesize:96 320-240\r\n";
159#endif
160
161    sp<ASessionDescription> desc = new ASessionDescription;
162    CHECK(desc->setTo(raw, strlen(raw)));
163
164    CHECK_EQ(session->setup(desc), (status_t)OK);
165
166    if (rtp_pusher != NULL) {
167        rtp_pusher->start();
168    }
169
170    if (rtcp_pusher != NULL) {
171        rtcp_pusher->start();
172    }
173
174    looper->start(false /* runOnCallingThread */);
175
176    CHECK_EQ(session->countTracks(), 1u);
177    sp<MediaSource> source = session->trackAt(0);
178
179    sp<MediaSource> decoder = SimpleDecodingSource::Create(
180            source, 0 /* flags: ACodec::kPreferSoftwareCodecs */);
181    CHECK(decoder != NULL);
182
183    CHECK_EQ(decoder->start(), (status_t)OK);
184
185    for (;;) {
186        MediaBuffer *buffer;
187        status_t err = decoder->read(&buffer);
188
189        if (err != OK) {
190            if (err == INFO_FORMAT_CHANGED) {
191                int32_t width, height;
192                CHECK(decoder->getFormat()->findInt32(kKeyWidth, &width));
193                CHECK(decoder->getFormat()->findInt32(kKeyHeight, &height));
194                printf("INFO_FORMAT_CHANGED %d x %d\n", width, height);
195                continue;
196            }
197
198            ALOGE("decoder returned error 0x%08x", err);
199            break;
200        }
201
202#if 1
203        if (buffer->range_length() != 0) {
204            int64_t timeUs;
205            CHECK(buffer->meta_data()->findInt64(kKeyTime, &timeUs));
206
207            printf("decoder returned frame of size %zu at time %.2f secs\n",
208                   buffer->range_length(), timeUs / 1E6);
209        }
210#endif
211
212        buffer->release();
213        buffer = NULL;
214    }
215
216    CHECK_EQ(decoder->stop(), (status_t)OK);
217
218    looper->stop();
219
220    return 0;
221}
222