mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-11-24 00:47:21 +08:00
committed by
GitHub
parent
52c38f8aad
commit
d8980ac386
@@ -212,17 +212,27 @@ API_EXPORT int API_CALL mk_media_source_get_total_reader_count(const mk_media_so
|
|||||||
API_EXPORT int API_CALL mk_media_source_get_track_count(const mk_media_source ctx) {
|
API_EXPORT int API_CALL mk_media_source_get_track_count(const mk_media_source ctx) {
|
||||||
assert(ctx);
|
assert(ctx);
|
||||||
MediaSource *src = (MediaSource *)ctx;
|
MediaSource *src = (MediaSource *)ctx;
|
||||||
return src->getTracks(false).size();
|
try {
|
||||||
|
return src->getTracks(false).size();
|
||||||
|
} catch (std::exception &ex) {
|
||||||
|
WarnL << "getTracks failed: " << ex.what();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
API_EXPORT mk_track API_CALL mk_media_source_get_track(const mk_media_source ctx, int index) {
|
API_EXPORT mk_track API_CALL mk_media_source_get_track(const mk_media_source ctx, int index) {
|
||||||
assert(ctx);
|
assert(ctx);
|
||||||
MediaSource *src = (MediaSource *)ctx;
|
MediaSource *src = (MediaSource *)ctx;
|
||||||
auto tracks = src->getTracks(false);
|
try {
|
||||||
if (index < 0 || static_cast<size_t>(index) >= tracks.size()) {
|
auto tracks = src->getTracks(false);
|
||||||
|
if (index < 0 || static_cast<size_t>(index) >= tracks.size()) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
return (mk_track) new Track::Ptr(std::move(tracks[index]));
|
||||||
|
} catch (std::exception &ex) {
|
||||||
|
WarnL << "getTracks failed: " << ex.what();
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
return (mk_track) new Track::Ptr(std::move(tracks[index]));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
API_EXPORT float API_CALL mk_media_source_get_track_loss(const mk_media_source ctx, const mk_track track) {
|
API_EXPORT float API_CALL mk_media_source_get_track_loss(const mk_media_source ctx, const mk_track track) {
|
||||||
|
|||||||
@@ -135,7 +135,13 @@ vector<Track::Ptr> MediaSource::getTracks(bool ready) const {
|
|||||||
if (!listener) {
|
if (!listener) {
|
||||||
return vector<Track::Ptr>();
|
return vector<Track::Ptr>();
|
||||||
}
|
}
|
||||||
return listener->getMuxer(const_cast<MediaSource &>(*this))->getTracks(ready);
|
try {
|
||||||
|
auto muxer = listener->getMuxer(const_cast<MediaSource &>(*this));
|
||||||
|
return muxer->getTracks(ready);
|
||||||
|
} catch (std::exception &ex) {
|
||||||
|
WarnL << "getTracks failed: " << ex.what();
|
||||||
|
return vector<Track::Ptr>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MediaSource::setListener(const std::weak_ptr<MediaSourceEvent> &listener){
|
void MediaSource::setListener(const std::weak_ptr<MediaSourceEvent> &listener){
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ set(GATING_TESTS
|
|||||||
test_av1_rtmp
|
test_av1_rtmp
|
||||||
test_amf
|
test_amf
|
||||||
test_hls_cookie_data
|
test_hls_cookie_data
|
||||||
|
test_media_source_tracks
|
||||||
test_sortor
|
test_sortor
|
||||||
test_rtx_twcc
|
test_rtx_twcc
|
||||||
test_webrtc_regression)
|
test_webrtc_regression)
|
||||||
|
|||||||
96
tests/test_media_source_tracks.cpp
Normal file
96
tests/test_media_source_tracks.cpp
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
|
||||||
|
*
|
||||||
|
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
|
||||||
|
*
|
||||||
|
* Use of this source code is governed by MIT-like license that can be found in
|
||||||
|
* the LICENSE file in the root of the source tree. All contributing project authors
|
||||||
|
* may be found in the AUTHORS file in the root of the source tree.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <memory>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "Common/MediaSource.h"
|
||||||
|
#include "Common/MultiMediaSourceMuxer.h"
|
||||||
|
|
||||||
|
using namespace mediakit;
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
struct ThrowingListener : MediaSourceEvent {
|
||||||
|
std::shared_ptr<MultiMediaSourceMuxer> getMuxer(MediaSource &) const override {
|
||||||
|
throw std::runtime_error("getMuxer failed");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct TestSource : MediaSource {
|
||||||
|
explicit TestSource(const std::string &stream)
|
||||||
|
: MediaSource("rtsp", MediaTuple { "__defaultVhost__", "app", stream, "" }) {}
|
||||||
|
int readerCount() override { return 0; }
|
||||||
|
};
|
||||||
|
|
||||||
|
bool testNoListener() {
|
||||||
|
auto source = std::make_shared<TestSource>("no-listener");
|
||||||
|
auto tracks = source->getTracks(false);
|
||||||
|
if (!tracks.empty()) {
|
||||||
|
std::cerr << "[FAIL] getTracks without listener should return empty tracks" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
std::cout << "[ OK ] getTracks without listener returns empty tracks" << std::endl;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool testDeadInterceptor() {
|
||||||
|
auto source = std::make_shared<TestSource>("dead-interceptor");
|
||||||
|
auto interceptor = std::make_shared<MediaSourceEventInterceptor>();
|
||||||
|
{
|
||||||
|
auto delegate = std::make_shared<MediaSourceEvent>();
|
||||||
|
interceptor->setDelegate(delegate);
|
||||||
|
}
|
||||||
|
source->setListener(interceptor);
|
||||||
|
|
||||||
|
auto tracks = source->getTracks(false);
|
||||||
|
if (!tracks.empty()) {
|
||||||
|
std::cerr << "[FAIL] getTracks should return empty when the interceptor delegate has expired" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
std::cout << "[ OK ] getTracks returns empty when the interceptor delegate has expired" << std::endl;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool testThrowingListener() {
|
||||||
|
auto source = std::make_shared<TestSource>("throwing-listener");
|
||||||
|
auto listener = std::make_shared<ThrowingListener>();
|
||||||
|
source->setListener(listener);
|
||||||
|
|
||||||
|
auto tracks = source->getTracks(false);
|
||||||
|
if (!tracks.empty()) {
|
||||||
|
std::cerr << "[FAIL] getTracks should return empty when getMuxer throws" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
std::cout << "[ OK ] getTracks returns empty when getMuxer throws" << std::endl;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
try {
|
||||||
|
if (!testNoListener()) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (!testDeadInterceptor()) {
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
if (!testThrowingListener()) {
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
} catch (const std::exception &ex) {
|
||||||
|
std::cerr << "[FAIL] unexpected exception: " << ex.what() << std::endl;
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user