mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-07-03 17:27:33 +08:00
初步完成Windows下的移植
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
#ifndef __rtmp_h
|
||||
#define __rtmp_h
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include "Util/util.h"
|
||||
@@ -12,7 +11,14 @@ using namespace ZL::Util;
|
||||
|
||||
#define PORT 1935
|
||||
#define DEFAULT_CHUNK_LEN 128
|
||||
|
||||
#if !defined(_WIN32)
|
||||
#define PACKED __attribute__((packed))
|
||||
#else
|
||||
#define PACKED
|
||||
#endif //!defined(_WIN32)
|
||||
|
||||
|
||||
#define HANDSHAKE_PLAINTEXT 0x03
|
||||
#define RANDOM_LEN (1536 - 8)
|
||||
|
||||
@@ -52,6 +58,11 @@ using namespace ZL::Util;
|
||||
#define FLV_KEY_FRAME 1
|
||||
#define FLV_INTER_FRAME 2
|
||||
|
||||
|
||||
#if defined(_WIN32)
|
||||
#pragma pack(push, 1)
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
class RtmpHandshake {
|
||||
public:
|
||||
RtmpHandshake(uint32_t _time, uint8_t *_random = nullptr) {
|
||||
@@ -87,6 +98,9 @@ public:
|
||||
uint8_t streamId[4]; /* Note, this is little-endian while others are BE */
|
||||
}PACKED;
|
||||
|
||||
#if defined(_WIN32)
|
||||
#pragma pack(pop)
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
class RtmpPacket {
|
||||
public:
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
#ifndef SRC_RTMP_RTMPMEDIASOURCE_H_
|
||||
#define SRC_RTMP_RTMPMEDIASOURCE_H_
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include <mutex>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
@@ -19,14 +19,14 @@ RtmpParser::RtmpParser(const AMFValue &val) {
|
||||
//264
|
||||
m_bHaveVideo = true;
|
||||
} else {
|
||||
InfoL << "不支持RTMP视频格式:" << videoCodec.as_string();
|
||||
InfoL << "不支持RTMP视频格式:" << videoCodec.as_string();
|
||||
}
|
||||
}else if (videoCodec.type() != AMF_NULL){
|
||||
if (videoCodec.as_integer() == 7) {
|
||||
//264
|
||||
m_bHaveVideo = true;
|
||||
} else {
|
||||
InfoL << "不支持RTMP视频格式:" << videoCodec.as_integer();
|
||||
InfoL << "不支持RTMP视频格式:" << videoCodec.as_integer();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,14 +36,14 @@ RtmpParser::RtmpParser(const AMFValue &val) {
|
||||
//aac
|
||||
m_bHaveAudio = true;
|
||||
} else {
|
||||
InfoL << "不支持RTMP音频格式:" << audioCodec.as_string();
|
||||
InfoL << "不支持RTMP音频格式:" << audioCodec.as_string();
|
||||
}
|
||||
}else if (audioCodec.type() != AMF_NULL) {
|
||||
if (audioCodec.as_integer() == 10) {
|
||||
//aac
|
||||
m_bHaveAudio = true;
|
||||
} else {
|
||||
InfoL << "不支持RTMP音频格式:" << audioCodec.as_integer();
|
||||
InfoL << "不支持RTMP音频格式:" << audioCodec.as_integer();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -144,7 +144,7 @@ inline void RtmpPlayer::send_connect() {
|
||||
auto level = val["level"].as_string();
|
||||
auto code = val["code"].as_string();
|
||||
if(level != "status"){
|
||||
throw std::runtime_error(StrPrinter <<"connect 失败:" << level << " " << code << endl);
|
||||
throw std::runtime_error(StrPrinter <<"connect 失败:" << level << " " << code << endl);
|
||||
}
|
||||
send_createStream();
|
||||
});
|
||||
@@ -170,7 +170,7 @@ inline void RtmpPlayer::send_play() {
|
||||
auto level = val["level"].as_string();
|
||||
auto code = val["code"].as_string();
|
||||
if(level != "status"){
|
||||
throw std::runtime_error(StrPrinter <<"play 失败:" << level << " " << code << endl);
|
||||
throw std::runtime_error(StrPrinter <<"play 失败:" << level << " " << code << endl);
|
||||
}
|
||||
};
|
||||
addOnStatusCB(fun);
|
||||
@@ -187,7 +187,7 @@ inline void RtmpPlayer::send_pause(bool bPause) {
|
||||
auto code = val["code"].as_string();
|
||||
if(level != "status") {
|
||||
if(!bPause){
|
||||
throw std::runtime_error(StrPrinter <<"pause 恢复播放失败:" << level << " " << code << endl);
|
||||
throw std::runtime_error(StrPrinter <<"pause 恢复播放失败:" << level << " " << code << endl);
|
||||
}
|
||||
}else{
|
||||
m_bPaused = bPause;
|
||||
@@ -236,7 +236,7 @@ void RtmpPlayer::onCmd_onStatus(AMFDecoder &dec) {
|
||||
}
|
||||
}
|
||||
if(val.type() != AMF_OBJECT){
|
||||
throw std::runtime_error("onStatus: 未找到结果对象");
|
||||
throw std::runtime_error("onStatus:the result object was not found");
|
||||
}
|
||||
|
||||
lock_guard<recursive_mutex> lck(m_mtxOnStatusCB);
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
#ifndef SRC_RTMP_RtmpPlayer2_H_
|
||||
#define SRC_RTMP_RtmpPlayer2_H_
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <functional>
|
||||
|
||||
@@ -14,17 +14,29 @@
|
||||
using namespace ZL::Util;
|
||||
|
||||
#ifdef ENABLE_OPENSSL
|
||||
#include "Util/SSLBox.h"
|
||||
#include <openssl/hmac.h>
|
||||
static string openssl_HMACsha256(const void *key,unsigned int key_len,
|
||||
const void *data,unsigned int data_len){
|
||||
std::shared_ptr<char> out(new char[32],[](char *ptr){delete [] ptr;});
|
||||
unsigned int out_len;
|
||||
|
||||
#if defined(WIN32)
|
||||
HMAC_CTX *ctx = HMAC_CTX_new();
|
||||
HMAC_CTX_reset(ctx);
|
||||
HMAC_Init_ex(ctx, key, key_len, EVP_sha256(), NULL);
|
||||
HMAC_Update(ctx, (unsigned char*)data, data_len);
|
||||
HMAC_Final(ctx, (unsigned char *)out.get(), &out_len);
|
||||
HMAC_CTX_reset(ctx);
|
||||
HMAC_CTX_free(ctx);
|
||||
#else
|
||||
HMAC_CTX ctx;
|
||||
HMAC_CTX_init(&ctx);
|
||||
HMAC_Init_ex(&ctx, key, key_len, EVP_sha256(), NULL);
|
||||
HMAC_Update(&ctx, (unsigned char*)data, data_len);
|
||||
HMAC_Final(&ctx, (unsigned char *)out.get(), &out_len);
|
||||
HMAC_CTX_cleanup(&ctx);
|
||||
#endif // defined(WIN32)
|
||||
return string(out.get(),out_len);
|
||||
}
|
||||
#endif //ENABLE_OPENSSL
|
||||
@@ -159,7 +171,7 @@ void RtmpProtocol::sendRequest(int iCmd, const string& str) {
|
||||
void RtmpProtocol::sendRtmp(uint8_t ui8Type, uint32_t ui32StreamId,
|
||||
const std::string& strBuf, uint32_t ui32TimeStamp, int iChunkId) {
|
||||
if (iChunkId < 2 || iChunkId > 63) {
|
||||
auto strErr = StrPrinter << "不支持发送该类型的块流 ID:" << iChunkId << endl;
|
||||
auto strErr = StrPrinter << "不支持发送该类型的块流 ID:" << iChunkId << endl;
|
||||
throw std::runtime_error(strErr);
|
||||
}
|
||||
|
||||
@@ -275,7 +287,7 @@ void RtmpProtocol::handle_C1_simple(){
|
||||
}
|
||||
#ifdef ENABLE_OPENSSL
|
||||
void RtmpProtocol::handle_C1_complex(){
|
||||
//参考自:http://blog.csdn.net/win_lin/article/details/13006803
|
||||
//参考自:http://blog.csdn.net/win_lin/article/details/13006803
|
||||
//skip c0,time,version
|
||||
const char *c1_start = m_strRcvBuf.data() + 1;
|
||||
const char *schema_start = c1_start + 8;
|
||||
@@ -318,6 +330,9 @@ void RtmpProtocol::handle_C1_complex(){
|
||||
}
|
||||
}
|
||||
|
||||
#if !defined(u_int8_t)
|
||||
#define u_int8_t unsigned char
|
||||
#endif // !defined(u_int8_t)
|
||||
|
||||
static u_int8_t FMSKey[] = {
|
||||
0x47, 0x65, 0x6e, 0x75, 0x69, 0x6e, 0x65, 0x20,
|
||||
@@ -344,7 +359,7 @@ static u_int8_t FPKey[] = {
|
||||
void RtmpProtocol::check_C1_Digest(const string &digest,const string &data){
|
||||
auto sha256 = openssl_HMACsha256(FPKey,C1_FPKEY_SIZE,data.data(),data.size());
|
||||
if(sha256 != digest){
|
||||
throw std::runtime_error("digest不匹配");
|
||||
throw std::runtime_error("digest mismatched");
|
||||
}else{
|
||||
InfoL << "check rtmp complex handshark success!";
|
||||
}
|
||||
@@ -383,7 +398,7 @@ string RtmpProtocol::get_C1_key(const uint8_t *ptr){
|
||||
return key;
|
||||
}
|
||||
void RtmpProtocol::send_complex_S0S1S2(int schemeType,const string &digest){
|
||||
//S1S2计算参考自:https://github.com/hitYangfei/golang/blob/master/rtmpserver.go
|
||||
//S1S2计算参考自:https://github.com/hitYangfei/golang/blob/master/rtmpserver.go
|
||||
//发送S0
|
||||
char handshake_head = HANDSHAKE_PLAINTEXT;
|
||||
onSendRawData(&handshake_head, 1);
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
#ifndef SRC_RTMP_RTMPPROTOCOL_H_
|
||||
#define SRC_RTMP_RTMPPROTOCOL_H_
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <functional>
|
||||
|
||||
@@ -26,7 +26,7 @@ RtmpPusher::RtmpPusher(const char *strApp,const char *strStream) {
|
||||
}, []() {});
|
||||
auto src = RtmpMediaSource::find(strApp,strStream);
|
||||
if (!src) {
|
||||
auto strErr = StrPrinter << "媒体源:" << strApp << "/" << strStream << "不存在" << endl;
|
||||
auto strErr = StrPrinter << "media source:" << strApp << "/" << strStream << "not found!" << endl;
|
||||
throw std::runtime_error(strErr);
|
||||
}
|
||||
m_pMediaSrc = src;
|
||||
@@ -132,7 +132,7 @@ inline void RtmpPusher::send_connect() {
|
||||
auto level = val["level"].as_string();
|
||||
auto code = val["code"].as_string();
|
||||
if(level != "status"){
|
||||
throw std::runtime_error(StrPrinter <<"connect 失败:" << level << " " << code << endl);
|
||||
throw std::runtime_error(StrPrinter <<"connect 失败:" << level << " " << code << endl);
|
||||
}
|
||||
send_createStream();
|
||||
});
|
||||
@@ -157,7 +157,7 @@ inline void RtmpPusher::send_publish() {
|
||||
auto level = val["level"].as_string();
|
||||
auto code = val["code"].as_string();
|
||||
if(level != "status") {
|
||||
throw std::runtime_error(StrPrinter <<"publish 失败:" << level << " " << code << endl);
|
||||
throw std::runtime_error(StrPrinter <<"publish 失败:" << level << " " << code << endl);
|
||||
}
|
||||
//start send media
|
||||
send_metaData();
|
||||
@@ -167,10 +167,10 @@ inline void RtmpPusher::send_publish() {
|
||||
inline void RtmpPusher::send_metaData(){
|
||||
auto src = m_pMediaSrc.lock();
|
||||
if (!src) {
|
||||
throw std::runtime_error("媒体源已被释放");
|
||||
throw std::runtime_error("the media source was released");
|
||||
}
|
||||
if (!src->ready()) {
|
||||
throw std::runtime_error("媒体源尚未准备就绪");
|
||||
throw std::runtime_error("the media source is not ready");
|
||||
}
|
||||
|
||||
AMFEncoder enc;
|
||||
@@ -219,7 +219,7 @@ void RtmpPusher::onCmd_onStatus(AMFDecoder &dec) {
|
||||
}
|
||||
}
|
||||
if(val.type() != AMF_OBJECT){
|
||||
throw std::runtime_error("onStatus: 未找到结果对象");
|
||||
throw std::runtime_error("onStatus:the result object was not found");
|
||||
}
|
||||
|
||||
lock_guard<recursive_mutex> lck(m_mtxOnStatusCB);
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
#ifndef SRC_RTMP_RTMPSESSION_H_
|
||||
#define SRC_RTMP_RTMPSESSION_H_
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include <unordered_map>
|
||||
#include "amf.h"
|
||||
#include "Rtmp.h"
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#include <string.h>
|
||||
#include <stdexcept>
|
||||
#include <arpa/inet.h>
|
||||
#include "amf.h"
|
||||
#include "utils.h"
|
||||
#include "Util/logger.h"
|
||||
|
||||
@@ -3,7 +3,9 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <arpa/inet.h>
|
||||
#include "Util/util.h"
|
||||
|
||||
using namespace ZL::Util;
|
||||
|
||||
/*
|
||||
* Used to do unaligned loads on archs that don't support them. GCC can mostly
|
||||
|
||||
Reference in New Issue
Block a user