File: videoplayer.cpp

package info (click to toggle)
openmw 0.49.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,992 kB
  • sloc: cpp: 372,479; xml: 2,149; sh: 1,403; python: 797; makefile: 26
file content (139 lines) | stat: -rw-r--r-- 2,450 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "videoplayer.hpp"

#include <iostream>

#include <osg/Notify>
#include <osg/Texture2D>

#include "audiofactory.hpp"
#include "videostate.hpp"

namespace Video
{

VideoPlayer::VideoPlayer()
    : mState(nullptr)
{

}

VideoPlayer::~VideoPlayer()
{
    if(mState)
        close();
}

void VideoPlayer::setAudioFactory(MovieAudioFactory *factory)
{
    mAudioFactory.reset(factory);
}

void VideoPlayer::playVideo(std::unique_ptr<std::istream>&& inputstream, const std::string& name)
{
    if(mState)
        close();

    try {
        mState = new VideoState;
        mState->setAudioFactory(mAudioFactory.get());
        mState->init(std::move(inputstream), name);

        // wait until we have the first picture
        while (mState->video_st && !mState->mTexture.get())
        {
            if (!mState->update())
                break;
        }
    }
    catch(std::exception& e) {
        OSG_FATAL << "Failed to play video: " << e.what() << std::endl;
        close();
    }
}

bool VideoPlayer::update ()
{
    if(mState)
        return mState->update();
    return false;
}

osg::ref_ptr<osg::Texture2D> VideoPlayer::getVideoTexture()
{
    if (mState)
        return mState->mTexture;
    return osg::ref_ptr<osg::Texture2D>();
}

int VideoPlayer::getVideoWidth()
{
    int width=0;
    if (mState && mState->mTexture.get() && mState->mTexture->getImage())
        width = mState->mTexture->getImage()->s();
    return width;
}

int VideoPlayer::getVideoHeight()
{
    int height=0;
    if (mState && mState->mTexture.get() && mState->mTexture->getImage())
        height = mState->mTexture->getImage()->t();
    return height;
}

void VideoPlayer::close()
{
    if(mState)
    {
        mState->deinit();

        delete mState;
        mState = nullptr;
    }
}

bool VideoPlayer::hasAudioStream()
{
    return mState && mState->audio_st != nullptr;
}

void VideoPlayer::play()
{
    if (mState)
        mState->setPaused(false);
}

void VideoPlayer::pause()
{
    if (mState)
        mState->setPaused(true);
}

bool VideoPlayer::isPaused()
{
    if (mState)
        return mState->mPaused;
    return true;
}

double VideoPlayer::getCurrentTime()
{
    if (mState)
        return mState->get_master_clock();
    return 0.0;
}

void VideoPlayer::seek(double time)
{
    if (mState)
        mState->seekTo(time);
}

double VideoPlayer::getDuration()
{
    if (mState)
        return mState->getDuration();
    return 0.0;
}

}