Blame view

trunk/src/libs/srs_librtmp.cpp 10.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/*
The MIT License (MIT)

Copyright (c) 2013-2014 winlin

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <srs_librtmp.hpp>

#include <stdlib.h>

#include <string>
using namespace std;

#include <srs_kernel_error.hpp>
#include <srs_protocol_rtmp.hpp>
#include <srs_lib_simple_socket.hpp>
#include <srs_kernel_log.hpp>
#include <srs_protocol_utility.hpp>
36 37
#include <srs_core_autofree.hpp>
#include <srs_protocol_rtmp_stack.hpp>
38 39 40

// if user want to define log, define the folowing macro.
#ifndef SRS_RTMP_USER_DEFINED_LOG
41 42 43
    // kernel module.
    ISrsLog* _srs_log = new ISrsLog();
    ISrsThreadContext* _srs_context = new ISrsThreadContext();
44 45 46 47 48 49 50
#endif

/**
* export runtime context.
*/
struct Context
{
51 52 53 54 55 56 57 58
    std::string url;
    std::string tcUrl;
    std::string host;
    std::string port;
    std::string vhost;
    std::string app;
    std::string stream;
    
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    SrsRtmpClient* rtmp;
    SimpleSocketStream* skt;
    int stream_id;
    
    Context() {
        rtmp = NULL;
        skt = NULL;
        stream_id = 0;
    }
    virtual ~Context() {
        srs_freep(rtmp);
        srs_freep(skt);
    }
};

int srs_librtmp_context_connect(Context* context) 
{
76
    int ret = ERROR_SUCCESS;
77 78
    
    // parse uri
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    size_t pos = string::npos;
    string uri = context->url;
    // tcUrl, stream
    if ((pos = uri.rfind("/")) != string::npos) {
        context->stream = uri.substr(pos + 1);
        context->tcUrl = uri = uri.substr(0, pos);
    }
    // schema
    if ((pos = uri.find("rtmp://")) != string::npos) {
        uri = uri.substr(pos + 7);
    }
    // host/vhost/port
    if ((pos = uri.find(":")) != string::npos) {
        context->vhost = context->host = uri.substr(0, pos);
        uri = uri.substr(pos + 1);
        
        if ((pos = uri.find("/")) != string::npos) {
            context->port = uri.substr(0, pos);
            uri = uri.substr(pos + 1);
        }
    } else {
        if ((pos = uri.find("/")) != string::npos) {
            context->vhost = context->host = uri.substr(0, pos);
            uri = uri.substr(pos + 1);
        }
        context->port = RTMP_DEFAULT_PORT;
    }
    // app
    context->app = uri;
    // query of app
    if ((pos = uri.find("?")) != string::npos) {
        context->app = uri.substr(0, pos);
        string query = uri.substr(pos + 1);
        if ((pos = query.find("vhost=")) != string::npos) {
            context->vhost = query.substr(pos + 6);
            if ((pos = context->vhost.find("&")) != string::npos) {
                context->vhost = context->vhost.substr(pos);
            }
        }
    }
119 120
    
    // create socket
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    srs_freep(context->skt);
    context->skt = new SimpleSocketStream();
    
    if ((ret = context->skt->create_socket()) != ERROR_SUCCESS) {
        return ret;
    }
    
    // connect to server:port
    string server = srs_dns_resolve(context->host);
    if (server.empty()) {
        return -1;
    }
    if ((ret = context->skt->connect(server.c_str(), ::atoi(context->port.c_str()))) != ERROR_SUCCESS) {
        return ret;
    }
    
    return ret;
138 139 140 141 142 143 144 145 146
}

#ifdef __cplusplus
extern "C"{
#endif

srs_rtmp_t srs_rtmp_create(const char* url)
{
    Context* context = new Context();
147
    context->url = url;
148 149 150 151 152 153 154 155 156 157 158 159 160
    return context;
}

void srs_rtmp_destroy(srs_rtmp_t rtmp)
{
    srs_assert(rtmp != NULL);
    Context* context = (Context*)rtmp;
    
    srs_freep(context);
}

int srs_simple_handshake(srs_rtmp_t rtmp)
{
161 162
    int ret = ERROR_SUCCESS;
    
163 164 165 166 167 168 169
    srs_assert(rtmp != NULL);
    Context* context = (Context*)rtmp;
    
    // parse uri, resolve host, connect to server:port
    if ((ret = srs_librtmp_context_connect(context)) != ERROR_SUCCESS) {
        return ret;
    }
170 171 172 173 174 175 176 177 178 179
    
    // simple handshake
    srs_freep(context->rtmp);
    context->rtmp = new SrsRtmpClient(context->skt);
    
    if ((ret = context->rtmp->simple_handshake()) != ERROR_SUCCESS) {
        return ret;
    }
    
    return ret;
180 181 182 183
}

int srs_complex_handshake(srs_rtmp_t rtmp)
{
184
#ifndef SRS_AUTO_SSL
185
    return ERROR_RTMP_HS_SSL_REQUIRE;
186 187
#endif
188 189
    int ret = ERROR_SUCCESS;
    
190 191 192 193 194 195 196
    srs_assert(rtmp != NULL);
    Context* context = (Context*)rtmp;
    
    // parse uri, resolve host, connect to server:port
    if ((ret = srs_librtmp_context_connect(context)) != ERROR_SUCCESS) {
        return ret;
    }
197 198 199 200 201 202 203 204 205 206
    
    // complex handshake
    srs_freep(context->rtmp);
    context->rtmp = new SrsRtmpClient(context->skt);
    
    if ((ret = context->rtmp->complex_handshake()) != ERROR_SUCCESS) {
        return ret;
    }
    
    return ret;
207 208 209 210
}

int srs_connect_app(srs_rtmp_t rtmp)
{
211 212
    int ret = ERROR_SUCCESS;
    
213 214
    srs_assert(rtmp != NULL);
    Context* context = (Context*)rtmp;
215 216 217 218 219 220 221 222 223 224 225 226 227
    
    string tcUrl = "rtmp://";
    tcUrl += context->vhost;
    tcUrl += ":";
    tcUrl += context->port;
    tcUrl += "/";
    tcUrl += context->app;
    
    if ((ret = context->rtmp->connect_app(context->app, tcUrl)) != ERROR_SUCCESS) {
        return ret;
    }
    
    return ret;
228 229 230 231
}

int srs_play_stream(srs_rtmp_t rtmp)
{
232 233
    int ret = ERROR_SUCCESS;
    
234 235 236 237 238 239 240 241 242 243
    srs_assert(rtmp != NULL);
    Context* context = (Context*)rtmp;
    
    if ((ret = context->rtmp->create_stream(context->stream_id)) != ERROR_SUCCESS) {
        return ret;
    }
    if ((ret = context->rtmp->play(context->stream, context->stream_id)) != ERROR_SUCCESS) {
        return ret;
    }
    
244
    return ret;
245 246 247 248
}

int srs_publish_stream(srs_rtmp_t rtmp)
{
249 250
    int ret = ERROR_SUCCESS;
    
251 252 253
    srs_assert(rtmp != NULL);
    Context* context = (Context*)rtmp;
    
254 255 256 257
    if ((ret = context->rtmp->fmle_publish(context->stream, context->stream_id)) != ERROR_SUCCESS) {
        return ret;
    }
    
258
    return ret;
259 260
}
261 262
const char* srs_type2string(int type)
{
263 264 265 266 267 268 269 270
    switch (type) {
        case SRS_RTMP_TYPE_AUDIO: return "Audio";
        case SRS_RTMP_TYPE_VIDEO: return "Video";
        case SRS_RTMP_TYPE_SCRIPT: return "Data";
        default: return "Unknown";
    }
    
    return "Unknown";
271 272 273 274
}

int srs_read_packet(srs_rtmp_t rtmp, int* type, u_int32_t* timestamp, char** data, int* size)
{
275 276 277 278 279 280 281
    *type = 0;
    *timestamp = 0;
    *data = NULL;
    *size = 0;
    
    int ret = ERROR_SUCCESS;
    
282 283 284 285
    srs_assert(rtmp != NULL);
    Context* context = (Context*)rtmp;
    
    for (;;) {
286 287
        SrsMessage* msg = NULL;
        if ((ret = context->rtmp->recv_message(&msg)) != ERROR_SUCCESS) {
288 289 290 291 292 293
            return ret;
        }
        if (!msg) {
            continue;
        }
        
294
        SrsAutoFree(SrsMessage, msg, false);
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
        
        if (msg->header.is_audio()) {
            *type = SRS_RTMP_TYPE_AUDIO;
            *timestamp = (u_int32_t)msg->header.timestamp;
            *data = (char*)msg->payload;
            *size = (int)msg->size;
            // detach bytes from packet.
            msg->payload = NULL;
        } else if (msg->header.is_video()) {
            *type = SRS_RTMP_TYPE_VIDEO;
            *timestamp = (u_int32_t)msg->header.timestamp;
            *data = (char*)msg->payload;
            *size = (int)msg->size;
            // detach bytes from packet.
            msg->payload = NULL;
        } else if (msg->header.is_amf0_data() || msg->header.is_amf3_data()) {
            *type = SRS_RTMP_TYPE_SCRIPT;
            *data = (char*)msg->payload;
            *size = (int)msg->size;
            // detach bytes from packet.
            msg->payload = NULL;
        } else {
            // ignore and continue
            continue;
        }
        
        // got expected message.
        break;
    }
    
325
    return ret;
326 327 328 329
}

int srs_write_packet(srs_rtmp_t rtmp, int type, u_int32_t timestamp, char* data, int size)
{
330 331
    int ret = ERROR_SUCCESS;
    
332 333
    srs_assert(rtmp != NULL);
    Context* context = (Context*)rtmp;
334
    
335
    SrsSharedPtrMessage* msg = NULL;
336 337 338 339 340
    
    if (type == SRS_RTMP_TYPE_AUDIO) {
        SrsMessageHeader header;
        header.initialize_audio(size, timestamp, context->stream_id);
        
341
        msg = new SrsSharedPtrMessage();
342 343 344 345 346 347 348 349
        if ((ret = msg->initialize(&header, data, size)) != ERROR_SUCCESS) {
            srs_freepa(data);
            return ret;
        }
    } else if (type == SRS_RTMP_TYPE_VIDEO) {
        SrsMessageHeader header;
        header.initialize_video(size, timestamp, context->stream_id);
        
350
        msg = new SrsSharedPtrMessage();
351 352 353 354 355 356 357 358
        if ((ret = msg->initialize(&header, data, size)) != ERROR_SUCCESS) {
            srs_freepa(data);
            return ret;
        }
    } else if (type == SRS_RTMP_TYPE_SCRIPT) {
        SrsMessageHeader header;
        header.initialize_amf0_script(size, context->stream_id);
        
359
        msg = new SrsSharedPtrMessage();
360 361 362 363 364 365 366 367
        if ((ret = msg->initialize(&header, data, size)) != ERROR_SUCCESS) {
            srs_freepa(data);
            return ret;
        }
    }
    
    if (msg) {
        // send out encoded msg.
368
        if ((ret = context->rtmp->send_and_free_message(msg)) != ERROR_SUCCESS) {
369 370 371 372 373 374 375 376
            return ret;
        }
    } else {
        // directly free data if not sent out.
        srs_freepa(data);
    }
    
    return ret;
377 378
}
379 380
int srs_ssl_enabled()
{
381
#ifndef SRS_AUTO_SSL
382
    return false;
383
#endif
384
    return true;
385 386 387 388
}

int srs_version_major()
{
389
    return ::atoi(VERSION_MAJOR);
390 391 392 393
}

int srs_version_minor()
{
394
    return ::atoi(VERSION_MINOR);
395 396 397 398
}

int srs_version_revision()
{
399
    return ::atoi(VERSION_REVISION);
400 401 402 403 404
}

#ifdef __cplusplus
}
#endif