Пример #1
0
    def stream(self, **kwargs):

        user = cherrypy.request.user

        user.active = datetime.datetime.now()

        remotes.update_user(user)

        if 'dead' in kwargs and kwargs['dead'] == 'true':
            raise cherrypy.HTTPError(status=503)

        if 'User-Agent' in cherrypy.request.headers:
            user_agent = cherrypy.request.headers['User-Agent']
        else:
            user_agent = None

        # allow only one streaming client at once, or weird things might occur
        if (user.id in Play.STREAM_PLAYING and Play.STREAM_PLAYING[user.id] is not None and
                Play.STREAM_PLAYING[user.id] != user_agent):
            raise cherrypy.HTTPError(status=503)

        if user.id in Play.STREAM_MODE and Play.STREAM_MODE[user.id] is not None:
            mode = Play.STREAM_MODE[user.id]
        else:
            mode = 'regular'

        if mode == "random":
            track = queue_dao.get_random_track(user.id)
            current_seconds = 0
        else:
            queue = queue_dao.get_next(user.id)

            if queue is not None:
                track = queue.track
                current_seconds = queue.current_seconds
            else:
                track = None

        if track is None:
            raise cherrypy.HTTPError(status=409)

        transcoder, format = transcoding.determine_transcoder(
            track,
            user_agent,
            [accept.value for accept in cherrypy.request.headers.elements('Accept')]
        )

        cherrypy.log(
            '%s is streaming "%s" in %s (original was %s) with "%s"' %
            (user.login, track, format, track.format, user_agent)
        )

        cherrypy.response.headers['Content-Type'] = format

        def track_generator():
            yield track, current_seconds

        return transcoding.transcode(track_generator(), transcoder)
Пример #2
0
    def test_determine_transcoder(self):
        library_start()

        ogg_track = self.session.query(Track).filter(Track.name == "opmuse").one()

        # MPD ogg
        transcoder, format = transcoding.determine_transcoder(ogg_track, "Music Player Daemon 0.18.8", ['*/*'])

        assert format == "audio/ogg"
        assert transcoder == CopyFFMPEGTranscoder

        # firefox ogg
        user_agent = "Mozilla/5.0 (X11; Linux x86_64; rv:27.0) Gecko/20100101 Firefox/27.0 FirePHP/0.7.4"
        accepts = ['audio/webm', 'audio/wav', 'audio/ogg', 'audio/*', 'application/ogg', 'video/*', '*/*']
        transcoder, format = transcoding.determine_transcoder(ogg_track, user_agent, accepts)

        assert format == "audio/ogg"
        assert transcoder == CopyFFMPEGTranscoder

        # chrome ogg
        user_agent = ("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) " +
                      "Chrome/32.0.1700.107 Safari/537.36")
        accepts = ['*/*']
        transcoder, format = transcoding.determine_transcoder(ogg_track, user_agent, accepts)

        assert format == "audio/ogg"
        assert transcoder == CopyFFMPEGTranscoder

        mp3_track = self.session.query(Track).filter(Track.name == "opmuse mp3").one()

        # MPD mp3
        transcoder, format = transcoding.determine_transcoder(mp3_track, "Music Player Daemon 0.18.8", ['*/*'])

        assert format == "audio/mp3"
        assert transcoder == CopyFFMPEGTranscoder

        # firefox mp3
        user_agent = "Mozilla/5.0 (X11; Linux x86_64; rv:27.0) Gecko/20100101 Firefox/27.0 FirePHP/0.7.4",
        accepts = ['audio/webm', 'audio/wav', 'audio/ogg', 'audio/*', 'application/ogg', 'video/*', '*/*']
        transcoder, format = transcoding.determine_transcoder(mp3_track, user_agent, accepts)

        assert format == "audio/ogg"
        assert transcoder == OggFFMPEGTranscoder

        # chrome mp3
        user_agent = ("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) " +
                      "Chrome/32.0.1700.107 Safari/537.36")
        accepts = ['*/*']
        transcoder, format = transcoding.determine_transcoder(mp3_track, user_agent, accepts)

        assert format == "audio/mp3"
        assert transcoder == CopyFFMPEGTranscoder