MediaPlayer.cpp 1.78 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
xiongziliang committed
3 4 5
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
xiongziliang committed
6 7 8
 * Use of this source code is governed by MIT 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.
xzl committed
9 10
 */

xiongzilaing committed
11 12
#include <algorithm>
#include "MediaPlayer.h"
xzl committed
13 14
#include "Rtmp/RtmpPlayerImp.h"
#include "Rtsp/RtspPlayerImp.h"
xiongziliang committed
15
using namespace toolkit;
xzl committed
16

xiongziliang committed
17
namespace mediakit {
xzl committed
18

19
MediaPlayer::MediaPlayer(const EventPoller::Ptr &poller) {
20
    _poller = poller ? poller : EventPollerPool::Instance().getPoller();
xzl committed
21 22 23 24
}

MediaPlayer::~MediaPlayer() {
}
25 26 27 28 29 30 31 32 33 34 35 36

static void setOnCreateSocket_l(const std::shared_ptr<PlayerBase> &delegate, const Socket::onCreateSocket &cb){
    auto helper = dynamic_pointer_cast<SocketHelper>(delegate);
    if (helper) {
        helper->setOnCreateSocket(cb);
    }
}

void MediaPlayer::play(const string &url) {
    _delegate = PlayerBase::createPlayer(_poller, url);
    assert(_delegate);
    setOnCreateSocket_l(_delegate, _on_create_socket);
37 38
    _delegate->setOnShutdown(_shutdownCB);
    _delegate->setOnPlayResult(_playResultCB);
xiongziliang committed
39 40
    _delegate->setOnResume(_resumeCB);
    _delegate->setMediaSouce(_pMediaSrc);
41
    _delegate->mINI::operator=(*this);
42
    _delegate->play(url);
xzl committed
43 44
}

45
EventPoller::Ptr MediaPlayer::getPoller(){
46
    return _poller;
xiongziliang committed
47
}
xiongziliang committed
48

49 50 51 52 53 54
void MediaPlayer::setOnCreateSocket(Socket::onCreateSocket cb){
    setOnCreateSocket_l(_delegate, cb);
    _on_create_socket = std::move(cb);
}

void MediaPlayer::pause(bool pause) {
55
    if (_delegate) {
56
        _delegate->pause(pause);
57
    }
xzl committed
58 59 60
}

void MediaPlayer::teardown() {
61 62 63
    if (_delegate) {
        _delegate->teardown();
    }
xzl committed
64 65 66
}


xiongziliang committed
67
} /* namespace mediakit */