summaryrefslogtreecommitdiffstats
path: root/noatun/library/titleproxy.h
blob: 5c0d934994018ea96182b52eee99aca220f19804 (plain)
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
/***************************************************************************
                     titleproxy.h  -  description
                        -------------------
begin                : Nov 20 14:35:18 CEST 2003
copyright            : (C) 2003 by Mark Kretschmann
email                :
***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef AMAROK_TITLEPROXY_H
#define AMAROK_TITLEPROXY_H

#include <kurl.h>             //stack allocated

#include <tqobject.h>
#include <tqserversocket.h>    //baseclass
#include <tqsocket.h>          //stack allocated

class TQString;

namespace TitleProxy
{
    /**
     * Proxy Concept:
     * 1. Connect to streamserver
     * 2. Listen on localhost, let aRts connect to proxyserver
     * 3. Write GET request to streamserver, containing Icy-MetaData:1 token
     * 4. Read MetaInt token from streamserver (==metadata offset)
     *
     * 5. Read stream data (mp3 + metadata) from streamserver
     * 6. Filter out metadata, send to app
     * 7. Write mp3 data to proxyserver
     * 8. Goto 5
     *
     * Some info on the shoutcast metadata protocol can be found at:
     * @see http://www.smackfu.com/stuff/programming/shoutcast.html
     *
     * @short A proxy server for extracting metadata from Shoutcast streams.
     */

    class Proxy : public TQObject
    {
            TQ_OBJECT
  
        public:
            Proxy( KURL url );
            ~Proxy();

            bool initSuccess() { return m_initSuccess; }
            KURL proxyUrl();

        signals:
				void metaData(
					const TQString &streamName,
					const TQString &streamGenre,
					const TQString &streamUrl,
					const TQString &streamBitrate,
					const TQString &trackTitle,
					const TQString &trackUrl);
            void proxyError();

        private slots:
            void accept( int socket );
            void connectToHost();
            void sendRequest();
            void readRemote();
            void connectError();

        private:
            bool processHeader( TQ_LONG &index, TQ_LONG bytesRead );
            void transmitData( const TQString &data );
            void error();
            TQString extractStr( const TQString &str, const TQString &key );

        //ATTRIBUTES:
            KURL m_url;
            int m_streamingMode;
            bool m_initSuccess;
            bool m_connectSuccess;

            int m_metaInt;
            TQString m_bitRate;
            int m_byteCount;
            uint m_metaLen;

            TQString m_metaData;
            bool m_headerFinished;
            TQString m_headerStr;
            int m_usedPort;
            TQString m_lastMetadata;
            bool m_icyMode;

            TQString m_streamName;
            TQString m_streamGenre;
            TQString m_streamUrl;

            char *m_pBuf;

            TQSocket m_sockRemote;
            TQSocket m_sockProxy;
    };


    class Server : public TQServerSocket
    {
            TQ_OBJECT
  

        public:
            Server( TQ_UINT16 port, TQObject* parent )
                    : TQServerSocket( port, 1, parent, "TitleProxyServer" ) {};

        signals:
            void connected( int socket );

        private:
            void newConnection( int socket ) { emit connected( socket ); }
    };

} //namespace TitleProxy

#endif /*AMAROK_TITLEPROXY_H*/