Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Z
ZLMediaKit
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
0
Issues
0
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
张翔宇
ZLMediaKit
Commits
1afacdcf
Commit
1afacdcf
authored
5 years ago
by
xiongziliang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
初步添加HlsMediaSource
parent
e72fa359
显示空白字符变更
内嵌
并排
正在显示
11 个修改的文件
包含
92 行增加
和
124 行删除
+92
-124
src/Common/MediaSource.cpp
+5
-24
src/Common/MediaSource.h
+1
-1
src/Common/MultiMediaSourceMuxer.h
+3
-2
src/Common/config.h
+1
-0
src/Http/HttpFileManager.cpp
+1
-1
src/Record/HlsMakerImp.cpp
+8
-0
src/Record/HlsMakerImp.h
+10
-0
src/Record/HlsMediaSource.cpp
+9
-58
src/Record/HlsMediaSource.h
+48
-37
src/Record/HlsRecorder.h
+3
-0
src/Record/Recorder.cpp
+3
-1
没有找到文件。
src/Common/MediaSource.cpp
查看文件 @
1afacdcf
...
@@ -39,9 +39,7 @@ recursive_mutex MediaSource::g_mtxMediaSrc;
...
@@ -39,9 +39,7 @@ recursive_mutex MediaSource::g_mtxMediaSrc;
MediaSource
::
SchemaVhostAppStreamMap
MediaSource
::
g_mapMediaSrc
;
MediaSource
::
SchemaVhostAppStreamMap
MediaSource
::
g_mapMediaSrc
;
MediaSource
::
MediaSource
(
const
string
&
strSchema
,
const
string
&
strVhost
,
const
string
&
strApp
,
const
string
&
strId
)
:
MediaSource
::
MediaSource
(
const
string
&
strSchema
,
const
string
&
strVhost
,
const
string
&
strApp
,
const
string
&
strId
)
:
_strSchema
(
strSchema
),
_strSchema
(
strSchema
),
_strApp
(
strApp
),
_strId
(
strId
)
{
_strApp
(
strApp
),
_strId
(
strId
)
{
if
(
strVhost
.
empty
())
{
if
(
strVhost
.
empty
())
{
_strVhost
=
DEFAULT_VHOST
;
_strVhost
=
DEFAULT_VHOST
;
}
else
{
}
else
{
...
@@ -146,12 +144,7 @@ void MediaSource::for_each_media(const function<void(const MediaSource::Ptr &src
...
@@ -146,12 +144,7 @@ void MediaSource::for_each_media(const function<void(const MediaSource::Ptr &src
}
}
template
<
typename
MAP
,
typename
FUNC
>
template
<
typename
MAP
,
typename
FUNC
>
static
bool
searchMedia
(
MAP
&
map
,
static
bool
searchMedia
(
MAP
&
map
,
const
string
&
schema
,
const
string
&
vhost
,
const
string
&
app
,
const
string
&
id
,
FUNC
&&
func
)
{
const
string
&
schema
,
const
string
&
vhost
,
const
string
&
app
,
const
string
&
id
,
FUNC
&&
func
)
{
auto
it0
=
map
.
find
(
schema
);
auto
it0
=
map
.
find
(
schema
);
if
(
it0
==
map
.
end
())
{
if
(
it0
==
map
.
end
())
{
//未找到协议
//未找到协议
...
@@ -188,16 +181,9 @@ static void eraseIfEmpty(MAP &map, IT0 it0, IT1 it1, IT2 it2) {
...
@@ -188,16 +181,9 @@ static void eraseIfEmpty(MAP &map, IT0 it0, IT1 it1, IT2 it2) {
}
}
};
};
void
findAsync_l
(
const
MediaInfo
&
info
,
void
findAsync_l
(
const
MediaInfo
&
info
,
const
std
::
shared_ptr
<
TcpSession
>
&
session
,
bool
retry
,
const
std
::
shared_ptr
<
TcpSession
>
&
session
,
bool
retry
,
const
function
<
void
(
const
MediaSource
::
Ptr
&
src
)
>
&
cb
){
const
function
<
void
(
const
MediaSource
::
Ptr
&
src
)
>
&
cb
){
auto
src
=
MediaSource
::
find
(
info
.
_schema
,
info
.
_vhost
,
info
.
_app
,
info
.
_streamid
,
true
);
auto
src
=
MediaSource
::
find
(
info
.
_schema
,
info
.
_vhost
,
info
.
_app
,
info
.
_streamid
,
true
);
if
(
src
||
!
retry
){
if
(
src
||
!
retry
){
cb
(
src
);
cb
(
src
);
return
;
return
;
...
@@ -263,12 +249,7 @@ void MediaSource::findAsync(const MediaInfo &info, const std::shared_ptr<TcpSess
...
@@ -263,12 +249,7 @@ void MediaSource::findAsync(const MediaInfo &info, const std::shared_ptr<TcpSess
return
findAsync_l
(
info
,
session
,
true
,
cb
);
return
findAsync_l
(
info
,
session
,
true
,
cb
);
}
}
MediaSource
::
Ptr
MediaSource
::
find
(
MediaSource
::
Ptr
MediaSource
::
find
(
const
string
&
schema
,
const
string
&
vhost_tmp
,
const
string
&
app
,
const
string
&
id
,
bool
bMake
)
{
const
string
&
schema
,
const
string
&
vhost_tmp
,
const
string
&
app
,
const
string
&
id
,
bool
bMake
)
{
string
vhost
=
vhost_tmp
;
string
vhost
=
vhost_tmp
;
if
(
vhost
.
empty
()){
if
(
vhost
.
empty
()){
vhost
=
DEFAULT_VHOST
;
vhost
=
DEFAULT_VHOST
;
...
...
This diff is collapsed.
Click to expand it.
src/Common/MediaSource.h
查看文件 @
1afacdcf
...
@@ -132,7 +132,7 @@ public:
...
@@ -132,7 +132,7 @@ public:
// 观看者个数,包括(hls/rtsp/rtmp)
// 观看者个数,包括(hls/rtsp/rtmp)
virtual
int
totalReaderCount
();
virtual
int
totalReaderCount
();
// 获取流当前时间戳
// 获取流当前时间戳
virtual
uint32_t
getTimeStamp
(
TrackType
trackType
)
=
0
;
virtual
uint32_t
getTimeStamp
(
TrackType
trackType
)
{
return
0
;
}
;
// 拖动进度条
// 拖动进度条
bool
seekTo
(
uint32_t
ui32Stamp
);
bool
seekTo
(
uint32_t
ui32Stamp
);
...
...
This diff is collapsed.
Click to expand it.
src/Common/MultiMediaSourceMuxer.h
查看文件 @
1afacdcf
...
@@ -30,7 +30,7 @@
...
@@ -30,7 +30,7 @@
#include "Rtsp/RtspMediaSourceMuxer.h"
#include "Rtsp/RtspMediaSourceMuxer.h"
#include "Rtmp/RtmpMediaSourceMuxer.h"
#include "Rtmp/RtmpMediaSourceMuxer.h"
#include "Record/Recorder.h"
#include "Record/Recorder.h"
#include "Record/HlsM
anager
.h"
#include "Record/HlsM
ediaSource
.h"
class
MultiMediaSourceMuxer
:
public
MediaSink
,
public
std
::
enable_shared_from_this
<
MultiMediaSourceMuxer
>
{
class
MultiMediaSourceMuxer
:
public
MediaSink
,
public
std
::
enable_shared_from_this
<
MultiMediaSourceMuxer
>
{
public
:
public
:
...
@@ -66,7 +66,8 @@ public:
...
@@ -66,7 +66,8 @@ public:
}
}
_get_hls_player
=
[
vhost
,
strApp
,
strId
](){
_get_hls_player
=
[
vhost
,
strApp
,
strId
](){
return
HlsManager
::
Instance
().
hlsPlayerCount
(
vhost
,
strApp
,
strId
);
auto
src
=
MediaSource
::
find
(
HLS_SCHEMA
,
vhost
,
strApp
,
strId
);
return
src
?
src
->
readerCount
()
:
0
;
};
};
}
}
virtual
~
MultiMediaSourceMuxer
(){}
virtual
~
MultiMediaSourceMuxer
(){}
...
...
This diff is collapsed.
Click to expand it.
src/Common/config.h
查看文件 @
1afacdcf
...
@@ -62,6 +62,7 @@ bool loadIniConfig(const char *ini_path = nullptr);
...
@@ -62,6 +62,7 @@ bool loadIniConfig(const char *ini_path = nullptr);
#define HTTP_SCHEMA "http"
#define HTTP_SCHEMA "http"
#define RTSP_SCHEMA "rtsp"
#define RTSP_SCHEMA "rtsp"
#define RTMP_SCHEMA "rtmp"
#define RTMP_SCHEMA "rtmp"
#define HLS_SCHEMA "hls"
#define DEFAULT_VHOST "__defaultVhost__"
#define DEFAULT_VHOST "__defaultVhost__"
////////////广播名称///////////
////////////广播名称///////////
...
...
This diff is collapsed.
Click to expand it.
src/Http/HttpFileManager.cpp
查看文件 @
1afacdcf
...
@@ -32,7 +32,7 @@
...
@@ -32,7 +32,7 @@
#include "HttpFileManager.h"
#include "HttpFileManager.h"
#include "Util/File.h"
#include "Util/File.h"
#include "HttpSession.h"
#include "HttpSession.h"
#include "Record/HlsM
anager
.h"
#include "Record/HlsM
ediaSource
.h"
namespace
mediakit
{
namespace
mediakit
{
...
...
This diff is collapsed.
Click to expand it.
src/Record/HlsMakerImp.cpp
查看文件 @
1afacdcf
...
@@ -95,6 +95,9 @@ void HlsMakerImp::onWriteHls(const char *data, int len) {
...
@@ -95,6 +95,9 @@ void HlsMakerImp::onWriteHls(const char *data, int len) {
if
(
hls
){
if
(
hls
){
fwrite
(
data
,
len
,
1
,
hls
.
get
());
fwrite
(
data
,
len
,
1
,
hls
.
get
());
hls
.
reset
();
hls
.
reset
();
if
(
_media_src
){
_media_src
->
registHls
();
}
}
else
{
}
else
{
WarnL
<<
"create hls file falied,"
<<
_path_hls
<<
" "
<<
get_uv_errmsg
();
WarnL
<<
"create hls file falied,"
<<
_path_hls
<<
" "
<<
get_uv_errmsg
();
}
}
...
@@ -115,4 +118,8 @@ std::shared_ptr<FILE> HlsMakerImp::makeFile(const string &file,bool setbuf) {
...
@@ -115,4 +118,8 @@ std::shared_ptr<FILE> HlsMakerImp::makeFile(const string &file,bool setbuf) {
return
ret
;
return
ret
;
}
}
void
HlsMakerImp
::
setMediaInfo
(
const
string
&
vhost
,
const
string
&
app
,
const
string
&
stream_id
)
{
_media_src
=
std
::
make_shared
<
HlsMediaSource
>
(
vhost
,
app
,
stream_id
);
}
}
//
namespace
mediakit
}
//
namespace
mediakit
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/Record/HlsMakerImp.h
查看文件 @
1afacdcf
...
@@ -31,6 +31,7 @@
...
@@ -31,6 +31,7 @@
#include <string>
#include <string>
#include <stdlib.h>
#include <stdlib.h>
#include "HlsMaker.h"
#include "HlsMaker.h"
#include "HlsMediaSource.h"
using
namespace
std
;
using
namespace
std
;
namespace
mediakit
{
namespace
mediakit
{
...
@@ -43,6 +44,14 @@ public:
...
@@ -43,6 +44,14 @@ public:
float
seg_duration
=
5
,
float
seg_duration
=
5
,
uint32_t
seg_number
=
3
);
uint32_t
seg_number
=
3
);
virtual
~
HlsMakerImp
();
virtual
~
HlsMakerImp
();
/**
* 设置媒体信息
* @param vhost 虚拟主机
* @param app 应用名
* @param stream_id 流id
*/
void
setMediaInfo
(
const
string
&
vhost
,
const
string
&
app
,
const
string
&
stream_id
);
protected
:
protected
:
string
onOpenSegment
(
int
index
)
override
;
string
onOpenSegment
(
int
index
)
override
;
void
onDelSegment
(
int
index
)
override
;
void
onDelSegment
(
int
index
)
override
;
...
@@ -51,6 +60,7 @@ protected:
...
@@ -51,6 +60,7 @@ protected:
private
:
private
:
std
::
shared_ptr
<
FILE
>
makeFile
(
const
string
&
file
,
bool
setbuf
=
false
);
std
::
shared_ptr
<
FILE
>
makeFile
(
const
string
&
file
,
bool
setbuf
=
false
);
private
:
private
:
HlsMediaSource
::
Ptr
_media_src
;
map
<
int
/*index*/
,
string
/*file_path*/
>
_segment_file_paths
;
map
<
int
/*index*/
,
string
/*file_path*/
>
_segment_file_paths
;
std
::
shared_ptr
<
FILE
>
_file
;
std
::
shared_ptr
<
FILE
>
_file
;
std
::
shared_ptr
<
char
>
_file_buf
;
std
::
shared_ptr
<
char
>
_file_buf
;
...
...
This diff is collapsed.
Click to expand it.
src/Record/HlsM
anager
.cpp
→
src/Record/HlsM
ediaSource
.cpp
查看文件 @
1afacdcf
...
@@ -24,78 +24,29 @@
...
@@ -24,78 +24,29 @@
* SOFTWARE.
* SOFTWARE.
*/
*/
#include "HlsManager.h"
#include "HlsMediaSource.h"
#include "Util/util.h"
using
namespace
toolkit
;
namespace
mediakit
{
namespace
mediakit
{
HlsCookieData
::
HlsCookieData
(
const
MediaInfo
&
info
)
{
HlsCookieData
::
HlsCookieData
(
const
MediaInfo
&
info
)
{
_info
=
info
;
_info
=
info
;
_manager
=
HlsManager
::
Instance
().
shared_from_this
();
auto
src
=
dynamic_pointer_cast
<
HlsMediaSource
>
(
MediaSource
::
find
(
HLS_SCHEMA
,
_info
.
_vhost
,
_info
.
_app
,
_info
.
_streamid
));
_manager
->
onAddHlsPlayer
(
_info
);
if
(
src
){
src
->
modifyCount
(
true
);
}
}
}
HlsCookieData
::~
HlsCookieData
()
{
HlsCookieData
::~
HlsCookieData
()
{
_manager
->
onAddHlsPlayer
(
_info
);
auto
src
=
dynamic_pointer_cast
<
HlsMediaSource
>
(
MediaSource
::
find
(
HLS_SCHEMA
,
_info
.
_vhost
,
_info
.
_app
,
_info
.
_streamid
));
if
(
src
){
src
->
modifyCount
(
false
);
}
}
}
void
HlsCookieData
::
addByteUsage
(
uint64_t
bytes
)
{
void
HlsCookieData
::
addByteUsage
(
uint64_t
bytes
)
{
_bytes
+=
bytes
;
_bytes
+=
bytes
;
}
}
////////////////////////////////////////////////////////
HlsManager
::
HlsManager
()
{}
HlsManager
::~
HlsManager
()
{}
INSTANCE_IMP
(
HlsManager
);
void
HlsManager
::
onAddHlsPlayer
(
const
MediaInfo
&
info
)
{
lock_guard
<
decltype
(
_mtx
)
>
lck
(
_mtx
);
++
_player_counter
[
info
.
_vhost
][
info
.
_app
][
info
.
_streamid
].
_count
;
}
void
HlsManager
::
onDelHlsPlayer
(
const
MediaInfo
&
info
)
{
lock_guard
<
decltype
(
_mtx
)
>
lck
(
_mtx
);
auto
it0
=
_player_counter
.
find
(
info
.
_vhost
);
if
(
it0
==
_player_counter
.
end
()){
return
;
}
auto
it1
=
it0
->
second
.
find
(
info
.
_app
);
if
(
it1
==
it0
->
second
.
end
()){
return
;
}
auto
it2
=
it1
->
second
.
find
(
info
.
_streamid
);
if
(
it2
==
it1
->
second
.
end
()){
return
;
}
if
(
--
(
it2
->
second
.
_count
)
==
0
){
it1
->
second
.
erase
(
it2
);
if
(
it1
->
second
.
empty
()){
it0
->
second
.
erase
(
it1
);
if
(
it0
->
second
.
empty
()){
_player_counter
.
erase
(
it0
);
}
}
}
}
int
HlsManager
::
hlsPlayerCount
(
const
string
&
vhost
,
const
string
&
app
,
const
string
&
stream
)
{
lock_guard
<
decltype
(
_mtx
)
>
lck
(
_mtx
);
auto
it0
=
_player_counter
.
find
(
vhost
);
if
(
it0
==
_player_counter
.
end
()){
return
0
;
}
auto
it1
=
it0
->
second
.
find
(
app
);
if
(
it1
==
it0
->
second
.
end
()){
return
0
;
}
auto
it2
=
it1
->
second
.
find
(
stream
);
if
(
it2
==
it1
->
second
.
end
()){
return
0
;
}
return
it2
->
second
.
_count
;
}
}
//namespace mediakit
}
//namespace mediakit
This diff is collapsed.
Click to expand it.
src/Record/HlsM
anager
.h
→
src/Record/HlsM
ediaSource
.h
查看文件 @
1afacdcf
/*
/*
* MIT License
* MIT License
*
*
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
...
@@ -23,20 +23,13 @@
...
@@ -23,20 +23,13 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* SOFTWARE.
*/
*/
#ifndef ZLMEDIAKIT_HLSMEDIASOURCE_H
#define ZLMEDIAKIT_HLSMEDIASOURCE_H
#include <atomic>
#ifndef ZLMEDIAKIT_HLSMANAGER_H
#define ZLMEDIAKIT_HLSMANAGER_H
#include <memory>
#include <string>
#include <mutex>
#include "Common/MediaSource.h"
#include "Common/MediaSource.h"
using
namespace
std
;
namespace
mediakit
{
namespace
mediakit
{
class
HlsManager
;
class
HlsCookieData
{
class
HlsCookieData
{
public
:
public
:
HlsCookieData
(
const
MediaInfo
&
info
);
HlsCookieData
(
const
MediaInfo
&
info
);
...
@@ -45,41 +38,59 @@ public:
...
@@ -45,41 +38,59 @@ public:
private
:
private
:
uint64_t
_bytes
=
0
;
uint64_t
_bytes
=
0
;
MediaInfo
_info
;
MediaInfo
_info
;
std
::
shared_ptr
<
HlsManager
>
_manager
;
};
};
class
HlsMediaSource
:
public
MediaSource
{
class
HlsManager
:
public
std
::
enable_shared_from_this
<
HlsManager
>
{
public
:
public
:
friend
class
HlsCookieData
;
friend
class
HlsCookieData
;
~
HlsManager
();
typedef
std
::
shared_ptr
<
HlsMediaSource
>
Ptr
;
static
HlsManager
&
Instance
();
HlsMediaSource
(
const
string
&
vhost
,
const
string
&
app
,
const
string
&
stream_id
)
:
MediaSource
(
HLS_SCHEMA
,
vhost
,
app
,
stream_id
){
_readerCount
=
0
;
}
virtual
~
HlsMediaSource
()
=
default
;
/**
/**
* 获取hls播放器个数
* 获取播放器个数
* @param vhost 虚拟主机
* @return
* @param app 应用名
* @param stream 流id
* @return 播放器个数
*/
*/
int
hlsPlayerCount
(
const
string
&
vhost
,
const
string
&
app
,
const
string
&
stream
);
int
readerCount
()
override
{
private
:
return
_readerCount
.
load
();
void
onAddHlsPlayer
(
const
MediaInfo
&
info
);
}
void
onDelHlsPlayer
(
const
MediaInfo
&
info
);
HlsManager
();
private
:
class
HlsPlayerCounter
{
private
:
friend
class
HlsManager
;
int
_count
=
0
;
};
private
:
recursive_mutex
_mtx
;
unordered_map
<
string
/*vhost*/
,
unordered_map
<
string
/*app*/
,
unordered_map
<
string
/*stream*/
,
HlsPlayerCounter
>
>
>
_player_counter
;
/**
* 注册hls
*/
void
registHls
(){
if
(
!
_registed
){
regist
();
_registed
=
true
;
}
}
private
:
/**
* 修改观看者个数
* @param add 添加海思删除
*/
void
modifyCount
(
bool
add
)
{
if
(
add
)
{
++
_readerCount
;
return
;
}
if
(
--
_readerCount
==
0
&&
totalReaderCount
()
==
0
)
{
onNoneReader
();
}
}
private
:
atomic_int
_readerCount
;
bool
_registed
=
false
;
};
};
}
//namespace mediakit
}
//namespace mediakit
#endif //ZLMEDIAKIT_HLSM
ANAGER
_H
#endif //ZLMEDIAKIT_HLSM
EDIASOURCE
_H
This diff is collapsed.
Click to expand it.
src/Record/HlsRecorder.h
查看文件 @
1afacdcf
...
@@ -43,6 +43,9 @@ public:
...
@@ -43,6 +43,9 @@ public:
~
HlsRecorder
(){
~
HlsRecorder
(){
delete
_hls
;
delete
_hls
;
}
}
void
setMediaInfo
(
const
string
&
vhost
,
const
string
&
app
,
const
string
&
stream_id
){
_hls
->
setMediaInfo
(
vhost
,
app
,
stream_id
);
}
protected
:
protected
:
void
onTs
(
const
void
*
packet
,
int
bytes
,
uint32_t
timestamp
,
int
flags
)
override
{
void
onTs
(
const
void
*
packet
,
int
bytes
,
uint32_t
timestamp
,
int
flags
)
override
{
_hls
->
inputData
((
char
*
)
packet
,
bytes
,
timestamp
);
_hls
->
inputData
((
char
*
)
packet
,
bytes
,
timestamp
);
...
...
This diff is collapsed.
Click to expand it.
src/Record/Recorder.cpp
查看文件 @
1afacdcf
...
@@ -54,7 +54,9 @@ MediaSinkInterface *createHlsRecorder(const string &strVhost_tmp, const string &
...
@@ -54,7 +54,9 @@ MediaSinkInterface *createHlsRecorder(const string &strVhost_tmp, const string &
m3u8FilePath
=
strApp
+
"/"
+
strId
+
"/hls.m3u8"
;
m3u8FilePath
=
strApp
+
"/"
+
strId
+
"/hls.m3u8"
;
}
}
m3u8FilePath
=
File
::
absolutePath
(
m3u8FilePath
,
hlsPath
);
m3u8FilePath
=
File
::
absolutePath
(
m3u8FilePath
,
hlsPath
);
return
new
HlsRecorder
(
m3u8FilePath
,
params
);
auto
ret
=
new
HlsRecorder
(
m3u8FilePath
,
params
);
ret
->
setMediaInfo
(
strVhost
,
strApp
,
strId
);
return
ret
;
#else
#else
return
nullptr
;
return
nullptr
;
#endif //defined(ENABLE_HLS)
#endif //defined(ENABLE_HLS)
...
...
This diff is collapsed.
Click to expand it.
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论