1/*
2 * Copyright 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//#define LOG_NDEBUG 0
18#define LOG_TAG "wfd"
19#include <utils/Log.h>
20
21#include "sink/WifiDisplaySink.h"
22#include "source/WifiDisplaySource.h"
23
24#include <binder/ProcessState.h>
25#include <binder/IServiceManager.h>
26#include <media/IMediaPlayerService.h>
27#include <media/stagefright/DataSource.h>
28#include <media/stagefright/foundation/ADebug.h>
29
30namespace android {
31
32}  // namespace android
33
34static void usage(const char *me) {
35    fprintf(stderr,
36            "usage:\n"
37            "           %s -c host[:port]\tconnect to wifi source\n"
38            "           -u uri        \tconnect to an rtsp uri\n",
39            me);
40}
41
42int main(int argc, char **argv) {
43    using namespace android;
44
45    ProcessState::self()->startThreadPool();
46
47    DataSource::RegisterDefaultSniffers();
48
49    AString connectToHost;
50    int32_t connectToPort = -1;
51    AString uri;
52
53    int res;
54    while ((res = getopt(argc, argv, "hc:l:u:")) >= 0) {
55        switch (res) {
56            case 'c':
57            {
58                const char *colonPos = strrchr(optarg, ':');
59
60                if (colonPos == NULL) {
61                    connectToHost = optarg;
62                    connectToPort = WifiDisplaySource::kWifiDisplayDefaultPort;
63                } else {
64                    connectToHost.setTo(optarg, colonPos - optarg);
65
66                    char *end;
67                    connectToPort = strtol(colonPos + 1, &end, 10);
68
69                    if (*end != '\0' || end == colonPos + 1
70                            || connectToPort < 1 || connectToPort > 65535) {
71                        fprintf(stderr, "Illegal port specified.\n");
72                        exit(1);
73                    }
74                }
75                break;
76            }
77
78            case 'u':
79            {
80                uri = optarg;
81                break;
82            }
83
84            case '?':
85            case 'h':
86            default:
87                usage(argv[0]);
88                exit(1);
89        }
90    }
91
92    if (connectToPort < 0 && uri.empty()) {
93        fprintf(stderr,
94                "You need to select either source host or uri.\n");
95
96        exit(1);
97    }
98
99    if (connectToPort >= 0 && !uri.empty()) {
100        fprintf(stderr,
101                "You need to either connect to a wfd host or an rtsp url, "
102                "not both.\n");
103        exit(1);
104    }
105
106    sp<ANetworkSession> session = new ANetworkSession;
107    session->start();
108
109    sp<ALooper> looper = new ALooper;
110
111    sp<WifiDisplaySink> sink = new WifiDisplaySink(session);
112    looper->registerHandler(sink);
113
114    if (connectToPort >= 0) {
115        sink->start(connectToHost.c_str(), connectToPort);
116    } else {
117        sink->start(uri.c_str());
118    }
119
120    looper->start(true /* runOnCallingThread */);
121
122    return 0;
123}
124