Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: sfTheora 1.4 - Play videos in SFML  (Read 62697 times)

0 Members and 1 Guest are viewing this topic.

zorexx

  • Full Member
  • ***
  • Posts: 109
    • View Profile
    • zorexx@site
sfTheora 1.4 - Play videos in SFML
« on: March 25, 2011, 05:07:27 am »
What is sfTheora?
sfTheora (previously known as SFMLTheora) is a static library for loading and playing Theora videos on SFML with the help of libtheoraplayer (http://libtheoraplayer.sourceforge.net/).

GitHub repository:
https://github.com/eXpl0it3r/sfTheora

Hand over to new maintainer:
After a long period of inactivity, I have decided to hand over this project to eXpl0it3r.
Please checkout the GitHub repository for the latest updates on this project.
« Last Edit: September 30, 2018, 12:46:32 pm by zorexx »

zorexx

  • Full Member
  • ***
  • Posts: 109
    • View Profile
    • zorexx@site
SFMLTheora 1.3 - Play videos in SFML
« Reply #1 on: March 27, 2011, 03:24:43 am »
Added a short tutorial and a screenshot.

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
SFMLTheora 1.3 - Play videos in SFML
« Reply #2 on: March 27, 2011, 11:57:54 am »
Why do you use a pointer for your video object?
And couldn't Init() work be automatically done in the constructor?
Want to play movies in your SFML application? Check out sfeMovie!

zorexx

  • Full Member
  • ***
  • Posts: 109
    • View Profile
    • zorexx@site
SFMLTheora 1.3 - Play videos in SFML
« Reply #3 on: March 27, 2011, 12:56:19 pm »
You don't have to use a pointer, you can do
Code: [Select]
SFMLTheora::Video testVid;
as well. I just used pointer because I was testing whether it works that way or not, it really doesn't matter.

As for Init(), it's so that you can check if things are constructed properly, if they're not, Init() will return a false, else it'll return a true.

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
SFMLTheora 1.3 - Play videos in SFML
« Reply #4 on: March 27, 2011, 01:00:07 pm »
Maybe you could (and should) do that init stuff in LoadClip(), so that users can still whether something failed and don't have to call Init() first.
Want to play movies in your SFML application? Check out sfeMovie!

zorexx

  • Full Member
  • ***
  • Posts: 109
    • View Profile
    • zorexx@site
SFMLTheora 1.3 - Play videos in SFML
« Reply #5 on: March 27, 2011, 01:27:41 pm »
Good idea, I'll leave Init() there for flexibility purpose, but in LoadClip, I changed:
Code: [Select]
if (vidManager_ == NULL || ifaceFactory_ == NULL)
  return false;


to

Code: [Select]
if (vidManager_ == NULL || ifaceFactory_ == NULL) {
  Release();
  if (!Init())
    return false;
}


Thanks for your feedback!  :D

Re-uploaded updated SFMLTheora as SFMLTheora 1.1.1.

TechRogue

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
SFMLTheora 1.3 - Play videos in SFML
« Reply #6 on: March 30, 2011, 11:58:46 pm »
Does this also support audio? I only see a call to Draw().

zorexx

  • Full Member
  • ***
  • Posts: 109
    • View Profile
    • zorexx@site
SFMLTheora 1.3 - Play videos in SFML
« Reply #7 on: March 31, 2011, 04:13:42 am »
Yes, it supports audio (using sf::SoundStream).

Added Play(), Pause(), Stop(), and Seek() to the Video class.
Changed version to 1.2.0.

roosterkin123

  • Newbie
  • *
  • Posts: 43
    • MSN Messenger - rogerboy@hotmail.co.uk
    • View Profile
SFMLTheora 1.3 - Play videos in SFML
« Reply #8 on: April 13, 2011, 12:03:22 am »
Hi, No matter what I do the instance of
auto viddy = std::make_shared<SFMLTheora::Video>();
allways causes a heap corruption when shared_ptr is being deleted.

I am using sfml2 and 1.2 version of your library.
Any suggestions?

I had to add OnSeek again just like the bugfix from 1.0

zorexx

  • Full Member
  • ***
  • Posts: 109
    • View Profile
    • zorexx@site
SFMLTheora 1.3 - Play videos in SFML
« Reply #9 on: April 13, 2011, 08:06:24 pm »
Hi, I've never used std::make_shared as well as the auto keyword before, so I'm not very familiar with them.

But, according to the msdn library:
Quote
Since variables with auto storage class are not initialized automatically, you should either explicitly initialize them when you declare them, or assign initial values to them in statements within the block.


The Video class will not be initialized.
In the Release function:
Code: [Select]
if (ifaceFactory_ != NULL) {
  delete ifaceFactory_;
  ifaceFactory_ = NULL;
}

if (vidManager_ != NULL) {
  delete vidManager_;
  vidManager_ = NULL;
}


If the class is not initialized, the ifaceFactory_ and vidManager_ would not be set as NULL, so they will be deleted even if they're not created yet(by the Init function).

Calling the Init function before Release should automatically solve this problem, but in my code, I have this line as the first line in my Init function:
Code: [Select]
Release();

Can you try removing that line and tell me if it works that way?

I included that line so that there will not be a memory leak even if you forgotten to call Release when reusing the same variable for a new video (calling Init would automatically Release the previous video if it exists).

By the way, is there any specific reason for you to be using the auto keyword and std::make_shared?


Quote
I had to add OnSeek again just like the bugfix from 1.0

OnSeek? Where do you add it to?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
SFMLTheora 1.3 - Play videos in SFML
« Reply #10 on: April 13, 2011, 09:48:01 pm »
Quote
Hi, I've never used std::make_shared as well as the auto keyword before, so I'm not very familiar with them.

He's using features of the new C++ standard:
- "auto" is when you don't want to bother with the type of a variable, it deduces it from its initilization expression ; it has nothing to do with the current meaning of the auto keyword, that you found in the MSDN and which is deprecated
- "std::make_shared" constructs a std::shared_ptr ; it's slightly more optimized than calling "std::shared_ptr<T>(new ...)" since it can allocate both the object and the reference counter with a single call
Laurent Gomila - SFML developer

zorexx

  • Full Member
  • ***
  • Posts: 109
    • View Profile
    • zorexx@site
SFMLTheora 1.3 - Play videos in SFML
« Reply #11 on: April 14, 2011, 05:46:15 am »
Oh. Thanks for clearing things up, Laurent.
I tried reproducing the error, I replaced
Code: [Select]
testVid = new SFMLTheora::Video();
if (testVid == NULL)
  return 1;


with

Code: [Select]
auto testVid = std::make_shared<SFMLTheora::Video>()

I replaced the line
Code: [Select]
delete testVid;
with
Code: [Select]
testVid->Release;

and the code ran smooth without heap corruption.

I reread your post and noticed you mentioned
Quote
allways causes a heap corruption when shared_ptr is being deleted


I suspect what you did was replacing
Code: [Select]
delete testVid;
with
Code: [Select]
delete testVid.get();
instead.

I tried doing that and I got the heap corruption you're talking about.
After some reading, I found that a shared_ptr doesn't need to be deleted as it will be deleted when it goes out of scope, reassigned, or resetted.
The problem here is that, it is deleted twice: Once when you call delete testVid.get(); And once more when testVid goes out of scope(at the end of your program), since the pointer isn't set to NULL.

So the correct way to use it is to either totally skip the deleting part, or replace it with testVid->Release().



Anyway, I just remembered what happened to the OnSeek function, here's the story:
It was when I was making some major changes to the code, I found this OnSeek function that isn't used at all, so I thought it was just some mistake and removed it, woops. xD

Anyway, I've uploaded SFMLTheora 1.2.1 which contains the OnSeek function, thanks for notifying me about this.  :)

roosterkin123

  • Newbie
  • *
  • Posts: 43
    • MSN Messenger - rogerboy@hotmail.co.uk
    • View Profile
SFMLTheora 1.3 - Play videos in SFML
« Reply #12 on: April 14, 2011, 03:47:56 pm »
Hi, thanks for fixing the onseek thing.

Actually I wasnt deleting shared_ptr. I know that it doesnt need to be deleted manually. What I meant by it being deleted was the shared_ptr simply going out of scope.

Heres my code to play videos, I still get the heap corruption :(

Code: [Select]
bool PlayVideo(std::string name)
{
auto viddy = std::make_shared<SFMLTheora::Video>();
if(!viddy)return false;
viddy->LoadClip(name);
//viddy->SetX(0);
//viddy->SetY(0);
//GLR->GetMainWindow()->UseVerticalSync(true);

bool result = true;
bool stopvideo = false;

while(!viddy->GetVideoClip()->isDone())
{
sf::Event sfEvent;
while(GLRenderer::GetInstance()->GetMainWindow()->PollEvent(sfEvent))
{
 if(sfEvent.Type == sf::Event::Closed){result = false; stopvideo = true; break;}

 if((sfEvent.Type == sf::Event::KeyPressed) && (sfEvent.Key.Code == sf::Key::Escape))
 {
 viddy->GetVideoClip()->stop();
 viddy->GetVideoClip()->setAudioGain(0.0f);
 result = true;
 stopvideo = true;
 break;
 }

 if ((sfEvent.Type == sf::Event::KeyPressed) && (sfEvent.Key.Code == sf::Key::F12))Screenshot();
}

if(stopvideo)break;

viddy->Update(GLRenderer::GetInstance()->GetMainWindow()->GetFrameTime());
viddy->Resize(IniOptions::GetInstance()->window_width, IniOptions::GetInstance()->window_height);
GLRenderer::GetInstance()->GetMainWindow()->Clear();
GLRenderer::GetInstance()->GetMainWindow()->Draw(*viddy);
GLRenderer::GetInstance()->GetMainWindow()->Display();
}

//if(!options.vsync)GLR->GetMainWindow()->UseVerticalSync(false);
return result;
}

zorexx

  • Full Member
  • ***
  • Posts: 109
    • View Profile
    • zorexx@site
SFMLTheora 1.3 - Play videos in SFML
« Reply #13 on: April 14, 2011, 04:03:03 pm »
That's weird, I tried rewriting a similar piece of code:
Code: [Select]
int main()
{
  sf::RenderWindow sfApp(sf::VideoMode(800, 600, 32), "SFML Window");

  sfApp.UseVerticalSync(true);

  auto testVid = std::make_shared<SFMLTheora::Video>();
  if (!testVid)
    return 1;

  testVid->LoadClip("bunny.ogg");

  bool stopVideo = false;

  while (!testVid->GetVideoClip()->isDone()) {
    sf::Event sfEvent;
    while (sfApp.GetEvent(sfEvent)) {
      if (sfEvent.Type == sf::Event::Closed)
        sfApp.Close();

      if ((sfEvent.Type == sf::Event::KeyPressed)) {
        switch (sfEvent.Key.Code) {
          case sf::Key::Escape:
            {
              testVid->GetVideoClip()->stop();
              testVid->GetVideoClip()->setAudioGain(0.0f);
              stopVideo = true;
              break;
            }

          case sf::Key::F12:
            {
              sf::Image screen = sfApp.Capture();
              screen.SaveToFile("screenshot.png");

              break;
            }

          case sf::Key::Space:
            {
              testVid->TogglePause();
              break;
            }
        }
      }
    }

    if (stopVideo)
      break;

    testVid->Update(sfApp.GetFrameTime());
    testVid->Resize(800.0f, 600.0f);

    sfApp.Clear();

    sfApp.Draw(*testVid);

    sfApp.Display();
  }

  return EXIT_SUCCESS;
}


But there's no heap corruption, maybe it has something to do with sfml2?
Can you try using new and delete, or just plain SFMLTheora::Video viddy(without pointers), and see if it works that way?


By the way, please use viddy->IsDone() and viddy->Stop() instead of viddy->GetVideoClip()->... (available since 1.2 to avoid syncing problems)

roosterkin123

  • Newbie
  • *
  • Posts: 43
    • MSN Messenger - rogerboy@hotmail.co.uk
    • View Profile
SFMLTheora 1.3 - Play videos in SFML
« Reply #14 on: April 14, 2011, 04:44:21 pm »
Already tried normal new/delete and guess what running it of the stack causes stack corruption.

 

anything