Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to add support for Brightcove's implementation of Widevine? #3

Closed
glennguy opened this issue Dec 1, 2016 · 4 comments

Comments

@glennguy
Copy link
Contributor

glennguy commented Dec 1, 2016

Hi,

I'm working on an addon that has some content encrypted with Widevine using Brightcove. Unfortunately the license retrieval by sending a request to their server which then is modified and sent to their actual private widevine server. The process is outlined in brief here: https://support.brightcove.com/en/video-cloud/docs/protecting-your-content-using-google-widevine

I haven't been able to see much in comparing the hex from the POST data to what Chrome sends. Here is what the android app sends to the Brightcove license server:

08 01 12 8f 0d 0a a0 0c 08 01 12 eb 09 0a ae 02 08 02 12 10 20 65 ee b5 72 be 52 f1 91 02 06 3b dc 82 cc 96 18 c6 f5 c8 bf 05 22 8e 02 30 82 01 0a 02 82 01 01 20 c3 77 8c 17 b7 86 0f 33 02 c2 c9 03 66 b6 5c ec b3 f3 03 7f 36 a8 a0 77 07 93 f7 04 74 c5 20 ef f3 af a3 21 51 2a b5 a5 73 4a d6 d0 3a 70 04 b9 13 12 8f ee 92 1a b1 92 08 81 a5 ad 34 4c d9 59 b0 0c 88 12 dd d5 16 5b 70 f3 68 e0 3c 0f a9 5b bb 2f b2 4a 34 d0 43 49 da bf da da 8a 5b 78 91 3a 9b 8e 95 2e 09 dc a5 3f 4c 6a 7c a4 ea 87 92 09 2f 4e b8 f3 22 93 7a 71 20 f4 12 6e 17 a3 2d 3f 68 44 40 79 93 96 1a e7 79 c0 03 62 fa 72 0c 57 73 38 12 ef 53 93 10 5a ee 4e 8f c4 87 6f ea 44 af ef d9 45 c5 f4 88 46 7a e4 07 b3 7f 49 fa c9 b5 64 59 3c 5d 70 d5 b8 7f 1e eb 02 2d b0 39 71 05 10 48 53 8d a7 9a a9 24 d3 07 03 83 af ed 65 a7 e9 9b f8 7d 20 a2 2a 9a 41 cc 71 5f d3 06 56 5e 5d 53 82 f0 65 2e a4 1b 99 31 ad 55 51 21 c6 09 a2 df e4 35 bd cb 45 fa 0f 97 ba e6 4a c9 02 03 01 20 01 28 dd 22 12 80 02 3b f2 23 23 de 55 5e 20 f7 aa 6e de f6 ac 07 e4 3e f9 56 e8 72 16 38 4e e5 2d e9 02 07 e5 0d a4 96 55 b1 14 aa ac 26 5e ad 8e 71 98 1b e8 be b7 0f 4e 51 9a 7b 71 e7 f0 02 dc a0 e7 23 12 82 a8 40 9d c3 ad c6 c2 63 c0 b8 ca 45 4a d8 4f 53 8a 98 1e e3 c8 39 c5 22 5f 5b dc 32 8e df f7 41 a6 12 f7 50 d4 54 17 1d 66 4b eb b1 b7 47 0f 63 7e 97 c7 e9 0a 25 4c 34 5a 65 82 c3 e9 f7 ad e0 4a f5 23 0a c8 f5 05 ba 52 37 37 cb 5c 60 3e ab b0 5c ca 1e 5e a9 af 0e 6c eb f5 39 48 f6 6b 98 69 89 dc 5b 70 15 1a b2 8a 70 83 40 d0 ee 82 62 88 cb 8a 7f 87 12 06 5e 30 13 e6 97 a1 43 a1 98 ee d2 0e 1e ad 57 fd 7c 63 5c f3 e8 06 22 b6 99 7b 23 cd 7c 9d 77 ae 3b a3 09 c1 98 f8 f6 d4 7d 03 75 d6 87 f9 14 4a e8 02 7f 08 8d d6 34 76 f6 ba 71 ea e0 bb 85 e7 16 8c b1 3a 9e d9 4f b3 07 de 1a b4 05 0a ae 02 08 01 12 10 17 dc bc 27 d1 13 41 d4 97 13 54 42 a1 88 da a6 18 8f 89 80 91 05 22 8e 02 30 82 01 0a 02 82 01 01 20 d2 1a dd 75 49 d2 74 8b 34 94 52 6a 9c 3f b8 6c 79 37 6b be 8c 88 56 f6 01 b8 d1 04 61 f7 7a cc 73 31 b1 0d eb f3 65 12 20 56 cd b5 66 2d 25 90 7b 74 f1 23 82 f0 f4 a0 ca 47 5e ea 95 62 81 5c 62 28 f6 f6 98 ad a2 78 79 d8 89 0f 2a 2d 96 a7 46 dd ef 53 16 30 1c 20 35 19 c2 a2 25 03 54 67 41 69 fd da 41 ce 14 d3 c5 2b ea 7a 38 45 15 01 2d 59 52 b3 8a a1 9e 15 e8 56 3c c7 aa a8 1c 21 22 88 0a a3 70 a6 4f ea 23 c5 3f b8 3a c3 db 57 53 21 47 30 a3 49 e0 7f 64 bf 32 be 7e ad 30 d0 26 12 af 11 0b b4 4f b0 8e 1d 30 81 73 b3 27 ef 64 d4 0c 41 63 95 42 b2 d1 a7 3c 98 a6 60 7e c6 c6 83 b5 13 a5 84 70 51 41 06 ef 87 ae 1e 7b 9c 69 5b 93 a1 04 df 74 37 bf c4 16 77 89 74 8a 43 ed 8f 2c 1f a7 10 79 3c 68 88 85 ea e7 32 a8 bf df 5b 42 3b 23 d7 5b 88 fc 0a dc 8f bd b5 02 03 01 20 01 28 dd 22 12 80 03 72 d2 fb 88 09 8b a3 b8 5b 6b 43 54 e0 37 67 db e2 d7 72 46 63 fb 0a 62 ab f7 70 4e a9 10 e0 1f 22 13 49 ee 16 d0 15 2c 76 93 84 05 0c e7 85 66 8c 06 cc fd 3d 78 9a f3 eb 69 ff 16 36 15 cd 60 91 69 fd be 2e 15 a0 29 d3 4a d2 60 56 25 bc 81 84 4c 9d 1e 2c e0 51 90 39 f3 79 9a da ef 86 64 1e 20 b0 33 dc 16 df 2e 5b 9a 1a 2a 41 7b 8b b3 b7 a4 d9 ad 1a 99 36 74 48 58 7d a1 3d de 05 a3 ed 9d 62 fa 42 07 89 73 b4 aa 40 26 3d 7b fa 23 f1 07 2e 94 cd f3 23 fa 45 f7 84 08 82 3e 55 c4 f4 c5 c7 23 81 9c f4 4c e6 d9 8e 50 c0 4e c2 4d 93 b1 aa b8 87 7b 91 08 b9 ca 39 13 08 e1 a3 64 5e bb 0e 7c ac bb 40 b5 45 15 60 ed 79 94 21 87 3b fb 5a bb 91 7f a6 0d b9 c7 7c b8 60 6a f7 e3 14 26 26 f5 ea 40 e5 cb 8a a0 89 d8 e7 d6 a9 36 19 35 c4 26 a4 45 0e a8 bc 2e 57 29 0d 3b f0 a0 96 29 91 d2 a9 1b 75 2f c8 0c 3e 7e 4e 55 03 3d 71 c9 4b 32 53 07 a6 88 15 f0 26 44 8f 56 a2 74 1c eb ef c1 8e 8c 14 2f 5f 62 bf aa 67 a2 91 51 7d de 98 2d 8c d5 a9 df 6e 3d 3a 99 b8 06 f6 d6 09 91 35 8c 5b e7 71 17 d4 f3 16 8f 33 48 e9 a0 48 53 9f 89 2f 4d 78 31 52 c7 a8 09 52 24 aa 56 b7 8c 5c f7 bd 1a b1 b1 79 c0 c0 d1 1e 3c 3b ac 84 c1 41 a0 01 91 32 1e 3a cc 17 24 2e 68 3c 1a 13 0a 0c 63 6f 6d 70 61 6e 79 5f 6e 61 6d 65 12 03 48 54 43 1a 18 0a 0a 6d 6f 64 65 6c 5f 6e 61 6d 65 12 0a 48 54 43 20 4f 6e 65 5f 4d 38 1a 0a 11 61 72 63 68 69 74 65 63 74 75 72 65 5f 6e 61 6d 65 12 0b 61 72 6d 65 61 62 69 2d 76 37 61 1a 15 0a 0b 64 65 76 69 63 65 5f 6e 61 6d 65 12 06 68 74 63 5f 6d 38 1a 19 0a 0c 70 72 6f 64 75 63 74 5f 6e 61 6d 65 12 09 6d 38 5f 67 6f 6f 67 6c 65 1a 4a 0a 0a 62 75 69 6c 64 5f 69 6e 66 6f 12 3c 68 74 63 2f 6d 38 5f 67 6f 6f 67 6c 65 2f 68 74 63 5f 6d 38 3a 36 2e 30 2f 4d 52 41 35 38 4b 2e 48 31 35 2f 36 36 36 36 37 36 3a 75 73 65 72 2f 72 65 6c 65 61 73 65 2d 6b 65 79 73 1a 2d 0a 09 64 65 76 69 63 65 5f 69 64 12 20 71 4d 4f 71 78 75 4a 55 70 7a 43 73 6a 50 6a 49 4d 59 5a 4a 49 62 6a 52 4a 6a 6c 5a 6a 48 78 20 1a 26 0a 14 77 69 64 65 76 69 6e 65 5f 63 64 6d 5f 76 65 72 73 69 6f 6e 12 0e 76 33 2e 30 2e 30 2d 61 6e 64 72 6f 69 64 32 08 10 01 20 28 0a 30 20 12 5a 0a 58 0a 32 08 01 12 10 d4 35 b3 b2 be 2e 41 e3 a1 69 99 94 ac f2 a8 ec 1a 0a 62 72 69 67 68 74 63 6f 76 65 22 10 d4 35 b3 b2 be 2e 41 e3 a1 69 99 94 ac f2 a8 ec 10 01 1a 20 38 30 44 46 35 43 46 32 46 32 30 35 37 45 35 43 42 34 30 30 30 30 30 30 30 30 30 30 30 30 30 30 18 01 20 8c be fe c1 05 30 15 38 df 88 f4 e9 02 1a 80 02 64 c5 6d cb 1f e1 c4 63 c7 46 3a 2d ca fc d1 ec 34 03 52 c0 5c 05 2f 88 0f ed 44 a1 0f b7 b6 cc b4 30 62 37 80 2c ec fe 83 83 9d 64 1b ba b1 bd 92 37 b8 02 18 42 bf 82 9b c3 49 3c 02 e9 1e 44 d8 f7 4c f2 71 28 86 ef 75 59 79 6d c3 17 a8 4c 14 46 44 8b ce 21 91 48 e6 8a 51 45 d9 fa 50 6f d9 6c ea 58 25 e5 2f 2d 01 b9 96 8c 25 b1 06 e7 bb 41 7f c1 76 13 41 62 66 18 96 98 7d 32 56 16 a9 d0 5e a3 f5 44 1c e8 5e 26 f5 a9 f6 e0 dd de c3 7b bf f1 9f e7 ab 37 ac dc 0f ef 3a ee 6a b9 03 fe 6a 5b 03 d0 4e d3 74 d8 eb 4b d0 a6 70 e3 ea 7e d7 01 13 4c d9 10 36 82 4f b3 d8 1e 14 e4 4a 9b 98 21 18 25 0f b5 fd bd 8e d4 60 6d bd 5e 8e be 08 96 55 fe f9 1d 78 af 06 88 05 72 c3 e2 47 87 58 4f fc f8 66 b4 b8 35 b0 86 38 64 41 eb 45 1c 5f 24 1e 80 13 f2 70 26 c9 9c e1 f8 8e eb  

If you have any ideas or suggestions for how I could help solve this please let me know.

Thanks

@peak3d
Copy link
Contributor

peak3d commented Dec 2, 2016

Hi, you can look here for a widevine cdm implementation:
https://github.com/liberty-developer/inputstream.adaptive/tree/master/wvdecrypter

From concept its quite easy to build a BrightCoves SampleDecrypter.
You can find in the location above beside the widevine implementation as well an mediacodec implementation for android.

The hex sequence above seems to be for me a normal widevine challenge request.

@glennguy
Copy link
Contributor Author

glennguy commented Dec 3, 2016

I already found liberty developers page, unfortunately issues aren't enabled in the fork :/ so I thought I'd try raising it here.

I have however in the last couple of days started learning c++ and managed to make a few changes to get it working how it needs to - Amazon takes a URL encoded form whereas Brightcove just wants the unmodified binary challenge request (contrary to the link in the OP where it states the request is modified). I'll see if there's some way to add auto detecting or option selecting and make a PR on the fork. Thanks for your time!

@glennguy glennguy closed this as completed Dec 3, 2016
@peak3d
Copy link
Contributor

peak3d commented Dec 3, 2016

I guess you have mentioned that the license string passed to the wvdecrypter already offers some possibilities. Its a kind of template where you can specify a) how data is send to the server and b) how the returned data should be interpreted. Includin if send R(aw) or B(64) encoded

Normally you don't have to modify the cpp by itself, you only have to modify the license_ky string so it fits to your needs.

Format is url|header|postdata|resonse.

@glennguy
Copy link
Contributor Author

glennguy commented Dec 4, 2016

Awesome, thanks for the help! I'm adding |Content-Type=application%2Fx-www-form-urlencoded|A{SSM}| and it works great.

Unrelated to this particular problem, I'm getting a bunch of ERROR: AddOnLog: InputStream Adaptive: Decrypt Sample returns failure! at predictable points in playback. Audio keeps going but video freezes until you seek/skip. It always happens when the video goes black. I'm testing using an episode of The Big Bang Theory and video will stop at the start and finish of the title sequence. I can skip around it and view the whole episode with no problems, it just seems to get stuck at those points - as the widevine decrypter module isn't in the main repo, where should I raise the issue?

Thanks

Maven85 pushed a commit to Maven85/inputstream.adaptive that referenced this issue Aug 20, 2020
Maven85 pushed a commit to Maven85/inputstream.adaptive that referenced this issue Aug 27, 2020
Maven85 pushed a commit to Maven85/inputstream.adaptive that referenced this issue Aug 28, 2020
Maven85 pushed a commit to Maven85/inputstream.adaptive that referenced this issue Sep 7, 2020
dobo90 added a commit to dobo90/inputstream.adaptive that referenced this issue Feb 7, 2023
Sometimes I'm encountering a crash when starting a live dash stream with a following backtrace:

#0  0x910819d4 in adaptive::AdaptiveStream::start_stream() (this=<optimized out>) at /usr/src/debug/kodi-addon-inputstream-adaptive/inputstream.adaptive-20.3.2-Nexus/src/common/AdaptiveStream.cpp:606
xbmc#1  0x91069d0c in CInputStreamAdaptive::OpenStream(int) (this=0xaf51fa68, streamid=1006) at /usr/src/debug/kodi-addon-inputstream-adaptive/inputstream.adaptive-20.3.2-Nexus/src/main.cpp:309
xbmc#2  0x00e2f5d4 in CDVDDemuxClient::OpenStream(int) ()
xbmc#3  0x00e97ad4 in CVideoPlayer::OpenStream(CCurrentStream&, long long, int, int, bool) ()
xbmc#4  0x00e9e3c8 in CVideoPlayer::OpenDefaultStreams(bool) ()
xbmc#5  0x00ea3b18 in CVideoPlayer::Prepare() ()
xbmc#6  0x00ea5dc0 in CVideoPlayer::Process() ()
xbmc#7  0x011dcc6c in CThread::Action() ()

AdaptiveStream.cpp:606 points to a following line:
```
  currentPTSOffset_ = (next_segment->startPTS_ * current_rep_->timescale_ext_) /
    current_rep_->timescale_int_;
```

After taking a look on the assembly listing:
(gdb) disas /m _ZN8adaptive14AdaptiveStream12start_streamEv
...
   0x910819c8 <+1592>:  ldrne   r3, [sp, xbmc#28]
   0x910819d0 <+1600>:  ldr     r4, [r3, xbmc#324]  ; 0x144
=> 0x910819d4 <+1604>:  ldr     r0, [r8, xbmc#40]   ; 0x28
   0x910819d8 <+1608>:  ldr     r12, [r8, xbmc#44]  ; 0x2c
   0x910819dc <+1612>:  mov     r8, #0
   0x910819e4 <+1620>:  mov     r3, r8
   0x910819e8 <+1624>:  umull   r0, r1, r0, r4
   0x910819ec <+1628>:  mov     r2, r9
   0x910819f0 <+1632>:  mla     r1, r4, r12, r1
   0x910819f4 <+1636>:  bl      0x9105a6b4 <__aeabi_uldivmod@plt>
...

it appears that $r3 contains a valid representation:
(gdb) p *((AdaptiveTree::Representation*) $r3)
$1 = {url_ = "http://lb2-e2-18.pluscdn.pl/ch/1456330/123/dash/294f032c/1080p/p_3f26ad64-5258-452e-9bdc-209c361003e9_1667995241000/init.mp4", id = "1080p", codecs_ = "avc1.640028",
  codec_private_data_ = "", source_url_ = "", base_url_ = "http://lb2-e2-18.pluscdn.pl/ch/1456330/123/dash/294f032c/", bandwidth_ = 5000000, samplingRate_ = 0, width_ = 1920, height_ = 1080,
  fpsRate_ = 25, fpsScale_ = 1, aspect_ = 0, assured_buffer_duration_ = 20, max_buffer_duration_ = 40, static BYTERANGE = 0, static INDEXRANGEEXACT = 1, static TEMPLATE = 2, static TIMELINE = 4,
  static INITIALIZATION = 8, static SEGMENTBASE = 16, static SUBTITLESTREAM = 32, static INCLUDEDSTREAM = 64, static URLSEGMENTS = 128, static ENABLED = 256, static WAITFORSEGMENT = 512,
  static INITIALIZATION_PREFIXED = 1024, static DOWNLOADED = 2048, static INITIALIZED = 4096, flags_ = 14, hdcpVersion_ = 0, indexRangeMin_ = 0, indexRangeMax_ = 0, channelCount_ = 0 '\000',
  nalLengthSize_ = 0 '\000', pssh_set_ = 2, expired_segments_ = 0, containerType_ = adaptive::AdaptiveTree::CONTAINERTYPE_MP4, segtpl_ = {
    initialization = "http://lb2-e2-18.pluscdn.pl/ch/1456330/123/dash/294f032c/1080p/p_3f26ad64-5258-452e-9bdc-209c361003e9_1667995241000/init.mp4",
    media = "http://lb2-e2-18.pluscdn.pl/ch/1456330/123/dash/294f032c/$RepresentationID$/p_3f26ad64-5258-452e-9bdc-209c361003e9_1667995241000/t$Time$.mp4",
    media_url = "http://lb2-e2-18.pluscdn.pl/ch/1456330/123/dash/294f032c/1080p/p_3f26ad64-5258-452e-9bdc-209c361003e9_1667995241000/t$Time$.mp4", timescale = 90000, duration = 230395},
  startNumber_ = 4, nextPts_ = 20170141682400, ptsOffset_ = 0, duration_ = 972288000, timescale_ = 90000, timescale_ext_ = 100, timescale_int_ = 9, initialization_ = {range_begin_ = 0,
    range_end_ = 18446744073709551615, url = "", startPTS_ = 0, m_duration = 0, pssh_set_ = 0}, segments_ = {basePos = 0, data = std::vector of length 4220, capacity 4220 = {{
        range_begin_ = 20169170085600, range_end_ = 1, url = "", startPTS_ = 20169170085600, m_duration = 0, pssh_set_ = 0}, {range_begin_ = 20169170316000, range_end_ = 2, url = "",
        startPTS_ = 20169170316000, m_duration = 0, pssh_set_ = 0}, {range_begin_ = 20169170546400, range_end_ = 3, url = "", startPTS_ = 20169170546400, m_duration = 0, pssh_set_ = 0}, {
        range_begin_ = 20169170776800, range_end_ = 4, url = "", startPTS_ = 20169170776800, m_duration = 0, pssh_set_ = 0}, {range_begin_ = 20169171007200, range_end_ = 5, url = "",
        startPTS_ = 20169171007200, m_duration = 0, pssh_set_ = 0}, {range_begin_ = 20169171237600, range_end_ = 6, url = "", startPTS_ = 20169171237600, m_duration = 0, pssh_set_ = 0}, {
        range_begin_ = 20169171468000, range_end_ = 7, url = "", startPTS_ = 20169171468000, m_duration = 0, pssh_set_ = 0}, {range_begin_ = 20169171698400, range_end_ = 8, url = "",
...

$r8 should point to a valid next segment but doesn't:
(gdb) p *((adaptive::AdaptiveTree::Segment*) $r8)
Cannot access memory at address 0xa1a7dd88

After manually calculating next segment position in gdb it should point to:
(gdb) p ((AdaptiveTree::Representation*) $r3)->current_segment_ - &((AdaptiveTree::Representation*) $r3)->segments_.data[0] + 1
$2 = 4211
(gdb) p &((AdaptiveTree::Representation*) $r3)->segments_.data[4211]
$3 = (adaptive::AdaptiveTree::Segment *) 0x97ae9f10
(gdb) p ((AdaptiveTree::Representation*) $r3)->segments_.data[4211]
$4 = {range_begin_ = 20170140300000, range_end_ = 4212, url = "", startPTS_ = 20170140300000, m_duration = 0, pssh_set_ = 0}

My conclusion is that we are properly calculating next segment position
but for some reason the memory got deallocated. My assumption is
that updater thread has managed to parse the dash xml and update the segments.
dobo90 added a commit to dobo90/inputstream.adaptive that referenced this issue Feb 8, 2023
* Add sleep inside AdaptiveTree.cpp to trigger the issue

From Kodi logs:

2023-02-08 18:02:11.679 T:22020    info <general>: Creating InputStream
2023-02-08 18:02:11.691 T:22020    info <general>: AddOnLog: inputstream.adaptive: [Repr. chooser] Resolution set: 4096x2304, max allowed: 8152x4436, Adjust refresh rate: 0
2023-02-08 18:02:11.989 T:22020    info <general>: AddOnLog: inputstream.adaptive: Successfully parsed manifest file (Periods: 1, Streams in first period: 2, Type: live)
2023-02-08 18:02:12.989 T:22024   error <general>: AddOnLog: inputstream.adaptive: SegmentUpdateWorker: DEBUG before RefreshLiveSegments()
2023-02-08 18:02:13.028 T:22020    info <general>: Creating Demuxer
2023-02-08 18:02:13.029 T:22020    info <general>: Opening stream: 1006 source: 256
2023-02-08 18:02:13.114 T:22024   error <general>: AddOnLog: inputstream.adaptive: SegmentUpdateWorker: DEBUG after RefreshLiveSegments()
2023-02-08 18:02:13.160 T:22020   error <general>: AddOnLog: inputstream.adaptive: DEBUG before sleep()
2023-02-08 18:02:14.114 T:22024   error <general>: AddOnLog: inputstream.adaptive: SegmentUpdateWorker: DEBUG before RefreshLiveSegments()
2023-02-08 18:02:14.135 T:22024   error <general>: AddOnLog: inputstream.adaptive: SegmentUpdateWorker: DEBUG after RefreshLiveSegments()
2023-02-08 18:02:15.135 T:22024   error <general>: AddOnLog: inputstream.adaptive: SegmentUpdateWorker: DEBUG before RefreshLiveSegments()
2023-02-08 18:02:15.155 T:22024   error <general>: AddOnLog: inputstream.adaptive: SegmentUpdateWorker: DEBUG after RefreshLiveSegments()
2023-02-08 18:02:16.155 T:22024   error <general>: AddOnLog: inputstream.adaptive: SegmentUpdateWorker: DEBUG before RefreshLiveSegments()
2023-02-08 18:02:16.161 T:22020   error <general>: AddOnLog: inputstream.adaptive: DEBUG after sleep()

Address sanitizer report:

==21945==ERROR: AddressSanitizer: heap-use-after-free on address 0x7f402596e960 at pc 0x7f40131c4944 bp 0x7f402616de50 sp 0x7f402616de40
READ of size 8 at 0x7f402596e960 thread T53
    #0 0x7f40131c4943 in adaptive::AdaptiveStream::start_stream() /home/dobo/kodi/inputstream.adaptive/src/common/AdaptiveStream.cpp:610
    xbmc#1 0x7f4013178317 in CInputStreamAdaptive::OpenStream(int) /home/dobo/kodi/inputstream.adaptive/src/main.cpp:309
    xbmc#2 0x7f4013187f07 in kodi::addon::CInstanceInputStream::ADDON_OpenStream(AddonInstance_InputStream const*, int) (/usr/lib/kodi/addons/inputstream.adaptive/inputstream.adaptive.so.20.3.2+0x387f07)
    xbmc#3 0x55abad48474b in CDVDDemuxClient::OpenStream(int) (/usr/lib/kodi/kodi.bin+0xb7c74b)
    xbmc#4 0x55abad508385 in CVideoPlayer::OpenStream(CCurrentStream&, long, int, int, bool) (/usr/lib/kodi/kodi.bin+0xc00385)
    xbmc#5 0x55abad508bba in CVideoPlayer::OpenDefaultStreams(bool) (/usr/lib/kodi/kodi.bin+0xc00bba)
    xbmc#6 0x55abad50a857 in CVideoPlayer::Prepare() (/usr/lib/kodi/kodi.bin+0xc02857)
    xbmc#7 0x55abad510d45 in CVideoPlayer::Process() (/usr/lib/kodi/kodi.bin+0xc08d45)
    xbmc#8 0x55abad758396 in CThread::Action() (/usr/lib/kodi/kodi.bin+0xe50396)
    xbmc#9 0x55abae0f0e24  (/usr/lib/kodi/kodi.bin+0x17e8e24)
    xbmc#10 0x55abad755dba  (/usr/lib/kodi/kodi.bin+0xe4ddba)
    xbmc#11 0x7f403d4d72c2 in execute_native_thread_routine /usr/src/debug/gcc/gcc/libstdc++-v3/src/c++11/thread.cc:82
    xbmc#12 0x7f403dd8cbb4  (/usr/lib/libc.so.6+0x85bb4)
    xbmc#13 0x7f403de0ed8f  (/usr/lib/libc.so.6+0x107d8f)

0x7f402596e960 is located 303456 bytes inside of 303840-byte region [0x7f4025924800,0x7f402596eae0)
freed by thread T57 here:
    #0 0x7f403fac178a in operator delete(void*, unsigned long) /usr/src/debug/gcc/gcc/libsanitizer/asan/asan_new_delete.cpp:164
    xbmc#1 0x7f40131de17d in std::__new_allocator<adaptive::AdaptiveTree::Segment>::deallocate(adaptive::AdaptiveTree::Segment*, unsigned long) /usr/include/c++/12.2.1/bits/new_allocator.h:158
    xbmc#2 0x7f40131d974a in std::allocator_traits<std::allocator<adaptive::AdaptiveTree::Segment> >::deallocate(std::allocator<adaptive::AdaptiveTree::Segment>&, adaptive::AdaptiveTree::Segment*, unsigned long) /usr/include/c++/12.2.1/bits/alloc_traits.h:496
    xbmc#3 0x7f40131d33cb in std::_Vector_base<adaptive::AdaptiveTree::Segment, std::allocator<adaptive::AdaptiveTree::Segment> >::_M_deallocate(adaptive::AdaptiveTree::Segment*, unsigned long) /usr/include/c++/12.2.1/bits/stl_vector.h:387
    xbmc#4 0x7f40131edabd in std::_Vector_base<adaptive::AdaptiveTree::Segment, std::allocator<adaptive::AdaptiveTree::Segment> >::~_Vector_base() /usr/include/c++/12.2.1/bits/stl_vector.h:366
    xbmc#5 0x7f40131edb57 in std::vector<adaptive::AdaptiveTree::Segment, std::allocator<adaptive::AdaptiveTree::Segment> >::~vector() /usr/include/c++/12.2.1/bits/stl_vector.h:733
    xbmc#6 0x7f40131ea67f in adaptive::SPINCACHE<adaptive::AdaptiveTree::Segment>::~SPINCACHE() /home/dobo/kodi/inputstream.adaptive/src/common/AdaptiveTree.h:44
    xbmc#7 0x7f40131eae13 in adaptive::AdaptiveTree::Representation::~Representation() /home/dobo/kodi/inputstream.adaptive/src/common/AdaptiveTree.h:208
    xbmc#8 0x7f40131eb5c2 in adaptive::AdaptiveTree::AdaptationSet::~AdaptationSet() /home/dobo/kodi/inputstream.adaptive/src/parser/../common/AdaptiveTree.h:326
    xbmc#9 0x7f40131ecef6 in adaptive::AdaptiveTree::Period::~Period() /home/dobo/kodi/inputstream.adaptive/src/parser/../common/AdaptiveTree.h:462
    xbmc#10 0x7f40131e19ef in adaptive::AdaptiveTree::~AdaptiveTree() /home/dobo/kodi/inputstream.adaptive/src/common/AdaptiveTree.cpp:92
    xbmc#11 0x7f401323f6a4 in adaptive::DASHTree::~DASHTree() /home/dobo/kodi/inputstream.adaptive/src/parser/DASHTree.h:16
    xbmc#12 0x7f401323f6bf in adaptive::DASHTree::~DASHTree() /home/dobo/kodi/inputstream.adaptive/src/parser/DASHTree.h:16
    xbmc#13 0x7f401323f73c in std::default_delete<adaptive::DASHTree>::operator()(adaptive::DASHTree*) const /usr/include/c++/12.2.1/bits/unique_ptr.h:95
    xbmc#14 0x7f401323c926 in std::unique_ptr<adaptive::DASHTree, std::default_delete<adaptive::DASHTree> >::~unique_ptr() /usr/include/c++/12.2.1/bits/unique_ptr.h:396
    xbmc#15 0x7f4013237aa8 in adaptive::DASHTree::RefreshLiveSegments() /home/dobo/kodi/inputstream.adaptive/src/parser/DASHTree.cpp:1962
    xbmc#16 0x7f40131e7842 in adaptive::AdaptiveTree::SegmentUpdateWorker() /home/dobo/kodi/inputstream.adaptive/src/common/AdaptiveTree.cpp:431
    xbmc#17 0x7f4013208eea in void std::__invoke_impl<void, void (adaptive::AdaptiveTree::*)(), adaptive::AdaptiveTree*>(std::__invoke_memfun_deref, void (adaptive::AdaptiveTree::*&&)(), adaptive::AdaptiveTree*&&) /usr/include/c++/12.2.1/bits/invoke.h:74
    xbmc#18 0x7f4013208d56 in std::__invoke_result<void (adaptive::AdaptiveTree::*)(), adaptive::AdaptiveTree*>::type std::__invoke<void (adaptive::AdaptiveTree::*)(), adaptive::AdaptiveTree*>(void (adaptive::AdaptiveTree::*&&)(), adaptive::AdaptiveTree*&&) /usr/include/c++/12.2.1/bits/invoke.h:96
    xbmc#19 0x7f4013208cc6 in void std::thread::_Invoker<std::tuple<void (adaptive::AdaptiveTree::*)(), adaptive::AdaptiveTree*> >::_M_invoke<0ul, 1ul>(std::_Index_tuple<0ul, 1ul>) /usr/include/c++/12.2.1/bits/std_thread.h:258
    xbmc#20 0x7f4013208c7f in std::thread::_Invoker<std::tuple<void (adaptive::AdaptiveTree::*)(), adaptive::AdaptiveTree*> >::operator()() /usr/include/c++/12.2.1/bits/std_thread.h:265
    xbmc#21 0x7f4013208c63 in std::thread::_State_impl<std::thread::_Invoker<std::tuple<void (adaptive::AdaptiveTree::*)(), adaptive::AdaptiveTree*> > >::_M_run() /usr/include/c++/12.2.1/bits/std_thread.h:210
    xbmc#22 0x7f403d4d72c2 in execute_native_thread_routine /usr/src/debug/gcc/gcc/libstdc++-v3/src/c++11/thread.cc:82

previously allocated by thread T53 here:
    #0 0x7f403fac0672 in operator new(unsigned long) /usr/src/debug/gcc/gcc/libsanitizer/asan/asan_new_delete.cpp:95
    xbmc#1 0x7f40131deab1 in std::__new_allocator<adaptive::AdaptiveTree::Segment>::allocate(unsigned long, void const*) /usr/include/c++/12.2.1/bits/new_allocator.h:137
    xbmc#2 0x7f40131db727 in std::allocator_traits<std::allocator<adaptive::AdaptiveTree::Segment> >::allocate(std::allocator<adaptive::AdaptiveTree::Segment>&, unsigned long) /usr/include/c++/12.2.1/bits/alloc_traits.h:464
    xbmc#3 0x7f40131d5b81 in std::_Vector_base<adaptive::AdaptiveTree::Segment, std::allocator<adaptive::AdaptiveTree::Segment> >::_M_allocate(unsigned long) /usr/include/c++/12.2.1/bits/stl_vector.h:378
    xbmc#4 0x7f40131d1308 in std::vector<adaptive::AdaptiveTree::Segment, std::allocator<adaptive::AdaptiveTree::Segment> >::reserve(unsigned long) /usr/include/c++/12.2.1/bits/vector.tcc:79
    xbmc#5 0x7f401322dd61 in end /home/dobo/kodi/inputstream.adaptive/src/parser/DASHTree.cpp:1253
    xbmc#6 0x7f403c4d8263  (/usr/lib/libexpat.so.1+0xd263)

Thread T53 created by T0 here:
    #0 0x7f403fa64207 in __interceptor_pthread_create /usr/src/debug/gcc/gcc/libsanitizer/asan/asan_interceptors.cpp:207
    xbmc#1 0x7f403d4d73a9 in __gthread_create /usr/src/debug/gcc/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/x86_64-pc-linux-gnu/bits/gthr-default.h:663
    xbmc#2 0x7f403d4d73a9 in std::thread::_M_start_thread(std::unique_ptr<std::thread::_State, std::default_delete<std::thread::_State> >, void (*)()) /usr/src/debug/gcc/gcc/libstdc++-v3/src/c++11/thread.cc:147

Thread T57 created by T53 here:
    #0 0x7f403fa64207 in __interceptor_pthread_create /usr/src/debug/gcc/gcc/libsanitizer/asan/asan_interceptors.cpp:207
    xbmc#1 0x7f403d4d73a9 in __gthread_create /usr/src/debug/gcc/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/x86_64-pc-linux-gnu/bits/gthr-default.h:663
    xbmc#2 0x7f403d4d73a9 in std::thread::_M_start_thread(std::unique_ptr<std::thread::_State, std::default_delete<std::thread::_State> >, void (*)()) /usr/src/debug/gcc/gcc/libstdc++-v3/src/c++11/thread.cc:147
    xbmc#3 0x7f40131e751e in adaptive::AdaptiveTree::StartUpdateThread() /home/dobo/kodi/inputstream.adaptive/src/common/AdaptiveTree.cpp:418
    xbmc#4 0x7f401323398d in adaptive::DASHTree::open(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >) /home/dobo/kodi/inputstream.adaptive/src/parser/DASHTree.cpp:1639
    xbmc#5 0x7f401328c8ed in SESSION::CSession::Initialize() /home/dobo/kodi/inputstream.adaptive/src/Session.cpp:248
    xbmc#6 0x7f4013175f30 in CInputStreamAdaptive::Open(kodi::addon::InputstreamProperty const&) /home/dobo/kodi/inputstream.adaptive/src/main.cpp:86
    xbmc#7 0x7f40131871d5 in kodi::addon::CInstanceInputStream::ADDON_Open(AddonInstance_InputStream const*, INPUTSTREAM_PROPERTY*) (/usr/lib/kodi/addons/inputstream.adaptive/inputstream.adaptive.so.20.3.2+0x3871d5)
    xbmc#8 0x55abad46eddc in CInputStreamAddon::Open() (/usr/lib/kodi/kodi.bin+0xb66ddc)

SUMMARY: AddressSanitizer: heap-use-after-free /home/dobo/kodi/inputstream.adaptive/src/common/AdaptiveStream.cpp:610 in adaptive::AdaptiveStream::start_stream()
Shadow bytes around the buggy address:
  0x0fe884b25cd0: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x0fe884b25ce0: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x0fe884b25cf0: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x0fe884b25d00: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x0fe884b25d10: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
=>0x0fe884b25d20: fd fd fd fd fd fd fd fd fd fd fd fd[fd]fd fd fd
  0x0fe884b25d30: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x0fe884b25d40: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x0fe884b25d50: fd fd fd fd fd fd fd fd fd fd fd fd fa fa fa fa
  0x0fe884b25d60: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0fe884b25d70: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
==21945==ABORTING
dobo90 pushed a commit to dobo90/inputstream.adaptive that referenced this issue Feb 14, 2023
…hile CloseSession is active to avoid endless loop.

Sometimes when stopping a playback Kodi crashes with a following backtrace:

    (__m=<optimized out>, this=<optimized out>)
    at /usr/include/c++/12.1.0/bits/atomic_base.h:486
        __b = <optimized out>
    at /usr/include/c++/12.1.0/atomic:87
    (adp=std::shared_ptr<media::CdmAdapter> (use count 1, weak count 1) = {...}, delay=<optimized out>, context=<optimized out>)
    at /usr/src/debug/kodi-addon-inputstream-adaptive/inputstream.adaptive-20.3.3-Nexus/wvdecrypter/cdm/media/cdm/cdm_adapter.cc:81
    at /usr/include/c++/12.1.0/bits/invoke.h:96, unsigned long long, void*), std::shared_ptr<media::CdmAdapter>, long long, void*> >::_M_invoke<0u, 1u, 2u, 3u>(std::_Index_tuple<0u, 1u, 2u, 3u>)
    (this=<optimized out>) at /usr/include/c++/12.1.0/bits/std_thread.h:252
    at /usr/include/c++/12.1.0/bits/std_thread.h:259
    at /usr/include/c++/12.1.0/bits/std_thread.h:210
    at /usr/src/debug/gcc/libstdc++-v3/src/c++11/thread.cc:82
        ret = <optimized out>
        pd = 0x85cf9080
        unwind_buf = {cancel_jmp_buf = {{jmp_buf = {857629366, 66004830, -2049994624, -2008033272, -2008033282, 338, -2008033281, 8387456, -2058383360, -2049995908, 1920, 1080, 3840, 2160, 0, 10000000, -1431355392, 1107558643, 0 <repeats 46 times>}, mask_was_saved = 0}}, priv = {pad = {0x0, 0x0, 0x0, 0x0}, data = {prev = 0x0, cleanup = 0x0, canceltype = 0}}}
        not_first_call = <optimized out>
        robust = <optimized out>

After searching in the github history it came out that there was a
similar issue adressed in xbmc#728.

For some reason that piece of code got removed in
xbmc@8889fe9
(in pull request xbmc#883).

The stack trace is from the Raspberry Pi 4 machine. But I can also reproduce the issue
on my x86_64 Linux machine by adding a 1s sleep in the beginning of CdmAdapter::SetTimer.

In order to reproduce it is necessary to quickly play/stop streams.
Script attached at the end of the commit message can be used to ease the reproduction.
In my environment running the script for 1-3 minutes makes Kodi crash.
Although the root cause described in xbmc#729
was a busy loop. Right now address sanitizer reports:
```
AddressSanitizer:DEADLYSIGNAL
=================================================================
==108079==ERROR: AddressSanitizer: SEGV on unknown address 0x7f6e314f7d4a (pc 0x7f6e314f7d4a bp 0x7f6e263fd830 sp 0x7f6e263fd720 T464)
==108079==The signal is caused by a READ memory access.
==108079==Hint: PC is at a non-executable region. Maybe a wild jump?
    #0 0x7f6e314f7d4a  (<unknown module>)
    xbmc#1 0x7f6e3151c066  (<unknown module>)
    xbmc#2 0x7f6e31526947  (<unknown module>)
    xbmc#3 0x7f6e31526729  (<unknown module>)
    xbmc#4 0x7f6e31526620  (<unknown module>)
    xbmc#5 0x7f6e315265a1  (<unknown module>)
    xbmc#6 0x7f6e31526585  (<unknown module>)
    xbmc#7 0x7f6e46ed72c2 in execute_native_thread_routine /usr/src/debug/gcc/gcc/libstdc++-v3/src/c++11/thread.cc:82

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (<unknown module>)
Thread T464 created by T51 here:
    #0 0x7f6e49464207 in __interceptor_pthread_create /usr/src/debug/gcc/gcc/libsanitizer/asan/asan_interceptors.cpp:207
    xbmc#1 0x7f6e46ed73a9 in __gthread_create /usr/src/debug/gcc/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/x86_64-pc-linux-gnu/bits/gthr-default.h:663
    xbmc#2 0x7f6e46ed73a9 in std::thread::_M_start_thread(std::unique_ptr<std::thread::_State, std::default_delete<std::thread::_State> >, void (*)()) /usr/src/debug/gcc/gcc/libstdc++-v3/src/c++11/thread.cc:147
    xbmc#3 0x7f6e31522028  (<unknown module>)
    xbmc#4 0x7f6e27569d40  (/home/dobo/.kodi/cdm/libwidevinecdm.so+0x969d40)
    xbmc#5 0x7f6e2757e7fc  (/home/dobo/.kodi/cdm/libwidevinecdm.so+0x97e7fc)
    xbmc#6 0x7f6e27569c36  (/home/dobo/.kodi/cdm/libwidevinecdm.so+0x969c36)
    xbmc#7 0x7f6e3151fb28  (<unknown module>)
    xbmc#8 0x7f6e3151c0b1  (<unknown module>)
    xbmc#9 0x7f6e31526947  (<unknown module>)
    xbmc#10 0x7f6e31526729  (<unknown module>)
    xbmc#11 0x7f6e31526620  (<unknown module>)
    xbmc#12 0x7f6e315265a1  (<unknown module>)
    xbmc#13 0x7f6e31526585  (<unknown module>)
    xbmc#14 0x7f6e46ed72c2 in execute_native_thread_routine /usr/src/debug/gcc/gcc/libstdc++-v3/src/c++11/thread.cc:82

Thread T51 created by T45 here:
    #0 0x7f6e49464207 in __interceptor_pthread_create /usr/src/debug/gcc/gcc/libsanitizer/asan/asan_interceptors.cpp:207
    xbmc#1 0x7f6e46ed73a9 in __gthread_create /usr/src/debug/gcc/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/x86_64-pc-linux-gnu/bits/gthr-default.h:663
    xbmc#2 0x7f6e46ed73a9 in std::thread::_M_start_thread(std::unique_ptr<std::thread::_State, std::default_delete<std::thread::_State> >, void (*)()) /usr/src/debug/gcc/gcc/libstdc++-v3/src/c++11/thread.cc:147
    xbmc#3 0x7f6e31522028  (<unknown module>)
    xbmc#4 0x7f6e27569d40  (/home/dobo/.kodi/cdm/libwidevinecdm.so+0x969d40)
    xbmc#5 0x7f6e2757e7fc  (/home/dobo/.kodi/cdm/libwidevinecdm.so+0x97e7fc)
    xbmc#6 0x7f6e2757e70e  (/home/dobo/.kodi/cdm/libwidevinecdm.so+0x97e70e)
    xbmc#7 0x7f6e274e4ecc  (/home/dobo/.kodi/cdm/libwidevinecdm.so+0x8e4ecc)
    xbmc#8 0x7f6e3151f280  (<unknown module>)
    xbmc#9 0x7f6e314e9ea1  (<unknown module>)
    xbmc#10 0x7f6e314e49bb  (<unknown module>)
    xbmc#11 0x7f6e314f45ce  (<unknown module>)
    xbmc#12 0x7f6e2889228f in SESSION::CSession::InitializeDRM(bool) /home/dobo/kodi/inputstream.adaptive/src/Session.cpp:643
    xbmc#13 0x7f6e28893279 in SESSION::CSession::InitializePeriod(bool) /home/dobo/kodi/inputstream.adaptive/src/Session.cpp:723
    xbmc#14 0x7f6e2888e36d in SESSION::CSession::Initialize() /home/dobo/kodi/inputstream.adaptive/src/Session.cpp:266
    xbmc#15 0x7f6e28775f4c in CInputStreamAdaptive::Open(kodi::addon::InputstreamProperty const&) /home/dobo/kodi/inputstream.adaptive/src/main.cpp:101
    xbmc#16 0x7f6e287871a1 in kodi::addon::CInstanceInputStream::ADDON_Open(AddonInstance_InputStream const*, INPUTSTREAM_PROPERTY*) (/usr/lib/kodi/addons/inputstream.adaptive/inputstream.adaptive.so.20.3.2+0x3871a1)
    xbmc#17 0x55c2811f4ddc in CInputStreamAddon::Open() (/usr/lib/kodi/kodi.bin+0xb66ddc)

Thread T45 created by T0 here:
    #0 0x7f6e49464207 in __interceptor_pthread_create /usr/src/debug/gcc/gcc/libsanitizer/asan/asan_interceptors.cpp:207
    xbmc#1 0x7f6e46ed73a9 in __gthread_create /usr/src/debug/gcc/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/x86_64-pc-linux-gnu/bits/gthr-default.h:663
    xbmc#2 0x7f6e46ed73a9 in std::thread::_M_start_thread(std::unique_ptr<std::thread::_State, std::default_delete<std::thread::_State> >, void (*)()) /usr/src/debug/gcc/gcc/libstdc++-v3/src/c++11/thread.cc:147
```

The root cause of the crash is unknown as it comes directly from
libwidevine library. But it crashes when we call cdm->TimerExpired when
the session is closed. After applying mentioned commit I'm not able
to reproduce the crash anymore.

Script to reproduce the issue:
```
set -x

URI='Change it to some uri to play'

while true; do
  curl -s "http://kodi:kodi@127.0.0.1:8080/jsonrpc" -H 'Content-Type: application/json' --data "{\"jsonrpc\":\"2.0\",\"method\":\"Player.Open\",\"params\":{\"item\":{\"file\":\"$URI\"}}}"
  sleep $[ ($RANDOM % 70 + 30) / 10.0 ]

  curl -s "http://kodi:kodi@127.0.0.1:8080/jsonrpc" -H 'Content-Type: application/json' --data '{"jsonrpc":"2.0","method":"Player.Stop","params":{"playerid":1}}'
  sleep 1
done
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants