summaryrefslogtreecommitdiffstats
path: root/xbmc/cores/VideoPlayer/VideoPlayerAudioID3.cpp
blob: b4db5bfdd84ce7425fb5982040987e1c315eb606 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
 *  Copyright (C) 2005-2022 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "VideoPlayerAudioID3.h"

#include "DVDStreamInfo.h"
#include "GUIInfoManager.h"
#include "Interface/DemuxPacket.h"
#include "ServiceBroker.h"
#include "application/Application.h"
#include "guilib/GUIComponent.h"
#include "music/tags/MusicInfoTag.h"
#include "utils/log.h"

#include <taglib/attachedpictureframe.h>
#include <taglib/commentsframe.h>
#include <taglib/id3v1genres.h>
#include <taglib/id3v2framefactory.h>
#include <taglib/mpegfile.h>
#include <taglib/tbytevectorstream.h>
#include <taglib/textidentificationframe.h>

using namespace TagLib;

CVideoPlayerAudioID3::CVideoPlayerAudioID3(CProcessInfo& processInfo)
  : IDVDStreamPlayer(processInfo), CThread("VideoPlayerAudioID3"), m_messageQueue("id3")
{
  CLog::Log(LOGDEBUG, "Audio ID3 tag processor - new {}", __FUNCTION__);
}

CVideoPlayerAudioID3::~CVideoPlayerAudioID3()
{
  CLog::Log(LOGDEBUG, "Audio ID3 tag processor - delete {}", __FUNCTION__);
  StopThread();
}

bool CVideoPlayerAudioID3::CheckStream(const CDVDStreamInfo& hints)
{
  return hints.type == STREAM_AUDIO_ID3;
}

void CVideoPlayerAudioID3::Flush()
{
  if (m_messageQueue.IsInited())
  {
    m_messageQueue.Flush();
    m_messageQueue.Put(std::make_shared<CDVDMsg>(CDVDMsg::GENERAL_FLUSH));
  }
}

void CVideoPlayerAudioID3::WaitForBuffers()
{
  m_messageQueue.WaitUntilEmpty();
}

bool CVideoPlayerAudioID3::OpenStream(CDVDStreamInfo hints)
{
  CloseStream(true);
  m_messageQueue.Init();

  if (hints.type == STREAM_AUDIO_ID3)
  {
    Flush();
    CLog::Log(LOGINFO, "Creating Audio ID3 tag processor data thread");
    Create();
    return true;
  }

  return false;
}

void CVideoPlayerAudioID3::CloseStream(bool bWaitForBuffers)
{
  m_messageQueue.Abort();

  CLog::Log(LOGINFO, "Audio ID3 tag processor - waiting for data thread to exit");
  StopThread();

  m_messageQueue.End();
}

void CVideoPlayerAudioID3::SendMessage(std::shared_ptr<CDVDMsg> pMsg, int priority)
{
  if (m_messageQueue.IsInited())
    m_messageQueue.Put(pMsg, priority);
}

void CVideoPlayerAudioID3::FlushMessages()
{
  m_messageQueue.Flush();
}

bool CVideoPlayerAudioID3::IsInited() const
{
  return true;
}

bool CVideoPlayerAudioID3::AcceptsData() const
{
  return !m_messageQueue.IsFull();
}

bool CVideoPlayerAudioID3::IsStalled() const
{
  return true;
}

void CVideoPlayerAudioID3::OnExit()
{
  CLog::Log(LOGINFO, "Audio ID3 tag processor - thread end");
}

void CVideoPlayerAudioID3::Process()
{
  CLog::Log(LOGINFO, "Audio ID3 tag processor - running thread");

  while (!m_bStop)
  {
    std::shared_ptr<CDVDMsg> pMsg;
    int iPriority = (m_speed == DVD_PLAYSPEED_PAUSE) ? 1 : 0;
    MsgQueueReturnCode ret = m_messageQueue.Get(pMsg, 2000, iPriority);

    // Timeout for ID3 tag data is not a bad thing, so we continue without error
    if (ret == MSGQ_TIMEOUT)
      continue;

    if (MSGQ_IS_ERROR(ret))
    {
      if (!m_messageQueue.ReceivedAbortRequest())
        CLog::Log(LOGERROR, "MSGQ_IS_ERROR returned true ({})", ret);

      break;
    }

    if (pMsg->IsType(CDVDMsg::DEMUXER_PACKET))
    {
      std::unique_lock<CCriticalSection> lock(m_critSection);
      DemuxPacket* pPacket = std::static_pointer_cast<CDVDMsgDemuxerPacket>(pMsg)->GetPacket();
      if (pPacket)
        ProcessID3(pPacket->pData, pPacket->iSize);
    }
    else if (pMsg->IsType(CDVDMsg::PLAYER_SETSPEED))
    {
      m_speed = std::static_pointer_cast<CDVDMsgInt>(pMsg)->m_value;
    }
  }
}

void CVideoPlayerAudioID3::ProcessID3(const unsigned char* data, unsigned int length) const
{
  if (data && length > 0)
  {
    ByteVectorStream tagStream(ByteVector(reinterpret_cast<const char*>(data), length));
    if (tagStream.isOpen())
    {
      MPEG::File tagFile = MPEG::File(&tagStream, ID3v2::FrameFactory::instance());
      if (tagFile.isOpen())
      {
        if (tagFile.hasID3v1Tag())
          ProcessID3v1(tagFile.ID3v1Tag(false));

        else if (tagFile.hasID3v2Tag())
          ProcessID3v2(tagFile.ID3v2Tag(false));
      }
    }
  }
}

void CVideoPlayerAudioID3::ProcessID3v1(const ID3v1::Tag* tag) const
{
  if (tag != nullptr && !tag->isEmpty())
  {
    MUSIC_INFO::CMusicInfoTag* currentMusic = g_application.CurrentFileItem().GetMusicInfoTag();
    if (currentMusic)
    {
      bool changed = false;

      const String title = tag->title();
      if (!title.isEmpty())
      {
        currentMusic->SetTitle(title.to8Bit(true));
        changed = true;
      }

      const String artist = tag->artist();
      if (!artist.isEmpty())
      {
        currentMusic->SetArtist(artist.to8Bit(true));
        changed = true;
      }

      const String album = tag->album();
      if (!album.isEmpty())
      {
        currentMusic->SetAlbum(album.to8Bit(true));
        changed = true;
      }

      const String comment = tag->comment();
      if (!comment.isEmpty())
      {
        currentMusic->SetComment(comment.to8Bit(true));
        changed = true;
      }

      const String genre = tag->genre();
      if (!genre.isEmpty())
      {
        currentMusic->SetGenre(genre.to8Bit(true));
        changed = true;
      }

      const unsigned int year = tag->year();
      if (year != 0)
      {
        currentMusic->SetYear(year);
        changed = true;
      }

      const unsigned int track = tag->track();
      if (track != 0)
      {
        currentMusic->SetTrackNumber(track);
        changed = true;
      }

      if (changed)
        CServiceBroker::GetGUI()->GetInfoManager().SetCurrentItem(g_application.CurrentFileItem());
    }
  }
}

void CVideoPlayerAudioID3::ProcessID3v2(const ID3v2::Tag* tag) const
{
  if (tag != nullptr && !tag->isEmpty())
  {
    MUSIC_INFO::CMusicInfoTag* currentMusic = g_application.CurrentFileItem().GetMusicInfoTag();
    if (currentMusic)
    {
      bool changed = false;

      const ID3v2::FrameListMap& frameListMap = tag->frameListMap();
      for (const auto& it : frameListMap)
      {
        if (!it.second.isEmpty())
        {
          if (it.first == "TIT2")
          {
            currentMusic->SetTitle(it.second.front()->toString().to8Bit(true));
            changed = true;
          }

          else if (it.first == "TPE1")
          {
            currentMusic->SetArtist(GetID3v2StringList(it.second));
            changed = true;
          }

          else if (it.first == "TALB")
          {
            currentMusic->SetAlbum(it.second.front()->toString().to8Bit(true));
            changed = true;
          }

          else if (it.first == "COMM")
          {
            // Loop through and look for the main (no description) comment
            for (const auto& ct : it.second)
            {
              auto commentsFrame = dynamic_cast<const ID3v2::CommentsFrame*>(ct);
              if (commentsFrame && commentsFrame->description().isEmpty())
              {
                currentMusic->SetComment(commentsFrame->text().to8Bit(true));
                changed = true;
                break;
              }
            }
          }

          else if (it.first == "TCON")
          {
            currentMusic->SetGenre(GetID3v2StringList(it.second));
            changed = true;
          }

          else if (it.first == "TYER")
          {
            currentMusic->SetYear(static_cast<int>(
                strtol(it.second.front()->toString().toCString(true), nullptr, 10)));
            changed = true;
          }

          else if (it.first == "TRCK")
          {
            currentMusic->SetTrackNumber(static_cast<int>(
                strtol(it.second.front()->toString().toCString(true), nullptr, 10)));
            changed = true;
          }

          // Support for setting the cover art image via CMusicInfoTag does not currently exist,
          // the code sample below would check for an ID3v2 "APIC" tag of the proper type and
          // convert the information into an EmbeddedArt object instance
          //
          // else if (it.first == "APIC")
          // {
          //  // Loop through and look for the FrontCover picture frame
          //  for (const auto& pi : it.second)
          //  {
          //    auto pictureFrame = dynamic_cast<ID3v2::AttachedPictureFrame*>(pi);
          //    if (pictureFrame && pictureFrame->type() == ID3v2::AttachedPictureFrame::FrontCover)
          //    {
          //      EmbeddedArt coverArt(
          //          reinterpret_cast<const uint8_t*>(pictureFrame->picture().data()),
          //          pictureFrame->size(), pictureFrame->mimeType().to8Bit(true));

          //      // Assumes "void CMusicInfoTag::SetCoverArt(const EmbeddedArt& art)" exists
          //      currentMusic->SetCoverArt(coverArt);
          //      changed = true;
          //    }
          //  }
          // }
        }
      }

      if (changed)
        CServiceBroker::GetGUI()->GetInfoManager().SetCurrentItem(g_application.CurrentFileItem());
    }
  }
}

std::vector<std::string> CVideoPlayerAudioID3::GetID3v2StringList(const ID3v2::FrameList& frameList)
{
  auto frame = dynamic_cast<const ID3v2::TextIdentificationFrame*>(frameList.front());
  if (frame)
    return StringListToVectorString(frame->fieldList());
  return {};
}

std::vector<std::string> CVideoPlayerAudioID3::StringListToVectorString(
    const StringList& stringList)
{
  std::vector<std::string> values;
  for (const auto& value : stringList)
    values.emplace_back(value.to8Bit(true));
  return values;
}