Transfer Mode | Description |
Broadcasting mode | You can send redundant data over multiple internet connections to ensure data integrity and transfer reliability. |
Primary/Backup mode | In this mode, only one connection is active at a time. The connection is selected in real time based on stability and reliability. This also reduces bandwidth usage because no redundant data is sent. |
Aggregation mode | In scenarios requiring a high bitrate and bandwidth, the bandwidth of a single internet connection may be unable to meet the requirements. This mode can split data, send them over multiple connections, and then combine them at the receiver end. |
TmioProxy
instance:std::unique_ptr<tmio::TmioProxy> proxy_ = tmio::TmioProxy::createUnique();
void setListener(TmioProxyListener *listener);
TmioProxyListener
:Tmio
parameters in this callback. For simple configuration, you can use the auxiliary methods provided in tmio-preset.h
./*void onTmioConfig(Tmio *tmio);*/void onTmioConfig(tmio::Tmio *tmio) override {auto protocol = tmio->getProtocol();if (protocol == tmio::Protocol::SRT) {tmio::SrtPreset::rtmp(tmio);} else if (protocol == tmio::Protocol::RIST) {tmio->setIntOption(tmio::base_options::RECV_SEND_FLAGS,tmio::base_options::FLAG_SEND);}}
/*void onStart(const char *local_addr, uint16_t local_port);*/void onStart(const char *addr, uint16_t port) override {LOGFI("ip %s, port %" PRIu16, addr, port);}
/*void onError(ErrorType type, const std::error_code &err);*/void onError(tmio::TmioProxyListener::ErrorType type,const std::error_code &err) override {LOGFE("error type %s, %s, %d", tmio::TmioProxyListener::errorType(type),err.message().c_str(), err.value());}
ErrorType
to determine whether an error is a local or remote I/O error. A local I/O error is usually because RTMP streaming is stopped by the streamer. Therefore, if streaming has ended, you can ignore such errors. However, a remote I/O error usually needs to be handled.std::error_code start(const std::string &local_url, const std::string &remote_url, void * config=nullptr)
Parameter | Description |
local_url | It supports only the TCP scheme in the format of tcp://${ip}:${port}. `port` can be `0`, which indicates to bind a random port. After the binding succeeds, the bound port will be returned to the application through the `onStart()` callback. Using port `0` can avoid binding failures due to issues such as port occupancy and no permissions. |
remote_url | The remote server URL. |
config | The configuration parameter. This parameter is valid if SRT connection bonding or QUIC H3 is enabled. For details, see the definition of the `SrtFeatureConfig` structure in tmio.h. |
proxy_->start(local_url, remote_url, NULL);
tmio::TmioFeatureConfig option;option_.protocol = tmio::Protocol::SRT;option_.trans_mode = static_cast<int>(tmio::SrtTransMode::SRT_TRANS_BACKUP);/*-----------------------------------------------------------*/{// You can add multiple connections as needed.option_.addAvailableNet(net_name, local_addr, remote_url, 0, weight, -1);}/*-----------------------------------------------------------*/proxy_->start(local_url, remote_url, &option_);
/*void stop();*/proxy_.stop();
Tmio
instance and configure the parameters:tmio_ = tmio::TmioFactory::createUnique(tmio::Protocol::SRT);tmio::SrtPreset::mpegTsLossless(tmio_.get());tmio_->setIntOption(tmio::srt_options::CONNECT_TIMEOUT, 4000);tmio_->setBoolOption(tmio::base_options::THREAD_SAFE_CHECK, true);
Tmio
instance: You can use TmioFactory
to create it.// Select an appropriate configuration based on different parameter attributesbool setBoolOption(const std::string &optname, bool value);bool setIntOption(const std::string &optname, int64_t value);bool setDoubleOption(const std::string &optname, double value);bool setStrOption(const std::string &optname, const std::string &value);...
/*** open the stream specified by url** @param config protocol dependent*/virtual std::error_code open(const std::string &url,void *config = nullptr) = 0;
config
can be NULL
)// A single connection by defaultauto err = tmio->open(TMIO_SRT_URL);if (err) {LOGE("open failed, %d, %s", err.value(), err.message().c_str());}
config
for the SRT bonding feature, see the definition of the TmioFeatureConfig
structure in the tmio.h
file.tmio::TmioFeatureConfig option_;option_.protocol = tmio::Protocol::SRT;option_.trans_mode = static_cast<int>(tmio::SrtTransMode::SRT_TRANS_BACKUP);option_.addAvailableNet(net_name, local_addr, remote_url, 0, weight, -1);
// Multi-connection bondingauto err = tmio_->open(TMIO_SRT_URL, &option_);if (err) {LOGE("open failed, %d, %s", err.value(), err.message().c_str());}
open
API to add new transfer connections to the group.int ret = tmio_->send(buf.data(), datalen, err);if (ret < 0) {LOGE("send failed, %d, %s", err.value(), err.message().c_str());break;}
/*** receive data** @param err return error details* @return number of bytes which were received, or < 0 to indicate error*/virtual int recv(uint8_t *buf, int len, std::error_code &err) = 0;using RecvCallback = std::function<bool(const uint8_t *buf, int len, const std::error_code &err)>;/*** receive data in event loop** recvLoop() block current thread, receive data in a loop and pass the data to recvCallback* @param recvCallback return true to continue the receive loop, false for break*/virtual void recvLoop(const RecvCallback &recvCallback) = 0;
while (true) {ret = tmio_->recv(buf.data(), buf.size(), err);if (ret < 0) {LOGE("recv error: %d, %s", err.value(), err.message().c_str());break;}...}
FILE *file = fopen(output_path, "w");tmio_->recvLoop([file](const uint8_t *buf, int len,const std::error_code &err) {if (len < 0) {fwrite(buf, 1, len, file);} else if (len < 0) {LOGE("recv error: %d, %s", err.value(), err.message().c_str());}return true;});
Tmio
instance:tmio_->close();
tmio::PerfStats stats_;tmio_->control(tmio::ControlCmd::GET_STATS, &stats_);
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);NetworkRequest request = new NetworkRequest.Builder().addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR).addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET).build();ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback(){@Overridepublic void onAvailable(@NonNull Network network) {Log.d(TAG, "The mobile data network has been enabled");super.onAvailable(network);}}
Was this page helpful?