files.new_contents: """High-level sound and video player."""
  from __future__ import print_function
  from __future__ import division
  from builtins import object
  # ----------------------------------------------------------------------------
  # pyglet
  # Copyright (c) 2006-2008 Alex Holkner
  # All rights reserved.
  #
  # Redistribution and use in source and binary forms, with or without
  # modification, are permitted provided that the following conditions
  # are met:
  #
  #  * Redistributions of source code must retain the above copyright
  #    notice, this list of conditions and the following disclaimer.
  #  * Redistributions in binary form must reproduce the above copyright
  #    notice, this list of conditions and the following disclaimer in
  #    the documentation and/or other materials provided with the
  #    distribution.
  #  * Neither the name of pyglet nor the names of its
  #    contributors may be used to endorse or promote products
  #    derived from this software without specific prior written
  #    permission.
  #
  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  # POSSIBILITY OF SUCH DAMAGE.
  # ----------------------------------------------------------------------------
  
  from collections import deque
  
  import pyglet
  from pyglet.media import buffered_logger as bl
  from pyglet.media.drivers import get_audio_driver
  from pyglet.media.codecs
  
  [... the rest is omitted ...]
files.new_path: pyglet/media/player.py
files.old_contents: """High-level sound and video player."""
  from __future__ import print_function
  from __future__ import division
  from builtins import object
  # ----------------------------------------------------------------------------
  # pyglet
  # Copyright (c) 2006-2008 Alex Holkner
  # All rights reserved.
  #
  # Redistribution and use in source and binary forms, with or without
  # modification, are permitted provided that the following conditions
  # are met:
  #
  #  * Redistributions of source code must retain the above copyright
  #    notice, this list of conditions and the following disclaimer.
  #  * Redistributions in binary form must reproduce the above copyright
  #    notice, this list of conditions and the following disclaimer in
  #    the documentation and/or other materials provided with the
  #    distribution.
  #  * Neither the name of pyglet nor the names of its
  #    contributors may be used to endorse or promote products
  #    derived from this software without specific prior written
  #    permission.
  #
  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  # POSSIBILITY OF SUCH DAMAGE.
  # ----------------------------------------------------------------------------
  
  from collections import deque
  
  import pyglet
  from pyglet.media import buffered_logger as bl
  from pyglet.media.drivers import get_audio_driver
  from pyglet.media.codecs
  
  [... the rest is omitted ...]
files.old_path: pyglet/media/player.py
files.new_contents: from __future__ import absolute_import, print_function
  import pyglet
  import pytest
  _debug = False
  
  
  class MockPlayer(object):
      def __init__(self, event_loop):
          self.event_loop = event_loop
          self.events = []
          self.pyclock = pyglet.clock.get_default()
          super(MockPlayer, self).__init__()
  
      def dispatch_event(self, event_type, *args):
          super(MockPlayer, self).dispatch_event(event_type, *args)
          if _debug:
              print('{}: event {} received @ {}'.format(
                  self.__class__.__name__,
                  event_type, self.pyclock.time()
              ))
          self.events.append((event_type, args))
          pyglet.clock.unschedule(self.event_loop.interrupt_event_loop)
          self.event_loop.interrupt_event_loop()
  
      def wait_for_event(self, timeout, *event_types):
          end_time = self.pyclock.time() + timeout
  
          while self.pyclock.time() < end_time:
              if _debug:
                  print('{}: run for {} sec @ {}'.format(self.__class__.__name__,
                        end_time - self.pyclock.time(), self.pyclock.time()))
              self.event_loop.run_event_loop(duration=end_time - self.pyclock.time())
              if not self.events:
                  continue
              event_type, args = self.events.pop()
              if event_type in event_types:
                  return event_type, args
          return None, None
  
      def wait_for_all_events(self, timeout, *expected_events):
          now = self.pyclock.time()
          if _debug:
              print('Wait for events @ {}'.format(now))
          end_time = self.pyclock.time() + timeout
          expected_events = list(expected_events)
          received_events = []
          while expected_events:
              event_type, args = self.wait_for_event(timeout, *expected_events)
              if _debug:
                  print('{}: got event {} @ {}'.format(self.__class__.__name__,
                        event_type, self.pyclock.time()))
              if event_type is None and self.pyclock.time() >= end_time:
                  pytest.fail('Timeout before all events have been received. '
                              'Still waiting for: ' + ','.join(expected_events
  
  [... the rest is omitted ...]
files.new_path: tests/integration/media/mock_player.py
files.old_contents: from __future__ import absolute_import, print_function
  import pyglet
  _debug = False
  
  
  class MockPlayer(object):
      def __init__(self, event_loop):
          self.event_loop = event_loop
          self.events = []
          self.pyclock = pyglet.clock.get_default()
          super(MockPlayer, self).__init__()
  
      def dispatch_event(self, event_type, *args):
          super(MockPlayer, self).dispatch_event(event_type, *args)
          if _debug:
              print('{}: event {} received @ {}'.format(self.__class__.__name__,
              	event_type, self.pyclock.time()))
          self.events.append((event_type, args))
          pyglet.clock.unschedule(self.event_loop.interrupt_event_loop)
          self.event_loop.interrupt_event_loop()
  
      def wait_for_event(self, timeout, *event_types):
          end_time = self.pyclock.time() + timeout
  
          while self.pyclock.time() < end_time:
              if _debug:
                  print('{}: run for {} sec @ {}'.format(self.__class__.__name__,
                  	end_time-self.pyclock.time(), self.pyclock.time()))
              self.event_loop.run_event_loop(duration=end_time-self.pyclock.time())
              if not self.events:
                  continue
              event_type, args = self.events.pop()
              if event_type in event_types:
                  return event_type, args
          return None, None
  
      def wait_for_all_events(self, timeout, *expected_events):
          now = self.pyclock.time()
          if _debug:
              print('Wait for events @ {}'.format(now))
          end_time = self.pyclock.time() + timeout
          expected_events = list(expected_events)
          received_events = []
          while expected_events:
              event_type, args = self.wait_for_event(timeout, *expected_events)
              if _debug:
                  print('{}: got event {} @ {}'.format(self.__class__.__name__,
                  	event_type, self.pyclock.time()))
              if event_type is None and self.pyclock.time() >= end_time:
                  pytest.fail('Timeout before all events have been received. Still waiting for: '
                          + ','.join(expected_events))
              elif event_type is not None:
  
  
  [... the rest is omitted ...]
files.old_path: tests/integration/media/mock_player.py
files.new_contents: from __future__ import print_function
  from future import standard_library
  standard_library.install_aliases()
  
  import pytest
  from tests import mock
  import time
  
  import pyglet
  _debug = False
  pyglet.options['debug_media'] = _debug
  pyglet.options['debug_media_buffers'] = _debug
  
  from pyglet.media import Player
  from pyglet.media.synthesis import Silence
  from .mock_player import MockPlayer
  
  
  class PlayerTest(MockPlayer, Player):
      pass
  
  
  @pytest.fixture
  def player(event_loop):
      return PlayerTest(event_loop)
  
  
  class SilentTestSource(Silence):
      def __init__(self, duration, sample_rate=44800, sample_size=16):
          super(Silence, self).__init__(duration, sample_rate, sample_size)
          self.bytes_read = 0
  
      def get_audio_data(self, nbytes, compensation_time=0.0):
          data = super(Silence, self).get_audio_data(nbytes, compensation_time)
          if data is not None:
              self.bytes_read += data.length
          return data
  
      def has_fully_played(self):
          return self.bytes_read == self._max_offset
  
  
  def test_player_play(player):
      source = SilentTestSource(.1)
      player.queue(source)
  
      player.play()
      player.wait_for_all_events(
          1.,
          'on_eos',
          'on_player_eos'
      )
      assert source.has_fully_played(), 'Source not fully played'
  
  
  def test_player_play_multiple(player):
      sources = (SilentTestSource(.1), SilentTestSource(.1))
      for source in sources:
          player.queue(source)
  
      player.play()
      player.wait_for_all_events(
          1.,
          'on_eos',
          'on_player_next_source',
          'on_eos',
          'on_player_eos'
      )
      for source in sources:
          assert source.has_fully_played(), 'Source not fully played'
  
  
  def test_multiple_fire_and_forget_players():
      """
      Test an issue where the driver crashed when starting multiple players, but not
      keeping a reference to these players.
      """
      for _ in range(10):
          Silence(1).play()
      time.sleep(1)
  
  
  def test_player
  
  [... the rest is omitted ...]
files.new_path: tests/integration/media/test_player.py
files.old_contents: from __future__ import print_function
  from future import standard_library
  standard_library.install_aliases()
  
  import gc
  import pytest
  from tests import mock
  import time
  import unittest
  
  import pyglet
  _debug = False
  pyglet.options['debug_media'] = _debug
  pyglet.options['debug_media_buffers'] = _debug
  
  from pyglet.media import Player
  from pyglet.media.synthesis import Silence
  from .mock_player import MockPlayer
  
  
  class PlayerTest(MockPlayer, Player):
      pass
  
  
  @pytest.fixture
  def player(event_loop):
      return PlayerTest(event_loop)
  
  class SilentTestSource(Silence):
      def __init__(self, duration, sample_rate=44800, sample_size=16):
          super(Silence, self).__init__(duration, sample_rate, sample_size)
          self.bytes_read = 0
  
      def get_audio_data(self, nbytes, compensation_time=0.0):
          data = super(Silence, self).get_audio_data(nbytes, compensation_time)
          if data is not None:
              self.bytes_read += data.length
          return data
  
      def has_fully_played(self):
          return self.bytes_read == self._max_offset
  
  
  
  def test_player_play(player):
      source = SilentTestSource(.1)
      player.queue(source)
  
      player.play()
      player.wait_for_all_events(1., 
          'on_eos', 'on_player_eos')
      assert source.has_fully_played(), 'Source not fully played'
  
  
  def test_player_play_multiple(player):
      sources = (SilentTestSource(.1), SilentTestSource(.1))
      for source in sources:
          player.queue(source)
  
      player.play()
      player.wait_for_all_events(1., 
          'on_eos', 'on_player_next_source', 'on_eos', 'on_player_eos')
      for source in sources:
          assert source.has_fully_played(), 'Source not fully played'
  
  
  def test_multiple_fire_and_forget_players():
      """
      Test an issue where the driver crashed when starting multiple players, but not keeping a
      reference to these players.
      """
      for _ in range(10):
          Silence(1).play()
      time.sleep(1)
  
  
  [... the rest is omitted ...]
files.old_path: tests/integration/media/test_player.py
files.new_contents: from __future__ import division
  from builtins import range
  from tests import mock
  import random
  from tests.base.future_test import FutureTestCase
  
  from pyglet.media.player import Player, PlayerGroup
  from pyglet.media.codecs.base import AudioFormat, VideoFormat, Source
  
  # pyglet.options['debug_media'] = True
  
  
  class PlayerTestCase(FutureTestCase):
      # Default values to use
      audio_format_1 = AudioFormat(1, 8, 11025)
      audio_format_2 = AudioFormat(2, 8, 11025)
      audio_format_3 = AudioFormat(2, 16, 44100)
      video_format_1 = VideoFormat(800, 600)
      video_format_2 = VideoFormat(1920, 1280)
      video_format_2.frame_rate = 25
  
      def setUp(self):
          self.player = Player()
  
          self._get_audio_driver_patcher = mock.patch('pyglet.media.player.get_audio_driver')
          self.mock_get_audio_driver = self._get_audio_driver_patcher.start()
          self.mock_audio_driver = self.mock_get_audio_driver.return_value
          self.mock_audio_driver_player = self.mock_audio_driver.create_audio_player.return_value
  
          self._clock_patcher = mock.patch('pyglet.clock')
          self.mock_clock = self._clock_patcher.start()
  
          self._texture_patcher = mock.patch('pyglet.image.Texture.create')
          self.mock_texture_create = self._texture_patcher.start()
          self.mock_texture = self.mock_texture_create.return_value
          # Need to do this as side_effect instead of return_value, or reset_mock will recurse
          self.mock_texture.get_transform.side_effect = lambda flip_y: self.mock_texture
  
      def tearDown(self):
          self._get_audio_driver_patcher.stop()
          self._clock_patcher.stop()
          self._texture_patcher.stop()
  
      def reset_mocks(self):
          # These mocks will recursively reset their children
          self.mock_get_audio_driver.reset_mock()
          self.mock_clock.reset_mock()
          self.mock_texture_create.reset_m
  
  [... the rest is omitted ...]
files.new_path: tests/unit/media/test_player.py
files.old_contents: from __future__ import division
  from builtins import range
  import ctypes
  from tests import mock
  import os
  import random
  from collections import deque
  from itertools import product
  from tests.base.future_test import FutureTestCase
  
  import pytest
  
  import pyglet
  from pyglet.media.player import Player, PlayerGroup
  from pyglet.media.codecs.base import *
  
  #pyglet.options['debug_media'] = True
  
  
  class PlayerTestCase(FutureTestCase):
      # Default values to use
      audio_format_1 = AudioFormat(1, 8, 11025)
      audio_format_2 = AudioFormat(2, 8, 11025)
      audio_format_3 = AudioFormat(2, 16, 44100)
      video_format_1 = VideoFormat(800, 600)
      video_format_2 = VideoFormat(1920, 1280)
      video_format_2.frame_rate = 25
  
      def setUp(self):
          self.player = Player()
  
          self._get_audio_driver_patcher = mock.patch('pyglet.media.player.get_audio_driver')
          self.mock_get_audio_driver = self._get_audio_driver_patcher.start()
          self.mock_audio_driver = self.mock_get_audio_driver.return_value
          self.mock_audio_driver_player = self.mock_audio_driver.create_audio_player.return_value
  
          self._clock_patcher = mock.patch('pyglet.clock')
          self.mock_clock = self._clock_patcher.start()
  
          self._texture_patcher = mock.patch('pyglet.image.Texture.create')
          self.mock_texture_create = self._texture_patcher.start()
          self.mock_texture = self.mock_texture_create.return_value
          # Need to do this as side_effect instead of return_value, or reset_mock will recurse
          self.mock_texture.get_transform.side_effect = lambda flip_y: self.mock_texture
  
      def tearDown(self):
          self._get_audio_driver_patcher.stop()
          self._clock_patcher.stop()
          self._texture_patcher.stop()
  
      def reset_mocks(self):
          # These mocks will recursively reset their children
          self.mock_get_audio_driver.reset_mock
  
  [... the rest is omitted ...]
files.old_path: tests/unit/media/test_player.py