Recently in window is a platform to do a function to capture audio and video through Obs, and through the RTMP protocol to its encoded compressed data into its own program, since the OBS software with very powerful game recording and desktop recording functions, as well as input, output audio device data acquisition and mixing function , the current fight fish game Live is also used by this software as a recording tool.
OBS software because of the use of the Window SDK for audio acquisition, so only the Windows Vista version above the platform, so XP system users can not use this software,: https://obsproject.com/download
OBS currently only supports the RTMP protocol for broadcast, so need to build a rtmp server in their own programs, let Obs come in, and then through the OBS source, docking received rtmp audio and video data to reverse encapsulation, and finally assembled into the original audio and video raw code stream, such as H. MP3, Aac.
Below is a detailed description of the following main processes:
1. How to build rtmp server
First, the user can download my uploaded librtmp library http://down.51cto.com/data/1904438;
2, received the processing of the video
Because OBS after encoding in the first frame will be the PPS, SPS, SPI and other decoding key information protocol after the start of a separate transmission, so on the receiving end must save the data content, and added to each subsequent key frame head, otherwise it will not be normal decoding. After each receive a keyframe (by judging if its first byte is 0x17), add that information to its head, and then precede all the data frames with 3 bytes 0x00 0x00 0x01, the approximate process as above, refer to the code for details.
3, received the processing of audio
Currently OBS supports MP3 and AAC two audio encoding formats, for MP3 encoding, the receiver only needs to store the packet from the second byte all the way through the MP3 decoder for normal decoding. There is no processing for AAC encoding.
The code below has been stripped of some sensitive code for reference only.
Header file # include <rtmp/srs_protocol_rtmp.hpp> #include <rtmp/srs_protocol_rtmp_stack.hpp> #include < libs/srs_librtmp.hpp> #include <process.h> #define Initsockets () {WORD version; Wsadata wsadata;version = Makeword (n); WSAStartup (version, &wsadata);} #defineCleanupSockets () WSACleanup () class Rtmpserver:public cthread{public:rtmpserver (); ~rtmpserver (); int Startrtmpserver (); void Stoprtmpserver (); int Fmle_publish (srsrtmpserver* rtmp, srsrequest* req); Virtual int32_t Terminate (); virtual void execute ();p rivate:rtmpcapturedatacallback* callback_;shortm_port; Simplesocketstream *m_psocket;};
Definition file #include "rtmpsrv.h" #include "rtmp_input_device.h" Rtmpserver::rtmpserver ( ) {callback_= null;m_port= 1935;m_psocket= null;} Rtmpserver::~rtmpserver () {return;} Int rtmpserver::fmle_publish (srsrtmpserver* rtmp, srsrequest* req ) {if (NULL == rtmp | | NULL == req ) {return -1;} Int ret = error_success;bool hashead = false, bvfirst = true, bafirst = true;char head[128] = {0};char commonhead[4] = {0x00, 0x00, 0x00, 0x01};string video_data, audio_data;int headLen = 0; Uint64_t audio_last_recv_time (0), video_last_recv_time (0);while ( false == get_terminated () ) {Sleep (5); srsmessage* msg = null;if ((Ret = rtmp->recv_message (&msg)) != ERROR_SUCCESS) {break;} Srsautofree (SRSMESSAGE,&Nbsp;msg);if (Msg->header.is_amf0_command () | | msg->header.is_amf3_command ()) {SrsPacket* pkt = NULL;if (ret = Rtmp->decode_message (MSG,&NBSP;&PKT)) != error_success) {break;} Srsautofree (SRSPACKET,&NBSP;PKT);if (dynamic_cast<srsfmlestartpacket*> (PKT)) { Srsfmlestartpacket* unpublish = dynamic_cast<srsfmlestartpacket*> (PKT);if (ret = rtmp->fmle_unpublish (10, unpublish->transaction_id)) != error_success) {break;} break;} Continue;} if (Msg->header.is_audio ()) {audio_data.clear ();if (Bafirst) {audio_data.append (char *) msg->payload + 1, msg->size - 1); bafirst = false;} Elseaudio_data.append ((char *) msg->payload, msg->size -1);} else if (Msg->header.is_video ()) {if (!hashead) {memcpy (head, commonhead, 4); int8_t *skip = msg->payload;int8_t *skip2 = msg->payload;while (* (skip++) != 0x67); (* (skip2++) != 0x68);int diff = skip2 - skip;if (diff <= 0) {continue;} memcpy (head + 4, skip - 1, diff - 4);//copy spsmemcpy (head + &NBSP;4&NBSP;+&NBSP;DIFF&NBSP;-&NBSP;4,&NBSP;COMMONHEAD,&NBSP;4); memcpy (Head + 4 + diff &NBSP;-&NBSP;4&NBSP;+&NBSP;4,&NBSP;SKIP2&NBSP;-&NBSP;1,&NBSP;4); //copy ppshashead = true; Headlen = 4 + diff - 4 + 4 + 4;} Else{video_data.clear ();if (bvfirst) {Video_last_recv_time = msg->header.timestamp;video_ Data.append (head, 128); Video_data.append (const char *) msg->payload+9, msg->size &NBSP;-&NBSP;9); bvfirst = false;} else{if (msg->header.timestamp > video_last_recv_time) {video_last_recv_time&Nbsp;= msg->header.timestamp;} if (msg->payload[0] == 0x17)//i frame{video_data.append (Head, headLen);} Video_data.append (commonhead + 1, 3); Video_data.append (const char *) msg->payload+ 9, msg->size - 9);}}} Return ret;} Void rtmpserver::execute ( ) {if (null == m_psocket) {return ;} M_psocket->listen ("0.0.0.0", m_port, 10); simplesocketstream* server = new simplesocketstream;while ( false == get_terminated () ) {if (null == server) {simplesocketstream* server = New simplesocketstream;} if (Null != m_psocket && 0 == m_psocket->accept (*server)) {AUDIO_ INFO ("rtmpserver thread accept obs connected!"); Srsrtmpserver* rtmp = new srsrtmpserver (server); Srsrequest* req = new srsrequest (); int ret = ERROR_SUCCESS;if ((Ret = rtmp->handshake ()) != error_success ) {break;} srsmessage* msg = null; srsconnectapppacket* pkt = null;if ((ret = srs_rtmp_expect_message< Srsconnectapppacket> (Rtmp->get_protocol (), &msg, &pkt)) != error_success) {break;} if ((Ret = rtmp->response_connect_app (req, 0)) != error_success) {break;} while ( false == get_terminated () ) {srsrtmpconntype type;if ((Ret = rtmp->identify_client (10, type, req->stream, req->duration)) != error_success) {terminate ();// rtmpserver thread peer shutdownbreak;} ASSERT (Type == srsrtmpconnfmlepublish); Req->strip (); Rtmp->start_fmle_publish (ten); Int ret = fmle_publish ( rtmp, req );if (Ret !=&nbsP Error_control_republish) && (ret != error_control_rtmp_close)) //obs active stop Stream { Break;}} if (null != rtmp) {delete rtmp;rtmp = null;} if (null != req) {delete req;req = null;} if (null != server) {server->close_socket ();}} Else{sleep (5);}} if (null != server) {delete server;server = null;}} Int rtmpserver::startrtmpserver ( ) {initsockets ();m_port = 1935;if (NULL != M_psocket) {m_psocket->close_socket ();d elete m_psocket;m_psocket = null;} M_psocket = new simplesocketstream;m_psocket->create_socket (); Return start ();} Void rtmpserver::stoprtmpserver ( ) {terminate ();if (null != m_psocket) {m_psocket-> Close_socket ();d elete m_psocket;m_psocket = null;} Cleanupsockets ();}; Int32_t rtmpserver::terminate () {terminated_ = true;if (thr_handle_ != null) {if (Wait_timeout == waitforsingleobject (thr_handle_, 100)) {TerminateThread (thr_ HANDLE_,&NBSP;-1);} Thr_handle_ = null;} return 0;} #endif
Finally, fill in the Rtmp://ip:port in the OBS broadcast Setup server.
This article is from the "Big bro" blog, make sure to keep this source http://9662841.blog.51cto.com/9652841/1583498
Bring OBS recording data into your program via RTMP protocol