RTMP代码示例

简单的rtmp推流代码

手写推流代码,该代码可以完成服务端推流rtmp功能

//
//  rtmp_client.cpp
//  myFirstDemoC++
//

#include "rtmp_client.hpp"
using namespace std;

rtmp_client::rtmp_client(char * server_url):server_url_(server_url)
{
    rtmp_ctx_ = nullptr;
    cout << "rtmp client init url " << server_url_ << endl;
}

rtmp_client::~rtmp_client()
{

    rtmp_process_.join();
    
    if (list_data_.size() > 0)
    {
        rtmp_data * data = nullptr;
        
        for(list<rtmp_data *>::iterator it = list_data_.begin(); it != list_data_.end(); it++)
        {
            data = *it;
            delete [] data->data;
            delete data;
            list_data_.pop_front();
        }
    }
    
    if (rtmp_ctx_)
    {
        RTMP_Close(rtmp_ctx_);
        RTMP_Free(rtmp_ctx_);
        rtmp_ctx_ = nullptr;
    }
}

void rtmp_client::rtmp_send_data_(rtmp_data *data)
{
    RTMPPacket * packet = nullptr;
    if (!data)
        return ;
    
    cout << "send rtmp data private start" << endl;
    
    packet = new RTMPPacket;
    RTMPPacket_Alloc(packet, 64*1024);
    RTMPPacket_Reset(packet);
    
    packet->m_nChannel = 0x4;
    packet->m_hasAbsTimestamp = 0;
    
    if (RTMP_IsConnected(rtmp_ctx_))
    {
        packet->m_headerType = RTMP_PACKET_SIZE_LARGE;
        packet->m_nTimeStamp = data->m_nTimeStamp;//时间戳
        packet->m_packetType = data->m_packetType;//包类型
        packet->m_nBodySize  = data->len;//包大小
        
        memcpy(packet->m_body, data->data, data->len);
    
        RTMP_SendPacket(rtmp_ctx_, packet, 0);
    }
    
    RTMPPacket_Free(packet);
    delete packet;
    
    return ;
}

void rtmp_client::rtmp_process()
{
    rtmp_data * data = nullptr;
    while(1)
    {
        mtx_.lock();
        data = list_data_.front();
        if (data)
        {
            list_data_.pop_front();
            mtx_.unlock();
            cout << "list size before"  << list_data_.size()<<endl;
            cout << "rtmp process send data len " << data->len <<endl;
            rtmp_send_data_(data);
            delete [] data->data;
            delete data;
            cout << "list size after"  << list_data_.size()<<endl;
        }
        else
        {
            mtx_.unlock();
            this_thread::sleep_for(chrono::microseconds(10));
            //cout << "rtmp process no data" << endl;
        }
        
    }
}
int rtmp_client::rtmp_send_packet(rtmp_data * data)
{
    if (!data)
        return -1;
    mtx_.lock();
    list_data_.push_back(data);
    mtx_.unlock();
    return 0;
}
int rtmp_client::rtmp_connect_server()
{
    int ret = -1;
    rtmp_ctx_ = RTMP_Alloc();
    if (!rtmp_ctx_)
    {
        return ret;
    }

    RTMP_Init(rtmp_ctx_);
    RTMP_SetupURL(rtmp_ctx_, const_cast<char *>(server_url_.c_str()));
    RTMP_EnableWrite(rtmp_ctx_);
    rtmp_ctx_->Link.timeout = 5;
    
    RTMP_Connect(rtmp_ctx_, NULL);

    RTMP_ConnectStream(rtmp_ctx_, 0);

    rtmp_process_= thread(&rtmp_client::rtmp_process, this);
    
    ret = 0;
    return ret;
}

//
//  rtmp_client.hpp
//  myFirstDemoC++

#ifndef rtmp_client_hpp
#define rtmp_client_hpp

#include <stdio.h>
#include <iostream>
#include <string>
#include <thread>
#include <list>
#include "librtmp/rtmp.h"

typedef struct {
    uint8_t *data;
    uint32_t     len;
    uint32_t m_nBodySize;
    uint8_t m_packetType;
    uint32_t m_nTimeStamp;    /* timestamp */
}rtmp_data;

class rtmp_client{
public:
    rtmp_client(char * server_url);
    ~rtmp_client();
    int rtmp_connect_server();
    int rtmp_send_packet(rtmp_data *data);
    int rtmp_disconnect_server();
private:
    void rtmp_process();
    void rtmp_send_data_(rtmp_data * data);
    std::list<rtmp_data *> list_data_;
    std::thread rtmp_process_;
    std::string server_url_;
    std::mutex mtx_;
    RTMP *rtmp_ctx_;
};


#endif /* rtmp_client_hpp */

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 Live555 库进行 RTMP 推流的示例代码,需要在项目中引入 Live555 库: ```csharp using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using Live; using Live.LiveMedia; namespace RtmpPusher { class RtmpPusher { private RTMPClient rtmpClient; private H264VideoStreamSource videoSource; public RtmpPusher() { rtspClient = new RTSPClient(Environment.GetCommandLineArgs()[1], 0, null, 0, -1); videoSource = H264VideoStreamSource.createNew(Envir.instance(), null); videoSource.setFramerate(30); videoSource.start(); videoSource.getNextFrame(true); rtspClient.setStreamSource(videoSource, null, null, null); rtspClient.sendDescribeCommand(new RTSPClient.DescribeCallback(describeCallback)); } private void describeCallback(RTSPClient.clientContinueFunc continueFunc) { rtspClient.sendSetupCommand(0, new RTSPClient.SetupCallback(setupCallback)); } private void setupCallback(RTSPClient.clientContinueFunc continueFunc, int resultCode, string resultString, Session session, MediaSubsession subsession) { rtspClient.sendPlayCommand(continueFunc, 0.0f, -1.0f, 1.0f); rtspClient.sendPauseCommand(continueFunc, 0.0f); rtspClient.sendPauseCommand(continueFunc, 0.0f); rtspClient.sendPlayCommand(continueFunc, 0.0f, -1.0f, 1.0f); rtspClient.sendPauseCommand(continueFunc, 0.0f); rtspClient.sendPauseCommand(continueFunc, 0.0f); rtspClient.sendPlayCommand(continueFunc, 0.0f, -1.0f, 1.0f); rtspClient.sendPauseCommand(continueFunc, 0.0f); rtspClient.sendPauseCommand(continueFunc, 0.0f); rtspClient.sendPlayCommand(continueFunc, 0.0f, -1.0f, 1.0f); rtspClient.sendOptionsCommand(continueFunc, 0); rtspClient.sendAnnounceCommand(continueFunc, null); rtspClient.sendRecordCommand(continueFunc, null); rtspClient.sendTeardownCommand(continueFunc, null); rtspClient.sendOptionsCommand(continueFunc, 1); rtspClient.sendSetParameterCommand(continueFunc, null); rtspClient.sendGetParameterCommand(continueFunc, null); rtspClient.sendPauseCommand(continueFunc, 0.0f); rtspClient.sendPlayCommand(continueFunc, 0.0f, -1.0f, 1.0f); rtspClient.sendGetParameterCommand(continueFunc, null); } public void pushStream() { // 指定 RTMP 服务器地址和应用名称以及流名称 rtspClient.sendAnnounceCommand(new RTSPClient.AnnounceCallback(announceCallback), "rtmp://server-ip-address/application/stream-name", "rtmp", "video/avc", "H264", "96", null); } private void announceCallback(RTSPClient.clientContinueFunc continueFunc, int resultCode, string resultString) { rtspClient.sendSetParameterCommand(continueFunc, null); rtspClient.sendOptionsCommand(continueFunc, 1); rtspClient.sendGetParameterCommand(continueFunc, null); rtspClient.sendPauseCommand(continueFunc, 0.0f); rtspClient.sendPlayCommand(continueFunc, 0.0f, -1.0f, 1.0f); rtspClient.sendGetParameterCommand(continueFunc, null); } public void close() { rtspClient.sendTeardownCommand(null, null); } } } ``` 这个示例代码使用了 Live555 库中的 RTSPClient 和 H264VideoStreamSource 类,通过 RTSPClient 从 RTSP 服务器获取视频流,然后使用 H264VideoStreamSource 将视频流转换为 H.264 编码格式,并将其推送到 RTMP 服务器上。需要注意的是,这只是一个简单的示例,实际应用中还需要根据具体需求进行修改和调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值