Repository: ossrs/srs
File: trunk/src/app/srs_app_http_static.cpp (C++)

Code before:
    }
    
    return err;
}

srs_error_t SrsVodStream::serve_m3u8_secret(ISrsHttpResponseWriter * w, ISrsHttpMessage * r, std::string fullpath)
{
    srs_error_t err = srs_success;

    SrsHttpMessage* hr = dynamic_cast<SrsHttpMessage*>(r);
    srs_assert(hr);

    SrsRequest* req = hr->to_request(hr->host())->as_http();
    SrsAutoFree(SrsRequest, req);

    string secret = r->query_get(SRS_SECRET_IN_HLS);
    if (!secret.empty() && secret_is_exist(secret)) {
        alive(secret);
        return SrsHttpFileServer::serve_m3u8_secret(w, r, fullpath);
    }

    if ((err = http_hooks_on_play(req)) != srs_success) {
        return srs_error_wrap(err, "HLS: http_hooks_on_play");
    }

    if (secret.empty()) {
        // make sure unique
        do {
            secret = srs_random_str(8);
        } while (secret_is_exist(secret));
    }

    std::string res = "#EXTM3U\r";
    res += "#EXT-X-STREAM-INF:BANDWIDTH=1,AVERAGE-BANDWIDTH=1\r";
    res += hr->path() + "?" + SRS_SECRET_IN_HLS + "=" + secret;

    int length = res.length();

    w->header()->set_content_length(length);
    w->header()->set_content_type("application/vnd.apple.mpegurl");
    w->write_header(SRS_CONSTS_HTTP_OK);

    if ((err = w->write((char*)res.c_str(), length)) != srs_success) {
        return srs_error_wrap(err, "write  bytes=%d", length);
    }

    if ((err = w->final_request()) != srs_success) {
        return srs_error_wrap(err, "final request");
    }

    // update the statistic when source disconveried.

Review comment (suggestion):
Here, you can consider using the UUID method to generate a unique ID for each client.

`TRANS_BY_GPT3`

Code after:
    
    return err;
}

srs_error_t SrsVodStream::serve_m3u8_ctx(ISrsHttpResponseWriter * w, ISrsHttpMessage * r, std::string fullpath)
{
    srs_error_t err = srs_success;

    SrsHttpMessage* hr = dynamic_cast<SrsHttpMessage*>(r);
    srs_assert(hr);

    SrsRequest* req = hr->to_request(hr->host())->as_http();
    SrsAutoFree(SrsRequest, req);

    string ctx = hr->query_get(SRS_CONTEXT_IN_HLS);
    if (!ctx.empty() && ctx_is_exist(ctx)) {
        alive(ctx, NULL);
        return SrsHttpFileServer::serve_m3u8_ctx(w, r, fullpath);
    }

    if ((err = http_hooks_on_play(req)) != srs_success) {
        return srs_error_wrap(err, "HLS: http_hooks_on_play");
    }

    if (ctx.empty()) {
        // make sure unique
        do {
            ctx = srs_random_str(8);  // the same as cid
        } while (ctx_is_exist(ctx));
    }

    std::stringstream ss;
    ss << "#EXTM3U" << SRS_CONSTS_LF;
    ss << "#EXT-X-STREAM-INF:BANDWIDTH=1,AVERAGE-BANDWIDTH=1" << SRS_CONSTS_LF;
    ss << hr->path() << "?" << SRS_CONTEXT_IN_HLS << "=" << ctx;
    if (!hr->query().empty() && hr->query_get(SRS_CONTEXT_IN_HLS).empty())
    {
        ss << "&" << hr->query();
    }

    std::string res = ss.str();
    int length = res.length();

    w->header()->set_content_length(length);
    w->header()->set_content_type("application/vnd.apple.mpegurl");
    w->write_header(SRS_CONSTS_HTTP_OK);

    if ((err = w->write((char*)res.c_str(), length)) != srs_success) {
        return srs_error_wrap(err, "write  bytes=%d", length);
    }