MediaPlayer.cpp 1.75 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
xiongziliang committed
3
 *
4
 * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
xiongziliang committed
5
 *
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"
夏楚 committed
13 14

using namespace std;
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 25
static void setOnCreateSocket_l(const std::shared_ptr<PlayerBase> &delegate, const Socket::onCreateSocket &cb){
    auto helper = dynamic_pointer_cast<SocketHelper>(delegate);
    if (helper) {
26 27 28 29 30 31 32 33
        if (cb) {
            helper->setOnCreateSocket(cb);
        } else {
            //客户端,确保开启互斥锁
            helper->setOnCreateSocket([](const EventPoller::Ptr &poller) {
                return Socket::createSocket(poller, true);
            });
        }
34 35 36 37 38 39 40
    }
}

void MediaPlayer::play(const string &url) {
    _delegate = PlayerBase::createPlayer(_poller, url);
    assert(_delegate);
    setOnCreateSocket_l(_delegate, _on_create_socket);
41 42 43 44
    _delegate->setOnShutdown(_on_shutdown);
    _delegate->setOnPlayResult(_on_play_result);
    _delegate->setOnResume(_on_resume);
    _delegate->setMediaSource(_media_src);
45
    _delegate->mINI::operator=(*this);
46
    _delegate->play(url);
xzl committed
47 48
}

49
EventPoller::Ptr MediaPlayer::getPoller(){
50
    return _poller;
xiongziliang committed
51
}
xiongziliang committed
52

53 54 55 56 57
void MediaPlayer::setOnCreateSocket(Socket::onCreateSocket cb){
    setOnCreateSocket_l(_delegate, cb);
    _on_create_socket = std::move(cb);
}

xiongziliang committed
58
} /* namespace mediakit */